mnist.py 4.6 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
Y
Yi Wang 已提交
24
import subprocess
Y
Yu Yang 已提交
25
import numpy
Y
Yu Yang 已提交
26
import platform
M
minqiyang 已提交
27 28
import six
import tempfile
M
minqiyang 已提交
29
from six.moves import range
Y
Your Name 已提交
30
__all__ = ['train', 'test', 'convert']
Y
Yi Wang 已提交
31

Y
Yi Wang 已提交
32 33
URL_PREFIX = 'http://yann.lecun.com/exdb/mnist/'
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():
Y
Yu Yang 已提交
45 46 47 48 49 50 51
        if platform.system() == 'Darwin':
            zcat_cmd = 'gzcat'
        elif platform.system() == 'Linux':
            zcat_cmd = 'zcat'
        else:
            raise NotImplementedError()

Y
Yi Wang 已提交
52 53
        # According to http://stackoverflow.com/a/38061619/724872, we
        # cannot use standard package gzip here.
M
minqiyang 已提交
54 55 56 57
        tmp_image_file = tempfile.TemporaryFile(prefix='paddle_dataset')
        m = subprocess.Popen(
            [zcat_cmd, image_filename], stdout=tmp_image_file).communicate()
        tmp_image_file.seek(16)  # skip some magic bytes
Y
Yi Wang 已提交
58

M
minqiyang 已提交
59 60 61 62 63
        # Python3 will not take stdout as file
        tmp_label_file = tempfile.TemporaryFile(prefix='paddle_dataset')
        l = subprocess.Popen(
            [zcat_cmd, label_filename], stdout=tmp_label_file).communicate()
        tmp_label_file.seek(8)  # skip some magic bytes
Y
Yu Yang 已提交
64

Y
Yu Yang 已提交
65 66 67
        try:  # reader could be break.
            while True:
                labels = numpy.fromfile(
M
minqiyang 已提交
68
                    tmp_label_file, 'ubyte', count=buffer_size).astype("int")
Y
Yu Yang 已提交
69

Y
Yu Yang 已提交
70 71
                if labels.size != buffer_size:
                    break  # numpy.fromfile returns empty slice after EOF.
Y
Yu Yang 已提交
72

Y
Yu Yang 已提交
73
                images = numpy.fromfile(
M
minqiyang 已提交
74 75
                    tmp_image_file, 'ubyte', count=buffer_size * 28 *
                    28).reshape((buffer_size, 28 * 28)).astype('float32')
Y
Yu Yang 已提交
76

Y
Yu Yang 已提交
77
                images = images / 255.0 * 2.0 - 1.0
Y
Yu Yang 已提交
78

79
                for i in range(buffer_size):
Y
Yu Yang 已提交
80 81
                    yield images[i, :], int(labels[i])
        finally:
82 83 84 85 86 87 88 89
            try:
                m.terminate()
            except:
                pass
            try:
                l.terminate()
            except:
                pass
Y
Yu Yang 已提交
90

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

Y
Yi Wang 已提交
93

Y
Yi Wang 已提交
94
def train():
Y
Yu Yang 已提交
95
    """
Q
qijun 已提交
96
    MNIST training set creator.
Y
Yu Yang 已提交
97 98 99 100

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

Q
qijun 已提交
101
    :return: Training reader creator
Y
Yu Yang 已提交
102 103
    :rtype: callable
    """
Y
Yi Wang 已提交
104
    return reader_creator(
105 106 107 108
        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 已提交
109

Y
Yu Yang 已提交
110

Y
Yi Wang 已提交
111
def test():
Y
Yu Yang 已提交
112
    """
X
xuwei06 已提交
113
    MNIST test set creator.
Y
Yu Yang 已提交
114 115 116 117 118 119 120

    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 已提交
121
    return reader_creator(
122 123 124
        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 已提交
125 126


127
def fetch():
128 129 130
    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 已提交
131
    paddle.dataset.common.download(TEST_LABEL_URL, 'mnist', TEST_LABEL_MD5)
R
root 已提交
132 133 134 135 136 137


def convert(path):
    """
    Converts dataset to recordio format
    """
138 139
    paddle.dataset.common.convert(path, train(), 1000, "minist_train")
    paddle.dataset.common.convert(path, test(), 1000, "minist_test")