NathanGavenski commited on
Commit
c0aa6b1
·
verified ·
1 Parent(s): 91cf83f

Upload create_dataset_script.ipynb

Browse files
Files changed (1) hide show
  1. create_dataset_script.ipynb +166 -0
create_dataset_script.ipynb ADDED
@@ -0,0 +1,166 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "9985af01-9a57-451f-8c7d-842f9066414c",
6
+ "metadata": {},
7
+ "source": [
8
+ "# Crafter Human Dataset\n",
9
+ "\n",
10
+ "This dataset was created using the data available at https://archive.org/details/crafter_human_dataset. We only made it available to users using the following script. \n",
11
+ "\n",
12
+ "## Using this dataset\n",
13
+ "To use this dataset, be sure to check our [IL-Datasets](https://github.com/NathanGavenski/IL-Datasets) toolkit.\n",
14
+ "\n",
15
+ "## Original citation\n",
16
+ "Please, if using this dataset, don't forget to cite Hafner's original work:\n",
17
+ "```\n",
18
+ "@article{hafner2021crafter,\n",
19
+ " title={Benchmarking the Spectrum of Agent Capabilities},\n",
20
+ " author={Danijar Hafner},\n",
21
+ " year={2021},\n",
22
+ " journal={arXiv preprint arXiv:2109.06780},\n",
23
+ "}\n",
24
+ "```"
25
+ ]
26
+ },
27
+ {
28
+ "cell_type": "code",
29
+ "execution_count": null,
30
+ "id": "a07ce4e2",
31
+ "metadata": {},
32
+ "outputs": [],
33
+ "source": [
34
+ "import numpy as np\n",
35
+ "import os\n",
36
+ "from PIL import Image\n",
37
+ "from glob import glob"
38
+ ]
39
+ },
40
+ {
41
+ "cell_type": "code",
42
+ "execution_count": null,
43
+ "id": "c48dd39c",
44
+ "metadata": {},
45
+ "outputs": [],
46
+ "source": [
47
+ "files = glob(\"./files/*.npz\")\n",
48
+ "print(f\"Files found: {len(files)}\")"
49
+ ]
50
+ },
51
+ {
52
+ "cell_type": "code",
53
+ "execution_count": null,
54
+ "id": "3342b01a",
55
+ "metadata": {},
56
+ "outputs": [],
57
+ "source": [
58
+ "f = np.load(files[0])\n",
59
+ "for _file in files:\n",
60
+ " f = np.load(_file)\n",
61
+ " achieve_goal = 0\n",
62
+ " for k in [k for k in f.keys() if \"achivement\" in k]:\n",
63
+ " achieve_goal += f[k].sum()\n",
64
+ "\n",
65
+ " if achieve_goal == 0:\n",
66
+ " print(_file)"
67
+ ]
68
+ },
69
+ {
70
+ "cell_type": "code",
71
+ "execution_count": null,
72
+ "id": "52c8ca21-bddc-43e0-bd0d-994f9f2bd048",
73
+ "metadata": {},
74
+ "outputs": [],
75
+ "source": [
76
+ "from collections import defaultdict\n",
77
+ "from tqdm import tqdm\n",
78
+ "\n",
79
+ "count = 0\n",
80
+ "\n",
81
+ "os.makedirs(\"./images/\", exist_ok=True)\n",
82
+ "\n",
83
+ "dataset = defaultdict(list)\n",
84
+ "for _file in tqdm(files):\n",
85
+ " f = np.load(_file)\n",
86
+ "\n",
87
+ " for idx, image in enumerate(f['image']):\n",
88
+ " Image.fromarray(image).save(f\"images/{count}.png\")\n",
89
+ " dataset['obs'].append(f\"images/{count}.png\")\n",
90
+ " dataset['actions'].append(f['action'][idx].item())\n",
91
+ " dataset['rewards'].append(f['reward'][idx].item())\n",
92
+ " dataset['episode_starts'].append(f['done'][idx].item())\n",
93
+ " count += 1\n",
94
+ " dataset['episode_returns'].append(sum(dataset['rewards']))\n",
95
+ "\n",
96
+ "for k, v in dataset.items():\n",
97
+ " dataset[k] = np.array(dataset[k])\n",
98
+ "\n",
99
+ "np.savez(\"crafter\", **dataset)"
100
+ ]
101
+ },
102
+ {
103
+ "cell_type": "code",
104
+ "execution_count": null,
105
+ "id": "4a0eee2d-9cab-4a0c-b221-b4d1f0155cc7",
106
+ "metadata": {},
107
+ "outputs": [],
108
+ "source": [
109
+ "from imitation_datasets.dataset.huggingface import baseline_to_huggingface\n",
110
+ "\n",
111
+ "baseline_to_huggingface(\"./crafter.npz\", \"./crafter.jsonl\")"
112
+ ]
113
+ },
114
+ {
115
+ "cell_type": "code",
116
+ "execution_count": null,
117
+ "id": "3a506b25-760e-4ffe-afa2-6d6d73058bf7",
118
+ "metadata": {},
119
+ "outputs": [],
120
+ "source": [
121
+ "import tarfile\n",
122
+ "\n",
123
+ "with tarfile.open(\"dataset.tar.gz\", \"w:gz\") as tar_file:\n",
124
+ " tar_file.add(\"./crafter.jsonl\", \"crafter.jsonl\")\n",
125
+ "\n",
126
+ "with tarfile.open(\"images.tar.gz\", \"w:gz\") as tar_file:\n",
127
+ " tar_file.add(\"images/\", \"images/\")"
128
+ ]
129
+ },
130
+ {
131
+ "cell_type": "code",
132
+ "execution_count": null,
133
+ "id": "30224224-fd9c-453c-b202-7fad802026f5",
134
+ "metadata": {},
135
+ "outputs": [],
136
+ "source": [
137
+ "import shutil\n",
138
+ "\n",
139
+ "shutil.rmtree(\"images/\")\n",
140
+ "os.remove(\"crafter.npz\")\n",
141
+ "os.remove(\"crafter.jsonl\")"
142
+ ]
143
+ }
144
+ ],
145
+ "metadata": {
146
+ "kernelspec": {
147
+ "display_name": "Python 3 (ipykernel)",
148
+ "language": "python",
149
+ "name": "python3"
150
+ },
151
+ "language_info": {
152
+ "codemirror_mode": {
153
+ "name": "ipython",
154
+ "version": 3
155
+ },
156
+ "file_extension": ".py",
157
+ "mimetype": "text/x-python",
158
+ "name": "python",
159
+ "nbconvert_exporter": "python",
160
+ "pygments_lexer": "ipython3",
161
+ "version": "3.10.12"
162
+ }
163
+ },
164
+ "nbformat": 4,
165
+ "nbformat_minor": 5
166
+ }