Datasets:
Create cifar10.py
Browse files- cifar10.py +121 -0
cifar10.py
ADDED
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# coding=utf-8
|
2 |
+
# Copyright 2020 The TensorFlow Datasets Authors and the HuggingFace Datasets Authors.
|
3 |
+
#
|
4 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5 |
+
# you may not use this file except in compliance with the License.
|
6 |
+
# You may obtain a copy of the License at
|
7 |
+
#
|
8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9 |
+
#
|
10 |
+
# Unless required by applicable law or agreed to in writing, software
|
11 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
12 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 |
+
# See the License for the specific language governing permissions and
|
14 |
+
# limitations under the License.
|
15 |
+
|
16 |
+
# Lint as: python3
|
17 |
+
"""CIFAR-10 Data Set"""
|
18 |
+
|
19 |
+
|
20 |
+
import pickle
|
21 |
+
|
22 |
+
import numpy as np
|
23 |
+
|
24 |
+
import datasets
|
25 |
+
from datasets.tasks import ImageClassification
|
26 |
+
|
27 |
+
|
28 |
+
_CITATION = """\
|
29 |
+
@TECHREPORT{Krizhevsky09learningmultiple,
|
30 |
+
author = {Alex Krizhevsky},
|
31 |
+
title = {Learning multiple layers of features from tiny images},
|
32 |
+
institution = {},
|
33 |
+
year = {2009}
|
34 |
+
}
|
35 |
+
"""
|
36 |
+
|
37 |
+
_DESCRIPTION = """\
|
38 |
+
The CIFAR-10 dataset consists of 60000 32x32 colour images in 10 classes, with 6000 images
|
39 |
+
per class. There are 50000 training images and 10000 test images.
|
40 |
+
"""
|
41 |
+
|
42 |
+
_DATA_URL = "https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz"
|
43 |
+
|
44 |
+
_NAMES = [
|
45 |
+
"airplane",
|
46 |
+
"automobile",
|
47 |
+
"bird",
|
48 |
+
"cat",
|
49 |
+
"deer",
|
50 |
+
"dog",
|
51 |
+
"frog",
|
52 |
+
"horse",
|
53 |
+
"ship",
|
54 |
+
"truck",
|
55 |
+
]
|
56 |
+
|
57 |
+
|
58 |
+
class Cifar10(datasets.GeneratorBasedBuilder):
|
59 |
+
"""CIFAR-10 Data Set"""
|
60 |
+
|
61 |
+
BUILDER_CONFIGS = [
|
62 |
+
datasets.BuilderConfig(
|
63 |
+
name="plain_text",
|
64 |
+
version=datasets.Version("1.0.0", ""),
|
65 |
+
description="Plain text import of CIFAR-10 Data Set",
|
66 |
+
)
|
67 |
+
]
|
68 |
+
|
69 |
+
def _info(self):
|
70 |
+
return datasets.DatasetInfo(
|
71 |
+
description=_DESCRIPTION,
|
72 |
+
features=datasets.Features(
|
73 |
+
{
|
74 |
+
"img": datasets.Image(),
|
75 |
+
"label": datasets.features.ClassLabel(names=_NAMES),
|
76 |
+
}
|
77 |
+
),
|
78 |
+
supervised_keys=("img", "label"),
|
79 |
+
homepage="https://www.cs.toronto.edu/~kriz/cifar.html",
|
80 |
+
citation=_CITATION,
|
81 |
+
task_templates=ImageClassification(image_column="img", label_column="label"),
|
82 |
+
)
|
83 |
+
|
84 |
+
def _split_generators(self, dl_manager):
|
85 |
+
archive = dl_manager.download(_DATA_URL)
|
86 |
+
|
87 |
+
return [
|
88 |
+
datasets.SplitGenerator(
|
89 |
+
name=datasets.Split.TRAIN, gen_kwargs={"files": dl_manager.iter_archive(archive), "split": "train"}
|
90 |
+
),
|
91 |
+
datasets.SplitGenerator(
|
92 |
+
name=datasets.Split.TEST, gen_kwargs={"files": dl_manager.iter_archive(archive), "split": "test"}
|
93 |
+
),
|
94 |
+
]
|
95 |
+
|
96 |
+
def _generate_examples(self, files, split):
|
97 |
+
"""This function returns the examples in the raw (text) form."""
|
98 |
+
|
99 |
+
if split == "train":
|
100 |
+
batches = ["data_batch_1", "data_batch_2", "data_batch_3", "data_batch_4", "data_batch_5"]
|
101 |
+
|
102 |
+
if split == "test":
|
103 |
+
batches = ["test_batch"]
|
104 |
+
batches = [f"cifar-10-batches-py/{filename}" for filename in batches]
|
105 |
+
|
106 |
+
for path, fo in files:
|
107 |
+
|
108 |
+
if path in batches:
|
109 |
+
dict = pickle.load(fo, encoding="bytes")
|
110 |
+
|
111 |
+
labels = dict[b"labels"]
|
112 |
+
images = dict[b"data"]
|
113 |
+
|
114 |
+
for idx, _ in enumerate(images):
|
115 |
+
|
116 |
+
img_reshaped = np.transpose(np.reshape(images[idx], (3, 32, 32)), (1, 2, 0))
|
117 |
+
|
118 |
+
yield f"{path}_{idx}", {
|
119 |
+
"img": img_reshaped,
|
120 |
+
"label": labels[idx],
|
121 |
+
}
|