mnist.py 5.1 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
Yi Wang 已提交
14 15
"""
MNIST dataset.
Y
Yu Yang 已提交
16 17

This module will download dataset from http://yann.lecun.com/exdb/mnist/ and
Q
qijun 已提交
18
parse training set and test set into paddle reader creators.
Y
Yi Wang 已提交
19
"""
20 21 22

from __future__ import print_function

23
import paddle.dataset.common
24
import paddle.utils.deprecated as deprecated
25
import gzip
Y
Yu Yang 已提交
26
import numpy
27
import struct
M
minqiyang 已提交
28
from six.moves import range
Y
Yi Wang 已提交
29

30 31
__all__ = []

32
URL_PREFIX = 'https://dataset.bj.bcebos.com/mnist/'
Y
Yi Wang 已提交
33
TEST_IMAGE_URL = URL_PREFIX + 't10k-images-idx3-ubyte.gz'
Y
Yu Yang 已提交
34
TEST_IMAGE_MD5 = '9fb629c4189551a2d022fa330f9573f3'
Y
Yi Wang 已提交
35
TEST_LABEL_URL = URL_PREFIX + 't10k-labels-idx1-ubyte.gz'
Y
Yu Yang 已提交
36
TEST_LABEL_MD5 = 'ec29112dd5afa0611ce80d1b7f02629c'
Y
Yi Wang 已提交
37 38 39 40
TRAIN_IMAGE_URL = URL_PREFIX + 'train-images-idx3-ubyte.gz'
TRAIN_IMAGE_MD5 = 'f68b3c2dcbeaaa9fbdd348bbdeb94873'
TRAIN_LABEL_URL = URL_PREFIX + 'train-labels-idx1-ubyte.gz'
TRAIN_LABEL_MD5 = 'd53e105ee54ea40749a09fcbcd1e9432'
Y
Yu Yang 已提交
41 42


Y
Yi Wang 已提交
43 44
def reader_creator(image_filename, label_filename, buffer_size):
    def reader():
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
        with gzip.GzipFile(image_filename, 'rb') as image_file:
            img_buf = image_file.read()
            with gzip.GzipFile(label_filename, 'rb') as label_file:
                lab_buf = label_file.read()

                step_label = 0

                offset_img = 0
                # read from Big-endian
                # get file info from magic byte
                # image file : 16B
                magic_byte_img = '>IIII'
                magic_img, image_num, rows, cols = struct.unpack_from(
                    magic_byte_img, img_buf, offset_img)
                offset_img += struct.calcsize(magic_byte_img)

                offset_lab = 0
                # label file : 8B
                magic_byte_lab = '>II'
                magic_lab, label_num = struct.unpack_from(magic_byte_lab,
                                                          lab_buf, offset_lab)
                offset_lab += struct.calcsize(magic_byte_lab)

                while True:
                    if step_label >= label_num:
                        break
                    fmt_label = '>' + str(buffer_size) + 'B'
                    labels = struct.unpack_from(fmt_label, lab_buf, offset_lab)
                    offset_lab += struct.calcsize(fmt_label)
                    step_label += buffer_size

                    fmt_images = '>' + str(buffer_size * rows * cols) + 'B'
                    images_temp = struct.unpack_from(fmt_images, img_buf,
                                                     offset_img)
                    images = numpy.reshape(images_temp, (
                        buffer_size, rows * cols)).astype('float32')
                    offset_img += struct.calcsize(fmt_images)

83 84 85 86
                    images = images / 255.0
                    images = images * 2.0
                    images = images - 1.0

87 88
                    for i in range(buffer_size):
                        yield images[i, :], int(labels[i])
Y
Yu Yang 已提交
89

Y
Yu Yang 已提交
90
    return reader
Y
Yu Yang 已提交
91

Y
Yi Wang 已提交
92

93 94 95 96
@deprecated(
    since="2.0.0",
    update_to="paddle.vision.datasets.MNIST",
    reason="Please use new dataset API which supports paddle.io.DataLoader")
Y
Yi Wang 已提交
97
def train():
Y
Yu Yang 已提交
98
    """
Q
qijun 已提交
99
    MNIST training set creator.
Y
Yu Yang 已提交
100 101

    It returns a reader creator, each sample in the reader is image pixels in
102
    [-1, 1] and label in [0, 9].
Y
Yu Yang 已提交
103

Q
qijun 已提交
104
    :return: Training reader creator
Y
Yu Yang 已提交
105 106
    :rtype: callable
    """
Y
Yi Wang 已提交
107
    return reader_creator(
108 109 110 111
        paddle.dataset.common.download(TRAIN_IMAGE_URL, 'mnist',
                                       TRAIN_IMAGE_MD5),
        paddle.dataset.common.download(TRAIN_LABEL_URL, 'mnist',
                                       TRAIN_LABEL_MD5), 100)
Y
Yi Wang 已提交
112

Y
Yu Yang 已提交
113

114 115 116 117
@deprecated(
    since="2.0.0",
    update_to="paddle.vision.datasets.MNIST",
    reason="Please use new dataset API which supports paddle.io.DataLoader")
Y
Yi Wang 已提交
118
def test():
Y
Yu Yang 已提交
119
    """
X
xuwei06 已提交
120
    MNIST test set creator.
Y
Yu Yang 已提交
121 122

    It returns a reader creator, each sample in the reader is image pixels in
123
    [-1, 1] and label in [0, 9].
Y
Yu Yang 已提交
124 125 126 127

    :return: Test reader creator.
    :rtype: callable
    """
Y
Yi Wang 已提交
128
    return reader_creator(
129 130 131
        paddle.dataset.common.download(TEST_IMAGE_URL, 'mnist', TEST_IMAGE_MD5),
        paddle.dataset.common.download(TEST_LABEL_URL, 'mnist', TEST_LABEL_MD5),
        100)
Y
Yancey1989 已提交
132 133


134 135 136 137
@deprecated(
    since="2.0.0",
    update_to="paddle.vision.datasets.MNIST",
    reason="Please use new dataset API which supports paddle.io.DataLoader")
138
def fetch():
139 140 141
    paddle.dataset.common.download(TRAIN_IMAGE_URL, 'mnist', TRAIN_IMAGE_MD5)
    paddle.dataset.common.download(TRAIN_LABEL_URL, 'mnist', TRAIN_LABEL_MD5)
    paddle.dataset.common.download(TEST_IMAGE_URL, 'mnist', TEST_IMAGE_MD5)
Y
Yancey1989 已提交
142
    paddle.dataset.common.download(TEST_LABEL_URL, 'mnist', TEST_LABEL_MD5)