-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathNN_Project_plot_results.py
470 lines (313 loc) · 13.4 KB
/
NN_Project_plot_results.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
#!/usr/bin/env python
# coding: utf-8
# In[1]:
get_ipython().system('git clone https://github.com/Arminkhayati/CovidCT_CNN-')
# In[2]:
from google.colab import drive
drive.mount('/content/drive')
# # Initializing
# In[3]:
from tensorflow.keras.layers import Activation, Dense, Input
from tensorflow.keras.layers import Conv2D, Flatten
from tensorflow.keras.layers import Reshape, Conv2DTranspose
from tensorflow.keras.layers import LeakyReLU
from tensorflow.keras.layers import BatchNormalization
from tensorflow.keras.layers import concatenate
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import RMSprop
from tqdm.notebook import trange, tqdm
import numpy as np
import pandas as pd
import cv2
import time
import matplotlib.pyplot as plt
from pylab import rcParams
rcParams['figure.figsize'] = 10, 10
import cv2
import os
import math
import seaborn as sns
from itertools import cycle
import matplotlib.pyplot as plt
from sklearn.metrics import accuracy_score, classification_report
from sklearn.metrics import confusion_matrix
from sklearn.metrics import roc_curve, roc_auc_score
from sklearn.metrics import precision_recall_curve
from sklearn.metrics import average_precision_score, auc
from sklearn.model_selection import train_test_split
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.layers import Input, Dense, Dropout, Flatten, Conv2D, MaxPool2D, BatchNormalization, Activation, Concatenate, GlobalMaxPooling2D, GlobalAveragePooling2D, Softmax, Embedding
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.models import Model, load_model
from tensorflow.keras.callbacks import ReduceLROnPlateau, ModelCheckpoint, EarlyStopping, TensorBoard, LambdaCallback
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.applications.xception import Xception
from tensorflow.keras.applications import EfficientNetB7, ResNet50
import tensorflow as tf
# In[13]:
def plot_results(one_hot_labels, labels, predicted_class_indices, pred):
# Confusio Matrix
sns.heatmap(confusion_matrix(labels, predicted_class_indices),
annot=True, fmt="d", cbar = False, cmap = plt.cm.Blues)
# Roc curve and Average precision recall
sick_vec = labels>0
sick_score = np.sum(pred[:,1:],1)
fpr, tpr, _ = roc_curve(sick_vec, sick_score)
fig, ax1 = plt.subplots(1,1, figsize = (6, 6), dpi = 150)
ax1.plot(fpr, tpr, 'b.-', label = 'Model Prediction (AUC: %2.2f)' % roc_auc_score(sick_vec, sick_score))
ax1.plot(fpr, fpr, 'g-', label = 'Random Guessing')
ax1.legend()
ax1.set_xlabel('False Positive Rate')
ax1.set_ylabel('True Positive Rate')
n_classes=2
# For each class
precision = dict()
recall = dict()
average_precision = dict()
level_cat = one_hot_labels
# level_cat = np.array([l.flatten() for l in level_cat])
for i in range(n_classes):
precision[i], recall[i], _ = precision_recall_curve(level_cat[:, i],
pred[:, i])
average_precision[i] = average_precision_score(level_cat[:, i], pred[:, i])
precision["micro"], recall["micro"], _ = precision_recall_curve(level_cat.ravel(),
pred.ravel())
average_precision["micro"] = average_precision_score(level_cat, pred,
average="micro")
print('Average precision score, micro-averaged over all classes: {0:0.2f}\n\n\n\n\n'
.format(average_precision["micro"]))
plt.figure()
plt.step(recall['micro'], precision['micro'], where='post')
plt.xlabel('Recall')
plt.ylabel('Precision')
plt.ylim([0.0, 1.05])
plt.xlim([0.0, 1.0])
plt.title(
'Average precision score, micro-averaged over all classes: AP={0:0.2f}'
.format(average_precision["micro"]))
# Multi class precision recall
colors = cycle(['navy', 'turquoise', 'darkorange', 'cornflowerblue', 'teal'])
plt.figure(figsize=(7, 8))
f_scores = np.linspace(0.2, 0.8, num=4)
lines = []
labels = []
for f_score in f_scores:
x = np.linspace(0.01, 1)
y = f_score * x / (2 * x - f_score)
l, = plt.plot(x[y >= 0], y[y >= 0], color='gray', alpha=0.2)
plt.annotate('f1={0:0.1f}'.format(f_score), xy=(0.9, y[45] + 0.02))
lines.append(l)
labels.append('iso-f1 curves')
l, = plt.plot(recall["micro"], precision["micro"], color='gold', lw=2)
lines.append(l)
labels.append('micro-average Precision-recall (area = {0:0.2f})'
''.format(average_precision["micro"]))
for i, color in zip(range(n_classes), colors):
l, = plt.plot(recall[i], precision[i], color=color, lw=2)
lines.append(l)
labels.append('Precision-recall for class {0} (area = {1:0.2f})'
''.format(i, average_precision[i]))
fig = plt.gcf()
fig.subplots_adjust(bottom=0.1)
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('Recall')
plt.ylabel('Precision')
plt.title('Extension of Precision-Recall curve to multi-class')
plt.legend(lines, labels, loc=(0, -.38), prop=dict(size=14))
fpr = dict()
tpr = dict()
roc_auc = dict()
for i in range(n_classes):
fpr[i], tpr[i], _ = roc_curve(level_cat[:, i], pred[:, i])
roc_auc[i] = auc(fpr[i], tpr[i])
# Compute micro-average ROC curve and ROC area
fpr["micro"], tpr["micro"], _ = roc_curve(level_cat.ravel(), pred.ravel())
roc_auc["micro"] = auc(fpr["micro"], tpr["micro"])
all_fpr = np.unique(np.concatenate([fpr[i] for i in range(n_classes)]))
lw = 2
# Then interpolate all ROC curves at this points
mean_tpr = np.zeros_like(all_fpr)
for i in range(n_classes):
mean_tpr += np.interp(all_fpr, fpr[i], tpr[i])
# Finally average it and compute AUC
mean_tpr /= n_classes
fpr["macro"] = all_fpr
tpr["macro"] = mean_tpr
roc_auc["macro"] = auc(fpr["macro"], tpr["macro"])
# Multi class ROC curves
plt.figure()
plt.plot(fpr["micro"], tpr["micro"],
label='micro-average ROC curve (area = {0:0.2f})'
''.format(roc_auc["micro"]),
color='deeppink', linestyle=':', linewidth=4)
plt.plot(fpr["macro"], tpr["macro"],
label='macro-average ROC curve (area = {0:0.2f})'
''.format(roc_auc["macro"]),
color='navy', linestyle=':', linewidth=4)
colors = cycle(['navy', 'turquoise', 'darkorange', 'cornflowerblue', 'teal'])
for i, color in zip(range(n_classes), colors):
plt.plot(fpr[i], tpr[i], color=color, lw=lw,
label='ROC curve of class {0} (area = {1:0.2f})'
''.format(i, roc_auc[i]))
plt.plot([0, 1], [0, 1], 'k--', lw=lw)
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Some extension of Receiver operating characteristic to multi-class')
plt.legend(loc="lower right")
plt.show()
# In[9]:
# params
latent_size = 512
image_size = 256
num_labels = 2
channels = 3
batch_size = 16
train_steps = 3000
save_interval = 500
lr = 0.0002
beta = 0.5
decay = 6e-8
loss = ['binary_crossentropy', 'sparse_categorical_crossentropy']
epochs = 2000
# In[7]:
get_ipython().system('ls "/content/drive/My Drive/nn-prject"')
generator = load_model("/content/drive/My Drive/nn-prject/ACGAN1.h5")
classifier1 = load_model("/content/drive/My Drive/nn-prject/new_good_resnet50.h5")
classifier2 = load_model("/content/drive/My Drive/nn-prject/new_77-80_resnet50.h5")
# # Classifier Metrics on Original Dataset
# In[5]:
# https://keras.io/api/preprocessing/image/#flowfromdirectory-method
train_datagen = ImageDataGenerator(rescale=1./255)
test_datagen = ImageDataGenerator(rescale=1./255)
train_data = train_datagen.flow_from_directory(
'/content/CovidCT_CNN-/data/train',
target_size=(256, 256),
color_mode="rgb",
class_mode="categorical",
shuffle=True,
batch_size=batch_size
)
test_data = train_datagen.flow_from_directory(
'/content/CovidCT_CNN-/data/test',
target_size=(256, 256),
color_mode="rgb",
class_mode="categorical",
shuffle=False,
batch_size=batch_size
)
t_x, t_y = train_data.__getitem__(0)
fig, m_axs = plt.subplots(2, 4, figsize = (16, 8))
for (c_x, c_y, c_ax) in zip(t_x, t_y, m_axs.flatten()):
c_ax.imshow(np.clip(c_x * 255, 0, 255).astype('int'))
c_ax.set_title('Severity {}'.format(c_y))
c_ax.axis('off')
t_x.shape[1:]
# In[14]:
pred= classifier1.predict(test_data, verbose=1)
predicted_class_indices=np.argmax(pred,axis=1)
labels = test_data.classes[0:len(predicted_class_indices)]
print('Accuracy on Test Data: %2.2f%%' % (accuracy_score(labels, predicted_class_indices)))
print(classification_report(labels, predicted_class_indices))
# In[15]:
one_hot_labels = to_categorical(test_data.classes).astype('int')[:len(labels)]
plot_results(one_hot_labels, labels, predicted_class_indices, pred)
# In[16]:
pred= classifier2.predict(test_data, verbose=1)
predicted_class_indices=np.argmax(pred,axis=1)
labels = test_data.classes[0:len(predicted_class_indices)]
print('Accuracy on Test Data: %2.2f%%' % (accuracy_score(labels, predicted_class_indices)))
print(classification_report(labels, predicted_class_indices))
# In[17]:
one_hot_labels = to_categorical(test_data.classes).astype('int')[:len(labels)]
plot_results(one_hot_labels, labels, predicted_class_indices, pred)
# In[ ]:
# # Test Classifiers on Generated images
# In[10]:
noise_input = np.random.uniform(-1.0, 1.0, size=[16, latent_size])
noise_label = np.eye(num_labels)[np.arange(0, 16) % num_labels]
noise_inputs = [noise_input, noise_label]
t_x, t_y = generator.predict(noise_inputs, verbose=1), noise_label
fig, m_axs = plt.subplots(2, 4, figsize = (16, 8))
for (c_x, c_y, c_ax) in zip(t_x, t_y, m_axs.flatten()):
c_ax.imshow(np.clip(c_x * 255, 0, 255).astype('int'))
c_ax.set_title('Severity {}'.format(c_y))
c_ax.axis('off')
t_x.shape[1:]
# In[18]:
num_data = 750
rs = np.random.RandomState(9)
noise_input1 = rs.uniform(-1.0, 1.0, size=[num_data, latent_size])
noise_label1 = np.eye(num_labels)[np.arange(0, num_data) % num_labels]
noise_inputs = [noise_input1, noise_label1]
images = generator.predict(noise_inputs, verbose=1)
labels = np.argmax(noise_label1, axis = 1)
pred = classifier1.predict(images, verbose=1)
predicted_class_indices = np.argmax(pred,axis=1)
print('Accuracy on Test Data: %2.2f%%' % (accuracy_score(labels, predicted_class_indices)))
print(classification_report(labels, predicted_class_indices))
# In[19]:
one_hot_labels = noise_label1
plot_results(one_hot_labels, labels, predicted_class_indices, pred)
# In[20]:
num_data = 750
rs = np.random.RandomState(9)
noise_input1 = rs.uniform(-1.0, 1.0, size=[num_data, latent_size])
noise_label1 = np.eye(num_labels)[np.arange(0, num_data) % num_labels]
noise_inputs = [noise_input1, noise_label1]
images = generator.predict(noise_inputs, verbose=1)
labels = np.argmax(noise_label1, axis = 1)
pred = classifier2.predict(images, verbose=1)
predicted_class_indices = np.argmax(pred,axis=1)
print('Accuracy on Test Data: %2.2f%%' % (accuracy_score(labels, predicted_class_indices)))
print(classification_report(labels, predicted_class_indices))
# In[21]:
one_hot_labels = noise_label1
plot_results(one_hot_labels, labels, predicted_class_indices, pred)
# In[ ]:
# # Test Classifiers on Real and Fake images
# In[22]:
num_data = 750
rs = np.random.RandomState(9)
noise_input1 = rs.uniform(-1.0, 1.0, size=[num_data, latent_size])
noise_label1 = np.eye(num_labels)[np.arange(0, num_data) % num_labels]
noise_inputs = [noise_input1, noise_label1]
images = generator.predict(noise_inputs, verbose=1)
labels = np.argmax(noise_label1, axis = 1)
pred = classifier1.predict(images, verbose=1)
predicted_class_indices = np.argmax(pred,axis=1)
pred1 = classifier1.predict(test_data, verbose=1)
predicted_class_indices1 = np.argmax(pred1,axis=1)
labels1 = test_data.classes[0:len(predicted_class_indices1)]
all_pred = np.concatenate((pred, pred1), axis=0)
all_predicted_class_indices = np.concatenate((predicted_class_indices, predicted_class_indices1), axis=0)
all_labels = np.concatenate((labels, labels1), axis=0)
print('Accuracy on Test Data: %2.2f%%' % (accuracy_score(all_labels, all_predicted_class_indices)))
print(classification_report(all_labels, all_predicted_class_indices))
# In[31]:
one_hot_labels = to_categorical(all_labels)
plot_results(one_hot_labels, all_labels, all_predicted_class_indices, all_pred)
# In[32]:
num_data = 750
rs = np.random.RandomState(9)
noise_input1 = rs.uniform(-1.0, 1.0, size=[num_data, latent_size])
noise_label1 = np.eye(num_labels)[np.arange(0, num_data) % num_labels]
noise_inputs = [noise_input1, noise_label1]
images = generator.predict(noise_inputs, verbose=1)
labels = np.argmax(noise_label1, axis = 1)
pred = classifier2.predict(images, verbose=1)
predicted_class_indices = np.argmax(pred,axis=1)
pred1 = classifier1.predict(test_data, verbose=1)
predicted_class_indices2 = np.argmax(pred1,axis=1)
labels1 = test_data.classes[0:len(predicted_class_indices1)]
all_pred = np.concatenate((pred, pred1), axis=0)
all_predicted_class_indices = np.concatenate((predicted_class_indices, predicted_class_indices1), axis=0)
all_labels = np.concatenate((labels, labels1), axis=0)
print('Accuracy on Test Data: %2.2f%%' % (accuracy_score(all_labels, all_predicted_class_indices)))
print(classification_report(all_labels, all_predicted_class_indices))
# In[33]:
one_hot_labels = to_categorical(all_labels)
plot_results(one_hot_labels, all_labels, all_predicted_class_indices, all_pred)
# In[ ]: