cifar10_small_test_set.py 2.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
# 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.
"""
CIFAR dataset.

This module will download dataset from
https://www.cs.toronto.edu/~kriz/cifar.html and parse train/test set into
paddle reader creators.

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.

"""

import itertools
import numpy
M
minqiyang 已提交
33
import paddle.dataset.common
34
import tarfile
M
minqiyang 已提交
35
import six
36
from six.moves import cPickle as pickle
37 38 39 40 41 42 43 44 45 46

__all__ = ['train10']

URL_PREFIX = 'https://www.cs.toronto.edu/~kriz/'
CIFAR10_URL = URL_PREFIX + 'cifar-10-python.tar.gz'
CIFAR10_MD5 = 'c58f30108f718f92721af3b95e74349a'


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

    def reader():
        with tarfile.open(filename, mode='r') as f:
M
minqiyang 已提交
56 57 58
            names = [
                each_item.name for each_item in f if sub_name in each_item.name
            ]
59 60 61

            batch_count = 0
            for name in names:
M
minqiyang 已提交
62 63 64 65
                if six.PY2:
                    batch = pickle.load(f.extractfile(name))
                else:
                    batch = pickle.load(f.extractfile(name), encoding='bytes')
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
                for item in read_batch(batch):
                    if isinstance(batch_size, int) and batch_count > batch_size:
                        break
                    batch_count += 1
                    yield item

    return reader


def train10(batch_size=None):
    """
    CIFAR-10 training set creator.

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

    :return: Training reader creator
    :rtype: callable
    """
    return reader_creator(
M
minqiyang 已提交
86
        paddle.dataset.common.download(CIFAR10_URL, 'cifar', CIFAR10_MD5),
87 88
        'data_batch',
        batch_size=batch_size)