filwsyl commited on
Commit
4d5dac5
1 Parent(s): 088a211

Upload mnist.py

Browse files
Files changed (1) hide show
  1. mnist.py +120 -0
mnist.py ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2020 The TensorFlow Datasets Authors and the HuggingFace Datasets Authors.
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
+
16
+ # Lint as: python3
17
+ """MNIST Data Set"""
18
+
19
+
20
+ import struct
21
+
22
+ import numpy as np
23
+
24
+ import datasets
25
+ from datasets.tasks import ImageClassification
26
+
27
+
28
+ _CITATION = """\
29
+ @article{lecun2010mnist,
30
+ title={MNIST handwritten digit database},
31
+ author={LeCun, Yann and Cortes, Corinna and Burges, CJ},
32
+ journal={ATT Labs [Online]. Available: http://yann.lecun.com/exdb/mnist},
33
+ volume={2},
34
+ year={2010}
35
+ }
36
+ """
37
+
38
+ _DESCRIPTION = """\
39
+ The MNIST dataset consists of 70,000 28x28 black-and-white images in 10 classes (one for each digits), with 7,000
40
+ images per class. There are 60,000 training images and 10,000 test images.
41
+ """
42
+
43
+ _URL = "https://storage.googleapis.com/cvdf-datasets/mnist/"
44
+ _URLS = {
45
+ "train_images": "train-images-idx3-ubyte.gz",
46
+ "train_labels": "train-labels-idx1-ubyte.gz",
47
+ "test_images": "t10k-images-idx3-ubyte.gz",
48
+ "test_labels": "t10k-labels-idx1-ubyte.gz",
49
+ }
50
+
51
+
52
+ class MNIST(datasets.GeneratorBasedBuilder):
53
+ """MNIST Data Set"""
54
+
55
+ BUILDER_CONFIGS = [
56
+ datasets.BuilderConfig(
57
+ name="mnist",
58
+ version=datasets.Version("1.0.0"),
59
+ description=_DESCRIPTION,
60
+ )
61
+ ]
62
+
63
+ def _info(self):
64
+ return datasets.DatasetInfo(
65
+ description=_DESCRIPTION,
66
+ features=datasets.Features(
67
+ {
68
+ "image": datasets.Image(),
69
+ "label": datasets.features.ClassLabel(names=["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]),
70
+ }
71
+ ),
72
+ supervised_keys=("image", "label"),
73
+ homepage="http://yann.lecun.com/exdb/mnist/",
74
+ citation=_CITATION,
75
+ task_templates=[
76
+ ImageClassification(
77
+ image_column="image",
78
+ label_column="label",
79
+ )
80
+ ],
81
+ )
82
+
83
+ def _split_generators(self, dl_manager):
84
+ urls_to_download = {key: _URL + fname for key, fname in _URLS.items()}
85
+ downloaded_files = dl_manager.download_and_extract(urls_to_download)
86
+ return [
87
+ datasets.SplitGenerator(
88
+ name=datasets.Split.TRAIN,
89
+ gen_kwargs={
90
+ "filepath": [downloaded_files["train_images"], downloaded_files["train_labels"]],
91
+ "split": "train",
92
+ },
93
+ ),
94
+ datasets.SplitGenerator(
95
+ name=datasets.Split.TEST,
96
+ gen_kwargs={
97
+ "filepath": [downloaded_files["test_images"], downloaded_files["test_labels"]],
98
+ "split": "test",
99
+ },
100
+ ),
101
+ ]
102
+
103
+ def _generate_examples(self, filepath, split):
104
+ """This function returns the examples in the raw form."""
105
+ # Images
106
+ with open(filepath[0], "rb") as f:
107
+ # First 16 bytes contain some metadata
108
+ _ = f.read(4)
109
+ size = struct.unpack(">I", f.read(4))[0]
110
+ _ = f.read(8)
111
+ images = np.frombuffer(f.read(), dtype=np.uint8).reshape(size, 28, 28)
112
+
113
+ # Labels
114
+ with open(filepath[1], "rb") as f:
115
+ # First 8 bytes contain some metadata
116
+ _ = f.read(8)
117
+ labels = np.frombuffer(f.read(), dtype=np.uint8)
118
+
119
+ for idx in range(size):
120
+ yield idx, {"image": images[idx], "label": str(labels[idx])}