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