-
Notifications
You must be signed in to change notification settings - Fork 1
/
evaluate.py
executable file
·214 lines (172 loc) · 9.04 KB
/
evaluate.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
import matplotlib.pyplot as plt
from timeit import default_timer as timer
import numpy as np
import pandas as pd
import torch
import torchvision
from torch import nn
from torchvision import transforms
from torchinfo import summary
import albumentations as A
from albumentations.pytorch import ToTensorV2
from model_parameters import PH2Derm7pt_params, PH2Derm7pt_DLV3_FT_params, PH2Derm7pt_DLV3_params, \
PH2Derm7pt_Manually_params, \
PH2_params, PH2_DLV3_FT_params, PH2_DLV3_params, PH2_Manually_params, Derm7pt_params, \
Derm7pt_DLV3_FT_params, Derm7pt_DLV3_params, Derm7pt_Manually_params, production_raw_params, \
production_DLV3_params, production_manually_params
import model_params
import model_params as params
from modules import data_setup, engine, model_builder
from modules.utils import set_seeds, create_writer, print_train_time, write_to_txt, plot_roc_curve
# To run with updated APIs, we need torch 1.12+ and torchvision 0.13+
assert int(torch.__version__.split(".")[1]) >= 12, "torch version should be 1.12+"
assert int(torchvision.__version__.split(".")[1]) >= 13, "torchvision version should be 0.13+"
print(f"torch version: {torch.__version__}")
print(f"torchvision version: {torchvision.__version__}")
# Setup device-agnostic code
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"[INFO] Using {device} device")
##################################################
# Define transform
##################################################
transform = A.Compose([
A.PadIfNeeded(512, 512),
A.CenterCrop(width=512, height=512),
A.Resize(width=224, height=224),
A.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)),
ToTensorV2(),
])
#################################################
# Create training and val dataloaders
#################################################
# PH2Derm7pt (Original)
test_dataloader_ph2derm7pt, class_names = data_setup.create_dataloader_for_evaluation(params=PH2Derm7pt_params,
transform=transform)
# PH2Derm7pt (DeepLabV3 FT on HAM10000)
test_dataloader_ph2derm7pt_dlv3_ft, class_names = data_setup.create_dataloader_for_evaluation(
params=PH2Derm7pt_DLV3_FT_params,
transform=transform)
# PH2Derm7pt (Manually)
test_dataloader_ph2derm7pt_manually, class_names = data_setup.create_dataloader_for_evaluation(
params=PH2Derm7pt_Manually_params,
transform=transform)
# PH2 (Original)
test_dataloader_ph2, _ = data_setup.create_dataloader_for_evaluation(params=PH2_params,
transform=transform)
# PH2 (DeepLabV3 FT on HAM1000)
test_dataloader_ph2_dlv3_ft, _ = data_setup.create_dataloader_for_evaluation(params=PH2_DLV3_FT_params,
transform=transform)
# PH2 (Manually)
test_dataloader_ph2_manually, _ = data_setup.create_dataloader_for_evaluation(params=PH2_Manually_params,
transform=transform)
# Derm7pt (Original)
test_dataloader_derm7pt, _ = data_setup.create_dataloader_for_evaluation(params=Derm7pt_params,
transform=transform)
# Derm7pt (Manually)
test_dataloader_derm7pt_manually, _ = data_setup.create_dataloader_for_evaluation(params=Derm7pt_Manually_params,
transform=transform)
# Derm7pt (DeepLabV3 FT on HAM10000)
test_dataloader_derm7pt_dlv3_ft, _ = data_setup.create_dataloader_for_evaluation(params=Derm7pt_DLV3_FT_params,
transform=transform)
# Production (Original)
test_dataloader_production_raw, _ = data_setup.create_dataloader_for_evaluation(params=production_raw_params,
transform=transform)
# Production (Manually)
test_dataloader_production_manually, _ = data_setup.create_dataloader_for_evaluation(params=production_manually_params,
transform=transform)
# Production (DeepLabV3 FT on HAM10000)
test_dataloader_production_dlv3_ft, _ = data_setup.create_dataloader_for_evaluation(params=production_DLV3_params,
transform=transform)
###############################################
# Set up evaluation experiments
###############################################
models = ["densenet201"] # "resnet101", "seresnext"
# Create dataloaders dictionary for the various dataloaders
dataloaders = {#"ph2": [test_dataloader_ph2, PH2_params],
#"ph2derm7pt": [test_dataloader_ph2derm7pt, PH2Derm7pt_params],
#"derm7pt": [test_dataloader_derm7pt, Derm7pt_params],
"ph2_dlv3_ft": [test_dataloader_ph2_dlv3_ft, PH2_DLV3_FT_params],
#"ph2derm7pt_dlv3_ft": [test_dataloader_ph2derm7pt_dlv3_ft, PH2Derm7pt_DLV3_FT_params],
#"derm7pt_dlv3_ft": [test_dataloader_derm7pt_dlv3_ft, Derm7pt_DLV3_FT_params],
#"ph2_manually": [test_dataloader_ph2_manually, PH2_Manually_params],
#"ph2derm7pt_manually": [test_dataloader_ph2derm7pt_manually, PH2Derm7pt_Manually_params],
#"derm7pt_manually": [test_dataloader_derm7pt_manually, Derm7pt_Manually_params]
}
# OUR METHOD
# gammas = {"ph2": [0.6, 0.6, 0.6],
# "ph2_dlv3_ft": [0.6, 0.6, 0.6],
# "ph2_manually": [0.6, 0.6, 0.6],
# "derm7pt": [0.3, 0.7, 0.3],
# "derm7pt_dlv3_ft": [0.6, 0.5, 0.6],
# "derm7pt_manually": [0.6, 0.5, 0.5],
# "ph2derm7pt": [0.4, 0.9, 0.6],
# "ph2derm7pt_dlv3_ft": [0.4, 0.7, 0.6],
# "ph2derm7pt_manually": [0.4, 0.7, 0.6]}
# BASELINE
gammas = {"ph2": [None, None, None],
"ph2_dlv3_ft": [None, None, None],
"ph2_manually": [None, None, None],
"derm7pt": [None, None, None],
"derm7pt_dlv3_ft": [None, None, None],
"derm7pt_manually": [None, None, None],
"ph2derm7pt": [None, None, None],
"ph2derm7pt_dlv3_ft": [None, None, None],
"ph2derm7pt_manually": [None, None, None]}
comment = "relu_tanh_"
# 1. Set the random seeds
set_seeds(seed=42)
# 2. Load pretrained model
# Keep track of experiment numbers
experiment_number = 0
time_start = timer()
# 2. Loop through each DataLoader
for dataloader_name, (test_dataloader, dataset_params) in dataloaders.items():
# 3. Loop through each model name and create a new model based on the name
for i, model_name in enumerate(models):
gamma = gammas[dataloader_name][1] # 0 - ResNet-101 / 1 - DenseNet-201 / 2 - SEResNeXt
experiment_number += 1
experiment_name = dataloader_name
print(f"[INFO] Experiment number: {experiment_number}")
print(f"[INFO] Model: {model_name}")
print(f"[INFO] DataLoader: {dataloader_name}")
print(f"[INFO] Dataset: {dataset_params.DATASET}")
print(f"[INFO] Gamma: {gamma}")
# 4. Load pretrained model
model = model_builder.load_ccbm_model(model=model_name,
model_name=model_name,
params=params,
dataset_params=dataset_params,
gamma=gamma,
comments=comment,
device=device)
# 5. Create the loss function
loss_fn = nn.CrossEntropyLoss()
# 6. Evaluate on the test dataloader
test_loss, test_acc, class_report, conf_matrix, gt_concepts, predicted_concepts, auc, bacc, SE, SP = engine.evaluate(
model=model,
dataloader=test_dataloader,
loss_fn=loss_fn,
device=device,
params=dataset_params,
model_name=model_name,
baseline=True if gamma is None else False,
plot_results=False)
# 7. Write results to a TXT file
if not model_params.CALCULATE_FILTER_DISTRIBUTION:
write_to_txt(save_dir=f"results/{model_name}",
params=dataset_params,
accuracy=test_acc,
class_report=class_report,
conf_matrix=conf_matrix,
auc=auc,
bacc=bacc,
sensitivity=SE,
specificity=SP,
gt_concepts=np.asarray(gt_concepts),
predicted_concepts=np.asarray(predicted_concepts),
gamma=gamma,
comments=comment,
model_name=model_name)
print("-" * 100 + "\n")
time_stop = timer()
print(f"[INFO] Task completed in {(time_stop - time_start):.4f} seconds.")