|
import datasets |
|
import json |
|
import tarfile |
|
from os import listdir, makedirs |
|
from os.path import isfile, join |
|
|
|
from datasets import DatasetInfo, GeneratorBasedBuilder, Split, SplitGenerator |
|
|
|
|
|
_CITATION = "" |
|
|
|
_DESCRIPTION = "Dataset for training agents with Labyrinth-v0 environment." |
|
|
|
_HOMEPAGE = "https://huggingface.co/datasets/NathanGavenski/Labyrinth-v0_4x4" |
|
|
|
_LICENSE = "" |
|
|
|
_REPO = "https://huggingface.co/datasets/NathanGavenski/Labyrinth-v0_4x4" |
|
|
|
|
|
class ImageSet(GeneratorBasedBuilder): |
|
|
|
def _info(self): |
|
return DatasetInfo( |
|
description=_DESCRIPTION, |
|
features=datasets.Features({ |
|
"obs": datasets.Value("string"), |
|
"actions": datasets.Value("int32"), |
|
"rewards": datasets.Value("float32"), |
|
"episode_starts": datasets.Value("bool"), |
|
"info": datasets.Value("string"), |
|
}), |
|
supervised_keys=None, |
|
homepage=_HOMEPAGE, |
|
citation=_CITATION, |
|
) |
|
|
|
def _split_generators(self, dl_manager): |
|
info_path = dl_manager.download_and_extract(f"{_REPO}/resolve/main/dataset.tar.gz") |
|
image_path = dl_manager.download_and_extract(f"{_REPO}/resolve/main/images.tar.gz") |
|
|
|
return [ |
|
SplitGenerator( |
|
name=Split.TRAIN, |
|
gen_kwargs={ |
|
"images_paths": f"{image_path}/images/train", |
|
"infos": f"{info_path}/train_shortest_route.jsonl" |
|
} |
|
), |
|
SplitGenerator( |
|
name=Split.VALIDATION, |
|
gen_kwargs={ |
|
"images_paths": f"{image_path}/images/eval", |
|
"infos": f"{info_path}/eval_shortest_route.jsonl" |
|
} |
|
), |
|
SplitGenerator( |
|
name=Split.TEST, |
|
gen_kwargs={ |
|
"images_paths": f"{image_path}/images/test", |
|
"infos": f"{info_path}/test_shortest_route.jsonl", |
|
} |
|
), |
|
SplitGenerator( |
|
name="random", |
|
gen_kwargs={ |
|
"images_paths": f"{image_path}/images/random", |
|
"infos": f"{info_path}/random_shortest_route.jsonl", |
|
} |
|
) |
|
] |
|
|
|
def _generate_examples(self, images_paths, infos): |
|
images = [join(images_paths, f) for f in listdir(images_paths) if isfile(join(images_paths, f))] |
|
|
|
images_dict = {} |
|
for image in images: |
|
images_dict[image.split("/")[-1].split(".")[0]] = image |
|
|
|
with open(infos, encoding="utf-8") as data: |
|
for idx, line in enumerate(data): |
|
record = json.loads(line) |
|
index = record["obs"].split(".")[0] |
|
yield idx, { |
|
"obs": images_dict[index], |
|
"actions": record["actions"], |
|
"rewards": record["rewards"], |
|
"episode_starts": record["episode_starts"], |
|
"info": record["info"], |
|
} |