Create notmnist.py
Browse files- notmnist.py +72 -0
notmnist.py
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import datasets
|
3 |
+
import struct
|
4 |
+
|
5 |
+
|
6 |
+
class NotMNIST(datasets.GeneratorBasedBuilder):
|
7 |
+
"""NotMNIST Dataset that contains images of letters A-J."""
|
8 |
+
|
9 |
+
VERSION = datasets.Version("1.0.0")
|
10 |
+
|
11 |
+
def _info(self):
|
12 |
+
return datasets.DatasetInfo(
|
13 |
+
description="""A dataset that was created by Yaroslav Bulatov by taking some publicly available fonts and
|
14 |
+
extracting glyphs from them to make a dataset similar to MNIST. There are 10 classes, with letters A-J.""",
|
15 |
+
features=datasets.Features(
|
16 |
+
{
|
17 |
+
"image": datasets.Image(),
|
18 |
+
"label": datasets.ClassLabel(num_classes=10),
|
19 |
+
}
|
20 |
+
),
|
21 |
+
supervised_keys=("image", "label"),
|
22 |
+
homepage="https://yaroslavvb.blogspot.com/2011/09/notmnist-dataset.html",
|
23 |
+
citation="""@misc{bulatov2011notmnist,
|
24 |
+
author={Yaroslav Bulatov},
|
25 |
+
title={notMNIST dataset},
|
26 |
+
year={2011},
|
27 |
+
url={http://yaroslavvb.blogspot.com/2011/09/notmnist-dataset.html}
|
28 |
+
}""",
|
29 |
+
)
|
30 |
+
|
31 |
+
def _split_generators(self, dl_manager):
|
32 |
+
# Download IDX files from the specified links
|
33 |
+
urls = {
|
34 |
+
"train_images": "https://github.com/davidflanagan/notMNIST-to-MNIST/raw/refs/heads/master/train-images-idx3-ubyte.gz",
|
35 |
+
"train_labels": "https://github.com/davidflanagan/notMNIST-to-MNIST/raw/refs/heads/master/train-labels-idx1-ubyte.gz",
|
36 |
+
"test_images": "https://github.com/davidflanagan/notMNIST-to-MNIST/raw/refs/heads/master/t10k-images-idx3-ubyte.gz",
|
37 |
+
"test_labels": "https://github.com/davidflanagan/notMNIST-to-MNIST/raw/refs/heads/master/t10k-labels-idx1-ubyte.gz",
|
38 |
+
}
|
39 |
+
downloaded_files = dl_manager.download_and_extract(urls)
|
40 |
+
|
41 |
+
return [
|
42 |
+
datasets.SplitGenerator(
|
43 |
+
name=datasets.Split.TRAIN,
|
44 |
+
gen_kwargs={
|
45 |
+
"images_path": downloaded_files["train_images"],
|
46 |
+
"labels_path": downloaded_files["train_labels"],
|
47 |
+
},
|
48 |
+
),
|
49 |
+
datasets.SplitGenerator(
|
50 |
+
name=datasets.Split.TEST,
|
51 |
+
gen_kwargs={
|
52 |
+
"images_path": downloaded_files["test_images"],
|
53 |
+
"labels_path": downloaded_files["test_labels"],
|
54 |
+
},
|
55 |
+
),
|
56 |
+
]
|
57 |
+
|
58 |
+
def _generate_examples(self, images_path, labels_path):
|
59 |
+
# Read and parse the decompressed IDX files
|
60 |
+
with open(images_path, "rb") as img_file:
|
61 |
+
_, num_images, rows, cols = struct.unpack(">IIII", img_file.read(16))
|
62 |
+
images = np.frombuffer(img_file.read(), dtype=np.uint8).reshape(num_images, rows, cols)
|
63 |
+
|
64 |
+
with open(labels_path, "rb") as lbl_file:
|
65 |
+
_, num_labels = struct.unpack(">II", lbl_file.read(8))
|
66 |
+
labels = np.frombuffer(lbl_file.read(), dtype=np.uint8)
|
67 |
+
|
68 |
+
assert len(images) == len(labels), "Mismatch between image and label counts."
|
69 |
+
|
70 |
+
for idx, (image, label) in enumerate(zip(images, labels)):
|
71 |
+
# Remove channel dimension for PIL compatibility
|
72 |
+
yield idx, {"image": image, "label": label}
|