Create kmnist.py
Browse files
kmnist.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import datasets
|
| 3 |
+
from PIL import Image
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class KMNIST(datasets.GeneratorBasedBuilder):
|
| 7 |
+
"""Kuzushiji-MNIST and Kuzushiji-49 datasets."""
|
| 8 |
+
VERSION = datasets.Version("1.0.0")
|
| 9 |
+
|
| 10 |
+
BUILDER_CONFIGS = [
|
| 11 |
+
datasets.BuilderConfig(name="kmnist", description="Kuzushiji-MNIST dataset with 10 classes."),
|
| 12 |
+
datasets.BuilderConfig(name="k49mnist", description="Kuzushiji-49 dataset with 49 classes."),
|
| 13 |
+
]
|
| 14 |
+
|
| 15 |
+
def _info(self):
|
| 16 |
+
if self.config.name == "kmnist":
|
| 17 |
+
num_classes = 10
|
| 18 |
+
else:
|
| 19 |
+
num_classes = 49
|
| 20 |
+
return datasets.DatasetInfo(
|
| 21 |
+
description="Kuzushiji-MNIST and Kuzushiji-49 datasets.",
|
| 22 |
+
features=datasets.Features({
|
| 23 |
+
"image": datasets.Image(), # Automatically converts to PIL.Image
|
| 24 |
+
"label": datasets.ClassLabel(num_classes=num_classes),
|
| 25 |
+
}),
|
| 26 |
+
supervised_keys=("image", "label"),
|
| 27 |
+
license="CC BY-SA 4.0",
|
| 28 |
+
homepage="https://github.com/rois-codh/kmnist",
|
| 29 |
+
citation="""
|
| 30 |
+
@online{clanuwat2018deep,
|
| 31 |
+
author = {Tarin Clanuwat and Mikel Bober-Irizar and Asanobu Kitamoto and Alex Lamb and Kazuaki Yamamoto and David Ha},
|
| 32 |
+
title = {Deep Learning for Classical Japanese Literature},
|
| 33 |
+
date = {2018-12-03},
|
| 34 |
+
year = {2018},
|
| 35 |
+
eprintclass = {cs.CV},
|
| 36 |
+
eprinttype = {arXiv},
|
| 37 |
+
eprint = {cs.CV/1812.01718},
|
| 38 |
+
}
|
| 39 |
+
"""
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
def _split_generators(self, dl_manager):
|
| 43 |
+
urls = {
|
| 44 |
+
"kmnist": {
|
| 45 |
+
"train_imgs": "http://codh.rois.ac.jp/kmnist/dataset/kmnist/kmnist-train-imgs.npz",
|
| 46 |
+
"train_labels": "http://codh.rois.ac.jp/kmnist/dataset/kmnist/kmnist-train-labels.npz",
|
| 47 |
+
"test_imgs": "http://codh.rois.ac.jp/kmnist/dataset/kmnist/kmnist-test-imgs.npz",
|
| 48 |
+
"test_labels": "http://codh.rois.ac.jp/kmnist/dataset/kmnist/kmnist-test-labels.npz",
|
| 49 |
+
},
|
| 50 |
+
"k49mnist": {
|
| 51 |
+
"train_imgs": "http://codh.rois.ac.jp/kmnist/dataset/k49/k49-train-imgs.npz",
|
| 52 |
+
"train_labels": "http://codh.rois.ac.jp/kmnist/dataset/k49/k49-train-labels.npz",
|
| 53 |
+
"test_imgs": "http://codh.rois.ac.jp/kmnist/dataset/k49/k49-test-imgs.npz",
|
| 54 |
+
"test_labels": "http://codh.rois.ac.jp/kmnist/dataset/k49/k49-test-labels.npz",
|
| 55 |
+
},
|
| 56 |
+
}
|
| 57 |
+
selected_urls = urls[self.config.name]
|
| 58 |
+
downloaded_files = dl_manager.download(selected_urls)
|
| 59 |
+
|
| 60 |
+
return [
|
| 61 |
+
datasets.SplitGenerator(
|
| 62 |
+
name=datasets.Split.TRAIN,
|
| 63 |
+
gen_kwargs={
|
| 64 |
+
"images_path": downloaded_files["train_imgs"],
|
| 65 |
+
"labels_path": downloaded_files["train_labels"]
|
| 66 |
+
}
|
| 67 |
+
),
|
| 68 |
+
datasets.SplitGenerator(
|
| 69 |
+
name=datasets.Split.TEST,
|
| 70 |
+
gen_kwargs={
|
| 71 |
+
"images_path": downloaded_files["test_imgs"],
|
| 72 |
+
"labels_path": downloaded_files["test_labels"]
|
| 73 |
+
}
|
| 74 |
+
),
|
| 75 |
+
]
|
| 76 |
+
|
| 77 |
+
def _generate_examples(self, images_path, labels_path):
|
| 78 |
+
images = np.load(images_path)["arr_0"]
|
| 79 |
+
labels = np.load(labels_path)["arr_0"]
|
| 80 |
+
|
| 81 |
+
for idx, (image, label) in enumerate(zip(images, labels)):
|
| 82 |
+
# Convert each image to a PIL.Image object
|
| 83 |
+
image = Image.fromarray(image, mode="L") # Mode "L" for grayscale images
|
| 84 |
+
yield idx, {"image": image, "label": int(label)}
|