-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgetMNIST.py
126 lines (96 loc) · 3.18 KB
/
getMNIST.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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import urllib
import gzip
import struct
from array import array
import numpy as np
import os
"""
Downloads MNIST data set and saves the data set as pickled numpy arrays
Script adapted from: http://abel.ee.ucla.edu/cvxopt/_downloads/mnist.py
and python documentation
"""
def gzip_extract(filenameIn,filenameOut):
f_in=gzip.open(filenameIn,'rb')
f_out=open(filenameOut,'wb')
f_out.write(f_in.read())
f_in.close()
f_out.close()
def unpack_mnist(filenameIm,filenameLabels):
flbl = open(filenameLabels, 'rb')
magic_nr, size = struct.unpack(">II", flbl.read(8))
lbl = array("b", flbl.read())
flbl.close()
fimg = open(filenameIm, 'rb')
magic_nr, size, rows, cols = struct.unpack(">IIII", fimg.read(16))
img=array("B",fimg.read())
fimg.close()
ind = [ k for k in xrange(size)]
N=len(ind)
images = np.zeros((rows,cols,N),dtype=np.uint8)
labels = np.zeros((N, 1),dtype=np.uint8)
for i in xrange(N):
images[:, :,i] = np.array(img[ i*rows*cols : (i+1)*rows*cols]).reshape(rows,cols)
labels[i] = lbl[i]
return images,labels
train_img_url="http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz"
train_label_url="http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz"
test_img_url="http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz"
test_label_url="http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz"
train_img_gz="train-images-idx3-ubyte.gz"
train_label_gz="train-labels-idx1-ubyte.gz"
test_img_gz="t10k-images-idx3-ubyte.gz"
test_label_gz="t10k-labels-idx1-ubyte.gz"
trainIm_file="train-images-idx3-ubyte"
trainLabel_file="train-labels-idx1-ubyte"
testIm_file="t10k-images-idx3-ubyte"
testLabel_file="t10k-labels-idx1-ubyte"
print "Downloading MNIST from http://yann.lecun.com/exdb/mnist/index.html ..."
print "Downloading training set...",
urllib.urlretrieve(train_img_url,train_img_gz)
urllib.urlcleanup()
urllib.urlretrieve(train_label_url,train_label_gz)
urllib.urlcleanup()
print "Done!"
print "Extracting ubyte files from gz files..."
print "-"+train_img_gz
print "-"+train_label_gz
gzip_extract(train_img_gz,trainIm_file)
gzip_extract(train_label_gz,trainLabel_file)
print "...Done!"
print "Extracting training data into numpy arrays...",
trainIm,trainLabel=unpack_mnist(trainIm_file,trainLabel_file)
print "Done!"
print "Pickling numpy arrays...",
trainIm.dump("trainIm.pkl");
trainLabel.dump("trainLabel.pkl")
print "Done!"
print " "
print "Downloading test set...",
urllib.urlretrieve(test_img_url,test_img_gz)
urllib.urlcleanup()
urllib.urlretrieve(test_label_url,test_label_gz)
urllib.urlcleanup()
print "Done!"
print "Extracting ubyte files from gz files..."
print "-"+test_img_gz
print "-"+test_label_gz
gzip_extract(test_img_gz,testIm_file)
gzip_extract(test_label_gz,testLabel_file)
print "...Done!"
print "Extracting test data into numpy arrays...",
testIm,testLabel=unpack_mnist(testIm_file,testLabel_file)
print "Done!"
print "Pickling numpy arrays...",
testIm.dump("testIm.pkl");
testLabel.dump("testLabel.pkl")
print "Done!"
print "Cleaning up...",
os.remove(train_img_gz)
os.remove(train_label_gz)
os.remove(test_img_gz)
os.remove(test_label_gz)
os.remove(trainIm_file)
os.remove(trainLabel_file)
os.remove(testIm_file)
os.remove(testLabel_file)
print "Done!"