-
Notifications
You must be signed in to change notification settings - Fork 1
/
tut_k_single_1_small.py
338 lines (246 loc) · 10.2 KB
/
tut_k_single_1_small.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import cv2
import numpy as np
import os
import keras
from keras import backend as K
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D, SeparableConv2D
from keras.utils import np_utils
import itertools
import matplotlib.pyplot as plt
import matplotlib
#from matplotlib import save_model_to_file
from keras.utils.vis_utils import plot_model
from numpy import *
from sklearn.utils import shuffle
from sklearn.model_selection import train_test_split
from sklearn.model_selection import StratifiedKFold
import time
import keras.utils.vis_utils
from keras.utils.vis_utils import plot_model
import keras.backend as K
config = K.tf.ConfigProto()
config.gpu_options.allow_growth = True
session = K.tf.Session(config=config)
start = time.time()
def plot_confusion_matrix(cm, classes,
normalize=False,
title='Confusion matrix',
cmap=plt.cm.Blues):
"""
This function prints and plots the confusion matrix.
Normalization can be applied by setting `normalize=True`.
"""
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
print("Normalized confusion matrix")
else:
print('Confusion matrix, without normalization')
print(cm)
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes)
fmt = '.2f' if normalize else 'd'
thresh = cm.max() / 2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, format(cm[i, j], fmt),
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
#May need to move all pics into a single folder
path1 = 'C:/Users/Luke/Documents/Computer_Science/CSC3095/Software/Data/train_channels_tsne/ch3'
path2 = 'C:/Users/Luke/Documents/Computer_Science/CSC3095/Software/Data/train_channels_tsne/ch4'
path3 = 'C:/Users/Luke/Documents/Computer_Science/CSC3095/Software/Data/train_channels_tsne/ch6'
#BLACK = [0,0,0]
#size of resized images
img_rows = 66
img_cols = 66
listing = os.listdir(path1)
#print(listing)
num_samples=size(listing)
#print(num_samples)
#0 rather than -1 loads the image as 8-bit rather than 16-bit
ch3_matrix = array([array(cv2.resize(cv2.copyMakeBorder(cv2.imread(path1 + '\\' + im2,-1),
int((img_rows - (cv2.imread(path1 + '\\' + im2,-1).shape[0]))/2),
int((img_rows - (cv2.imread(path1 + '\\' + im2,-1).shape[0]))/2),
int((img_cols - (cv2.imread(path1 + '\\' + im2,-1).shape[1]))/2),
int((img_cols - (cv2.imread(path1 + '\\' + im2,-1).shape[1]))/2),
cv2.BORDER_CONSTANT, value=int(cv2.imread(path1 + '\\' + im2,-1)[0][0])),(img_rows,img_cols))).flatten()
for im2 in listing])
print(ch3_matrix.shape)
print(ch3_matrix[0][0])
mean = np.mean(ch3_matrix)
print("mean = ", mean)
sd = np.std(ch3_matrix)
print("std = ", sd)
ch3_matrix = ((ch3_matrix - mean) / sd)
print("ch3max value", np.amax(ch3_matrix))
print("ch3min value", np.amin(ch3_matrix))
#print("Channel3 " , ch3_matrix)
print(ch3_matrix.shape)
ch4_matrix = array([array(cv2.resize(cv2.copyMakeBorder(cv2.imread(path2 + '\\' + im2,-1),
int((img_rows - (cv2.imread(path2 + '\\' + im2,-1).shape[0]))/2),
int((img_rows - (cv2.imread(path2 + '\\' + im2,-1).shape[0]))/2),
int((img_cols - (cv2.imread(path2 + '\\' + im2,-1).shape[1]))/2),
int((img_cols - (cv2.imread(path2 + '\\' + im2,-1).shape[1]))/2),
cv2.BORDER_CONSTANT, value=int(cv2.imread(path2 + '\\' + im2,-1)[0][0])),(img_rows,img_cols))).flatten()
for im2 in listing])
mean = np.mean(ch4_matrix)
print("mean = ", mean)
sd = np.std(ch4_matrix)
print("std = ", sd)
ch4_matrix = ((ch4_matrix - mean) / sd)
print("ch4max value", np.amax(ch4_matrix))
print("ch4min value", np.amin(ch4_matrix))
#print("Channel6 " , ch6_matrix)
print(ch4_matrix.shape)
ch6_matrix = array([array(cv2.resize(cv2.copyMakeBorder(cv2.imread(path3 + '\\' + im2,-1),
int((img_rows - (cv2.imread(path3 + '\\' + im2,-1).shape[0]))/2),
int((img_rows - (cv2.imread(path3 + '\\' + im2,-1).shape[0]))/2),
int((img_cols - (cv2.imread(path3 + '\\' + im2,-1).shape[1]))/2),
int((img_cols - (cv2.imread(path3 + '\\' + im2,-1).shape[1]))/2),
cv2.BORDER_CONSTANT, value=int(cv2.imread(path3 + '\\' + im2,-1)[0][0])),(img_rows,img_cols))).flatten()
for im2 in listing])
mean = np.mean(ch6_matrix)
print("mean = ", mean)
sd = np.std(ch6_matrix)
print("std = ", sd)
ch6_matrix = ((ch6_matrix - mean) / sd)
#print("Channel6 " , ch6_matrix)
print(ch6_matrix.shape)
print("ch6max value", np.amax(ch6_matrix))
print("ch6min value", np.amin(ch6_matrix))
#matrix of all channels per image
comb_matrix = np.zeros((num_samples,(img_rows * img_cols * 3)))
#print("Size = ", len(im_matrix))
for i in range(0,num_samples):
comb_matrix[i] = np.concatenate((ch3_matrix[i],ch4_matrix[i], ch6_matrix[i]), axis =0)
print("Combined = " , comb_matrix)
print(comb_matrix.shape)
print(comb_matrix[60])
#img1 = comb_matrix[0].reshape(2, 66, 66)
#img1 = img1[0]
#plt.imshow(img1)
#plt.show()
#Can normalise here or for each matrix
#Label images
#create an array of 1s
label = np.ones((num_samples,), dtype = int)
label[0:14]=0 #Ana
label[14:114]=1 #G1
label[114:214]=2 #G2
label[214:282]=3 #Meta
label[282:382]=4 #Pro
label[382:482]=5 #S
label[482:509]=6 #Telo
data, Label = shuffle(comb_matrix, label, random_state=2)
#data, Label = comb_matrix, label
#train_data = [data,Label]
batch_size = 32
num_classes = 7
epochs = 15
input_shape = (3, img_rows, img_cols)
def base_model():
model = Sequential()
model.add(Conv2D(64, kernel_size=(3, 3),
activation='relu',
input_shape=input_shape,data_format='channels_first'))
model.add(Conv2D(64, kernel_size=(3, 3),
activation='relu',
input_shape=input_shape,data_format='channels_first'))
model.add(MaxPooling2D(pool_size=(2, 2), data_format='channels_first'))
model.add(Conv2D(128, kernel_size=(3, 3),
activation='relu',
input_shape=input_shape,data_format='channels_first'))
model.add(Conv2D(128, kernel_size=(3, 3),
activation='relu',
input_shape=input_shape,data_format='channels_first'))
model.add(MaxPooling2D(pool_size=(2, 2), data_format='channels_first'))
model.add(Conv2D(256, kernel_size=(3, 3),
activation='relu',
input_shape=input_shape,data_format='channels_first'))
model.add(Conv2D(256, kernel_size=(3, 3),
activation='relu',
input_shape=input_shape,data_format='channels_first'))
model.add(MaxPooling2D(pool_size=(2, 2), data_format='channels_first'))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])
return model
cnn_m = base_model()
cnn_m.summary()
#plot_model(cnn_m, to_file='model.png', show_shapes=True)
from sklearn.model_selection import KFold
kf = KFold(n_splits=5)
KFold(n_splits=5, random_state=5, shuffle=False)
average_accuracy = []
average_loss = []
sum_y_pred = []
sum_Y_test = []
it = 0
for train_index, test_index in kf.split(data):
#print("TRAIN:", train_index, "TEST:", test_index)
X_train, X_test = data[train_index], data[test_index]
y_train, y_test = Label[train_index], Label[test_index]
#if(y_train = 0 or y_train = 3 or y_train = 6):
# X_train.transform
X_train = X_train.reshape(X_train.shape[0], 3, img_rows, img_cols)
X_test = X_test.reshape(X_test.shape[0],3,img_rows,img_cols)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
#X_train /= 4096
#X_test /= 4096
Y_train = keras.utils.to_categorical(y_train, num_classes)
Y_test = keras.utils.to_categorical(y_test, num_classes)
cnn_m.fit(X_train, Y_train,
batch_size=batch_size,
epochs=epochs,
verbose=2,
validation_data=(X_test, Y_test))
score = cnn_m.evaluate(X_test, Y_test, verbose=0)
from sklearn.metrics import confusion_matrix
Y_pred = cnn_m.predict(X_test,verbose=2)
y_pred = np.argmax(Y_pred,axis=1)
sum_y_pred.extend(y_pred)
sum_Y_test.extend(Y_test)
for ix in range(0):
print (ix, confusion_matrix(np.argmax(Y_test,axis=1), y_pred)[ix].sum())
print (confusion_matrix(np.argmax(Y_test,axis=1), y_pred))
print('Test loss:', score[0])
print('Test accuracy:', score[1])
average_accuracy.append(score[0])
average_loss.append(score[1])
#it = it + 1
average_accuracy = sum(average_accuracy) / len(average_accuracy)
average_loss = sum(average_loss) / len(average_loss)
print("Combined Confusion Matrix")
print("Combined Confusion Matrix")
#for ix in range(7):
#print (ix, confusion_matrix(np.argmax(sum_Y_test,axis=1), sum_y_pred)[ix].sum())
#print("y_pred = ", sum_y_pred)
#print("y_test = ", sum_Y_test)
cm = confusion_matrix(np.argmax(sum_Y_test,axis=1), sum_y_pred)
plt.figure()
plot_confusion_matrix(cm, classes=["Ana","G1","G2","Meta","Pro","S","Telo"],
title='Confusion matrix, without normalization')
# Plot normalized confusion matrix
#plt.figure()
#plot_confusion_matrix(cnf_matrix, classes=class_names, normalize=True,
# title='Normalized confusion matrix')
plt.show()
print('Average test loss:', average_accuracy)
print('Average test accuracy:', average_loss)
cnn_m.save("model_small.h5")
end = time.time()
print(end-start)