cifar.py 5.2 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.

17
This module will download dataset from https://dataset.bj.bcebos.com/cifar/cifar-10-python.tar.gz and https://dataset.bj.bcebos.com/cifar/cifar-100-python.tar.gz, parse train/test set into
Q
qijun 已提交
18
paddle reader creators.
Q
qijun 已提交
19

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

Q
qijun 已提交
24 25 26
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 已提交
27

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

30 31
from __future__ import print_function

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

40 41
__all__ = []

42
URL_PREFIX = 'https://dataset.bj.bcebos.com/cifar/'
Y
Yi Wang 已提交
43
CIFAR10_URL = URL_PREFIX + 'cifar-10-python.tar.gz'
Y
Yu Yang 已提交
44
CIFAR10_MD5 = 'c58f30108f718f92721af3b95e74349a'
Y
Yi Wang 已提交
45
CIFAR100_URL = URL_PREFIX + 'cifar-100-python.tar.gz'
Y
Yu Yang 已提交
46 47 48
CIFAR100_MD5 = 'eb9058c3a382ffc7106e4002c42a8d85'


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

Y
Yi Wang 已提交
58
    def reader():
59 60 61 62
        while True:
            with tarfile.open(filename, mode='r') as f:
                names = (each_item.name for each_item in f
                         if sub_name in each_item.name)
Y
Yu Yang 已提交
63

64
                for name in names:
M
minqiyang 已提交
65 66 67
                    if six.PY2:
                        batch = pickle.load(f.extractfile(name))
                    else:
M
minqiyang 已提交
68 69
                        batch = pickle.load(
                            f.extractfile(name), encoding='bytes')
70 71
                    for item in read_batch(batch):
                        yield item
72 73 74

            if not cycle:
                break
Y
Yu Yang 已提交
75

Y
Yu Yang 已提交
76
    return reader
Y
Yu Yang 已提交
77 78


79 80 81
@deprecated(
    since="2.0.0",
    update_to="paddle.vision.datasets.Cifar100",
82
    level=1,
83
    reason="Please use new dataset API which supports paddle.io.DataLoader")
Y
Yi Wang 已提交
84
def train100():
Q
qijun 已提交
85
    """
Q
qijun 已提交
86
    CIFAR-100 training set creator.
Q
qijun 已提交
87 88 89 90

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

Q
qijun 已提交
91
    :return: Training reader creator
Q
qijun 已提交
92 93
    :rtype: callable
    """
Y
Yi Wang 已提交
94
    return reader_creator(
95
        paddle.dataset.common.download(CIFAR100_URL, 'cifar', CIFAR100_MD5),
R
root 已提交
96
        'train')
Y
Yu Yang 已提交
97 98


99 100 101
@deprecated(
    since="2.0.0",
    update_to="paddle.vision.datasets.Cifar100",
102
    level=1,
103
    reason="Please use new dataset API which supports paddle.io.DataLoader")
Y
Yi Wang 已提交
104
def test100():
Q
qijun 已提交
105
    """
X
xuwei06 已提交
106
    CIFAR-100 test set creator.
Q
qijun 已提交
107 108

    It returns a reader creator, each sample in the reader is image pixels in
109
    [0, 1] and label in [0, 99].
Q
qijun 已提交
110 111 112 113

    :return: Test reader creator.
    :rtype: callable
    """
R
root 已提交
114
    return reader_creator(
115
        paddle.dataset.common.download(CIFAR100_URL, 'cifar', CIFAR100_MD5),
R
root 已提交
116
        'test')
Y
Yu Yang 已提交
117 118


119 120 121
@deprecated(
    since="2.0.0",
    update_to="paddle.vision.datasets.Cifar10",
122
    level=1,
123
    reason="Please use new dataset API which supports paddle.io.DataLoader")
124
def train10(cycle=False):
Q
qijun 已提交
125
    """
Q
qijun 已提交
126
    CIFAR-10 training set creator.
Q
qijun 已提交
127 128 129 130

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

131 132
    :param cycle: whether to cycle through the dataset
    :type cycle: bool
Q
qijun 已提交
133
    :return: Training reader creator
Q
qijun 已提交
134 135
    :rtype: callable
    """
Y
Yi Wang 已提交
136
    return reader_creator(
137
        paddle.dataset.common.download(CIFAR10_URL, 'cifar', CIFAR10_MD5),
138 139
        'data_batch',
        cycle=cycle)
Y
Yu Yang 已提交
140 141


142 143 144
@deprecated(
    since="2.0.0",
    update_to="paddle.vision.datasets.Cifar10",
145
    level=1,
146
    reason="Please use new dataset API which supports paddle.io.DataLoader")
147
def test10(cycle=False):
Q
qijun 已提交
148
    """
X
xuwei06 已提交
149
    CIFAR-10 test set creator.
Q
qijun 已提交
150 151 152 153

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

154 155
    :param cycle: whether to cycle through the dataset
    :type cycle: bool
Q
qijun 已提交
156 157 158
    :return: Test reader creator.
    :rtype: callable
    """
Y
Yi Wang 已提交
159
    return reader_creator(
160
        paddle.dataset.common.download(CIFAR10_URL, 'cifar', CIFAR10_MD5),
161 162
        'test_batch',
        cycle=cycle)
Y
Yancey1989 已提交
163 164


165 166 167
@deprecated(
    since="2.0.0",
    update_to="paddle.vision.datasets.Cifar10",
168
    level=1,
169
    reason="Please use new dataset API which supports paddle.io.DataLoader")
170
def fetch():
171 172
    paddle.dataset.common.download(CIFAR10_URL, 'cifar', CIFAR10_MD5)
    paddle.dataset.common.download(CIFAR100_URL, 'cifar', CIFAR100_MD5)