-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConvNet.py
303 lines (236 loc) · 7.74 KB
/
ConvNet.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import numpy as np
import theano
import lasagne
from lasagne import layers
import time
from lasagne.updates import nesterov_momentum
from nolearn.lasagne import NeuralNet
from nolearn.lasagne import visualize
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
import readFile
import cv2
from skimage.feature import local_binary_pattern
from sklearn.metrics import accuracy_score
import labels
import featureGeneration
from sklearn.svm import LinearSVC
folder="D:\data-1"
train=labels.train_image
labels_train=labels.train_labels
test=labels.test_image
labels_test=labels.test_labels
train_images=readFile.resize_images(folder,train)
test_images=readFile.resize_images(folder,test)
print "Read the images"
train_im=np.array(train_images).reshape(-1,3,128,64)
test_im=np.array(test_images).reshape(-1,3,128,64)
print "reshaped the images"
training_label=np.array(labels_train).astype(np.uint8)
testing_label=np.array(labels_test).astype(np.uint8)
print "running the neural net"
time.sleep(10000)
# convolutionNet1=NeuralNet(
# layers=[('input',layers.InputLayer),
# ('conv2d1',layers.Conv2DLayer),
# ('maxpool1',layers.MaxPool2DLayer),
# ('conv2d2',layers.Conv2DLayer),
# ('maxpool2',layers.MaxPool2DLayer),
# ('conv2d3',layers.Conv2DLayer),
# ('conv2d4',layers.Conv2DLayer),
# ('conv2d5',layers.Conv2DLayer),
# ('maxpool5',layers.MaxPool2DLayer),
# ('dense6',layers.DenseLayer),
# ('dropout6',layers.DropoutLayer),
# ('dense7',layers.DenseLayer),
# ('dropout7',layers.DropoutLayer),
# ('output', layers.DenseLayer)
# ],
# #input_layer
# input_shape=(None,3,128,64),
# #conv2d1
# conv2d1_num_filters=8,
# conv2d1_filter_size=(5, 5),
# conv2d1_nonlinearity=lasagne.nonlinearities.rectify,
# conv2d1_W=lasagne.init.GlorotUniform(),
# #maxpool1
# maxpool1_pool_size=(2, 2),
# #conv2d2
# conv2d2_num_filters=16,
# conv2d2_filter_size=(5, 3),
# conv2d2_nonlinearity=lasagne.nonlinearities.rectify,
# #maxpool2
# maxpool2_pool_size=(2, 2),
# #conv2d3
# conv2d3_num_filters=32,
# conv2d3_filter_size=(6, 3),
# conv2d3_nonlinearity=lasagne.nonlinearities.rectify,
# #conv2d4
# conv2d4_num_filters=64,
# conv2d4_filter_size=(5, 3),
# conv2d4_nonlinearity=lasagne.nonlinearities.rectify,
# #conv2d5
# conv2d5_num_filters=128,
# conv2d5_filter_size=(5, 3),
# conv2d5_nonlinearity=lasagne.nonlinearities.rectify,
# #maxpool5
# maxpool5_pool_size=(2, 2),
# #dense6
# dense6_num_units=1024,
# dense6_nonlinearity=lasagne.nonlinearities.rectify,
# #drop6
# dropout6_p=0.5,
# #dense7
# dense7_num_units=1024,
# dense7_nonlinearity=lasagne.nonlinearities.rectify,
# #drop7
# dropout7_p=0.5,
# #output
# output_nonlinearity=lasagne.nonlinearities.softmax,
# output_num_units=7,
# #optimization method params
# update=nesterov_momentum,
# update_learning_rate=0.01,
# update_momentum=0.9,
# max_epochs=10,
# verbose=1,
# )
convolutionNet=NeuralNet(
layers=[('input',layers.InputLayer),
('conv2d1',layers.Conv2DLayer),
('maxpool1',layers.MaxPool2DLayer),
('conv2d2',layers.Conv2DLayer),
('maxpool2',layers.MaxPool2DLayer),
('conv2d3',layers.Conv2DLayer),
('maxpool3',layers.MaxPool2DLayer),
('dense4',layers.DenseLayer),
('dropout4',layers.DropoutLayer),
('output', layers.DenseLayer)
],
#input layer
input_shape=(None,3,128,64),
#layer conv2d1
conv2d1_num_filters=8,
conv2d1_filter_size=(9, 5),
conv2d1_nonlinearity=lasagne.nonlinearities.rectify,
conv2d1_W=lasagne.init.GlorotUniform(),
# layer maxpool1
maxpool1_pool_size=(2, 2),
# layer conv2d1
conv2d2_num_filters=16,
conv2d2_filter_size=(9, 5),
conv2d2_nonlinearity=lasagne.nonlinearities.rectify,
# layer maxpool1
maxpool2_pool_size=(2, 2),
# layer conv2d1
conv2d3_num_filters=32,
conv2d3_filter_size=(9, 6),
conv2d3_nonlinearity=lasagne.nonlinearities.rectify,
# layer maxpool1
maxpool3_pool_size=(2, 2),
#dense4
dense4_num_units=256,
dense4_nonlinearity=lasagne.nonlinearities.rectify,
# dropout4
dropout4_p=0.5,
# output
output_nonlinearity=lasagne.nonlinearities.softmax,
output_num_units=7,
# optimization method params
update=nesterov_momentum,
update_learning_rate=0.01,
update_momentum=0.9,
max_epochs=10,
verbose=1,
)
# convolutionNet=NeuralNet(
# layers=[('input',layers.InputLayer),
# ('conv2d1',layers.Conv2DLayer),
# ('maxpool1',layers.MaxPool2DLayer),
# ('conv2d2',layers.Conv2DLayer),
# ('maxpool2',layers.MaxPool2DLayer),
# ('dense3',layers.DenseLayer),
# ('dropout3',layers.DropoutLayer),
# ('output', layers.DenseLayer)
# ],
# #input layer
# input_shape=(None,3,128,64),
# #layer conv2d1
# conv2d1_num_filters=8,
# conv2d1_filter_size=(19, 15),
# conv2d1_nonlinearity=lasagne.nonlinearities.rectify,
# conv2d1_W=lasagne.init.GlorotUniform(),
# # layer maxpool1
# maxpool1_pool_size=(2, 2),
# #layer conv2d1
# conv2d2_num_filters=16,
# conv2d2_filter_size=(20, 10),
# conv2d2_nonlinearity=lasagne.nonlinearities.rectify,
# # layer maxpool1
# maxpool2_pool_size=(2, 2),
# #dense4
# dense3_num_units=512,
# dense3_nonlinearity=lasagne.nonlinearities.rectify,
# # dropout3
# dropout3_p=0.5,
# # output
# output_nonlinearity=lasagne.nonlinearities.softmax,
# output_num_units=7,
# # optimization method params
# update=nesterov_momentum,
# update_learning_rate=0.01,
# update_momentum=0.9,
# max_epochs=10,
# verbose=1,
# )
nn=convolutionNet.fit(train_im,training_label)
preds=convolutionNet.predict(test_im)
acc=accuracy_score(testing_label,preds)
print acc
# #feature extraction from Neural Net
# train_2=labels.train_image_2
# labels_train_2=labels.train_labels_2
# test_2=labels.test_image_2
# labels_test_2=labels.test_labels_2
# train_images_2=readFile.resize_images(folder,train_2)
# test_images_2=readFile.resize_images(folder,test_2)
# train_im_2=np.array(train_images_2).reshape(-1,3,128,64)
# test_im_2=np.array(test_images_2).reshape(-1,3,128,64)
# training_label_2=np.array(labels_train_2).astype(np.uint8)
# testing_label_2=np.array(labels_test_2).astype(np.uint8)
# dense_layer6 = layers.get_output(convolutionNet.layers_['dense6'], deterministic=True)
# dense_layer7 = layers.get_output(convolutionNet.layers_['dense7'], deterministic=True)
# input_var = convolutionNet.layers_['input'].input_var
# f_dense6=theano.function([input_var], dense_layer6)
# f_dense7=theano.function([input_var], dense_layer7)
# #train svm on dense6 features
# dense6_features=[]
# dense6_features_test=[]
# for im in train_im_2:
# pred=f_dense6(im)
# dense6_features.append(pred.ravel())
# for im in test_im_2:
# pred=dense6(im)
# dense6_features_test.append(pred.ravel())
# model=LinearSVC()
# model.fit(dense6_features,training_label_2)
# res=model.predict(dense6_features_test)
# acc1=accuracy_score(testing_label_2,res)
# print acc1
# #train svm on dense7 features
# dense7_features=[]
# dense7_features_test=[]
# for im in train_im_2:
# pred=f_dense7(im)
# dense7_features.append(pred.ravel())
# for im in test_im_2:
# pred=dense7(im)
# dense7_features_test.append(pred.ravel())
# mode1=LinearSVC()
# mode1.fit(dense7_features,training_label_2)
# res=mode1.predict(dense7_features_test)
# acc2=accuracy_score(testing_label_2,res)
# print acc2