-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrainNetwork.py
141 lines (108 loc) · 3.32 KB
/
trainNetwork.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Activation, Conv2D
from keras.layers.convolutional import Convolution2D, MaxPooling2D, ZeroPadding2D
from keras.optimizers import SGD
from keras import backend as k
import numpy as np
import pandas as pd
import os
os.system("cls")
WIDTH = 80
HEIGHT = 60
MAX_CLASSIFIERS = 3 # left & right
alpha = 0.09
EPOCHS = 8
BATCH = 10
fname = 'keras-trained-E%d.h5' % EPOCHS
def createConvNet(w,h):
sgd = SGD(lr=alpha, decay=1e-6, momentum=0.9, nesterov=True)
model = Sequential()
'''
model.add(Conv2D(32,3,3,input_shape=(w,h,1)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Conv2D(32,3,3))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Conv2D(64,3,3))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Conv2D(64,3,3))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(96))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(96))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(48))
model.add(Activation('relu'))
model.add(Dense(MAX_CLASSIFIERS))
model.add(Activation('softmax'))
'''
#VGG-16
model.add(ZeroPadding2D((1,1),input_shape=(w,h,1)))
model.add(Conv2D(32,3,3))
model.add(Activation('relu'))
model.add(MaxPooling2D((2,2),strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Conv2D(64,3,3))
model.add(Activation('relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Conv2D(64,3,3))
model.add(Activation('relu'))
model.add(MaxPooling2D((2,2),strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Conv2D(128,3,3))
model.add(Activation('relu'))
model.add(MaxPooling2D((2,2),strides=(2,2)))
model.add(Flatten())
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(MAX_CLASSIFIERS))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy',optimizer=sgd,metrics=['accuracy'])
return model
print("Loading numpy data...")
data = np.load('final_train_data.npy')
print("Splitting train-test sets...")
train_test_split_factor = int(round(0.1*len(data)))
print("train_test_split_factor = %d" % train_test_split_factor)
train = data[:-train_test_split_factor]
test = data[-train_test_split_factor:]
q = []
y = []
for i in range(train.shape[0]):
try:
t = train[i]
x = t[0].reshape(WIDTH,HEIGHT,1)
q.append(x)
y.append(t[1])
except Exception as e:
print(str(e))
x = np.array(q,dtype='uint8')
print(x.shape)
print(len(y))
x_test = []
y_test = []
for i in test:
x_test.append(i[0])
y_test.append(i[1])
x_test = np.array(x_test,dtype='uint8')
x_test = x_test.reshape(-1,WIDTH,HEIGHT,1)
print(x_test.shape)
print("Creating neural network...")
network = createConvNet(WIDTH,HEIGHT)
print("training network...")
network.fit(x,y,batch_size=BATCH,nb_epoch=EPOCHS,verbose=1,validation_data=(x_test,y_test))
score = network.evaluate(x_test,y_test,verbose=2)
print('Test loss: ',score[0])
print('Test accuracy: ', score[1])
network.save(fname)