-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtrain.py
93 lines (71 loc) · 2.37 KB
/
train.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
import tflearn
import cv2
from tflearn.data_preprocessing import ImagePreprocessing
from tflearn.data_augmentation import ImageAugmentation
import numpy as np
print "Loading data-----------------------"
k = 0
X = []
Y_t = []
for i in [1,2,3,4,5]:
while True:
img = cv2.imread('./'+str(i)+'/'+str(k)+'.jpg')
if img == None:
break
X.append(cv2.resize(img.astype('float'),(100,100)))
Y_t.append([i-1])
k += 1
print X
print Y_t
Y = tflearn.data_utils.to_categorical(Y_t,5)
print Y
Y_t=[]
'''
image_prep = ImagePreprocessing()
image_prep.add_featurewise_zero_center()
image_prep.add_featurewise_stdnorm()
image_aug = ImageAugmentation()
image_aug.add_random_flip_leftright()
image_aug.add_random_rotation(max_angle=25.)
'''
print "Creating net----------------"
#t_norm=tflearn.initializations.uniform(minval=-1.0,maxval=1.0)
net = tflearn.input_data(shape = [None,100,100,3]
#,data_preprocessing = image_prep)
#,data_augmentation = image_aug
)
net = tflearn.conv_2d(net,100,3 ,activation = 'ReLU')#,weights_init=t_norm)
net = tflearn.conv_2d(net, 80,3 ,activation = 'ReLU')#,weights_init=t_norm)
net = tflearn.max_pool_2d(net,2)
net = tflearn.conv_2d(net,75,3 ,activation = 'ReLU')#,weights_init=t_norm)
net = tflearn.conv_2d(net, 40,3 ,activation = 'ReLU')#,weights_init=t_norm)
net = tflearn.max_pool_2d(net,2)
net = tflearn.conv_2d(net,64,3 ,activation = 'ReLU')#,weights_init=t_norm)
net = tflearn.conv_2d(net,32,3 ,activation = 'ReLU')#,weights_init=t_norm)
net = tflearn.max_pool_2d(net,2)
net = tflearn.fully_connected(net, 200, activation = 'ReLU')#,weights_init=t_norm)
#net = tflearn.dropout(net,0.5)
net = tflearn.fully_connected(net, 5, activation = 'softmax')#,weights_init=t_norm)
rm = tflearn.optimizers.RMSprop (learning_rate=0.001, decay=0.5, momentum=0.2)
net = tflearn.regression(net, optimizer = rm,
loss = 'categorical_crossentropy')
model = tflearn.DNN(net)
print "Net created------------------"
model.fit(X, Y, n_epoch=20, shuffle=True,
snapshot_epoch=False,
batch_size=10)
print np.argmax(model.predict(X[:15]))+1
print Y[:15]
model.save('test.tflearn')
cam = cv2.VideoCapture(0)
while True:
ret_val, img = cam.read()
cv2.imshow('my webcam', img)
img = cv2.resize(img.astype('float'), (100, 100))
k = cv2.waitKey(1)
if k==27:
break
elif k==32:
print np.argmax(model.predict([img]))
print model.predict([img])
cv2.destroyAllWindows()