File size: 3,844 Bytes
fb20c38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
592761d
fb20c38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a918863
 
fb20c38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
592761d
fb20c38
592761d
 
fb20c38
 
 
 
 
 
592761d
fb20c38
 
 
 
592761d
 
 
 
 
fb20c38
 
 
592761d
 
 
 
 
fb20c38
 
 
592761d
 
 
 
 
 
 
 
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
# Copyright 2022 The HuggingFace Datasets Authors and the current dataset script contributor.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""NYU-Depth V2."""


import io

import datasets
import h5py
import numpy as np

_CITATION = """\
@inproceedings{Silberman:ECCV12,
  author    = {Nathan Silberman, Derek Hoiem, Pushmeet Kohli and Rob Fergus},
  title     = {Indoor Segmentation and Support Inference from RGBD Images},
  booktitle = {ECCV},
  year      = {2012}
}
@inproceedings{icra_2019_fastdepth,
  author    = {Wofk, Diana and Ma, Fangchang and Yang, Tien-Ju and Karaman, Sertac and Sze, Vivienne},
  title     = {FastDepth: Fast Monocular Depth Estimation on Embedded Systems},
  booktitle = {IEEE International Conference on Robotics and Automation (ICRA)},
  year      = {2019}
}
"""

_DESCRIPTION = """\
The NYU-Depth V2 data set is comprised of video sequences from a variety of indoor scenes as recorded by both the RGB and Depth cameras from the Microsoft Kinect.
"""

_HOMEPAGE = "https://cs.nyu.edu/~silberman/datasets/nyu_depth_v2.html"

_LICENSE = "Apace 2.0 License"

_URLS = {
    "train": [f"data/train-{i:06d}.tar" for i in range(12)],
    "val": [f"data/val-{i:06d}.tar" for i in range(2)],
}

_IMG_EXTENSIONS = [".h5"]


class NYUDepthV2(datasets.GeneratorBasedBuilder):
    """NYU-Depth V2 dataset."""

    VERSION = datasets.Version("1.0.0")

    def _info(self):
        features = datasets.Features(
            {"image": datasets.Image(), "depth_map": datasets.Image()}
        )
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )

    def _is_image_file(self, filename):
        # Reference: https://github.com/dwofk/fast-depth/blob/master/dataloaders/dataloader.py#L21-L23
        return any(filename.endswith(extension) for extension in _IMG_EXTENSIONS)

    def _h5_loader(self, bytes_stream):
        # Reference: https://github.com/dwofk/fast-depth/blob/master/dataloaders/dataloader.py#L8-L13
        f = io.BytesIO(bytes_stream)
        h5f = h5py.File(f, "r")
        rgb = np.array(h5f["rgb"])
        rgb = np.transpose(rgb, (1, 2, 0))
        depth = np.array(h5f["depth"])
        return rgb, depth

    def _split_generators(self, dl_manager):
        archives = dl_manager.download(_URLS)

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "archives": [
                        dl_manager.iter_archive(archive) for archive in archives["train"]
                    ]
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs={
                    "archives": [
                        dl_manager.iter_archive(archive) for archive in archives["val"]
                    ]
                },
            ),
        ]

    def _generate_examples(self, archives):
        idx = 0
        for archive in archives:
            for path, file in archive:
                if self._is_image_file(path):
                    image, depth = self._h5_loader(file.read())
                    yield idx, {"image": image, "depth_map": depth}
                    idx += 1