File size: 5,301 Bytes
c48d412
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c495116
 
 
 
 
 
c48d412
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
168
169
170
171
172
173
174
175
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