File size: 3,324 Bytes
e416d01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import io
import itertools as it
import numpy as np
import datasets as d


_DESCRIPTION = """\
The Dropjects dataset was created at the Chair of Cyber-Physical Systems in Production \
Engineering at the Technical University of Munich.
"""

SUBSETS = [
    "omni",
    "cps",
    "linemod",
    "ycbv",
    "homebreweddb",
    "hope",
    "tless",
]

NUM_SHARDS = {
    "cps": 1000,
    "ycbv": 1000,
    "linemod": 1000,
    "tless": 1000,
    "omni": 10_000,
}


BASE_PATH = "https://huggingface.co/datasets/LukasDb/dropjects/resolve/main/data/train/{subset}/{shard}.tar"


h = 1440
w = 2560


class Dropjects(d.GeneratorBasedBuilder):
    BUILDER_CONFIGS = list(d.BuilderConfig(name=x) for x in SUBSETS)

    def _info(self):

        features = d.Features(
            {
                # TODO at least the resolution is different
                "rgb": d.Array3D((h, w, 3), dtype="uint8"),
                "rgb_R": d.Array3D((h, w, 3), dtype="uint8"),
                "depth": d.Array2D((h, w), dtype="float32"),
                "depth_R": d.Array2D((h, w), dtype="float32"),
                "mask": d.Array2D((h, w), 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):
        subset = self.config.name

        archive_paths = [
            BASE_PATH.format(subset=subset, shard=i) for i in range(NUM_SHARDS[subset])
        ]

        downloaded = dl_manager.download(archive_paths)

        return [
            d.SplitGenerator(
                name=d.Split.TRAIN,
                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