-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_loader.py
53 lines (42 loc) · 1.53 KB
/
data_loader.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
import numpy as np
import struct as st
def load_mnist_train():
train_data_file = 'MNIST/train-images-idx3-ubyte'
train_labels_file = 'MNIST/train-labels-idx1-ubyte'
data = load_images(train_data_file)
labels = load_labels(train_labels_file)
return data,labels
def load_mnist_test():
train_data_file = 'MNIST/t10k-images-idx3-ubyte'
train_labels_file = 'MNIST/t10k-labels-idx1-ubyte'
data = load_images(train_data_file)
labels = load_labels(train_labels_file)
return data,labels
def load_images(fname=None):
assert fname is not None, "No file specified"
fid = open(fname,'r')
magic = fid.read(4)
numims = st.unpack('>I',fid.read(4))[0]
cols = st.unpack('>I',fid.read(4))[0]
rows = st.unpack('>I',fid.read(4))[0]
rawdat = np.fromfile(fid,dtype=np.uint8)
rawdat = rawdat.astype(np.float64)
rawdat = rawdat.reshape(numims,rows,cols)
rawdat = rawdat.transpose(1,2,0)
# rescale to [0,1]
rawdat = rawdat / np.max(rawdat)
return rawdat
def load_labels(fname=None):
fid = open(fname,'r')
magic = fid.read(4)
numims = st.unpack('>I',fid.read(4))[0]
labels = np.fromfile(fid,dtype=np.uint8)
labels = labels.astype(np.int32)
return labels
if __name__=='__main__':
train_data_file = 'MNIST/train-images-idx3-ubyte'
train_labels_file = 'MNIST/train-labels-idx1-ubyte'
data = load_images(train_data_file)
labels = load_labels(train_labels_file)
print "Shape of data is ",data.shape
print "Shape of labels is ",labels.shape