-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
test_mnist_ubyte.py
52 lines (46 loc) · 1.71 KB
/
test_mnist_ubyte.py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import struct
import tempfile
import numpy
from theano.compat import six
from pylearn2.utils.mnist_ubyte import read_mnist_images, read_mnist_labels
from pylearn2.utils.mnist_ubyte import MNIST_LABEL_MAGIC, MNIST_IMAGE_MAGIC
def test_read_labels():
with tempfile.TemporaryFile() as f:
data = struct.pack('>iiBBBB', MNIST_LABEL_MAGIC, 4, 9, 4, 3, 1)
f.write(data)
f.seek(0)
arr = read_mnist_labels(f)
assert arr.shape == (4,)
assert arr.dtype == numpy.dtype('uint8')
assert arr[0] == 9
assert arr[1] == 4
assert arr[2] == 3
assert arr[3] == 1
def test_read_images():
header = struct.pack('>iiii', MNIST_IMAGE_MAGIC, 4, 3, 2)
data = six.b('\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00'
'\t\x00\x00\x00\x00\x00\x00\xff.\x00\x00\x00\x00\x00')
with tempfile.TemporaryFile() as f:
buf = header + data
f.write(buf)
f.seek(0)
arr = read_mnist_images(f)
assert arr.dtype == numpy.dtype('uint8')
assert arr[0, 1, 1] == 4
assert arr[1, 2, 0] == 9
assert arr[2, 2, 1] == 255
assert arr[3, 0, 0] == 46
assert (arr == 0).sum() == 20
f.seek(0)
arr = read_mnist_images(f, dtype='float32')
assert arr.dtype == numpy.dtype('float32')
assert arr[0, 1, 1] == numpy.float32(4 / 255.)
assert arr[1, 2, 0] == numpy.float32(9 / 255.)
assert arr[2, 2, 1] == 1.0
assert arr[3, 0, 0] == numpy.float32(46 / 255.)
assert (arr == 0).sum() == 20
f.seek(0)
arr = read_mnist_images(f, dtype='bool')
assert arr.dtype == numpy.dtype('bool')
assert arr[2, 2, 1]
assert (arr == 0).sum() == 23