{ "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 }