File size: 5,735 Bytes
b998535
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ced6b1f
 
 
 
 
 
 
 
b998535
cdb404a
 
b998535
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cdb404a
 
b998535
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0834ca1
 
 
 
 
 
b998535
 
 
 
 
0834ca1
 
b998535
 
 
 
 
0834ca1
 
b998535
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
 # 
 # This file is part of the oe_dataset distribution (https://huggingface.co/datasets/ABC-iRobotics/oe_dataset).
 # Copyright (c) 2023 ABC-iRobotics.
 # 
 # This program is free software: you can redistribute it and/or modify  
 # it under the terms of the GNU General Public License as published by  
 # the Free Software Foundation, version 3.
 #
 # This program is distributed in the hope that it will be useful, but 
 # WITHOUT ANY WARRANTY; without even the implied warranty of 
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
 # General Public License for more details.
 #
 # You should have received a copy of the GNU General Public License 
 # along with this program. If not, see <http://www.gnu.org/licenses/>.
 #
"""OE dataset"""

import sys
if sys.version_info < (3, 9):
    from typing import Sequence, Generator, Tuple
else:
    from collections.abc import Sequence, Generator
    Tuple = tuple

from typing import Optional, IO

import datasets
import itertools


# ---- Constants ----

_CITATION = """\
@ARTICLE{10145828,
  author={Károly, Artúr István and Tirczka, Sebestyén and Gao, Huijun and Rudas, Imre J. and Galambos, Péter},
  journal={IEEE Transactions on Cybernetics}, 
  title={Increasing the Robustness of Deep Learning Models for Object Segmentation: A Framework for Blending Automatically Annotated Real and Synthetic Data}, 
  year={2023},
  volume={},
  number={},
  pages={1-14},
  doi={10.1109/TCYB.2023.3276485}}

"""

_DESCRIPTION = """\
An instance segmentation dataset for robotic manipulation in a tabletop environment.
The dataset incorporates real and synthetic images for testing sim-to-real model transfer after fine-tuning.
"""

_HOMEPAGE = "https://huggingface.co/ABC-iRobotics/oe_dataset"

_LICENSE = "GNU General Public License v3.0"

_LATEST_VERSIONS = {
    "all": "1.0.0",
    "real": "1.0.0",
    "synthetic": "1.0.0",
    "photoreal": "1.0.0",
    "random": "1.0.0",
}



# ---- OE dataset Configs ----

class OEDatasetConfig(datasets.BuilderConfig):
    """BuilderConfig for OE dataset."""

    def __init__(self, name: str, imgs_urls: Sequence[str], masks_urls: Sequence[str],  version: Optional[str] = None, **kwargs):
        _version = _LATEST_VERSIONS[name] if version is None else version
        _name = f"{name}_v{_version}"
        super(OEDatasetConfig, self).__init__(version=datasets.Version(_version), name=_name, **kwargs)
        self._imgs_urls = {"train": [url + "/train.tar.gz" for url in imgs_urls], "val": [url + "/val.tar.gz" for url in imgs_urls]}
        self._masks_urls = {"train": [url + "/train.tar.gz" for url in masks_urls], "val": [url + "/val.tar.gz" for url in masks_urls]}

    @property
    def features(self):
        return datasets.Features(
            {
                "image": datasets.Image(),
                "mask": datasets.Image(),
            }
        )
    
    @property
    def supervised_keys(self):
        return ("image", "mask")



# ---- OE dataset Loader ----

class OEDataset(datasets.GeneratorBasedBuilder):
    """OE dataset."""

    BUILDER_CONFIG_CLASS = OEDatasetConfig
    BUILDER_CONFIGS = [
        OEDatasetConfig(
            name = "photoreal",
            description = "Photorealistic synthetic images",
            imgs_urls = ["https://huggingface.co/datasets/ABC-iRobotics/oe_dataset/resolve/main/synthetic/photoreal/imgs"],
            masks_urls = ["https://huggingface.co/datasets/ABC-iRobotics/oe_dataset/resolve/main/synthetic/photoreal/masks"]
            ),
    ]

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=self.config.features,
            supervised_keys=self.config.supervised_keys,
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
            version=self.config.version,
        )

    def _split_generators(self, dl_manager):
        train_imgs_paths = dl_manager.download(self.config._imgs_urls["train"])
        val_imgs_paths = dl_manager.download(self.config._imgs_urls["val"])

        train_masks_paths = dl_manager.download(self.config._masks_urls["train"])
        val_masks_paths = dl_manager.download(self.config._masks_urls["val"])

        train_imgs_gen = itertools.chain.from_iterable([dl_manager.iter_archive(path) for path in train_imgs_paths])
        val_imgs_gen = itertools.chain.from_iterable([dl_manager.iter_archive(path) for path in val_imgs_paths])

        train_masks_gen = itertools.chain.from_iterable([dl_manager.iter_archive(path) for path in train_masks_paths])
        val_masks_gen = itertools.chain.from_iterable([dl_manager.iter_archive(path) for path in val_masks_paths])
        
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "images": train_imgs_gen,
                    "masks": train_masks_gen,
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs={
                    "images": val_imgs_gen,
                    "masks": val_masks_gen,
                },
            ),
        ]

    def _generate_examples(
        self,
        images: Generator[Tuple[str,IO], None, None],
        masks: Generator[Tuple[str,IO], None, None],
    ):
        for i, (img_info, mask_info) in enumerate(zip(images, masks)):
            img_file_path, img_file_obj = img_info
            mask_file_path, mask_file_obj = mask_info
            yield i, {
                "image": {"path": img_file_path, "bytes": img_file_obj.read()},
                "mask": {"path": mask_file_path, "bytes": mask_file_obj.read()},
            }