cifar.py 3.5 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 17
CIFAR dataset.

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

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.

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 已提交
25

Y
Yu Yang 已提交
26
"""
D
dangqingqing 已提交
27

Y
Yu Yang 已提交
28 29 30
import cPickle
import itertools
import numpy
31
from common import download
Y
Yi Wang 已提交
32
import tarfile
Y
Yu Yang 已提交
33

Y
Yi Wang 已提交
34
__all__ = ['train100', 'test100', 'train10', 'test10']
Y
Yu Yang 已提交
35

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


Y
Yi Wang 已提交
43 44 45 46 47 48 49
def reader_creator(filename, sub_name):
    def read_batch(batch):
        data = batch['data']
        labels = batch.get('labels', batch.get('fine_labels', None))
        assert labels is not None
        for sample, label in itertools.izip(data, labels):
            yield (sample / 255.0).astype(numpy.float32), int(label)
Y
Yu Yang 已提交
50

Y
Yi Wang 已提交
51
    def reader():
Y
Yu Yang 已提交
52
        with tarfile.open(filename, mode='r') as f:
Y
Yu Yang 已提交
53 54 55 56 57
            names = (each_item.name for each_item in f
                     if sub_name in each_item.name)

            for name in names:
                batch = cPickle.load(f.extractfile(name))
Y
Yi Wang 已提交
58
                for item in read_batch(batch):
Y
Yu Yang 已提交
59 60
                    yield item

Y
Yu Yang 已提交
61
    return reader
Y
Yu Yang 已提交
62 63


Y
Yi Wang 已提交
64
def train100():
Q
qijun 已提交
65 66 67 68 69 70 71 72 73
    """
    CIFAR-100 train set creator.

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

    :return: Train reader creator
    :rtype: callable
    """
Y
Yi Wang 已提交
74
    return reader_creator(
75
        download(CIFAR100_URL, 'cifar', CIFAR100_MD5), 'train')
Y
Yu Yang 已提交
76 77


Y
Yi Wang 已提交
78
def test100():
Q
qijun 已提交
79 80 81 82 83 84 85 86 87
    """
    CIFAR-100 test set cretor.

    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
    """
88
    return reader_creator(download(CIFAR100_URL, 'cifar', CIFAR100_MD5), 'test')
Y
Yu Yang 已提交
89 90


Y
Yi Wang 已提交
91
def train10():
Q
qijun 已提交
92 93 94 95 96 97 98 99 100
    """
    CIFAR-10 train set creator.

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

    :return: Train reader creator
    :rtype: callable
    """
Y
Yi Wang 已提交
101
    return reader_creator(
102
        download(CIFAR10_URL, 'cifar', CIFAR10_MD5), 'data_batch')
Y
Yu Yang 已提交
103 104


Y
Yi Wang 已提交
105
def test10():
Q
qijun 已提交
106 107 108 109 110 111 112 113 114
    """
    CIFAR-10 test set cretor.

    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
    """
Y
Yi Wang 已提交
115
    return reader_creator(
116
        download(CIFAR10_URL, 'cifar', CIFAR10_MD5), 'test_batch')
Y
Yancey1989 已提交
117 118


119 120 121
def fetch():
    download(CIFAR10_URL, 'cifar', CIFAR10_MD5)
    download(CIFAR100_URL, 'cifar', CIFAR100_MD5)