|
"""Corrupted Fashion-Mnist Data Set. |
|
|
|
This module contains the huggingface dataset adaptation of |
|
the Corrupted Fashion-Mnist Data Set. |
|
Find the full code at `https://github.com/testingautomated-usi/fashion-mnist-c`.""" |
|
import struct |
|
|
|
import datasets |
|
import numpy as np |
|
from datasets.tasks import ImageClassification |
|
|
|
_CITATION = """\ |
|
@inproceedings{Weiss2022SimpleTechniques, |
|
title={Simple Techniques Work Surprisingly Well for Neural Network Test Prioritization and Active Learning}, |
|
author={Weiss, Michael and Tonella, Paolo}, |
|
booktitle={Proceedings of the 31th ACM SIGSOFT International Symposium on Software Testing and Analysis}, |
|
year={2022} |
|
} |
|
""" |
|
|
|
_DESCRIPTION = """\ |
|
Fashion-MNIST is dataset of fashion images, indended as a drop-in replacement for the MNIST dataset. |
|
This dataset (Fashion-Mnist-Corrupted) provides out-of-distribution data for the Fashion-Mnist |
|
dataset. Fashion-Mnist-Corrupted is based on a similar project for MNIST, called MNIST-C, by Mu et. al. |
|
""" |
|
|
|
CONFIG = datasets.BuilderConfig(name="fashion_mnist_corrupted", |
|
version=datasets.Version("1.0.0"), |
|
description=_DESCRIPTION, ) |
|
|
|
_HOMEPAGE = "https://github.com/testingautomated-usi/fashion-mnist-c" |
|
_LICENSE = "https://github.com/testingautomated-usi/fashion-mnist-c/blob/main/LICENSE" |
|
|
|
if CONFIG.version == datasets.Version("1.0.0"): |
|
_CURRENT_VERSION_TAG = "e31d36a102cdd8c5e2690533eb2aaec7c296fcb6" |
|
else: |
|
raise ValueError("Unsupported version.") |
|
|
|
_URL = f"https://github.com/testingautomated-usi/fashion-mnist-c/blob/{_CURRENT_VERSION_TAG}/generated/ubyte/" |
|
|
|
_URLS = { |
|
"train_images": "fmnist-c-train-ubyte.gz", |
|
"train_labels": "fmnist-c-train-labels-ubyte.gz", |
|
"test_images": "fmnist-c-test-ubyte.gz", |
|
"test_labels": "fmnist-c-test-labels-ubyte.gz", |
|
} |
|
|
|
_NAMES = [ |
|
"T - shirt / top", |
|
"Trouser", |
|
"Pullover", |
|
"Dress", |
|
"Coat", |
|
"Sandal", |
|
"Shirt", |
|
"Sneaker", |
|
"Bag", |
|
"Ankle boot", |
|
] |
|
|
|
|
|
class FashionMnistCorrupted(datasets.GeneratorBasedBuilder): |
|
"""FashionMNIST-Corrupted Data Set""" |
|
|
|
BUILDER_CONFIGS = [ |
|
CONFIG |
|
] |
|
|
|
def _info(self): |
|
return datasets.DatasetInfo( |
|
description=_DESCRIPTION, |
|
features=datasets.Features( |
|
{ |
|
"image": datasets.Image(), |
|
"label": datasets.features.ClassLabel(names=_NAMES), |
|
} |
|
), |
|
supervised_keys=("image", "label"), |
|
homepage=_HOMEPAGE, |
|
citation=_CITATION, |
|
task_templates=[ImageClassification(image_column="image", label_column="label")], |
|
) |
|
|
|
def _split_generators(self, dl_manager): |
|
urls_to_download = {key: _URL + fname + "?raw=true" for key, fname in _URLS.items()} |
|
downloaded_files = dl_manager.download_and_extract(urls_to_download) |
|
|
|
return [ |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TRAIN, |
|
gen_kwargs={ |
|
"filepath": [downloaded_files["train_images"], downloaded_files["train_labels"]], |
|
"split": "train", |
|
}, |
|
), |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TEST, |
|
gen_kwargs={ |
|
"filepath": [downloaded_files["test_images"], downloaded_files["test_labels"]], |
|
"split": "test", |
|
}, |
|
), |
|
] |
|
|
|
def _generate_examples(self, filepath, split): |
|
"""This function returns the examples in the raw form.""" |
|
|
|
with open(filepath[0], "rb") as f: |
|
|
|
_ = f.read(4) |
|
size = struct.unpack(">I", f.read(4))[0] |
|
_ = f.read(8) |
|
images = np.frombuffer(f.read(), dtype=np.uint8).reshape(size, 28, 28) |
|
|
|
|
|
with open(filepath[1], "rb") as f: |
|
|
|
_ = f.read(8) |
|
labels = np.frombuffer(f.read(), dtype=np.uint8) |
|
|
|
for idx in range(size): |
|
yield idx, {"image": images[idx], "label": int(labels[idx])} |
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
FashionMnistCorrupted().download_and_prepare() |
|
|