cifar.py 2.3 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
"""
Y
Yi Wang 已提交
15
CIFAR dataset: https://www.cs.toronto.edu/~kriz/cifar.html
Y
Yu Yang 已提交
16
"""
D
dangqingqing 已提交
17

Y
Yu Yang 已提交
18 19 20
import cPickle
import itertools
import numpy
Y
Yi Wang 已提交
21 22
import paddle.v2.dataset.common
import tarfile
Y
Yu Yang 已提交
23

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

Y
Yi Wang 已提交
26 27
URL_PREFIX = 'https://www.cs.toronto.edu/~kriz/'
CIFAR10_URL = URL_PREFIX + 'cifar-10-python.tar.gz'
Y
Yu Yang 已提交
28
CIFAR10_MD5 = 'c58f30108f718f92721af3b95e74349a'
Y
Yi Wang 已提交
29
CIFAR100_URL = URL_PREFIX + 'cifar-100-python.tar.gz'
Y
Yu Yang 已提交
30 31 32
CIFAR100_MD5 = 'eb9058c3a382ffc7106e4002c42a8d85'


Y
Yi Wang 已提交
33 34 35 36 37 38 39
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 已提交
40

Y
Yi Wang 已提交
41
    def reader():
Y
Yu Yang 已提交
42
        with tarfile.open(filename, mode='r') as f:
Y
Yu Yang 已提交
43 44 45 46 47
            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 已提交
48
                for item in read_batch(batch):
Y
Yu Yang 已提交
49 50
                    yield item

Y
Yu Yang 已提交
51
    return reader
Y
Yu Yang 已提交
52 53


Y
Yi Wang 已提交
54 55 56 57
def train100():
    return reader_creator(
        paddle.v2.dataset.common.download(CIFAR100_URL, 'cifar', CIFAR100_MD5),
        'train')
Y
Yu Yang 已提交
58 59


Y
Yi Wang 已提交
60 61 62 63
def test100():
    return reader_creator(
        paddle.v2.dataset.common.download(CIFAR100_URL, 'cifar', CIFAR100_MD5),
        'test')
Y
Yu Yang 已提交
64 65


Y
Yi Wang 已提交
66 67 68 69
def train10():
    return reader_creator(
        paddle.v2.dataset.common.download(CIFAR10_URL, 'cifar', CIFAR10_MD5),
        'data_batch')
Y
Yu Yang 已提交
70 71


Y
Yi Wang 已提交
72 73 74 75
def test10():
    return reader_creator(
        paddle.v2.dataset.common.download(CIFAR10_URL, 'cifar', CIFAR10_MD5),
        'test_batch')