import io import itertools as it import numpy as np import datasets as d _DESCRIPTION = """\ The Dropjects Real dataset was created at the Chair of Cyber-Physical Systems in Production \ Engineering at the Technical University of Munich. """ CLASSES = [ "battery_holder", "buckle_socket", "buckle_plug", "chew_toy_big", "cpsduck", "cpsglue_big", "group1", "group2", "nema_holder", "stapler", ] SCENES = [ "box", "array", # but only for group1 and group2, and only uncluttered ] CLUTTER = [ "cluttered", "uncluttered", ] LIGHTING = [ "diffuseRight", "diffuseLeft", "spotRight", "spotLeft", "dark", "normal", ] NUM_SHARDS = 3 WILDCARD = "{}" def is_valid_config(spec): cls, scene, clutter, lighting = spec if scene == "array" and cls not in ["group1", "group2", WILDCARD]: return False if scene == "array" and clutter not in ["uncluttered", WILDCARD]: return False return True ALL_CONFIGS = list( it.product( [WILDCARD] + CLASSES, [WILDCARD] + SCENES, [WILDCARD] + CLUTTER, [WILDCARD] + LIGHTING ) ) ALL_CONFIGS = [x for x in ALL_CONFIGS if is_valid_config(x)] BASE_PATH = "https://huggingface.co/datasets/LukasDb/dropjects_real/resolve/main/data/test/{cls}/{scene}/{clutter}/{lighting}/{shard}.tar" class DropjectsRealConfig(d.BuilderConfig): def __init__(self, cls: str, scene: str, clutter: str, lighting: str, **kwargs): name = f"{cls}-{scene}-{clutter}-{lighting}" super().__init__(version=d.Version("1.0.0"), **kwargs, name=name) # transform wildcards into concrete lists # this does not respect the validity of the config cls = CLASSES if cls == WILDCARD else [cls] scene = SCENES if scene == WILDCARD else [scene] clutter = CLUTTER if clutter == WILDCARD else [clutter] lighting = LIGHTING if lighting == WILDCARD else [lighting] self.cls = cls self.scene = scene self.clutter = clutter self.lighting = lighting class DropjectsReal(d.GeneratorBasedBuilder): BUILDER_CONFIGS = list( DropjectsRealConfig(cls=cls, scene=scene, clutter=clutter, lighting=lighting) for cls, scene, clutter, lighting in ALL_CONFIGS ) DEFAULT_CONFIG_NAME = f"{WILDCARD}-{WILDCARD}-{WILDCARD}-{WILDCARD}" def _info(self): features = d.Features( { "rgb": d.Array3D((1242, 2208, 3), dtype="uint8"), "rgb_R": d.Array3D((1242, 2208, 3), dtype="uint8"), "depth": d.Array2D((1242, 2208), dtype="float32"), "depth_gt": d.Array2D((1242, 2208), dtype="float32"), "mask": d.Array2D((1242, 2208), dtype="int32"), "obj_ids": d.Sequence(d.Value("int32")), "obj_classes": d.Sequence(d.Value("string")), "obj_pos": d.Sequence(d.Sequence(d.Value("float32"))), "obj_rot": d.Sequence(d.Sequence(d.Value("float32"))), "obj_bbox_obj": d.Sequence(d.Sequence(d.Value("int32"))), "obj_bbox_visib": d.Sequence(d.Sequence(d.Value("int32"))), "cam_location": d.Sequence(d.Value("float32")), "cam_rotation": d.Sequence(d.Value("float32")), "cam_matrix": d.Array2D((3, 3), dtype="float32"), "obj_px_count_all": d.Sequence(d.Value("int32")), "obj_px_count_valid": d.Sequence(d.Value("int32")), "obj_px_count_visib": d.Sequence(d.Value("int32")), "obj_visib_fract": d.Sequence(d.Value("float32")), } ) return d.DatasetInfo( description=_DESCRIPTION, citation="", # TODO homepage="", # TODO license="cc", features=features, ) def _split_generators(self, dl_manager): clss = self.config.cls scenes = self.config.scene clutters = self.config.clutter lightings = self.config.lighting # wildcards to concrete list can generate invalid configs configs = [c for c in it.product(clss, scenes, clutters, lightings) if is_valid_config(c)] archive_paths = [ BASE_PATH.format(cls=c, scene=s, clutter=cl, lighting=l, shard=i) for c, s, cl, l in configs for i in range(NUM_SHARDS) ] downloaded = dl_manager.download(archive_paths) return [ d.SplitGenerator( name=d.Split.TEST, gen_kwargs={"tars": [dl_manager.iter_archive(d) for d in downloaded]}, ), ] def _generate_examples(self, tars): sample = {} id = None for tar in tars: for file_path, file_obj in tar: new_id = file_path.split(".")[0] if id is None: id = new_id else: if id != new_id: yield id, sample sample = {} id = new_id key = file_path.split(".")[1] bytes = io.BytesIO(file_obj.read()) value = np.load(bytes, allow_pickle=False) sample[key] = value