Datasets:

Modalities:
Image
Languages:
English
ArXiv:
Libraries:
Datasets
License:
sayakpaul HF staff commited on
Commit
592761d
1 Parent(s): 12c54ce

feat: dataloader with multiple shards.

Browse files
Files changed (1) hide show
  1. nyu_depth_v2.py +31 -46
nyu_depth_v2.py CHANGED
@@ -14,7 +14,7 @@
14
  """NYU-Depth V2."""
15
 
16
 
17
- import os
18
 
19
  import datasets
20
  import h5py
@@ -44,9 +44,14 @@ _HOMEPAGE = "https://cs.nyu.edu/~silberman/datasets/nyu_depth_v2.html"
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"]
@@ -57,16 +62,6 @@ class NYUDepthV2(datasets.GeneratorBasedBuilder):
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()}
@@ -83,52 +78,42 @@ class NYUDepthV2(datasets.GeneratorBasedBuilder):
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)["train/val"]
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={"filepaths": train_data_files},
 
 
 
 
124
  ),
125
  datasets.SplitGenerator(
126
  name=datasets.Split.VALIDATION,
127
- gen_kwargs={"filepaths": val_data_files},
 
 
 
 
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}
 
 
 
 
 
14
  """NYU-Depth V2."""
15
 
16
 
17
+ import io
18
 
19
  import datasets
20
  import h5py
 
44
  _LICENSE = "Apace 2.0 License"
45
 
46
  _URLS = {
47
+ "train": [
48
+ f"https://huggingface.co/datasets/sayakpaul/nyu_depth_v2/resolve/main/data/train-{i:06d}.tar"
49
+ for i in range(12)
50
+ ],
51
+ "val": [
52
+ f"https://huggingface.co/datasets/sayakpaul/nyu_depth_v2/resolve/main/data/val-{i:06d}.tar"
53
+ for i in range(2)
54
+ ],
55
  }
56
 
57
  _IMG_EXTENSIONS = [".h5"]
 
62
 
63
  VERSION = datasets.Version("1.0.0")
64
 
 
 
 
 
 
 
 
 
 
 
65
  def _info(self):
66
  features = datasets.Features(
67
  {"image": datasets.Image(), "depth_map": datasets.Image()}
 
78
  # Reference: https://github.com/dwofk/fast-depth/blob/master/dataloaders/dataloader.py#L21-L23
79
  return any(filename.endswith(extension) for extension in _IMG_EXTENSIONS)
80
 
81
+ def _h5_loader(self, bytes_stream):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
  # Reference: https://github.com/dwofk/fast-depth/blob/master/dataloaders/dataloader.py#L8-L13
83
+ f = io.BytesIO(bytes_stream)
84
+ h5f = h5py.File(f, "r")
85
  rgb = np.array(h5f["rgb"])
86
  rgb = np.transpose(rgb, (1, 2, 0))
87
  depth = np.array(h5f["depth"])
88
  return rgb, depth
89
 
90
  def _split_generators(self, dl_manager):
91
+ archives = dl_manager.download(_URLS)
 
 
 
 
 
 
92
 
93
  return [
94
  datasets.SplitGenerator(
95
  name=datasets.Split.TRAIN,
96
+ gen_kwargs={
97
+ "archives": [
98
+ dl_manager.iter_archive(archive) for archive in archives["train"]
99
+ ]
100
+ },
101
  ),
102
  datasets.SplitGenerator(
103
  name=datasets.Split.VALIDATION,
104
+ gen_kwargs={
105
+ "archives": [
106
+ dl_manager.iter_archive(archive) for archive in archives["val"]
107
+ ]
108
+ },
109
  ),
110
  ]
111
 
112
+ def _generate_examples(self, archives):
113
+ idx = 0
114
+ for archive in archives:
115
+ for path, file in archive:
116
+ if self._is_image_file(path):
117
+ image, depth = self._h5_loader(file.read())
118
+ yield idx, {"image": image, "depth_map": depth}
119
+ idx += 1