-
Notifications
You must be signed in to change notification settings - Fork 2k
/
main.py
72 lines (57 loc) · 1.68 KB
/
main.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
import numpy as np
import struct
import matplotlib.pyplot as plt
from perceptron import *
def loadImageSet(which=0):
print "load image set"
binfile=None
if which==0:
binfile = open("data/train-images.idx3-ubyte", 'rb')
else:
binfile= open("data/t10k-images.idx3-ubyte", 'rb')
buffers = binfile.read()
head = struct.unpack_from('>IIII' , buffers ,0)
print "head,",head
offset=struct.calcsize('>IIII')
imgNum=head[1]
width=head[2]
height=head[3]
#[60000]*28*28
bits=imgNum*width*height
bitsString='>'+str(bits)+'B' #like '>47040000B'
imgs=struct.unpack_from(bitsString,buffers,offset)
binfile.close()
imgs=np.reshape(imgs,[imgNum,width,height])
print "load imgs finished"
return imgs
def loadLabelSet(which=0):
print "load label set"
binfile=None
if which==0:
binfile = open("data/train-labels.idx1-ubyte", 'rb')
else:
binfile= open("data/t10k-labels.idx1-ubyte", 'rb')
buffers = binfile.read()
head = struct.unpack_from('>II' , buffers ,0)
print "head,",head
imgNum=head[1]
offset = struct.calcsize('>II')
numString='>'+str(imgNum)+"B"
labels= struct.unpack_from(numString , buffers , offset)
binfile.close()
labels=np.reshape(labels,[imgNum,1])
#print labels
print 'load label finished'
return labels
if __name__=="__main__":
imgs = loadImageSet()
labels = loadLabelSet()
index = 10
print imgs[index]
print labels[index]
fig = plt.figure()
plotwindow = fig.add_subplot(111)
plt.imshow(imgs[index] , cmap='gray')
# plt.show()
perceptron = Perceptron()
perceptron.hog_test(imgs[index])