Create mnist.py
Browse files
mnist.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import datasets
|
| 3 |
+
import struct
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class MNIST(datasets.GeneratorBasedBuilder):
|
| 7 |
+
"""MNIST Dataset using raw IDX files for digit classification."""
|
| 8 |
+
|
| 9 |
+
VERSION = datasets.Version("1.0.0")
|
| 10 |
+
|
| 11 |
+
def _info(self):
|
| 12 |
+
return datasets.DatasetInfo(
|
| 13 |
+
description="""The MNIST database of handwritten digits, with a training set of 60,000 examples, and a test
|
| 14 |
+
set of 10,000 examples.""",
|
| 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="http://yann.lecun.com/exdb/mnist/",
|
| 23 |
+
citation="""@misc{lecun1998mnist,
|
| 24 |
+
author={Yann LeCun and Corinna Cortes and Christopher J.C. Burges},
|
| 25 |
+
title={The MNIST database of handwritten digits},
|
| 26 |
+
year={1998},
|
| 27 |
+
url={http://yann.lecun.com/exdb/mnist/}
|
| 28 |
+
}""",
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
def _split_generators(self, dl_manager):
|
| 32 |
+
# Download IDX files from the specified links
|
| 33 |
+
urls = {
|
| 34 |
+
"train_images": "https://storage.googleapis.com/cvdf-datasets/mnist/train-images-idx3-ubyte.gz",
|
| 35 |
+
"train_labels": "https://storage.googleapis.com/cvdf-datasets/mnist/train-labels-idx1-ubyte.gz",
|
| 36 |
+
"test_images": "https://storage.googleapis.com/cvdf-datasets/mnist/t10k-images-idx3-ubyte.gz",
|
| 37 |
+
"test_labels": "https://storage.googleapis.com/cvdf-datasets/mnist/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}
|