-
Notifications
You must be signed in to change notification settings - Fork 0
/
conceito2_testing.py
292 lines (227 loc) · 11 KB
/
conceito2_testing.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
# -*- coding: utf-8 -*-
"""
Created on Thu May 26 11:36:35 2022
@author: Érica Gomes and Manuel Fortunato
"""
#Pytorch imports
import torch
import torchvision.transforms as transforms
import matplotlib.pyplot as plt
import torchvision.models as models
import torch.nn as nn
#Other imports
import os
import numpy as np
os.environ['KMP_DUPLICATE_LIB_OK']='True'
# Confusion matrix and roc auc
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay, roc_auc_score
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import roc_curve, auc
#TSNE
from sklearn.manifold import TSNE
import matplotlib
# Import/Load bestModel 1
from conceito2_model import DatasetSimplifiedVersion, myModelPre
from itertools import cycle
if __name__ == "__main__":
bestModelS = myModelPre()
bestModelS.load_state_dict(torch.load("myModelS_C2"))
bestModelM = myModelPre()
bestModelM.load_state_dict(torch.load("myModelM_C2"))
# check if CUDA is available
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
#Passing to GPU cuda
bestModelS.to(device)
bestModelM.to(device)
transform_test = transforms.Compose([transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
transform_finaltest=transforms.Compose([transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
# initialize batch_size
batch_size = 16
# building trainset, trainloader, testset and testloader
csv_file = 'attr.csv' # 'att.csv'
if device.type == 'cpu':
initPath = r'C:\Users\efgom\OneDrive\Documentos\GitHub\breast-cancer-classification'
num_workers = 0
else:
initPath = '/nas-ctm01-homes/efgomes'
num_workers = 0
root_dir_train = initPath + '/data/conceptl/conceito2/masses/train'
root_dir_val = initPath + '/data/conceptl/conceito2/masses/val'
root_dir_test = initPath + '/data/conceptl/conceito2/masses/test'
testset = DatasetSimplifiedVersion(csv_file = csv_file, root_dir = root_dir_test, transform = transform_test)
testloader = torch.utils.data.DataLoader(testset, batch_size = batch_size, shuffle = True, num_workers = num_workers)
# defining the possible shapes and margins (used for confusion matrix)
shape_classes = ('Irregular', 'Oval', 'Lobulated',
'Architectural Distortion', 'Round')
margin_classes = ('Circumscribed', 'Ill Defined',
'Microlobulated', 'Obscured', 'Spiculated')
# testing the trained model for shapes using finaltestset
bestModelS.eval()
correct_shape = 0
total_shape = 0
true_shape = np.array([])
pred_shape = np.array([])
testS_prev_before_out1=[]
testS_truelabels=[]
probS=[]
with torch.no_grad():
for data in testloader:
images = data ['image'].to(device)
shape = data ['shape'].to(device)
x1= bestModelS.model(images)
out1 = bestModelS.fc1(x1)
_, predicted_shape = torch.max(out1.data, 1)
total_shape += shape.size(0)
correct_shape += (predicted_shape == shape).sum().item()
# Building confusion Matrix arrays
true_shape = np.concatenate ((true_shape, shape.cpu().numpy()))
pred_shape = np.concatenate ((pred_shape, predicted_shape.cpu().numpy()))
testS_prev_before_out1.append((x1.cpu().numpy()))
testS_truelabels.append((shape.cpu().numpy()))
prob1=torch.softmax(out1.data,-1)
probS.append(prob1.cpu().numpy())
probS=np.concatenate(probS)
testS_prev_before_out1 = np.concatenate(testS_prev_before_out1)
testS_truelabels = np.concatenate(testS_truelabels)
acc_shape_test = 100 * correct_shape // total_shape
# testing the trained model for margins using finaltestset
bestModelM.eval()
correct_margin = 0
total_margin = 0
true_margin = np.array([])
pred_margin = np.array([])
testM_prev_before_out1=[]
testM_truelabels=[]
probM=[]
with torch.no_grad():
for data in testloader:
images = data ['image'].to(device)
margin = data ['margin'].to(device)
x2= bestModelM.model(images)
out2 = bestModelM.fc2(x2)
_, predicted_margin = torch.max(out2.data, 1)
total_margin += margin.size(0)
correct_margin += (predicted_margin == margin).sum().item()
# Building confusion Matrix arrays
true_margin = np.concatenate ((true_margin, margin.cpu().numpy()))
pred_margin = np.concatenate ((pred_margin, predicted_margin.cpu().numpy()))
testM_prev_before_out1.append((x2.cpu().numpy()))
testM_truelabels.append((margin.cpu().numpy()))
prob2=torch.softmax(out2.data,-1)
probM.append(prob2.cpu().numpy())
probM=np.concatenate(probM)
testM_prev_before_out1 = np.concatenate(testM_prev_before_out1)
testM_truelabels = np.concatenate(testM_truelabels)
acc_margin_test = 100 * correct_margin // total_margin
print('Best Model Accuracy in testset - shape : ',acc_shape_test, ' %')
print('Best Model Accuracy in testset - margin : ', acc_margin_test, ' %')
new_testS_truelabels=np.zeros((testS_truelabels.size,testS_truelabels.max()+1))
new_testS_truelabels[np.arange(testS_truelabels.size),testS_truelabels]=1
auc_valueS = roc_auc_score(new_testS_truelabels, probS,multi_class='ovr')
print('AUC Shape: ', auc_valueS)
new_testM_truelabels=np.zeros((testM_truelabels.size,testM_truelabels.max()+1))
new_testM_truelabels[np.arange(testM_truelabels.size),testM_truelabels]=1
auc_valueM = roc_auc_score(new_testM_truelabels, probM,multi_class='ovr')
print('AUC Margin: ', auc_valueM)
# vermelho, roxo, amarelo, verde, azul
colors = cycle(["#ee4035", "#6d39b7", "#f4e659", "#7bc043", "#0392cf"])
colors_TSNE=["#ee4035", "#6d39b7", "#f4e659", "#7bc043", "#0392cf"]
cmap=matplotlib.colors.ListedColormap(colors_TSNE)
fprS=dict()
tprS=dict()
roc_aucS=dict()
for i in range(5):
fprS[i], tprS[i], _ = roc_curve(new_testS_truelabels[:, i], probS[:, i])
roc_aucS[i] = auc(fprS[i], tprS[i])
lw=2
for i, color in zip(range(5), colors):
plt.plot(
fprS[i],
tprS[i],
color=color,
lw=lw,
label="{0} (area = {1:0.2f})".format(shape_classes[i], roc_aucS[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("ROC-AUC curves for each class (shape)")
plt.legend(loc="lower right")
plt.show()
# Save ROC-AUC margin figure into disk
figure_name = 'C2_ROC-AUC_Shape'
figure_path = os.path.join(initPath + '/concept_learning/plots', f"{figure_name}")
plt.savefig(fname = figure_path, bbox_inches = 'tight')
plt.close()
fprM=dict()
tprM=dict()
roc_aucM=dict()
for i in range(5):
fprM[i], tprM[i], _ = roc_curve(new_testM_truelabels[:, i], probM[:, i])
roc_aucM[i] = auc(fprM[i], tprM[i])
lw=2
for i, color in zip(range(5), colors):
plt.plot(
fprM[i],
tprM[i],
color=color,
lw=lw,
label="{0} (area = {1:0.2f})".format(margin_classes[i], roc_aucM[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("ROC-AUC curves for each class (margin)")
plt.legend(loc="lower right")
plt.show()
# Save ROC-AUC margin figure into disk
figure_name = 'C2_ROC-AUC_Margin'
figure_path = os.path.join(initPath + '/concept_learning/plots', f"{figure_name}")
plt.savefig(fname = figure_path, bbox_inches = 'tight')
plt.close()
# TSNE for shape
testS_TSNE = TSNE().fit_transform(testS_prev_before_out1)
tsne_shape= plt.scatter(*testS_TSNE.T, c=testS_truelabels, cmap=cmap, alpha=0.3)
plt.legend(handles=tsne_shape.legend_elements()[0], labels=shape_classes)
plt.title("TSNE for test set (shape)")
plt.show()
# Save TSNE shape figure into disk
figure_name = 'C2_TSNE_Shape'
figure_path = os.path.join(initPath + '/concept_learning/plots', f"{figure_name}")
plt.savefig(fname = figure_path, bbox_inches = 'tight')
plt.close()
#TSNE for margin
testM_TSNE = TSNE().fit_transform(testM_prev_before_out1)
tsne_margin= plt.scatter(*testM_TSNE.T, c=testM_truelabels, cmap=cmap, alpha=0.3)
plt.legend(handles=tsne_margin.legend_elements()[0], labels=margin_classes)
plt.title("TSNE for test set (margin)")
plt.show()
# Save TSNE margin figure into disk
figure_name = 'C2_TSNE_Margin'
figure_path = os.path.join(initPath + '/concept_learning/plots', f"{figure_name}")
plt.savefig(fname = figure_path, bbox_inches = 'tight')
plt.close()
# determine the confusion matrix for the data in the 'test' folder SHAPE
confMatrix_shape = confusion_matrix(true_shape, pred_shape, normalize = 'true')
disp_shape = ConfusionMatrixDisplay(confusion_matrix = confMatrix_shape, display_labels = shape_classes)
disp_shape=disp_shape.plot(cmap="Blues", xticks_rotation=45)
disp_shape.ax_.set_title('Confusion matrix for test set (shape)')
# Save _Shape__ Confusion matrix figure into disk
figure_name = 'C2_ConfMat_Shape'
figure_path = os.path.join(initPath + '/concept_learning/plots', f"{figure_name}")
plt.savefig(fname = figure_path, bbox_inches = 'tight')
plt.close()
# determine the confusion matrix for the data in the 'test' folder MARGIN
confMatrix_margin = confusion_matrix(true_margin, pred_margin, normalize = 'true')
disp_margin = ConfusionMatrixDisplay(confusion_matrix = confMatrix_margin, display_labels = margin_classes)
disp_margin=disp_margin.plot(cmap="Blues", xticks_rotation=45)
disp_margin.ax_.set_title('Confusion matrix for test set (margin)')
# Save Margin__ Confusion matrix figure into disk
figure_name = 'C2_ConfMat_Margin'
figure_path = os.path.join(initPath + '/concept_learning/plots', f"{figure_name}")
plt.savefig(fname = figure_path, bbox_inches = 'tight')
plt.close()