-
Notifications
You must be signed in to change notification settings - Fork 0
/
load.py
58 lines (56 loc) · 1.49 KB
/
load.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
from matplotlib import pyplot as plt
from mnist import MNIST
import numpy as np
def decimal_to_bit(position):
output=np.zeros([1,10])
output[0,position]='1'
return output
def reshape_image(orginal_img):
reshaped_image=np.reshape(orginal_img,(1,784))
#normalize image
reshaped_image=reshaped_image/255
return reshaped_image
def reshape_label(orginal_lbl):
reshaped_label=np.reshape(orginal_lbl,(1,1))
return reshaped_label
def load_training_data():
mndata=MNIST('./data/')
mndata.gz=True
images,label=mndata.load_training()
x=[]
y=[]
i=0
print('***Loading Training Data***')
for img,lbl in zip(images,label):
y.append(decimal_to_bit(lbl))
x.append(reshape_image(img))
i+=1
if i%1000==0:
print('loading image ',i)
return x,y
def load_test_data():
mndata=MNIST('./data/')
mndata.gz=True
images,label=mndata.load_testing()
test_x=[]
test_y=[]
i=0
print('***Loading Testing Data***')
for img,lbl in zip(images,label):
test_y.append(lbl)
test_x.append(reshape_image(img))
i+=1
if i%1000==0:
print('loading image ',i)
return test_x,test_y
def show_img(x,y):
print(y)
x=np.reshape(x,(28,28))
p=plt.imshow(x,shape=(28,28))
plt.show(p)
if __name__=='__main__':
#x,y=load_training_data()
#print(y[57])
test_x,test_y=load_test_data()
for x,y in zip(test_x,test_y):
show_img(x,y)