Commit
•
f54b2b5
1
Parent(s):
5ee4cad
Delete loading script
Browse files
svhn.py
DELETED
@@ -1,199 +0,0 @@
|
|
1 |
-
# coding=utf-8
|
2 |
-
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
|
3 |
-
#
|
4 |
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
5 |
-
# you may not use this file except in compliance with the License.
|
6 |
-
# You may obtain a copy of the License at
|
7 |
-
#
|
8 |
-
# http://www.apache.org/licenses/LICENSE-2.0
|
9 |
-
#
|
10 |
-
# Unless required by applicable law or agreed to in writing, software
|
11 |
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
12 |
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 |
-
# See the License for the specific language governing permissions and
|
14 |
-
# limitations under the License.
|
15 |
-
"""Street View House Numbers (SVHN) dataset."""
|
16 |
-
|
17 |
-
import io
|
18 |
-
import os
|
19 |
-
|
20 |
-
import h5py
|
21 |
-
import numpy as np
|
22 |
-
import scipy.io as sio
|
23 |
-
|
24 |
-
import datasets
|
25 |
-
from datasets.tasks import ImageClassification
|
26 |
-
|
27 |
-
|
28 |
-
logger = datasets.logging.get_logger(__name__)
|
29 |
-
|
30 |
-
|
31 |
-
_CITATION = """\
|
32 |
-
@article{netzer2011reading,
|
33 |
-
title={Reading digits in natural images with unsupervised feature learning},
|
34 |
-
author={Netzer, Yuval and Wang, Tao and Coates, Adam and Bissacco, Alessandro and Wu, Bo and Ng, Andrew Y},
|
35 |
-
year={2011}
|
36 |
-
}
|
37 |
-
"""
|
38 |
-
|
39 |
-
_DESCRIPTION = """\
|
40 |
-
SVHN is a real-world image dataset for developing machine learning and object recognition algorithms with minimal requirement on data preprocessing and formatting.
|
41 |
-
It can be seen as similar in flavor to MNIST (e.g., the images are of small cropped digits), but incorporates an order of magnitude more labeled data (over 600,000 digit images)
|
42 |
-
and comes from a significantly harder, unsolved, real world problem (recognizing digits and numbers in natural scene images). SVHN is obtained from house numbers in Google Street View images.
|
43 |
-
"""
|
44 |
-
|
45 |
-
_HOMEPAGE = "http://ufldl.stanford.edu/housenumbers/"
|
46 |
-
|
47 |
-
_LICENSE = "Custom (non-commercial)"
|
48 |
-
|
49 |
-
_URLs = {
|
50 |
-
"full_numbers": [
|
51 |
-
"http://ufldl.stanford.edu/housenumbers/train.tar.gz",
|
52 |
-
"http://ufldl.stanford.edu/housenumbers/test.tar.gz",
|
53 |
-
"http://ufldl.stanford.edu/housenumbers/extra.tar.gz",
|
54 |
-
],
|
55 |
-
"cropped_digits": [
|
56 |
-
"http://ufldl.stanford.edu/housenumbers/train_32x32.mat",
|
57 |
-
"http://ufldl.stanford.edu/housenumbers/test_32x32.mat",
|
58 |
-
"http://ufldl.stanford.edu/housenumbers/extra_32x32.mat",
|
59 |
-
],
|
60 |
-
}
|
61 |
-
|
62 |
-
_DIGIT_LABELS = [str(num) for num in range(10)]
|
63 |
-
|
64 |
-
|
65 |
-
class SVHN(datasets.GeneratorBasedBuilder):
|
66 |
-
"""Street View House Numbers (SVHN) dataset."""
|
67 |
-
|
68 |
-
VERSION = datasets.Version("1.0.0")
|
69 |
-
|
70 |
-
BUILDER_CONFIGS = [
|
71 |
-
datasets.BuilderConfig(
|
72 |
-
name="full_numbers",
|
73 |
-
version=VERSION,
|
74 |
-
description="Contains the original, variable-resolution, color house-number images with character level bounding boxes.",
|
75 |
-
),
|
76 |
-
datasets.BuilderConfig(
|
77 |
-
name="cropped_digits",
|
78 |
-
version=VERSION,
|
79 |
-
description="Character level ground truth in an MNIST-like format. All digits have been resized to a fixed resolution of 32-by-32 pixels. The original character bounding boxes are extended in the appropriate dimension to become square windows, so that resizing them to 32-by-32 pixels does not introduce aspect ratio distortions. Nevertheless this preprocessing introduces some distracting digits to the sides of the digit of interest.",
|
80 |
-
),
|
81 |
-
]
|
82 |
-
|
83 |
-
def _info(self):
|
84 |
-
if self.config.name == "full_numbers":
|
85 |
-
features = datasets.Features(
|
86 |
-
{
|
87 |
-
"image": datasets.Image(),
|
88 |
-
"digits": datasets.Sequence(
|
89 |
-
{
|
90 |
-
"bbox": datasets.Sequence(datasets.Value("int32"), length=4),
|
91 |
-
"label": datasets.ClassLabel(num_classes=10),
|
92 |
-
}
|
93 |
-
),
|
94 |
-
}
|
95 |
-
)
|
96 |
-
else:
|
97 |
-
features = datasets.Features(
|
98 |
-
{
|
99 |
-
"image": datasets.Image(),
|
100 |
-
"label": datasets.ClassLabel(num_classes=10),
|
101 |
-
}
|
102 |
-
)
|
103 |
-
return datasets.DatasetInfo(
|
104 |
-
description=_DESCRIPTION,
|
105 |
-
features=features,
|
106 |
-
supervised_keys=None,
|
107 |
-
homepage=_HOMEPAGE,
|
108 |
-
license=_LICENSE,
|
109 |
-
citation=_CITATION,
|
110 |
-
task_templates=[ImageClassification(image_column="image", label_column="label")]
|
111 |
-
if self.config.name == "cropped_digits"
|
112 |
-
else None,
|
113 |
-
)
|
114 |
-
|
115 |
-
def _split_generators(self, dl_manager):
|
116 |
-
if self.config.name == "full_numbers":
|
117 |
-
train_archive, test_archive, extra_archive = dl_manager.download(_URLs[self.config.name])
|
118 |
-
for path, f in dl_manager.iter_archive(train_archive):
|
119 |
-
if path.endswith("digitStruct.mat"):
|
120 |
-
train_annot_data = f.read()
|
121 |
-
break
|
122 |
-
for path, f in dl_manager.iter_archive(test_archive):
|
123 |
-
if path.endswith("digitStruct.mat"):
|
124 |
-
test_annot_data = f.read()
|
125 |
-
break
|
126 |
-
for path, f in dl_manager.iter_archive(extra_archive):
|
127 |
-
if path.endswith("digitStruct.mat"):
|
128 |
-
extra_annot_data = f.read()
|
129 |
-
break
|
130 |
-
train_archive = dl_manager.iter_archive(train_archive)
|
131 |
-
test_archive = dl_manager.iter_archive(test_archive)
|
132 |
-
extra_archive = dl_manager.iter_archive(extra_archive)
|
133 |
-
train_filepath, test_filepath, extra_filepath = None, None, None
|
134 |
-
else:
|
135 |
-
train_annot_data, test_annot_data, extra_annot_data = None, None, None
|
136 |
-
train_archive, test_archive, extra_archive = None, None, None
|
137 |
-
train_filepath, test_filepath, extra_filepath = dl_manager.download(_URLs[self.config.name])
|
138 |
-
return [
|
139 |
-
datasets.SplitGenerator(
|
140 |
-
name=datasets.Split.TRAIN,
|
141 |
-
gen_kwargs={
|
142 |
-
"annot_data": train_annot_data,
|
143 |
-
"files": train_archive,
|
144 |
-
"filepath": train_filepath,
|
145 |
-
},
|
146 |
-
),
|
147 |
-
datasets.SplitGenerator(
|
148 |
-
name=datasets.Split.TEST,
|
149 |
-
gen_kwargs={
|
150 |
-
"annot_data": test_annot_data,
|
151 |
-
"files": test_archive,
|
152 |
-
"filepath": test_filepath,
|
153 |
-
},
|
154 |
-
),
|
155 |
-
datasets.SplitGenerator(
|
156 |
-
name="extra",
|
157 |
-
gen_kwargs={
|
158 |
-
"annot_data": extra_annot_data,
|
159 |
-
"files": extra_archive,
|
160 |
-
"filepath": extra_filepath,
|
161 |
-
},
|
162 |
-
),
|
163 |
-
]
|
164 |
-
|
165 |
-
def _generate_examples(self, annot_data, files, filepath):
|
166 |
-
if self.config.name == "full_numbers":
|
167 |
-
|
168 |
-
def _get_digits(bboxes, h5_file):
|
169 |
-
def key_to_values(key, bbox):
|
170 |
-
if bbox[key].shape[0] == 1:
|
171 |
-
return [int(bbox[key][0][0])]
|
172 |
-
else:
|
173 |
-
return [int(h5_file[bbox[key][i][0]][()].item()) for i in range(bbox[key].shape[0])]
|
174 |
-
|
175 |
-
bbox = h5_file[bboxes[0]]
|
176 |
-
assert bbox.keys() == {"height", "left", "top", "width", "label"}
|
177 |
-
bbox_columns = [key_to_values(key, bbox) for key in ["left", "top", "width", "height", "label"]]
|
178 |
-
return [
|
179 |
-
{"bbox": [left, top, width, height], "label": label % 10}
|
180 |
-
for left, top, width, height, label in zip(*bbox_columns)
|
181 |
-
]
|
182 |
-
|
183 |
-
with h5py.File(io.BytesIO(annot_data), "r") as h5_file:
|
184 |
-
for path, f in files:
|
185 |
-
root, ext = os.path.splitext(path)
|
186 |
-
if ext != ".png":
|
187 |
-
continue
|
188 |
-
img_idx = int(os.path.basename(root)) - 1
|
189 |
-
yield img_idx, {
|
190 |
-
"image": {"path": path, "bytes": f.read()},
|
191 |
-
"digits": _get_digits(h5_file["digitStruct/bbox"][img_idx], h5_file),
|
192 |
-
}
|
193 |
-
else:
|
194 |
-
data = sio.loadmat(filepath)
|
195 |
-
for i, (image_array, label) in enumerate(zip(np.rollaxis(data["X"], -1), data["y"])):
|
196 |
-
yield i, {
|
197 |
-
"image": image_array,
|
198 |
-
"label": label.item() % 10,
|
199 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|