cifar.py 4.6 KB
Newer Older
D
dangqingqing 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
Y
Yu Yang 已提交
14
"""
Q
qijun 已提交
15 16
CIFAR dataset.

Q
qijun 已提交
17 18 19
This module will download dataset from
https://www.cs.toronto.edu/~kriz/cifar.html and parse train/test set into
paddle reader creators.
Q
qijun 已提交
20

Q
qijun 已提交
21 22 23
The CIFAR-10 dataset consists of 60000 32x32 colour images in 10 classes,
with 6000 images per class. There are 50000 training images and 10000 test
images.
Q
qijun 已提交
24

Q
qijun 已提交
25 26 27
The CIFAR-100 dataset is just like the CIFAR-10, except it has 100 classes
containing 600 images each. There are 500 training images and 100 testing
images per class.
Y
Yu Yang 已提交
28

Y
Yu Yang 已提交
29
"""
D
dangqingqing 已提交
30

Y
Yu Yang 已提交
31 32
import itertools
import numpy
33
import paddle.dataset.common
Y
Yi Wang 已提交
34
import tarfile
M
minqiyang 已提交
35
import six
36
from six.moves import cPickle as pickle
Y
Yu Yang 已提交
37

Y
Your Name 已提交
38
__all__ = ['train100', 'test100', 'train10', 'test10', 'convert']
Y
Yu Yang 已提交
39

Y
Yi Wang 已提交
40 41
URL_PREFIX = 'https://www.cs.toronto.edu/~kriz/'
CIFAR10_URL = URL_PREFIX + 'cifar-10-python.tar.gz'
Y
Yu Yang 已提交
42
CIFAR10_MD5 = 'c58f30108f718f92721af3b95e74349a'
Y
Yi Wang 已提交
43
CIFAR100_URL = URL_PREFIX + 'cifar-100-python.tar.gz'
Y
Yu Yang 已提交
44 45 46
CIFAR100_MD5 = 'eb9058c3a382ffc7106e4002c42a8d85'


47
def reader_creator(filename, sub_name, cycle=False):
Y
Yi Wang 已提交
48
    def read_batch(batch):
M
minqiyang 已提交
49 50
        data = batch[six.b('data')]
        labels = batch.get(six.b('labels'), batch.get(six.b('fine_labels'), None))
Y
Yi Wang 已提交
51
        assert labels is not None
M
minqiyang 已提交
52
        for sample, label in six.moves.zip(data, labels):
Y
Yi Wang 已提交
53
            yield (sample / 255.0).astype(numpy.float32), int(label)
Y
Yu Yang 已提交
54

Y
Yi Wang 已提交
55
    def reader():
56
        with tarfile.open(filename, mode='r') as f:
M
minqiyang 已提交
57
            names = [each_item.name for each_item in f if sub_name in each_item.name]
Y
Yu Yang 已提交
58

59 60
            while True:
                for name in names:
M
minqiyang 已提交
61 62 63 64
                    if six.PY2:
                        batch = pickle.load(f.extractfile(name))
                    else:
                        batch = pickle.load(f.extractfile(name), encoding='bytes')
65 66 67 68
                    for item in read_batch(batch):
                        yield item
                if not cycle:
                    break
Y
Yu Yang 已提交
69

Y
Yu Yang 已提交
70
    return reader
Y
Yu Yang 已提交
71 72


Y
Yi Wang 已提交
73
def train100():
Q
qijun 已提交
74
    """
Q
qijun 已提交
75
    CIFAR-100 training set creator.
Q
qijun 已提交
76 77 78 79

    It returns a reader creator, each sample in the reader is image pixels in
    [0, 1] and label in [0, 99].

Q
qijun 已提交
80
    :return: Training reader creator
Q
qijun 已提交
81 82
    :rtype: callable
    """
Y
Yi Wang 已提交
83
    return reader_creator(
84
        paddle.dataset.common.download(CIFAR100_URL, 'cifar', CIFAR100_MD5),
R
root 已提交
85
        'train')
Y
Yu Yang 已提交
86 87


Y
Yi Wang 已提交
88
def test100():
Q
qijun 已提交
89
    """
X
xuwei06 已提交
90
    CIFAR-100 test set creator.
Q
qijun 已提交
91 92 93 94 95 96 97

    It returns a reader creator, each sample in the reader is image pixels in
    [0, 1] and label in [0, 9].

    :return: Test reader creator.
    :rtype: callable
    """
R
root 已提交
98
    return reader_creator(
99
        paddle.dataset.common.download(CIFAR100_URL, 'cifar', CIFAR100_MD5),
R
root 已提交
100
        'test')
Y
Yu Yang 已提交
101 102


103
def train10(cycle=False):
Q
qijun 已提交
104
    """
Q
qijun 已提交
105
    CIFAR-10 training set creator.
Q
qijun 已提交
106 107 108 109

    It returns a reader creator, each sample in the reader is image pixels in
    [0, 1] and label in [0, 9].

110 111
    :param cycle: whether to cycle through the dataset
    :type cycle: bool
Q
qijun 已提交
112
    :return: Training reader creator
Q
qijun 已提交
113 114
    :rtype: callable
    """
Y
Yi Wang 已提交
115
    return reader_creator(
116
        paddle.dataset.common.download(CIFAR10_URL, 'cifar', CIFAR10_MD5),
117 118
        'data_batch',
        cycle=cycle)
Y
Yu Yang 已提交
119 120


121
def test10(cycle=False):
Q
qijun 已提交
122
    """
X
xuwei06 已提交
123
    CIFAR-10 test set creator.
Q
qijun 已提交
124 125 126 127

    It returns a reader creator, each sample in the reader is image pixels in
    [0, 1] and label in [0, 9].

128 129
    :param cycle: whether to cycle through the dataset
    :type cycle: bool
Q
qijun 已提交
130 131 132
    :return: Test reader creator.
    :rtype: callable
    """
Y
Yi Wang 已提交
133
    return reader_creator(
134
        paddle.dataset.common.download(CIFAR10_URL, 'cifar', CIFAR10_MD5),
135 136
        'test_batch',
        cycle=cycle)
Y
Yancey1989 已提交
137 138


139
def fetch():
140 141
    paddle.dataset.common.download(CIFAR10_URL, 'cifar', CIFAR10_MD5)
    paddle.dataset.common.download(CIFAR100_URL, 'cifar', CIFAR100_MD5)
R
root 已提交
142 143 144 145 146 147


def convert(path):
    """
    Converts dataset to recordio format
    """
148 149 150 151
    paddle.dataset.common.convert(path, train100(), 1000, "cifar_train100")
    paddle.dataset.common.convert(path, test100(), 1000, "cifar_test100")
    paddle.dataset.common.convert(path, train10(), 1000, "cifar_train10")
    paddle.dataset.common.convert(path, test10(), 1000, "cifar_test10")