mweiss commited on
Commit
e8b82c7
1 Parent(s): 2fb8c19

Upload fashion_mnist_corrupted.py

Browse files
Files changed (1) hide show
  1. fashion_mnist_corrupted.py +127 -0
fashion_mnist_corrupted.py ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Corrupted Fashion-Mnist Data Set.
2
+
3
+ This module contains the huggingface dataset adaptation of
4
+ the Corrupted Fashion-Mnist Data Set.
5
+ Find the full code at `https://github.com/testingautomated-usi/fashion-mnist-c`."""
6
+ import struct
7
+
8
+ import datasets
9
+ import numpy as np
10
+ from datasets.tasks import ImageClassification
11
+
12
+ _CITATION = """\
13
+ @inproceedings{Weiss2022SimpleTechniques,
14
+ title={Simple Techniques Work Surprisingly Well for Neural Network Test Prioritization and Active Learning},
15
+ author={Weiss, Michael and Tonella, Paolo},
16
+ booktitle={Proceedings of the 31th ACM SIGSOFT International Symposium on Software Testing and Analysis},
17
+ year={2022}
18
+ }
19
+ """
20
+
21
+ _DESCRIPTION = """\
22
+ Fashion-MNIST is dataset of fashion images, indended as a drop-in replacement for the MNIST dataset.
23
+ This dataset (Fashion-Mnist-Corrupted) provides out-of-distribution data for the Fashion-Mnist
24
+ dataset. Fashion-Mnist-Corrupted is based on a similar project for MNIST, called MNIST-C, by Mu et. al.
25
+ """
26
+
27
+ CONFIG = datasets.BuilderConfig(name="fashion_mnist_corrupted",
28
+ version=datasets.Version("1.0.0"),
29
+ description=_DESCRIPTION, )
30
+
31
+ _HOMEPAGE = "https://github.com/testingautomated-usi/fashion-mnist-c"
32
+ _LICENSE = "https://github.com/testingautomated-usi/fashion-mnist-c/blob/main/LICENSE"
33
+
34
+ if CONFIG.version == datasets.Version("1.0.0"):
35
+ _CURRENT_VERSION_TAG = "e31d36a102cdd8c5e2690533eb2aaec7c296fcb6"
36
+ else:
37
+ raise ValueError("Unsupported version.")
38
+
39
+ _URL = f"https://github.com/testingautomated-usi/fashion-mnist-c/blob/{_CURRENT_VERSION_TAG}/generated/ubyte/"
40
+
41
+ _URLS = {
42
+ "train_images": "fmnist-c-train-ubyte.gz",
43
+ "train_labels": "fmnist-c-train-labels-ubyte.gz",
44
+ "test_images": "fmnist-c-test-ubyte.gz",
45
+ "test_labels": "fmnist-c-test-labels-ubyte.gz",
46
+ }
47
+
48
+ _NAMES = [
49
+ "T - shirt / top",
50
+ "Trouser",
51
+ "Pullover",
52
+ "Dress",
53
+ "Coat",
54
+ "Sandal",
55
+ "Shirt",
56
+ "Sneaker",
57
+ "Bag",
58
+ "Ankle boot",
59
+ ]
60
+
61
+
62
+ class FashionMnistCorrupted(datasets.GeneratorBasedBuilder):
63
+ """FashionMNIST-Corrupted Data Set"""
64
+
65
+ BUILDER_CONFIGS = [
66
+ CONFIG
67
+ ]
68
+
69
+ def _info(self):
70
+ return datasets.DatasetInfo(
71
+ description=_DESCRIPTION,
72
+ features=datasets.Features(
73
+ {
74
+ "image": datasets.Image(),
75
+ "label": datasets.features.ClassLabel(names=_NAMES),
76
+ }
77
+ ),
78
+ supervised_keys=("image", "label"),
79
+ homepage=_HOMEPAGE,
80
+ citation=_CITATION,
81
+ task_templates=[ImageClassification(image_column="image", label_column="label")],
82
+ )
83
+
84
+ def _split_generators(self, dl_manager):
85
+ urls_to_download = {key: _URL + fname + "?raw=true" for key, fname in _URLS.items()}
86
+ downloaded_files = dl_manager.download_and_extract(urls_to_download)
87
+
88
+ return [
89
+ datasets.SplitGenerator(
90
+ name=datasets.Split.TRAIN,
91
+ gen_kwargs={
92
+ "filepath": [downloaded_files["train_images"], downloaded_files["train_labels"]],
93
+ "split": "train",
94
+ },
95
+ ),
96
+ datasets.SplitGenerator(
97
+ name=datasets.Split.TEST,
98
+ gen_kwargs={
99
+ "filepath": [downloaded_files["test_images"], downloaded_files["test_labels"]],
100
+ "split": "test",
101
+ },
102
+ ),
103
+ ]
104
+
105
+ def _generate_examples(self, filepath, split):
106
+ """This function returns the examples in the raw form."""
107
+ # Images
108
+ with open(filepath[0], "rb") as f:
109
+ # First 16 bytes contain some metadata
110
+ _ = f.read(4)
111
+ size = struct.unpack(">I", f.read(4))[0]
112
+ _ = f.read(8)
113
+ images = np.frombuffer(f.read(), dtype=np.uint8).reshape(size, 28, 28)
114
+
115
+ # Labels
116
+ with open(filepath[1], "rb") as f:
117
+ # First 8 bytes contain some metadata
118
+ _ = f.read(8)
119
+ labels = np.frombuffer(f.read(), dtype=np.uint8)
120
+
121
+ for idx in range(size):
122
+ yield idx, {"image": images[idx], "label": int(labels[idx])}
123
+
124
+
125
+ # For local development / debugger support only
126
+ if __name__ == '__main__':
127
+ FashionMnistCorrupted().download_and_prepare()