import datasets import json import os from .classes import IMAGENET2012_CLASSES _URL_BASE = "https://huggingface.co/datasets/Prisma-Multimodal/segmented-imagenet1k-subset/resolve/main/" _URLS = { "img_data": _URL_BASE + "images.tar.gz", "mask_data": _URL_BASE + "masks.tar.gz", "train_json": _URL_BASE + "train.json", "val_json": _URL_BASE + "val.json", "test_json": _URL_BASE + "test.json", } class SegmentedImagenet1kDataset(datasets.GeneratorBasedBuilder): datasets.Version("1.1.0") def _info(self): return datasets.DatasetInfo( description="Machine generated instance segmentation results of subset of ImageNet-1k", homepage="https://huggingface.co/datasets/Prisma-Multimodal/segmented-imagenet1k-subset", features = datasets.Features({ "image": datasets.Image(), "imagenet_label": datasets.Value("string"), "boxes": datasets.Sequence(datasets.Sequence(datasets.Value('int32'))), "labels": datasets.Sequence(datasets.Value("string")), "scores": datasets.Sequence(datasets.Value("float32")) , "masks": datasets.Sequence(datasets.Image()), }), ) def _split_generators(self, dl_manager: datasets.DownloadManager): dirs = dl_manager.download_and_extract(_URLS) root_folder_kwargs = {"image_root": dirs["img_data"], "mask_root": dirs["mask_data"]} return [ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"json_path": dirs["train_json"], "get_imagenet_string": True, **root_folder_kwargs}), datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"json_path": dirs["test_json"], "get_imagenet_string": False, **root_folder_kwargs}), datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"json_path": dirs["val_json"], "get_imagenet_string": True, **root_folder_kwargs}), ] def _generate_examples(self, json_path, image_root, mask_root, get_imagenet_string): with open(json_path, encoding="utf-8") as f: data = json.load(f) for id, item in enumerate(data): if get_imagenet_string: imagenet_label = IMAGENET2012_CLASSES[os.path.basename(item['image']).replace(".JPEG", "").rsplit("_", 1)[1]] pass else: imagenet_label = "None" yield id, { "image" : os.path.join(image_root,item['image']), "imagenet_label": imagenet_label, "boxes": item['boxes'], "scores": item['scores'], "labels": item['labels'], "masks": [os.path.join(mask_root, p) for p in item['masks']] }