mweiss commited on
Commit
a05e9d9
1 Parent(s): be52dad

Update fashion_mnist_corrupted.py

Browse files
Files changed (1) hide show
  1. fashion_mnist_corrupted.py +37 -34
fashion_mnist_corrupted.py CHANGED
@@ -24,25 +24,29 @@ This dataset (Fashion-Mnist-Corrupted) provides out-of-distribution data for the
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 = [
@@ -62,9 +66,7 @@ _NAMES = [
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(
@@ -78,25 +80,35 @@ class FashionMnistCorrupted(datasets.GeneratorBasedBuilder):
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
  ),
@@ -105,23 +117,14 @@ class FashionMnistCorrupted(datasets.GeneratorBasedBuilder):
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()
 
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(
28
+ name="fashion_mnist_corrupted",
29
+ version=datasets.Version("1.0.0"),
30
+ description=_DESCRIPTION,
31
+ )
32
 
33
  _HOMEPAGE = "https://github.com/testingautomated-usi/fashion-mnist-c"
34
  _LICENSE = "https://github.com/testingautomated-usi/fashion-mnist-c/blob/main/LICENSE"
35
 
36
  if CONFIG.version == datasets.Version("1.0.0"):
37
+ tag = "v1.0.0"
38
  else:
39
  raise ValueError("Unsupported version.")
40
 
41
+ _URL = (
42
+ f"https://github.com/testingautomated-usi/fashion-mnist-c/blob/{tag}/generated/npy/"
43
+ )
44
 
45
  _URLS = {
46
+ "train_images": "fmnist-c-train.npy",
47
+ "train_labels": "fmnist-c-train-labels.npy",
48
+ "test_images": "fmnist-c-test.npy",
49
+ "test_labels": "fmnist-c-test-labels.npy",
50
  }
51
 
52
  _NAMES = [
 
66
  class FashionMnistCorrupted(datasets.GeneratorBasedBuilder):
67
  """FashionMNIST-Corrupted Data Set"""
68
 
69
+ BUILDER_CONFIGS = [CONFIG]
 
 
70
 
71
  def _info(self):
72
  return datasets.DatasetInfo(
 
80
  supervised_keys=("image", "label"),
81
  homepage=_HOMEPAGE,
82
  citation=_CITATION,
83
+ task_templates=[
84
+ ImageClassification(image_column="image", label_column="label")
85
+ ],
86
  )
87
 
88
  def _split_generators(self, dl_manager):
89
+ urls_to_download = {
90
+ key: _URL + fname + "?raw=true" for key, fname in _URLS.items()
91
+ }
92
  downloaded_files = dl_manager.download_and_extract(urls_to_download)
93
 
94
  return [
95
  datasets.SplitGenerator(
96
  name=datasets.Split.TRAIN,
97
  gen_kwargs={
98
+ "filepath": [
99
+ downloaded_files["train_images"],
100
+ downloaded_files["train_labels"],
101
+ ],
102
  "split": "train",
103
  },
104
  ),
105
  datasets.SplitGenerator(
106
  name=datasets.Split.TEST,
107
  gen_kwargs={
108
+ "filepath": [
109
+ downloaded_files["test_images"],
110
+ downloaded_files["test_labels"],
111
+ ],
112
  "split": "test",
113
  },
114
  ),
 
117
  def _generate_examples(self, filepath, split):
118
  """This function returns the examples in the raw form."""
119
  # Images
120
+ images = np.load(filepath[0])
121
+ labels = np.load(filepath[1])
122
+
123
+ if images.shape[0] != labels.shape[0]:
124
+ raise ValueError(
125
+ f"Number of images {images.shape[0]} and labels {labels.shape[0]} do not match."
126
+ )
 
 
 
 
 
 
 
 
127
 
128
+ for idx in range(images.shape[0]):
129
+ yield idx, {"image": images[idx], "label": int(labels[idx])}
130