-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_vitmed.py
executable file
·327 lines (250 loc) · 14.2 KB
/
main_vitmed.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
"""
Created on May 4, 2023.
main_vitmed.py
@author: Soroosh Tayebi Arasteh <[email protected]>
https://github.com/tayebiarasteh/
"""
import pdb
import torch
import os
from torch.utils.data import Dataset
from torch.nn import BCEWithLogitsLoss
from torchvision import transforms, models
import timm
import numpy as np
from sklearn import metrics
from config.serde import open_experiment, create_experiment, delete_experiment, write_config
from Train_Valid_vitmed import Training
from Prediction_vitmed import Prediction
from data.data_provider import vindr_data_loader_2D, chexpert_data_loader_2D, mimic_data_loader_2D, cxr14_data_loader_2D, padchest_data_loader_2D
import warnings
warnings.filterwarnings('ignore')
def main_train_central_2D(global_config_path="/PATH/config.yaml", valid=False,
resume=False, augment=False, experiment_name='name', dataset_name='vindr', pretrained=False, vit=False, dinov2=True, image_size=224, batch_size=30, lr=1e-5):
"""Main function for training + validation centrally
Parameters
----------
global_config_path: str
always global_config_path="/PATH/config.yaml"
valid: bool
if we want to do validation
resume: bool
if we are resuming training on a model
augment: bool
if we want to have data augmentation during training
experiment_name: str
name of the experiment, in case of resuming training.
name of new experiment, in case of new training.
"""
if resume == True:
params = open_experiment(experiment_name, global_config_path)
else:
params = create_experiment(experiment_name, global_config_path)
cfg_path = params["cfg_path"]
if dataset_name == 'vindr':
train_dataset = vindr_data_loader_2D(cfg_path=cfg_path, mode='train', augment=augment, image_size=image_size)
valid_dataset = vindr_data_loader_2D(cfg_path=cfg_path, mode='test', augment=False, image_size=image_size)
elif dataset_name == 'chexpert':
train_dataset = chexpert_data_loader_2D(cfg_path=cfg_path, mode='train', augment=augment, image_size=image_size)
valid_dataset = chexpert_data_loader_2D(cfg_path=cfg_path, mode='test', augment=False, image_size=image_size)
elif dataset_name == 'mimic':
train_dataset = mimic_data_loader_2D(cfg_path=cfg_path, mode='train', augment=augment, image_size=image_size)
valid_dataset = mimic_data_loader_2D(cfg_path=cfg_path, mode='test', augment=False, image_size=image_size)
elif dataset_name == 'cxr14':
train_dataset = cxr14_data_loader_2D(cfg_path=cfg_path, mode='train', augment=augment, image_size=image_size)
valid_dataset = cxr14_data_loader_2D(cfg_path=cfg_path, mode='test', augment=False, image_size=image_size)
elif dataset_name == 'padchest':
train_dataset = padchest_data_loader_2D(cfg_path=cfg_path, mode='train', augment=augment, image_size=image_size)
valid_dataset = padchest_data_loader_2D(cfg_path=cfg_path, mode='test', augment=False, image_size=image_size)
train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=batch_size,
pin_memory=True, drop_last=True, shuffle=True, num_workers=10)
weight = train_dataset.pos_weight()
label_names = train_dataset.chosen_labels
if valid:
valid_loader = torch.utils.data.DataLoader(dataset=valid_dataset, batch_size=batch_size,
pin_memory=True, drop_last=False, shuffle=False, num_workers=5)
else:
valid_loader = None
# Changeable network parameters
if vit:
if dinov2:
model = load_pretrained_dinov2(num_classes=len(weight))
else:
model = load_pretrained_timm_model(num_classes=len(weight), pretrained=pretrained, imgsize=image_size)
else:
model = load_pretrained_timm_model(num_classes=len(weight), model_name='resnet50d', pretrained=pretrained)
loss_function = BCEWithLogitsLoss
model_info = params['Network']
model_info['lr'] = lr
model_info['batch_size'] = batch_size
params['Network'] = model_info
write_config(params, cfg_path, sort_keys=True)
if vit:
optimizer = torch.optim.AdamW(model.parameters(), lr=float(lr),
weight_decay=float(params['Network']['weight_decay']))
else:
optimizer = torch.optim.Adam(model.parameters(), lr=float(lr),
weight_decay=float(params['Network']['weight_decay']),
amsgrad=params['Network']['amsgrad'])
trainer = Training(cfg_path, resume=resume, label_names=label_names)
if resume == True:
trainer.load_checkpoint(model=model, optimiser=optimizer, loss_function=loss_function, weight=weight, label_names=label_names)
else:
trainer.setup_model(model=model, optimiser=optimizer, loss_function=loss_function, weight=weight)
trainer.train_epoch(train_loader=train_loader, valid_loader=valid_loader, num_epochs=params['Network']['num_epochs'])
def main_test_central_2D_pvalue_out_of_bootstrap(global_config_path="/PATH/config.yaml",
experiment_name1='central_exp_for_test', experiment_name2='central_exp_for_test',
experiment1_epoch_num=100, experiment2_epoch_num=100, dataset_name='vindr', vit_1=False, vit_2=False, dinov2_1=False, dinov2_2=False, image_size=224):
"""Main function for multi label prediction
Parameters
----------
experiment_name: str
name of the experiment to be loaded.
"""
params1 = open_experiment(experiment_name1, global_config_path)
cfg_path1 = params1['cfg_path']
if dataset_name == 'vindr':
test_dataset = vindr_data_loader_2D(cfg_path=cfg_path1, mode='test', augment=False, image_size=image_size)
elif dataset_name == 'chexpert':
test_dataset = chexpert_data_loader_2D(cfg_path=cfg_path1, mode='test', augment=False, image_size=image_size)
elif dataset_name == 'mimic':
test_dataset = mimic_data_loader_2D(cfg_path=cfg_path1, mode='test', augment=False, image_size=image_size)
elif dataset_name == 'cxr14':
test_dataset = cxr14_data_loader_2D(cfg_path=cfg_path1, mode='test', augment=False, image_size=image_size)
elif dataset_name == 'padchest':
test_dataset = padchest_data_loader_2D(cfg_path=cfg_path1, mode='test', augment=False, image_size=image_size)
weight = test_dataset.pos_weight()
label_names = test_dataset.chosen_labels
# Changeable network parameters for the global network
if vit_1:
if dinov2_1:
model1 = load_pretrained_dinov2(num_classes=len(weight))
else:
model1 = load_pretrained_timm_model(num_classes=len(weight), imgsize=image_size)
else:
model1 = load_pretrained_timm_model(num_classes=len(weight), model_name='resnet50d')
test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=params1['Network']['batch_size'],
pin_memory=True, drop_last=False, shuffle=False, num_workers=16)
index_list = []
for counter in range(1000):
index_list.append(np.random.choice(len(test_dataset), len(test_dataset)))
# Initialize prediction 1
predictor1 = Prediction(cfg_path1, label_names)
predictor1.setup_model(model=model1, epoch_num=experiment1_epoch_num)
pred_array1, target_array1 = predictor1.predict_only(test_loader)
AUC_list1 = predictor1.bootstrapper(pred_array1.cpu().numpy(), target_array1.int().cpu().numpy(), index_list, dataset_name)
# Changeable network parameters
if vit_2:
if dinov2_2:
model2 = load_pretrained_dinov2(num_classes=len(weight))
else:
model2 = load_pretrained_timm_model(num_classes=len(weight), imgsize=image_size)
else:
model2 = load_pretrained_timm_model(num_classes=len(weight), model_name='resnet50d')
# Initialize prediction 2
params2 = open_experiment(experiment_name2, global_config_path)
cfg_path2 = params2['cfg_path']
predictor2 = Prediction(cfg_path2, label_names)
predictor2.setup_model(model=model2, epoch_num=experiment2_epoch_num)
pred_array2, target_array2 = predictor2.predict_only(test_loader)
AUC_list2 = predictor2.bootstrapper(pred_array2.cpu().numpy(), target_array2.int().cpu().numpy(), index_list, dataset_name)
print('individual labels p-values:\n')
for idx, pathology in enumerate(label_names):
counter = AUC_list1[:, idx] > AUC_list2[:, idx]
ratio1 = (len(counter) - counter.sum()) / len(counter)
reject_fdr, ratio1 = fdr_correction(ratio1, alpha=0.05, method='indep')
if ratio1 <= 0.05:
print(f'\t{pathology} p-value: {ratio1}; model 1 significantly higher AUC than model 2')
else:
counter = AUC_list2[:, idx] > AUC_list1[:, idx]
ratio2 = (len(counter) - counter.sum()) / len(counter)
reject_fdr, ratio2 = fdr_correction(ratio2, alpha=0.05, method='indep')
if ratio2 <= 0.05:
print(f'\t{pathology} p-value: {ratio2}; model 2 significantly higher AUC than model 1')
else:
print(f'\t{pathology} p-value: {ratio1}; models NOT significantly different for this label')
print('\nAvg AUC of labels p-values:\n')
avgAUC_list1 = AUC_list1.mean(1)
avgAUC_list2 = AUC_list2.mean(1)
counter = avgAUC_list1 > avgAUC_list2
ratio1 = (len(counter) - counter.sum()) / len(counter)
reject_fdr, ratio1 = fdr_correction(ratio1, alpha=0.05, method='indep')
if ratio1 <= 0.05:
print(f'\tp-value: {ratio1}; model 1 significantly higher AUC than model 2 on average')
else:
counter = avgAUC_list2 > avgAUC_list1
ratio2 = (len(counter) - counter.sum()) / len(counter)
reject_fdr, ratio2 = fdr_correction(ratio2, alpha=0.05, method='indep')
if ratio2 <= 0.05:
print(f'\tp-value: {ratio2}; model 2 significantly higher AUC than model 1 on average')
else:
print(f'\tp-value: {ratio1}; models NOT significantly different on average for all labels')
msg = f'\n\nindividual labels p-values:\n'
with open(os.path.join(params1['target_dir'], params1['stat_log_path']) + '/Test_on_' + str(dataset_name), 'a') as f:
f.write(msg)
with open(os.path.join(params2['target_dir'], params2['stat_log_path']) + '/Test_on_' + str(dataset_name), 'a') as f:
f.write(msg)
for idx, pathology in enumerate(label_names):
counter = AUC_list1[:, idx] > AUC_list2[:, idx]
ratio1 = (len(counter) - counter.sum()) / len(counter)
reject_fdr, ratio1 = fdr_correction(ratio1, alpha=0.05, method='indep')
if ratio1 <= 0.05:
msg = f'\t{pathology} p-value: {ratio1}; model 1 significantly higher AUC than model 2'
else:
counter = AUC_list2[:, idx] > AUC_list1[:, idx]
ratio2 = (len(counter) - counter.sum()) / len(counter)
reject_fdr, ratio2 = fdr_correction(ratio2, alpha=0.05, method='indep')
if ratio2 <= 0.05:
msg = f'\t{pathology} p-value: {ratio2}; model 2 significantly higher AUC than model 1'
else:
msg = f'\t{pathology} p-value: {ratio1}; models NOT significantly different for this label'
with open(os.path.join(params1['target_dir'], params1['stat_log_path']) + '/Test_on_' + str(dataset_name), 'a') as f:
f.write(msg)
with open(os.path.join(params2['target_dir'], params2['stat_log_path']) + '/Test_on_' + str(dataset_name), 'a') as f:
f.write(msg)
msg = f'\n\nAvg AUC of labels p-values:\n'
with open(os.path.join(params1['target_dir'], params1['stat_log_path']) + '/Test_on_' + str(dataset_name), 'a') as f:
f.write(msg)
with open(os.path.join(params2['target_dir'], params2['stat_log_path']) + '/Test_on_' + str(dataset_name), 'a') as f:
f.write(msg)
avgAUC_list1 = AUC_list1.mean(1)
avgAUC_list2 = AUC_list2.mean(1)
counter = avgAUC_list1 > avgAUC_list2
ratio1 = (len(counter) - counter.sum()) / len(counter)
reject_fdr, ratio1 = fdr_correction(ratio1, alpha=0.05, method='indep')
if ratio1 <= 0.05:
msg = f'\tp-value: {ratio1}; model 1 significantly higher AUC than model 2 on average'
else:
counter = avgAUC_list2 > avgAUC_list1
ratio2 = (len(counter) - counter.sum()) / len(counter)
reject_fdr, ratio2 = fdr_correction(ratio2, alpha=0.05, method='indep')
if ratio2 <= 0.05:
msg = f'\tp-value: {ratio2}; model 2 significantly higher AUC than model 1 on average'
else:
msg = f'\tp-value: {ratio1}; models NOT significantly different on average for all labels'
with open(os.path.join(params1['target_dir'], params1['stat_log_path']) + '/Test_on_' + str(dataset_name), 'a') as f:
f.write(msg)
with open(os.path.join(params2['target_dir'], params2['stat_log_path']) + '/Test_on_' + str(dataset_name), 'a') as f:
f.write(msg)
def load_pretrained_timm_model(num_classes=2, model_name='vit_base_patch16_224_in21k', pretrained=False, imgsize=512):
# Load a pre-trained model from config file
pdb.set_trace()
if model_name == 'resnet50d':
model = timm.create_model(model_name, num_classes=num_classes, pretrained=pretrained)
else:
model = timm.create_model(model_name, num_classes=num_classes, img_size=imgsize, pretrained=pretrained)
model.load_state_dict(torch.load('/PATH/mimicpretraining_224.pth'))
for param in model.parameters():
param.requires_grad = True
return model
def load_pretrained_dinov2(num_classes=2):
# Load a pre-trained model from config file
model = torch.hub.load('facebookresearch/dinov2', 'dinov2_vitb14')
model.head = torch.nn.Linear(in_features=768, out_features=num_classes)
for param in model.parameters():
param.requires_grad = True
return model
if __name__ == '__main__':
main_train_central_2D(global_config_path="/PATH/config.yaml",
valid=True, resume=False, augment=True, experiment_name='NAME', dataset_name='cxr14',
pretrained=True, vit=False, dinov2=False, image_size=224, batch_size=128, lr=1e-4)