Create fashion-mnist.py
Browse files- fashion-mnist.py +69 -0
fashion-mnist.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gzip
|
2 |
+
import numpy as np
|
3 |
+
import datasets
|
4 |
+
|
5 |
+
|
6 |
+
class FashionMNIST(datasets.GeneratorBasedBuilder):
|
7 |
+
"""Grayscale image classification.
|
8 |
+
|
9 |
+
`Fashion-MNIST` is a dataset of Zalando's article images consisting of a training set of 60,000 examples
|
10 |
+
and a test set of 10,000 examples. Each example is a 28x28 grayscale image, associated with a label from 10 classes.
|
11 |
+
"""
|
12 |
+
|
13 |
+
VERSION = datasets.Version("1.0.0")
|
14 |
+
|
15 |
+
def _info(self):
|
16 |
+
return datasets.DatasetInfo(
|
17 |
+
description="Fashion-MNIST is a dataset of Zalando's article images for image classification tasks.",
|
18 |
+
features=datasets.Features(
|
19 |
+
{
|
20 |
+
"image": datasets.Image(),
|
21 |
+
"label": datasets.ClassLabel(names=[
|
22 |
+
"T-shirt/top", "Trouser", "Pullover", "Dress", "Coat",
|
23 |
+
"Sandal", "Shirt", "Sneaker", "Bag", "Ankle boot"
|
24 |
+
])
|
25 |
+
}
|
26 |
+
),
|
27 |
+
supervised_keys=("image", "label"),
|
28 |
+
homepage="https://github.com/zalandoresearch/fashion-mnist",
|
29 |
+
license="MIT License",
|
30 |
+
citation="""@article{xiao2017fashion,
|
31 |
+
title={Fashion-MNIST: a Novel Image Dataset for Benchmarking Machine Learning Algorithms},
|
32 |
+
author={Xiao, Han and Rasul, Kashif and Vollgraf, Roland},
|
33 |
+
journal={arXiv preprint arXiv:1708.07747},
|
34 |
+
year={2017}}"""
|
35 |
+
)
|
36 |
+
|
37 |
+
def _split_generators(self, dl_manager):
|
38 |
+
urls = {
|
39 |
+
"train_images": "http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-images-idx3-ubyte.gz",
|
40 |
+
"train_labels": "http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-labels-idx1-ubyte.gz",
|
41 |
+
"test_images": "http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-images-idx3-ubyte.gz",
|
42 |
+
"test_labels": "http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-labels-idx1-ubyte.gz",
|
43 |
+
}
|
44 |
+
downloaded_files = dl_manager.download(urls)
|
45 |
+
return [
|
46 |
+
datasets.SplitGenerator(
|
47 |
+
name=datasets.Split.TRAIN,
|
48 |
+
gen_kwargs={
|
49 |
+
"images_path": downloaded_files["train_images"],
|
50 |
+
"labels_path": downloaded_files["train_labels"],
|
51 |
+
},
|
52 |
+
),
|
53 |
+
datasets.SplitGenerator(
|
54 |
+
name=datasets.Split.TEST,
|
55 |
+
gen_kwargs={
|
56 |
+
"images_path": downloaded_files["test_images"],
|
57 |
+
"labels_path": downloaded_files["test_labels"],
|
58 |
+
},
|
59 |
+
),
|
60 |
+
]
|
61 |
+
|
62 |
+
def _generate_examples(self, images_path, labels_path):
|
63 |
+
with gzip.open(images_path, "rb") as img_path:
|
64 |
+
images = np.frombuffer(img_path.read(), dtype=np.uint8, offset=16).reshape(-1, 28, 28)
|
65 |
+
with gzip.open(labels_path, "rb") as lbl_path:
|
66 |
+
labels = np.frombuffer(lbl_path.read(), dtype=np.uint8, offset=8)
|
67 |
+
|
68 |
+
for idx, (image, label) in enumerate(zip(images, labels)):
|
69 |
+
yield idx, {"image": image, "label": label}
|