File size: 3,266 Bytes
5a009ef
 
 
 
 
 
09039e6
 
 
04a1354
d75821a
09039e6
5a009ef
09039e6
d75821a
 
cc7508e
09039e6
 
5a009ef
 
 
 
 
 
 
 
 
 
acc9038
5a009ef
 
 
cc7508e
4973c42
acc9038
 
 
 
 
 
 
 
 
 
 
 
 
 
5a009ef
 
 
 
8244ccb
5a009ef
 
 
 
 
 
 
 
 
09039e6
d75821a
5a009ef
 
acc9038
d75821a
5a009ef
 
 
 
 
 
 
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
import io
from PIL import Image
from datasets import GeneratorBasedBuilder, DatasetInfo, Features, SplitGenerator, Value, Array2D, Split
import datasets
import numpy as np
import h5py
class CustomConfig(datasets.BuilderConfig):
    def __init__(self, **kwargs):
        super(CustomConfig, self).__init__(**kwargs)
        self.dataset_type = kwargs.pop("name", "all")
        

class RGBSemanticDepthDataset(GeneratorBasedBuilder):
    BUILDER_CONFIGS = [
        CustomConfig(name="all", version="1.0.0", description="all"),
        CustomConfig(name="depth", version="1.0.0", description="depth"),
        CustomConfig(name="seg", version="1.0.0", description="segmentation"),
    ]    # Configs initialization
    BUILDER_CONFIG_CLASS = CustomConfig
    def _info(self):
        return DatasetInfo(
            features=Features({
                "left_rgb": datasets.Image(),
                "right_rgb": datasets.Image(),
                "left_seg": datasets.Image(),
                "left_depth": datasets.Image(),
                "right_depth": datasets.Image(),
            })
        )
    def _h5_loader(self, bytes_stream, type_dataset):
        # Reference: https://github.com/dwofk/fast-depth/blob/master/dataloaders/dataloader.py#L8-L13
        f = io.BytesIO(bytes_stream)
        h5f = h5py.File(f, "r")
        print(type_dataset)
        left_rgb = self._read_jpg(h5f['rgb_left'][:])
        if type_dataset == 'depth':
            right_rgb = self._read_jpg(h5f['rgb_right'][:])
            left_depth = h5f['depth_left'][:].astype(np.float32)
            right_depth = h5f['depth_right'][:].astype(np.float32)
            return left_rgb, right_rgb, np.zeros((1,1)), left_depth, right_depth
        elif type_dataset == 'seg':
            left_seg = h5f['seg_left'][:]
            return left_rgb, np.zeros((1,1)), left_seg, np.zeros((1,1)), np.zeros((1,1))
        else:
            right_rgb = self._read_jpg(h5f['rgb_right'][:])
            left_seg = h5f['seg_left'][:]
            left_depth = h5f['depth_left'][:].astype(np.float32)
            right_depth = h5f['depth_right'][:].astype(np.float32)
            return left_rgb, right_rgb, left_seg, left_depth, right_depth
    def _read_jpg(self, bytes_stream):
        return Image.open(io.BytesIO(bytes_stream))
    
    def _split_generators(self, dl_manager):
        archives = dl_manager.download({"train":["data/images_1730238419.175364.tar"]})
        return [
            SplitGenerator(
                name=Split.TRAIN,
                gen_kwargs={
                    "archives": [dl_manager.iter_archive(archive) for archive in archives["train"]],
                },
            ),
        ]

    def _generate_examples(self, archives):
        print(self.config.dataset_type)
        for archive in archives:
            for path, file in archive:
                #print(path)
                left_rgb, right_rgb, left_seg, left_depth, right_depth = self._h5_loader(file.read(), self.config.dataset_type)
                yield path, {
                    "left_rgb": left_rgb,
                    "right_rgb": right_rgb,
                    "left_seg": left_seg,
                    "left_depth": left_depth,
                    "right_depth": right_depth,
                }