AnaChikashua
commited on
Commit
•
487936c
1
Parent(s):
0de8685
Create handwriting.py
Browse files- handwriting.py +75 -0
handwriting.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import struct
|
2 |
+
|
3 |
+
import numpy as np
|
4 |
+
|
5 |
+
import datasets
|
6 |
+
from datasets.tasks import ImageClassification
|
7 |
+
|
8 |
+
|
9 |
+
_CITATION = """\
|
10 |
+
@article{lecun2010mnist,
|
11 |
+
title={MNIST handwritten digit database},
|
12 |
+
author={LeCun, Yann and Cortes, Corinna and Burges, CJ},
|
13 |
+
journal={ATT Labs [Online]. Available: http://yann.lecun.com/exdb/mnist},
|
14 |
+
volume={2},
|
15 |
+
year={2010}
|
16 |
+
}
|
17 |
+
"""
|
18 |
+
|
19 |
+
_DESCRIPTION = """\
|
20 |
+
The MNIST dataset consists of 70,000 28x28 black-and-white images in 10 classes (one for each digits), with 7,000
|
21 |
+
images per class. There are 60,000 training images and 10,000 test images.
|
22 |
+
"""
|
23 |
+
_URL = "https://huggingface.co/datasets/AnaChikashua/handwriting/resolve/main/handwriting_dataset.zip"
|
24 |
+
_NAMES = ['ა', 'ბ', 'გ', 'დ', 'ე', 'ვ', 'ზ', 'თ', 'ი', 'კ', 'ლ', 'მ', 'ნ', 'ო', 'პ', 'ჟ', 'რ', 'ს', 'ტ', 'უ', 'ფ', 'ქ', 'ღ', 'ყ', 'შ', 'ჩ', 'ც', 'ძ', 'წ', 'ჭ', 'ხ', 'ჯ', 'ჰ']
|
25 |
+
|
26 |
+
|
27 |
+
class MNIST(datasets.GeneratorBasedBuilder):
|
28 |
+
"""MNIST Data Set"""
|
29 |
+
|
30 |
+
BUILDER_CONFIGS = [
|
31 |
+
datasets.BuilderConfig(
|
32 |
+
name="data",
|
33 |
+
version=datasets.Version("1.0.0"),
|
34 |
+
description=_DESCRIPTION,
|
35 |
+
)
|
36 |
+
]
|
37 |
+
|
38 |
+
def _info(self):
|
39 |
+
return datasets.DatasetInfo(
|
40 |
+
description=_DESCRIPTION,
|
41 |
+
features=datasets.Features(
|
42 |
+
{
|
43 |
+
"image": datasets.Image(),
|
44 |
+
"label": datasets.features.ClassLabel(names=_NAMES),
|
45 |
+
}
|
46 |
+
),
|
47 |
+
supervised_keys=("image", "label"),
|
48 |
+
citation=_CITATION,
|
49 |
+
task_templates=[
|
50 |
+
ImageClassification(
|
51 |
+
image_column="image",
|
52 |
+
label_column="label",
|
53 |
+
)
|
54 |
+
],
|
55 |
+
)
|
56 |
+
|
57 |
+
def _split_generators(self, dl_manager):
|
58 |
+
path = dl_manager.dowload(_URL)
|
59 |
+
image_iters = dl_manager.iter_archive(path)
|
60 |
+
return [
|
61 |
+
datasets.SplitGenerator(
|
62 |
+
name=datasets.Split.TRAIN,
|
63 |
+
gen_kwargs={"images": image_iters}
|
64 |
+
),
|
65 |
+
]
|
66 |
+
|
67 |
+
def _generate_examples(self, images):
|
68 |
+
"""This function returns the examples in the raw form."""
|
69 |
+
for idx, filepath, image in enumerate(images):
|
70 |
+
# extract the text from the filename
|
71 |
+
text = [c for c in str(filepath) if not 0 <= ord(c) <= 127][0]
|
72 |
+
yield idx, {
|
73 |
+
"label": text,
|
74 |
+
"image": {"path": filepath, "bytes": image.read()}
|
75 |
+
}
|