-
Notifications
You must be signed in to change notification settings - Fork 2
/
Inference_ensembles.py
341 lines (252 loc) · 15.7 KB
/
Inference_ensembles.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
""" Authored by: Neel Kanwal ([email protected])"""
# This file provides inference code for ensembles of binary DCNN or ViT models mentioned in the paper.
# Update paths to processed datasets
if __name__ == "__main__":
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
warnings.simplefilter(action='ignore', category=DeprecationWarning)
warnings.simplefilter(action='ignore', category=RuntimeWarning)
warnings.simplefilter(action='ignore', category=UserWarning)
import matplotlib.pyplot as plt
import matplotlib
font = {'family' : 'serif','weight':'normal','size' : 36}
matplotlib.rc('font', **font)
plt.rcParams["figure.figsize"] = (20, 20)
import pandas as pd
import numpy as np
import seaborn as sns
import os
import torch
ensemble = "vits" # "vits", "cnns"
BATCH_SIZE = 128
evaluate_with_prob = 0.326 # Use this probablity for thresholding, set to None for not using this feature
# cuda_device = 5
# os.environ['CUDA_LAUNCH_BLOCKING'] = '1'
# sns.set_style("white")
# torch.cuda.set_device(cuda_device)
# torch.cuda.empty_cache()
# print("Current CUDA device = ", torch.cuda.current_device())
from torch.utils.data import DataLoader
import time
import pprint
from datetime import datetime
import json
import torch, torchvision
from torchvision import datasets, models
import torchvision.transforms as transforms
from torch import nn
from utils import make_cm, make_pretty_cm, infer_vit_v3, infer_cnn_v4, load_cnn_model, load_vit_model , plot_roc_curve_v4
from sklearn.metrics import confusion_matrix, classification_report, matthews_corrcoef, roc_auc_score, precision_score, accuracy_score, f1_score
from scikitplot.metrics import plot_roc, plot_precision_recall
import timm
from utils import plot_confusion_matrix, assign_class, best_prob, make_binary_label, make_pretty_cm_v3, truth_prob_ensemb
torch.manual_seed(250)
sens_thresh = 0.98 # for plot_roc curve to show probablity that gives this.
path_to_dataset = "path_to/multiclass_artifact_data" # Use processed datasets from zenodo link in the repository
models_location = "path_to/single_pipeline/model_weights/" # Use models from model_weights in repository
sav_dir = "path_to/preprocessing_models/"
# Names of DCNN weights
blood_cnn = "blood_cnn.dat"
blur_cnn = "blur_cnn.dat"
fold_cnn = "fold_cnn.dat"
damaged_cnn = "damage_cnn.dat"
airbubble_cnn = "airbubble_cnn.dat"
# Names of ViT weights
blood_vit = "blood_vit.dat"
blur_vit = "blur_vit.dat"
fold_vit = "fold_vit.dat"
damaged_vit = "damage_vit.dat"
airbubble_vit = "airbubble_vit.dat"
test_compose = val_compose = transforms.Compose([transforms.Resize((224, 224)),
transforms.ToTensor(),transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])])
t = time.time()
val_images = datasets.ImageFolder(root=path_to_dataset + "/test", transform=val_compose)
idx2class = {v: k for k, v in val_images.class_to_idx.items()}
classes_list = list(idx2class.values())
print("ID to classes ", idx2class)
num_classes = len(val_images.classes)
val_loader = DataLoader(val_images, batch_size=BATCH_SIZE, shuffle=False, num_workers=16, pin_memory=True)
test_images = datasets.ImageFolder(root=path_to_dataset + "/validation", transform=test_compose)
test_loader = DataLoader(test_images, batch_size=BATCH_SIZE, shuffle=False, num_workers=16, pin_memory=True)
print(f"Total data loading time in minutes: {(time.time() - t) / 60:.3f}")
# blur
if ensemble =="cnns":
print("\nLoading CNN ensemble of MobileNetv3\n")
blur_model = load_cnn_model(models_location, blur_cnn)
blood_model = load_cnn_model(models_location, blood_cnn)
fold_model = load_cnn_model(models_location, fold_cnn)
damaged_model = load_cnn_model(models_location, damaged_cnn)
airbubble_model = load_cnn_model(models_location, airbubble_cnn)
else:
print("\nLoading CNN ensemble of VITs\n")
blur_model = load_vit_model(models_location, blur_vit)
blood_model = load_vit_model(models_location, blood_vit)
fold_model = load_vit_model(models_location, fold_vit)
damaged_model = load_vit_model(models_location, damaged_vit)
airbubble_model = load_vit_model(models_location, airbubble_vit)
if torch.cuda.is_available():
print("Cuda is available\n")
# model should be on cuda before selection of optimizer
blur_model = blur_model.cuda()
blood_model = blood_model.cuda()
damaged_model = damaged_model.cuda()
fold_model = fold_model.cuda()
airbubble_model = airbubble_model.cuda()
print("--------------Validation Set-------------------------")
if evaluate_with_prob is not None:
print("Using thresholding @ ", evaluate_with_prob)
if ensemble == "cnns":
blur_pred, y_true, blur_prob = infer_cnn_v3(blur_model, val_loader)
blood_pred, y_true1, blood_prob= infer_cnn_v3(blood_model, val_loader)
damaged_pred, y_true2, damaged_prob = infer_cnn_v3(damaged_model, val_loader)
fold_pred, y_true, fold_prob = infer_cnn_v3(fold_model, val_loader)
airbubble_pred, y_true, airbubble_prob = infer_cnn_v3(airbubble_model, val_loader) # use_prob_threshold
else:
blur_pred, y_true, blur_prob = infer_vit_v3(blur_model, val_loader)
blood_pred, y_true1, blood_prob = infer_vit_v3(blood_model, val_loader)
damaged_pred, y_true2, damaged_prob = infer_vit_v3(damaged_model, val_loader)
fold_pred, y_true, fold_prob = infer_vit_v3(fold_model, val_loader)
airbubble_pred, y_true, airbubble_prob = infer_vit_v3(airbubble_model, val_loader)
# np.round(my_list, decimals=3)
blur_prob, blood_prob, damaged_prob, fold_prob, airbubble_prob = np.round(blur_prob, decimals=5), np.round(blood_prob, decimals=5),\
np.round(damaged_prob, decimals=5), np.round(fold_prob, decimals=5), np.round(airbubble_prob, decimals=5)
assert y_true == y_true1, "The GroundTruths are not similar"
assert y_true == y_true2, "The GroundTruths are not similar"
blur_pred_b = np.array(blur_pred).astype(bool)
blood_pred_b = np.array(blood_pred).astype(bool)
damaged_pred_b = np.array(damaged_pred).astype(bool)
fold_pred_b = np.array(fold_pred).astype(bool)
airbubble_pred_b = np.array(airbubble_pred).astype(bool)
artifact_list = [blur_pred_b[i] | blood_pred_b[i] | damaged_pred_b[i] | fold_pred_b[i] | airbubble_pred_b[i]
for i in range(len(blur_pred))]
artifact_list = [a.astype(int) for a in artifact_list]
file_names = [im[0].split("/")[-1] for im in val_loader.dataset.imgs]
data = {"files": file_names, "ground_truth": y_true, "predicted": artifact_list, "blood": blood_pred, "blur": blur_pred,
"bubble": airbubble_pred, "damage": damaged_pred, "fold": fold_pred, "blood_p": blood_prob, "blur_p": blur_prob, "bubble_p": airbubble_prob,
"damage_p": damaged_prob, "fold_p": fold_prob}
dframe = pd.DataFrame(data)
print("Length of dataframe ", len(dframe))
# print(dframe.tail(5))
# dframe["predited_class"] = dframe.apply(assign_class, axis=1)
dframe.insert(2, 'predicted_class', dframe.apply(assign_class, axis=1))
dframe['binary_label'] = dframe.apply(make_binary_label, axis=1)
dframe['max_prob'] = dframe.apply(best_prob, axis=1)
dframe['truth_label'] = dframe['binary_label'].apply(lambda x: 1 if x == 0 else 0)
# dframe['truth_prob'] = 1- dframe['max_prob']
dframe['truth_prob'] = dframe.apply(truth_prob_ensemb, axis=1)
with pd.ExcelWriter(f"{sav_dir}/{ensemble}_predictions_ensemble_validation.xlsx") as wr:
dframe.to_excel(wr, index=False)
labels = ['Artifact_free', 'Blood', 'Blur', 'Bubble', 'Damage', 'Fold']
y_true = y_true
y_pred = dframe['predicted_class'].tolist()
cm = make_cm(y_true, y_pred, classes_list)
# make_pretty_cm(cm, categories=labels, cmap="tab20b", figsize=(14,14), title=f"{ensemble}_ensemble")
# plot_confusion_matrix(cm, classes=labels, title=f"{ensemble}_ensemble")
# make_pretty_cm_v2(cm, categories=labels, title=f"{ensemble}_ensemble_validation")
make_pretty_cm_v3(cm, categories=labels, title=f"{ensemble}_ensemble_validation")
plt.savefig(f"{sav_dir}/{ensemble}_CM_ensemble_validation.png")
f1_mirco = f1_score(y_true, y_pred, average='micro')
print("\nMicro F1 Score: ", np.round(f1_mirco, decimals=4))
f1_macro = f1_score(y_true, y_pred, average='weighted')
print("\nWeighted F1 Score: ", np.round(f1_macro, decimals=4))
micro_acc = accuracy_score(y_true, y_pred)
print("\nMicro Accuracy: ", np.round(micro_acc, decimals=4))
macro_acc = accuracy_score(y_true, y_pred, normalize=True)
print("\nMacro Accuracy: ", np.round(macro_acc, decimals=4))
mcc = matthews_corrcoef(y_true, y_pred)
print("\nMCC: ", np.round(mcc, decimals=4))
class_index = 0 # Index of the artifact_free class
tp = cm[class_index, class_index]
fn = np.sum(cm[class_index, :]) - tp
fp = np.sum(cm[:, class_index]) - tp
tn = np.sum(cm) - np.sum(cm[class_index, :]) - np.sum(cm[:, class_index]) + tp
accuracy = (tp + tn) / (tp + fp + tn + fn)
print("\nAccuracy of artifact-free class: ", np.round(accuracy, decimals=4))
precision = tp / (tp + fp)
recall = tp / (tp + fn)
f1_score_af = 2 * (precision * recall) / (precision + recall)
print("\nF1-Score of artifact-free class: ", np.round(f1_score_af, decimals=4))
sens = tp/ (tp + fn)
print("\nSensitivity of artifact-free class: ", np.round(sens, decimals=4))
spec = tn/ (tn + fp)
print("\nSpecificity of artifact-free class: ", np.round(spec, decimals=4))
plt.clf()
plot_roc_curve_v4(dframe, sensitivity_val = sens_thresh, title=f"{ensemble}_ROC_ensemble_validation")
plt.savefig(f"{sav_dir}/{ensemble}_ROC_ensemble_validation.png")
print("--------------Test Set-------------------------")
if ensemble == "cnns":
blur_pred, y_true, blur_prob = infer_cnn_v4(blur_model, test_loader, use_prob_threshold=evaluate_with_prob)
blood_pred, y_true1, blood_prob= infer_cnn_v4(blood_model, test_loader, use_prob_threshold=evaluate_with_prob)
damaged_pred, y_true2, damaged_prob = infer_cnn_v4(damaged_model, test_loader, use_prob_threshold=evaluate_with_prob)
fold_pred, y_true, fold_prob = infer_cnn_v4(fold_model, test_loader, use_prob_threshold=evaluate_with_prob)
airbubble_pred, y_true, airbubble_prob = infer_cnn_v4(airbubble_model, test_loader, use_prob_threshold=evaluate_with_prob) # use_prob_threshold
else:
blur_pred, y_true, blur_prob = infer_vit_v3(blur_model, test_loader, use_prob_threshold=evaluate_with_prob)
blood_pred, y_true1, blood_prob = infer_vit_v3(blood_model, test_loader, use_prob_threshold=evaluate_with_prob)
damaged_pred, y_true2, damaged_prob = infer_vit_v3(damaged_model, test_loader, use_prob_threshold=evaluate_with_prob)
fold_pred, y_true, fold_prob = infer_vit_v3(fold_model, test_loader, use_prob_threshold=evaluate_with_prob)
airbubble_pred, y_true, airbubble_prob = infer_vit_v3(airbubble_model, test_loader, use_prob_threshold=evaluate_with_prob)
assert y_true == y_true1, "The GroundTruths are not similar"
assert y_true == y_true2, "The GroundTruths are not similar"
blur_prob, blood_prob, damaged_prob, fold_prob, airbubble_prob = np.round(blur_prob, decimals=5), np.round(blood_prob, decimals=5),\
np.round(damaged_prob, decimals=5), np.round(fold_prob, decimals=5), np.round(airbubble_prob, decimals=5)
blur_pred_b = np.array(blur_pred).astype(bool)
blood_pred_b = np.array(blood_pred).astype(bool)
damaged_pred_b = np.array(damaged_pred).astype(bool)
fold_pred_b = np.array(fold_pred).astype(bool)
airbubble_pred_b = np.array(airbubble_pred).astype(bool)
artifact_list = [blur_pred_b[i] | blood_pred_b[i] | damaged_pred_b[i] | fold_pred_b[i] | airbubble_pred_b[i]
for i in range(len(blur_pred))]
artifact_list = [a.astype(int) for a in artifact_list]
file_names = [im[0].split("/")[-1] for im in test_loader.dataset.imgs]
data = {"files": file_names, "ground_truth": y_true, "predicted_artifact": artifact_list, "blur": blur_pred, "blood": blood_pred,
"damage": damaged_pred, "fold": fold_pred, "bubble": airbubble_pred, "blur_p": blur_prob, "blood_p": blood_prob, "damage_p": damaged_prob,
"fold_p": fold_prob, "bubble_p": airbubble_prob}
dframe = pd.DataFrame(data)
print("Length of dataframe ", len(dframe))
dframe.insert(2, 'predicted_class', dframe.apply(assign_class, axis=1))
dframe['binary_label'] = dframe.apply(make_binary_label, axis=1)
dframe['max_prob'] = dframe.apply(best_prob, axis=1)
dframe['truth_label'] = dframe['binary_label'].apply(lambda x: 1 if x == 0 else 0)
# dframe['truth_prob'] = 1- dframe['max_prob']
dframe['truth_prob'] = dframe.apply(truth_prob_ensemb, axis=1)
# dframe['max_prob'] = dframe[['blood_p', 'blur_p', 'bubble_p', 'damage_p', 'fold_p']].apply(max, axis=1)
with pd.ExcelWriter(f"{sav_dir}/{ensemble}_predictions_ensemble_test.xlsx") as wr:
dframe.to_excel(wr, index=False)
labels = ['Artifact_free', 'Blood', 'Blur', 'Bubble', 'Damage', 'Fold']
y_true = y_true
y_pred = dframe['predicted_class'].tolist()
cm = make_cm(y_true, y_pred, classes_list)
# make_pretty_cm_v2(cm, categories=labels, title=f"{ensemble}_ensemble_test")
# make_pretty_cm(cm, categories=labels, cmap="tab20b", figsize=(14,14), title=f"{ensemble}_ensemble")
make_pretty_cm_v3(cm, categories=labels, title=f"{ensemble}_ensemble_test")
plt.savefig(f"{sav_dir}/{ensemble}_CM_ensemble_test.png")
f1_mirco = f1_score(y_true, y_pred, average='micro')
print("\nMicro F1 Score: ", np.round(f1_mirco, decimals=4))
f1_macro = f1_score(y_true, y_pred, average='weighted')
print("\nWeighted F1 Score: ", np.round(f1_macro, decimals=4))
micro_acc = accuracy_score(y_true, y_pred)
print("\nMicro Accuracy: ", np.round(micro_acc, decimals=4))
macro_acc = accuracy_score(y_true, y_pred, normalize=True)
print("\nMacro Accuracy: ", np.round(macro_acc, decimals=4))
mcc = matthews_corrcoef(y_true, y_pred)
print("\nMCC: ", np.round(mcc, decimals=4))
class_index = 0 # Index of the artifact_free class
tp = cm[class_index, class_index]
fn = np.sum(cm[class_index, :]) - tp
fp = np.sum(cm[:, class_index]) - tp
tn = np.sum(cm) - np.sum(cm[class_index, :]) - np.sum(cm[:, class_index]) + tp
accuracy = (tp + tn) / (tp + fp + tn + fn)
print("\nAccuracy of artifact-free class: ", np.round(accuracy, decimals=4))
precision = tp / (tp + fp)
recall = tp / (tp + fn)
f1_score_af = 2 * (precision * recall) / (precision + recall)
print("\nF1-Score of artifact-free class: ", np.round(f1_score_af, decimals=4))
sens = tp/ (tp + fn)
print("\nSensitivity of artifact-free class: ", np.round(sens, decimals=4))
spec = tn/ (tn + fp)
print("\nSpecificity of artifact-free class: ", np.round(spec, decimals=4))
plt.clf()
plot_roc_curve_v4(dframe, sensitivity_val = sens_thresh, title=f"{ensemble}_ROC_ensemble_test")
plt.savefig(f"{sav_dir}/{ensemble}_ROC_ensemble_test.png")
print("\n## Finished ##")