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|
+
"""MNIST text dataset with no spaces."""
|
| 3 |
+
|
| 4 |
+
from __future__ import absolute_import, division, print_function
|
| 5 |
+
|
| 6 |
+
import json
|
| 7 |
+
import os
|
| 8 |
+
import math
|
| 9 |
+
|
| 10 |
+
import numpy as np
|
| 11 |
+
import datasets
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
_DESCRIPTION = """\
|
| 15 |
+
MNIST dataset adapted to a text-based representation.
|
| 16 |
+
|
| 17 |
+
This allows testing interpolation quality for Transformer-VAEs.
|
| 18 |
+
|
| 19 |
+
System is heavily inspired by Matthew Rayfield's work https://youtu.be/Z9K3cwSL6uM
|
| 20 |
+
|
| 21 |
+
Works by quantising each MNIST pixel into one of 64 characters.
|
| 22 |
+
Every sample has an up & down version to encourage the model to learn rotation invarient features.
|
| 23 |
+
|
| 24 |
+
Use `.array_to_text(` and `.text_to_array(` methods to test your generated data.
|
| 25 |
+
|
| 26 |
+
Removed spaces to get better BPE compression on sequences.
|
| 27 |
+
**Should only be used with a trained tokenizer.**
|
| 28 |
+
|
| 29 |
+
Data format:
|
| 30 |
+
- text: (30 x 28 tokens, 840 tokens total): Textual representation of MNIST digit, for example:
|
| 31 |
+
```
|
| 32 |
+
00down!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
| 33 |
+
01down!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
| 34 |
+
02down!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
| 35 |
+
03down!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
| 36 |
+
04down!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
| 37 |
+
05down!!!!!!!!!!!!!%%%@CL'Ja^@!!!!
|
| 38 |
+
06down!!!!!!!!(*8GK`````YL`]Q1!!!!
|
| 39 |
+
07down!!!!!!!-\\````````_855/*!!!!!
|
| 40 |
+
08down!!!!!!!%W`````RN^]!!!!!!!!!!
|
| 41 |
+
09down!!!!!!!!5H;``T#!+G!!!!!!!!!!
|
| 42 |
+
10down!!!!!!!!!$!G`7!!!!!!!!!!!!!!
|
| 43 |
+
11down!!!!!!!!!!!C`P!!!!!!!!!!!!!!
|
| 44 |
+
12down!!!!!!!!!!!#P`2!!!!!!!!!!!!!
|
| 45 |
+
13down!!!!!!!!!!!!)]YI<!!!!!!!!!!!
|
| 46 |
+
14down!!!!!!!!!!!!!5]``>'!!!!!!!!!
|
| 47 |
+
15down!!!!!!!!!!!!!!,O``F'!!!!!!!!
|
| 48 |
+
16down!!!!!!!!!!!!!!!%8``O!!!!!!!!
|
| 49 |
+
17down!!!!!!!!!!!!!!!!!_`_1!!!!!!!
|
| 50 |
+
18down!!!!!!!!!!!!!!,AN``T!!!!!!!!
|
| 51 |
+
19down!!!!!!!!!!!!*FZ```_N!!!!!!!!
|
| 52 |
+
20down!!!!!!!!!!'=X````S4!!!!!!!!!
|
| 53 |
+
21down!!!!!!!!&1V````R5!!!!!!!!!!!
|
| 54 |
+
22down!!!!!!%KW````Q5#!!!!!!!!!!!!
|
| 55 |
+
23down!!!!.LY````^B#!!!!!!!!!!!!!!
|
| 56 |
+
24down!!!!C```VBB%!!!!!!!!!!!!!!!!
|
| 57 |
+
25down!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
| 58 |
+
26down!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
| 59 |
+
27down!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
| 60 |
+
```
|
| 61 |
+
- label: Just a number with the texts matching label.
|
| 62 |
+
|
| 63 |
+
"""
|
| 64 |
+
|
| 65 |
+
_CITATION = """\
|
| 66 |
+
@dataset{dataset,
|
| 67 |
+
author = {Fraser Greenlee},
|
| 68 |
+
year = {2021},
|
| 69 |
+
month = {2},
|
| 70 |
+
pages = {},
|
| 71 |
+
title = {MNIST text dataset (no spaces).},
|
| 72 |
+
doi = {}
|
| 73 |
+
}
|
| 74 |
+
"""
|
| 75 |
+
|
| 76 |
+
_TRAIN_DOWNLOAD_URL = "https://raw.githubusercontent.com/Fraser-Greenlee/my-huggingface-datasets/master/data/mnist-text-no-spaces/train.json.zip"
|
| 77 |
+
_TEST_DOWNLOAD_URL = "https://raw.githubusercontent.com/Fraser-Greenlee/my-huggingface-datasets/master/data/mnist-text-no-spaces/test.json"
|
| 78 |
+
|
| 79 |
+
LABELS = list(range(10))
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
class MnistText(datasets.GeneratorBasedBuilder):
|
| 83 |
+
"""MNIST represented by text."""
|
| 84 |
+
def array_to_text(pixels: np.array):
|
| 85 |
+
'''
|
| 86 |
+
Takes a 2D array of pixel brightness, converts to text using 64 tokens to represent all brightness values.
|
| 87 |
+
'''
|
| 88 |
+
width = pixels.shape[0]
|
| 89 |
+
height = pixels.shape[1]
|
| 90 |
+
|
| 91 |
+
lines = []
|
| 92 |
+
|
| 93 |
+
for y in range(height):
|
| 94 |
+
split = ['%02d down' % y]
|
| 95 |
+
|
| 96 |
+
for x in range(width):
|
| 97 |
+
brightness = pixels[y, x]
|
| 98 |
+
|
| 99 |
+
mBrightness = math.floor(brightness * 64)
|
| 100 |
+
s = chr(mBrightness + 33)
|
| 101 |
+
|
| 102 |
+
split.append(s)
|
| 103 |
+
|
| 104 |
+
lines.append(' '.join(split))
|
| 105 |
+
|
| 106 |
+
reversed = []
|
| 107 |
+
for line in lines:
|
| 108 |
+
reversed.insert(0, (line.replace(' down ', ' up ', 1)))
|
| 109 |
+
|
| 110 |
+
return ['\n'.join(lines), '\n'.join(reversed)]
|
| 111 |
+
|
| 112 |
+
def text_to_array(text: str):
|
| 113 |
+
lines = text.split('\n')
|
| 114 |
+
pixels = np.zeros((len(lines), len(lines[0].split(' ')) - 2))
|
| 115 |
+
|
| 116 |
+
for y, line in enumerate(lines):
|
| 117 |
+
tokens = line.split(' ')
|
| 118 |
+
assert(tokens[1] == 'down')
|
| 119 |
+
pixel_tokens = tokens[2:]
|
| 120 |
+
for x, token in enumerate(pixel_tokens):
|
| 121 |
+
pixels[y, x] = (ord(token) - 33) / 64
|
| 122 |
+
|
| 123 |
+
return pixels
|
| 124 |
+
|
| 125 |
+
def _info(self):
|
| 126 |
+
return datasets.DatasetInfo(
|
| 127 |
+
description=_DESCRIPTION,
|
| 128 |
+
features=datasets.Features(
|
| 129 |
+
{
|
| 130 |
+
'label': datasets.features.ClassLabel(names=LABELS),
|
| 131 |
+
'text': datasets.Value("string"),
|
| 132 |
+
}
|
| 133 |
+
),
|
| 134 |
+
homepage="https://github.com/Fraser-Greenlee/my-huggingface-datasets",
|
| 135 |
+
citation=_CITATION,
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
def _split_generators(self, dl_manager):
|
| 139 |
+
train_path = dl_manager.download_and_extract(_TRAIN_DOWNLOAD_URL)
|
| 140 |
+
test_path = dl_manager.download_and_extract(_TEST_DOWNLOAD_URL)
|
| 141 |
+
return [
|
| 142 |
+
datasets.SplitGenerator(
|
| 143 |
+
name=datasets.Split.TRAIN, gen_kwargs={"filepath": os.path.join(train_path, 'train.json')}
|
| 144 |
+
),
|
| 145 |
+
datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": test_path}),
|
| 146 |
+
]
|
| 147 |
+
|
| 148 |
+
def _generate_examples(self, filepath):
|
| 149 |
+
"""Generate examples."""
|
| 150 |
+
with open(filepath, encoding="utf-8") as json_lines_file:
|
| 151 |
+
data = []
|
| 152 |
+
for line in json_lines_file:
|
| 153 |
+
data.append(json.loads(line))
|
| 154 |
+
|
| 155 |
+
for id_, row in enumerate(data):
|
| 156 |
+
yield id_, row
|