mnist.py 2.6 KB
Newer Older
Y
Yi Wang 已提交
1 2 3
"""
MNIST dataset.
"""
Y
Yi Wang 已提交
4 5
import paddle.v2.dataset.common
import subprocess
Y
Yu Yang 已提交
6
import numpy
Y
Yu Yang 已提交
7
import platform
Y
Yi Wang 已提交
8 9
__all__ = ['train', 'test']

Y
Yi Wang 已提交
10 11
URL_PREFIX = 'http://yann.lecun.com/exdb/mnist/'
TEST_IMAGE_URL = URL_PREFIX + 't10k-images-idx3-ubyte.gz'
Y
Yu Yang 已提交
12
TEST_IMAGE_MD5 = '9fb629c4189551a2d022fa330f9573f3'
Y
Yi Wang 已提交
13
TEST_LABEL_URL = URL_PREFIX + 't10k-labels-idx1-ubyte.gz'
Y
Yu Yang 已提交
14
TEST_LABEL_MD5 = 'ec29112dd5afa0611ce80d1b7f02629c'
Y
Yi Wang 已提交
15 16 17 18
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 已提交
19 20


Y
Yi Wang 已提交
21 22
def reader_creator(image_filename, label_filename, buffer_size):
    def reader():
Y
Yu Yang 已提交
23 24 25 26 27 28 29
        if platform.system() == 'Darwin':
            zcat_cmd = 'gzcat'
        elif platform.system() == 'Linux':
            zcat_cmd = 'zcat'
        else:
            raise NotImplementedError()

Y
Yi Wang 已提交
30 31
        # According to http://stackoverflow.com/a/38061619/724872, we
        # cannot use standard package gzip here.
Y
Yu Yang 已提交
32
        m = subprocess.Popen([zcat_cmd, image_filename], stdout=subprocess.PIPE)
Y
Yi Wang 已提交
33
        m.stdout.read(16)  # skip some magic bytes
Y
Yi Wang 已提交
34

Y
Yu Yang 已提交
35
        l = subprocess.Popen([zcat_cmd, label_filename], stdout=subprocess.PIPE)
Y
Yi Wang 已提交
36
        l.stdout.read(8)  # skip some magic bytes
Y
Yu Yang 已提交
37

Y
Yu Yang 已提交
38 39 40 41
        try:  # reader could be break.
            while True:
                labels = numpy.fromfile(
                    l.stdout, 'ubyte', count=buffer_size).astype("int")
Y
Yu Yang 已提交
42

Y
Yu Yang 已提交
43 44
                if labels.size != buffer_size:
                    break  # numpy.fromfile returns empty slice after EOF.
Y
Yu Yang 已提交
45

Y
Yu Yang 已提交
46 47 48
                images = numpy.fromfile(
                    m.stdout, 'ubyte', count=buffer_size * 28 * 28).reshape(
                        (buffer_size, 28 * 28)).astype('float32')
Y
Yu Yang 已提交
49

Y
Yu Yang 已提交
50
                images = images / 255.0 * 2.0 - 1.0
Y
Yu Yang 已提交
51

Y
Yu Yang 已提交
52 53 54 55 56
                for i in xrange(buffer_size):
                    yield images[i, :], int(labels[i])
        finally:
            m.terminate()
            l.terminate()
Y
Yu Yang 已提交
57

Y
Yu Yang 已提交
58
    return reader
Y
Yu Yang 已提交
59

Y
Yi Wang 已提交
60

Y
Yi Wang 已提交
61 62
def train():
    return reader_creator(
Y
Yi Wang 已提交
63 64 65 66 67
        paddle.v2.dataset.common.download(TRAIN_IMAGE_URL, 'mnist',
                                          TRAIN_IMAGE_MD5),
        paddle.v2.dataset.common.download(TRAIN_LABEL_URL, 'mnist',
                                          TRAIN_LABEL_MD5), 100)

Y
Yu Yang 已提交
68

Y
Yi Wang 已提交
69 70
def test():
    return reader_creator(
Y
Yi Wang 已提交
71 72 73 74
        paddle.v2.dataset.common.download(TEST_IMAGE_URL, 'mnist',
                                          TEST_IMAGE_MD5),
        paddle.v2.dataset.common.download(TEST_LABEL_URL, 'mnist',
                                          TEST_LABEL_MD5), 100)