sayakpaul HF staff commited on
Commit
fb20c38
1 Parent(s): 58d9de7

initial commit.

Browse files
Files changed (1) hide show
  1. nyu_depth_v2.py +134 -0
nyu_depth_v2.py ADDED
@@ -0,0 +1,134 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2022 The HuggingFace Datasets Authors and the current dataset script contributor.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ """NYU-Depth V2."""
15
+
16
+
17
+ import os
18
+
19
+ import datasets
20
+ import h5py
21
+ import numpy as np
22
+
23
+ _CITATION = """\
24
+ @inproceedings{Silberman:ECCV12,
25
+ author = {Nathan Silberman, Derek Hoiem, Pushmeet Kohli and Rob Fergus},
26
+ title = {Indoor Segmentation and Support Inference from RGBD Images},
27
+ booktitle = {ECCV},
28
+ year = {2012}
29
+ }
30
+ @inproceedings{icra_2019_fastdepth,
31
+ author = {Wofk, Diana and Ma, Fangchang and Yang, Tien-Ju and Karaman, Sertac and Sze, Vivienne},
32
+ title = {FastDepth: Fast Monocular Depth Estimation on Embedded Systems},
33
+ booktitle = {IEEE International Conference on Robotics and Automation (ICRA)},
34
+ year = {2019}
35
+ }
36
+ """
37
+
38
+ _DESCRIPTION = """\
39
+ 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.
40
+ """
41
+
42
+ _HOMEPAGE = "https://cs.nyu.edu/~silberman/datasets/nyu_depth_v2.html"
43
+
44
+ _LICENSE = "Apace 2.0 License"
45
+
46
+ _URLS = {
47
+ "depth_estimation": {
48
+ "train/val": "http://datasets.lids.mit.edu/fastdepth/data/nyudepthv2.tar.gz",
49
+ }
50
+ }
51
+
52
+ _IMG_EXTENSIONS = [".h5"]
53
+
54
+
55
+ class NYUDepthV2(datasets.GeneratorBasedBuilder):
56
+ """NYU-Depth V2 dataset."""
57
+
58
+ VERSION = datasets.Version("1.0.0")
59
+
60
+ BUILDER_CONFIGS = [
61
+ datasets.BuilderConfig(
62
+ name="depth_estimation",
63
+ version=VERSION,
64
+ description="The depth estimation variant.",
65
+ ),
66
+ ]
67
+
68
+ DEFAULT_CONFIG_NAME = "depth_estimation"
69
+
70
+ def _info(self):
71
+ features = datasets.Features(
72
+ {"image": datasets.Image(), "depth_map": datasets.Image()}
73
+ )
74
+ return datasets.DatasetInfo(
75
+ description=_DESCRIPTION,
76
+ features=features,
77
+ homepage=_HOMEPAGE,
78
+ license=_LICENSE,
79
+ citation=_CITATION,
80
+ )
81
+
82
+ def _is_image_file(self, filename):
83
+ # Reference: https://github.com/dwofk/fast-depth/blob/master/dataloaders/dataloader.py#L21-L23
84
+ return any(filename.endswith(extension) for extension in _IMG_EXTENSIONS)
85
+
86
+ def _get_file_paths(self, dir):
87
+ # Reference: https://github.com/dwofk/fast-depth/blob/master/dataloaders/dataloader.py#L31-L44
88
+ file_paths = []
89
+ dir = os.path.expanduser(dir)
90
+
91
+ for target in sorted(os.listdir(dir)):
92
+ d = os.path.join(dir, target)
93
+ if not os.path.isdir(d):
94
+ continue
95
+ for root, _, fnames in sorted(os.walk(d)):
96
+ for fname in sorted(fnames):
97
+ if self._is_image_file(fname):
98
+ path = os.path.join(root, fname)
99
+ file_paths.append(path)
100
+
101
+ return file_paths
102
+
103
+ def _h5_loader(self, path):
104
+ # Reference: https://github.com/dwofk/fast-depth/blob/master/dataloaders/dataloader.py#L8-L13
105
+ h5f = h5py.File(path, "r")
106
+ rgb = np.array(h5f["rgb"])
107
+ rgb = np.transpose(rgb, (1, 2, 0))
108
+ depth = np.array(h5f["depth"])
109
+ return rgb, depth
110
+
111
+ def _split_generators(self, dl_manager):
112
+ urls = _URLS[self.config.name]
113
+ base_path = dl_manager.download_and_extract(urls)
114
+
115
+ train_data_files = self._get_file_paths(
116
+ os.path.join(base_path, "nyudepthv2", "train")
117
+ )
118
+ val_data_files = self._get_file_paths(os.path.join(base_path, "nyudepthv2" "val"))
119
+
120
+ return [
121
+ datasets.SplitGenerator(
122
+ name=datasets.Split.TRAIN,
123
+ gen_kwargs={"data": train_data_files, "split": "training"},
124
+ ),
125
+ datasets.SplitGenerator(
126
+ name=datasets.Split.VALIDATION,
127
+ gen_kwargs={"data": val_data_files, "split": "validation"},
128
+ ),
129
+ ]
130
+
131
+ def _generate_examples(self, filepaths):
132
+ for idx, filepath in enumerate(filepaths):
133
+ image, depth = self._h5_loader(filepath)
134
+ yield idx, {"image": image, "depth_map": depth}