-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrainClassification.py
More file actions
363 lines (281 loc) · 13.3 KB
/
trainClassification.py
File metadata and controls
363 lines (281 loc) · 13.3 KB
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
import argParser as ARG
ARG.initArgs() #init arguments before loading other modules
import torch
import torch.nn as nn
from torch.amp import GradScaler, autocast
from torch.utils.data import Dataset, DataLoader
from ChexpertDataset import ClassificationChexpertDataset
from NetworkModel import getModelClass
from tqdm import tqdm
import torchmetrics.functional as tmf
import wandb
import multiprocessing as mp
def initWandB():
wandb.login()
runVar = wandb.init(
project="MEDICAL_PATCH_NET",
config={
"learning_rate": ARG.LEARNING_RATE,
"epoch_num":ARG.EPOCH_NUM,
"batch_size":ARG.BATCH_SIZE,
"patch_size":ARG.PATCH_SIZE,
"wandb_name":ARG.WANDB_NAME
},
name=ARG.WANDB_NAME
)
def log(name,val,printLog=True,commit=False):
if isinstance(val,torch.Tensor): val = val.item()
if printLog: print("LOG",name,val)
if ARG.USE_WANDB: wandb.log({name:val},commit=commit)
NUM_PROC = 24 #number of processes used to load data
DEFAULT_MODEL_PATH = "savedModels/"
TEST = "test"
class Trainer:
def __init__(
self,
model: torch.nn.Module,
trainLoader: DataLoader,
validLoaderList: DataLoader,
optimizer: torch.optim.Optimizer,
scheduler: torch.optim.lr_scheduler.LRScheduler,
gpuId: int,
saveEvery: int = 1,
) -> None:
self.gpuId = gpuId
self.model = model.to(gpuId)
self.trainLoader = trainLoader
self.validLoaderList = validLoaderList
self.optimizer = optimizer
self.saveEvery = saveEvery
self.scheduler = scheduler
self.criterion = nn.BCEWithLogitsLoss()
self.ampScaler = GradScaler()
def _runBatch(self, img, label):
self.optimizer.zero_grad()
with autocast(device_type='cuda'):
output = self.model(img)
loss = self.criterion(output,label)
self.ampScaler.scale(loss).backward()
self.ampScaler.step(self.optimizer)
self.ampScaler.update()
self.scheduler.step()
log("learningRate",self.scheduler.get_last_lr()[0],printLog=False)
log("loss",loss,commit=True,printLog=False)
def _runEpoch(self, epoch):
self.model.train()
stepNum = len(self.trainLoader)
print(f"Epoch: {epoch} | Batchsize: {ARG.BATCH_SIZE} | Steps: {stepNum}")
for i, (img,label) in enumerate(tqdm(self.trainLoader)):
img = img.to(self.gpuId,non_blocking=True,dtype=torch.float)
label = label.to(self.gpuId,non_blocking=True)
self._runBatch(img,label)
for el in self.validLoaderList:
if el[2] == TEST and (epoch != ARG.EPOCH_NUM - 1): continue #if a test set is given, only apply it to the last
self.validate(*el)
def _saveCheckpoint(self, epoch):
saveName = DEFAULT_MODEL_PATH + ARG.MODEL_SAVE_PATH
torch.save(self.model, saveName) # save entire model
torch.save(self.model.state_dict(), saveName.replace(".pt","_weights.pt")) # save weights only
print(f"Epoch {epoch} | Checkpoint saved at {saveName}")
def train(self, maxEpochs: int):
for epoch in range(maxEpochs):
log("epoch",epoch)
self._runEpoch(epoch)
if epoch % self.saveEvery == 0:
self._saveCheckpoint(epoch)
if ARG.USE_WANDB: wandb.log({},commit=True)
def calculateThresholds(self, thresholdLoader):
#this function optimizes the threshold based on the validation data, NOT on the test data, to prevent data leakage
self.model.eval()
outputList = list()
labelList = list()
for img,label in tqdm(thresholdLoader,desc="optThreshold"):
img = img.to(self.gpuId, non_blocking=True, dtype=torch.float)
with torch.no_grad():
output = self.model(img).detach().cpu()
outputList.append(nn.functional.sigmoid(output))
labelList.append(label.cpu())
allOutput = torch.cat(outputList).to(dtype=torch.float32)
allLabel = torch.cat(labelList).to(dtype=torch.int32)
labelNameList = thresholdLoader.dataset.getLabelNames()
thrsDict = dict()
for i,labelName in enumerate(labelNameList):
preds = allOutput[:, i]
targets = allLabel[:, i]
fpr, tpr, thresh = tmf.roc(preds, targets, task="binary")
optIdx = torch.argmax(tpr - fpr)
thrsDict[labelName] = thresh[optIdx].item()
print(thrsDict)
self.model.train()
return thrsDict
def validate(self,dataLoader,datasetName,validType,thresholdValidLoader=None):
self.model.eval()
outputList = list()
labelList = list()
for img,label in tqdm(dataLoader):
img = img.to(self.gpuId,non_blocking=True,dtype=torch.float)
output = self.model(img).detach().to("cpu",non_blocking=True)
label = label.to("cpu",non_blocking=True,dtype=torch.int)
# studentVect = self.studentModel(studentImg).detach().to("cpu",non_blocking=True)
outputList.append(output)
labelList.append(label)
allOutput = torch.cat(outputList)
allOutput = nn.functional.sigmoid(allOutput) # apply sigmoid to logits
allLabel = torch.cat(labelList)
labelNameList = dataLoader.dataset.getLabelNames()
avgDict = dict()
#calculate threshold based on validation data, not based on training data
if thresholdValidLoader is not None: thrsDict = self.calculateThresholds(thresholdValidLoader)
print("LEN:",len(labelNameList),labelNameList,allOutput.size(),allLabel.size())
for i,labelName in enumerate(labelNameList):
preds = allOutput[:,i]
targets = allLabel[:,i]
def execMetric(func,name=None):
metricName = func.__name__ if name is None else name
metr = func(preds,targets,task="binary")
log(datasetName+"/"+labelName + "/" + metricName,metr)
if metricName not in avgDict: avgDict[metricName] = list()
avgDict[metricName].append(metr)
def execThrsMetric(func,threshold,name=None,execWithoutThreshold=True):
if execWithoutThreshold: execMetric(func,name)
metricName = func.__name__ if name is None else name
metr = func(preds,targets,task="binary",threshold=threshold)
log(datasetName+"/"+labelName + "/thrs_" + metricName,metr)
def execBootstrapMetric(func,name=None,threshold=None,bootstrapNum = 100000):
argList = [(preds,targets,func,threshold) for _ in range(bootstrapNum)]
with mp.Pool(ARG.PROC_NUM) as pool:
metrList = pool.map(execOneBootStrap,argList)
metrList.sort()
lowConfIdx = round(len(metrList)*0.025)
highConfIdx = round(len(metrList)*0.975)
medianIdx = round(len(metrList)*0.5)
meanVal = sum(metrList)/len(metrList)
metricName = func.__name__ if name is None else name
thrsTag = "" if threshold is None else "thrs_"
log(datasetName+"/"+labelName + f"/{thrsTag}bootstrap_low_" + metricName,metrList[lowConfIdx])
log(datasetName+"/"+labelName + f"/{thrsTag}bootstrap_high_" + metricName,metrList[highConfIdx])
log(datasetName+"/"+labelName + f"/{thrsTag}bootstrap_median_" + metricName,metrList[medianIdx])
log(datasetName+"/"+labelName + f"/{thrsTag}bootstrap_mean_" + metricName,meanVal)
execMetric(tmf.auroc)
if thresholdValidLoader is not None:
optThrs = thrsDict[labelName]
log(datasetName+"/"+labelName+"/opt_thrs",optThrs)
execThrsMetric(tmf.accuracy,threshold=optThrs)
execThrsMetric(tmf.precision,threshold=optThrs)
execThrsMetric(tmf.recall,threshold=optThrs,name="sensitivity")
execThrsMetric(tmf.specificity,threshold=optThrs)
execThrsMetric(tmf.f1_score,threshold=optThrs)
if validType == TEST:
execBootstrapMetric(tmf.auroc)
execBootstrapBoth = lambda func,name=None: (execBootstrapMetric(func,name=name),execBootstrapMetric(func,name=name,threshold=optThrs))
execBootstrapBoth(tmf.accuracy)
execBootstrapBoth(tmf.precision)
execBootstrapBoth(tmf.recall,name="sensitivity")
execBootstrapBoth(tmf.specificity)
execBootstrapBoth(tmf.f1_score)
for key in avgDict.keys():
valList = avgDict[key]
avgVal = sum(valList)/len(valList)
log(datasetName+"/avg/"+key,avgVal)
self.model.train()
def execOneBootStrap(inp):
preds,targets,func,threshold = inp
assert len(preds) == len(targets)
valCount = len(preds)
sampled_indices = torch.multinomial(torch.ones(valCount), valCount, replacement=True)
sampledPreds = preds[sampled_indices]
sampledTargets = targets[sampled_indices]
if threshold is None:
metr = func(sampledPreds,sampledTargets,task="binary").item()
else:
metr = func(sampledPreds,sampledTargets,task="binary",threshold=threshold).item()
return metr
def prepareDataloader(dataset: Dataset, batchSize: int, training: bool):
return DataLoader(
dataset,
batch_size=batchSize,
num_workers=NUM_PROC,
pin_memory=True,
shuffle=training,
drop_last=training,
)
def loadModelFromCheckpoint(weightPath):
ModelClass = getModelClass(ARG.MODEL_CLASS)
model = ModelClass(patchSize=ARG.PATCH_SIZE,outFeatures=14)
loadedWeights = torch.load(weightPath,weights_only=False)
loadedWeights = {k.replace("_orig_mod.",""):v for k,v in loadedWeights.items()}
model.load_state_dict(loadedWeights)
model = torch.compile(model)
return model
def loadTrainObjs():
trainSet = ClassificationChexpertDataset("train",augementImg=True)
ModelClass = getModelClass(ARG.MODEL_CLASS)
model = ModelClass(patchSize=ARG.PATCH_SIZE,outFeatures=14)
#compile model for speedup
model = torch.compile(model)
optimizer = torch.optim.AdamW(model.parameters(),lr=ARG.LEARNING_RATE)
return trainSet, model, optimizer
def loadValidationLoaderList():
validLoaderList = list()
chexpertValidationSet = ClassificationChexpertDataset("validate",False)
chexpertValidLoader = prepareDataloader(chexpertValidationSet,ARG.BATCH_SIZE,training=False)
validLoaderList.append((chexpertValidLoader,"val_Chexpert", "val", chexpertValidLoader))
########
# This section adding the test set was added after the hyperparameter optimization on the validation set.
# We added it for the final runs to then directly get the test results.
# During testing different achitectures and approaches this was not part of the code and the test dataset was not touched
chexpertTestSet = ClassificationChexpertDataset(TEST,False)
chexpertTestLoader = prepareDataloader(chexpertTestSet,ARG.BATCH_SIZE,training=False)
validLoaderList.append((chexpertTestLoader,"test_Chexpert", TEST, chexpertValidLoader)) # the chexpertValidLoader is used to calculate the optimal threshold based on the validation set
#######
return validLoaderList
def trainAndEval(device, totalEpochs, saveEvery, batchSize):
trainSet, model, optimizer = loadTrainObjs()
trainLoader = prepareDataloader(trainSet, batchSize, training=True)
#pretrainLoader = prepareDataloader(pretrainSet, batchSize, training=True) if pretrainSet is not None else None
validLoaderList = loadValidationLoaderList()
scheduler = torch.optim.lr_scheduler.OneCycleLR(optimizer=optimizer,total_steps=ARG.EPOCH_NUM*len(trainLoader),max_lr=ARG.LEARNING_RATE,pct_start=0.05)
trainer = Trainer(
model=model,
trainLoader=trainLoader,
validLoaderList=validLoaderList,
optimizer=optimizer,
scheduler=scheduler,
gpuId=device,
saveEvery=saveEvery,
#validsPerEpoch=ARG.VALIDS_PER_EPOCH,
)
trainer.train(totalEpochs)
def evalOnly(device,modelPath):
validLoaderList = loadValidationLoaderList()
model = loadModelFromCheckpoint(modelPath)
trainer = Trainer(
model=model,
trainLoader=None,
validLoaderList=None,
optimizer=None,
scheduler=None,
gpuId=device,
saveEvery=-1,
#validsPerEpoch=ARG.VALIDS_PER_EPOCH,
)
for el in validLoaderList:
trainer.validate(*el)
if __name__ == "__main__":
torch.set_float32_matmul_precision('high')
if ARG.USE_WANDB: initWandB()
device = 0 # shorthand for cuda:0
if ARG.EVAL_ONLY:
assert ARG.MODEL_LOAD_PATH is not None, "provide a path to model weights"
evalOnly(
device=device,
modelPath=ARG.MODEL_LOAD_PATH,
)
else:
assert ARG.MODEL_LOAD_PATH is None, "continue training not implemented yet."
trainAndEval(
device=device,
totalEpochs=ARG.EPOCH_NUM,
saveEvery=1,
batchSize=ARG.BATCH_SIZE,
)