-
Notifications
You must be signed in to change notification settings - Fork 1
/
ChexnetTrainer.py
329 lines (242 loc) · 14.5 KB
/
ChexnetTrainer.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
import os
import numpy as np
import time
import sys
import torch
import torch.nn as nn
import torch.backends.cudnn as cudnn
import torchvision
import torchvision.transforms as transforms
import torch.optim as optim
import torch.nn.functional as tfunc
from torch.utils.data import DataLoader
from torch.optim.lr_scheduler import ReduceLROnPlateau
import torch.nn.functional as func
from sklearn.metrics.ranking import roc_auc_score
from DensenetModels import DenseNet121
from DensenetModels import DenseNet169
from DensenetModels import DenseNet201
from DatasetGenerator import DatasetGenerator
from autoaugment import XRaysPolicy
# from tqdm import tqdm
from torch.nn.functional import kl_div, softmax, log_softmax
#--------------------------------------------------------------------------------
def get_uda_loss(preds1, preds2):
tmp = 1.0
preds1 = softmax(preds1/tmp, dim=1).detach()
preds2 = log_softmax(preds2/tmp, dim=1)
loss_kldiv = kl_div(preds2, preds1, reduction='none')
loss_kldiv = torch.sum(loss_kldiv, dim=1)
return torch.mean(loss_kldiv)
class ChexnetTrainer ():
#---- Train the densenet network
#---- pathDirData - path to the directory that contains images
#---- pathFileTrain - path to the file that contains image paths and label pairs (training set)
#---- pathFileVal - path to the file that contains image path and label pairs (validation set)
#---- nnArchitecture - model architecture 'DENSE-NET-121', 'DENSE-NET-169' or 'DENSE-NET-201'
#---- nnIsTrained - if True, uses pre-trained version of the network (pre-trained on imagenet)
#---- nnClassCount - number of output classes
#---- trBatchSize - batch size
#---- trMaxEpoch - number of epochs
#---- transResize - size of the image to scale down to (not used in current implementation)
#---- transCrop - size of the cropped image
#---- launchTimestamp - date/time, used to assign unique name for the checkpoint file
#---- checkpoint - if not None loads the model and continues training
def train (pathDirData, pathFileTrain, pathFileVal, nnArchitecture, nnIsTrained, nnClassCount, trBatchSize, trMaxEpoch, transResize, transCrop, launchTimestamp, checkpoint):
#-------------------- SETTINGS: NETWORK ARCHITECTURE
if nnArchitecture == 'DENSE-NET-121': model = DenseNet121(nnClassCount, nnIsTrained).cuda()
elif nnArchitecture == 'DENSE-NET-169': model = DenseNet169(nnClassCount, nnIsTrained).cuda()
elif nnArchitecture == 'DENSE-NET-201': model = DenseNet201(nnClassCount, nnIsTrained).cuda()
# model = torch.nn.DataParallel(model).cuda()
#-------------------- SETTINGS: DATA TRANSFORMS
normalize = transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
transformList = []
transformList.append(transforms.RandomResizedCrop(transCrop))
transformList.append(transforms.RandomHorizontalFlip())
transformList.append(transforms.ToTensor())
transformList.append(normalize)
transformSequence=transforms.Compose(transformList)
transform_only_aug = transforms.Compose([XRaysPolicy()])
transform_with_aug = transforms.Compose([
XRaysPolicy(),
transforms.RandomResizedCrop(transCrop),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize
])
#-------------------- SETTINGS: DATASET BUILDERS
datasetTrain = DatasetGenerator(pathDirData, pathFileTrain, transform=transform_with_aug)
datasetTrainUnsup = DatasetGenerator(pathDirData, pathFileTrain, transform=transformSequence, transform_aug=transform_only_aug)
datasetVal = DatasetGenerator(pathDirData, pathFileVal, transform=transformSequence)
dataLoaderTrain = DataLoader(dataset=datasetTrain, batch_size=trBatchSize, shuffle=True, num_workers=4, pin_memory=True)
dataLoaderUnsup = DataLoader(dataset=datasetTrainUnsup, batch_size=trBatchSize, shuffle=True, num_workers=4, pin_memory=True)
dataLoaderVal = DataLoader(dataset=datasetVal, batch_size=trBatchSize, shuffle=False, num_workers=4, pin_memory=True)
#-------------------- SETTINGS: OPTIMIZER & SCHEDULER
optimizer = optim.Adam (model.parameters(), lr=0.0001, betas=(0.9, 0.999), eps=1e-08, weight_decay=1e-5)
# optimizer = optim.SGD(
# filter(
# lambda p: p.requires_grad,
# model.parameters()),
# lr=0.01,
# momentum=0.9,
# weight_decay=5e-4)
scheduler = ReduceLROnPlateau(optimizer, factor = 0.1, patience = 5, mode = 'min')
#-------------------- SETTINGS: LOSS
loss = torch.nn.BCELoss(size_average = True)
#---- Load checkpoint
start_epoch = 0
if checkpoint != None:
modelCheckpoint = torch.load(checkpoint)
model.load_state_dict(modelCheckpoint['state_dict'])
optimizer.load_state_dict(modelCheckpoint['optimizer'])
start_epoch = modelCheckpoint['epoch']
#---- TRAIN THE NETWORK
lossMIN = 100000
max_auroc_mean = -1000
for epochID in range (start_epoch, trMaxEpoch):
timestampTime = time.strftime("%H%M%S")
timestampDate = time.strftime("%d%m%Y")
timestampSTART = timestampDate + '-' + timestampTime
ChexnetTrainer.epochTrain (model, dataLoaderTrain, optimizer, scheduler, trMaxEpoch, nnClassCount, loss, dataLoaderUnsup)
lossVal, losstensor, aurocMean = ChexnetTrainer.epochVal (model, dataLoaderVal, optimizer, scheduler, trMaxEpoch, nnClassCount, loss)
timestampTime = time.strftime("%H%M%S")
timestampDate = time.strftime("%d%m%Y")
timestampEND = timestampDate + '-' + timestampTime
scheduler.step(losstensor.item())
if aurocMean > max_auroc_mean:
max_auroc_mean = aurocMean
print ('Epoch [' + str(epochID + 1) + '] [save] [' + timestampEND + '] aurocMean= ' + str(aurocMean))
torch.save({'epoch': epochID + 1, 'state_dict': model.state_dict(), 'max_suroc_mean': max_auroc_mean, 'optimizer' : optimizer.state_dict()}, 'm-' + launchTimestamp + '_best_auroc.pth.tar')
if lossVal < lossMIN:
lossMIN = lossVal
torch.save({'epoch': epochID + 1, 'state_dict': model.state_dict(), 'best_loss': lossMIN, 'optimizer' : optimizer.state_dict()}, 'm-' + launchTimestamp + '.pth.tar')
print ('Epoch [' + str(epochID + 1) + '] [save] [' + timestampEND + '] loss= ' + str(lossVal))
else:
print ('Epoch [' + str(epochID + 1) + '] [----] [' + timestampEND + '] loss= ' + str(lossVal))
#--------------------------------------------------------------------------------
def epochTrain (model, dataLoader, optimizer, scheduler, epochMax, classCount, loss, unsup_loader):
unsup_ratio = 10
model.train()
iter_u = iter(unsup_loader)
for batchID, (inputs, target) in enumerate (dataLoader):
l_data_len = len(target)
try:
u_input_1, u_input_2 = next(iter_u)
except StopIteration:
iter_u = iter(self.unsup_loader)
u_input_1, u_input_2 = next(iter_u)
inputs = torch.cat([inputs, u_input_1, u_input_2])
target = target.cuda()
inputs = inputs.cuda()
varInput = torch.autograd.Variable(inputs)
varTarget = torch.autograd.Variable(target)
varOutput = model(inputs)
lossvalue = loss(varOutput[:l_data_len], varTarget)
# -------------------------uda loss
preds_unsup = varOutput[l_data_len:]
preds1, preds2 = torch.chunk(preds_unsup, 2)
loss_kl_div = get_uda_loss(preds1, preds2)
lossvalue = lossvalue + (unsup_ratio*loss_kl_div)
#--------------------------
optimizer.zero_grad()
lossvalue.backward()
optimizer.step()
if batchID % 10 == 9:
print(f"[{batchID:04}/{len(dataLoader)}] loss: {lossvalue.item():0.5f}")
# loss kl: {unsup_ratio*loss_kl_div.item():0.5f}
#--------------------------------------------------------------------------------
def epochVal (model, dataLoader, optimizer, scheduler, epochMax, classCount, loss):
model.eval ()
lossVal = 0
lossValNorm = 0
losstensorMean = 0
outGT = torch.FloatTensor().cuda()
outPRED = torch.FloatTensor().cuda()
for i, (input_, target) in enumerate (dataLoader):
with torch.no_grad():
target = target.cuda()
input_ = input_.cuda()
outGT = torch.cat((outGT, target), 0)
varInput = torch.autograd.Variable(input_)
varTarget = torch.autograd.Variable(target)
varOutput = model(varInput)
outPRED = torch.cat((outPRED, varOutput), 0)
losstensor = loss(varOutput, varTarget)
losstensorMean += losstensor
lossVal += losstensor.item()
lossValNorm += 1
del varOutput, varTarget, varInput, target, input_
outLoss = lossVal / lossValNorm
losstensorMean = losstensorMean / lossValNorm
aurocIndividual = ChexnetTrainer.computeAUROC(outGT, outPRED, classCount)
aurocMean = np.array(aurocIndividual).mean()
print ('AUROC mean ', aurocMean)
return outLoss, losstensorMean, aurocMean
#--------------------------------------------------------------------------------
#---- Computes area under ROC curve
#---- dataGT - ground truth data
#---- dataPRED - predicted data
#---- classCount - number of classes
def computeAUROC (dataGT, dataPRED, classCount):
outAUROC = []
datanpGT = dataGT.cpu().numpy()
datanpPRED = dataPRED.cpu().numpy()
for i in range(classCount):
outAUROC.append(roc_auc_score(datanpGT[:, i], datanpPRED[:, i]))
return outAUROC
#--------------------------------------------------------------------------------
#---- Test the trained network
#---- pathDirData - path to the directory that contains images
#---- pathFileTrain - path to the file that contains image paths and label pairs (training set)
#---- pathFileVal - path to the file that contains image path and label pairs (validation set)
#---- nnArchitecture - model architecture 'DENSE-NET-121', 'DENSE-NET-169' or 'DENSE-NET-201'
#---- nnIsTrained - if True, uses pre-trained version of the network (pre-trained on imagenet)
#---- nnClassCount - number of output classes
#---- trBatchSize - batch size
#---- trMaxEpoch - number of epochs
#---- transResize - size of the image to scale down to (not used in current implementation)
#---- transCrop - size of the cropped image
#---- launchTimestamp - date/time, used to assign unique name for the checkpoint file
#---- checkpoint - if not None loads the model and continues training
def test (pathDirData, pathFileTest, pathModel, nnArchitecture, nnClassCount, nnIsTrained, trBatchSize, transResize, transCrop, launchTimeStamp):
CLASS_NAMES = [ 'Atelectasis', 'Cardiomegaly', 'Effusion', 'Infiltration', 'Mass', 'Nodule', 'Pneumonia',
'Pneumothorax', 'Consolidation', 'Edema', 'Emphysema', 'Fibrosis', 'Pleural_Thickening', 'Hernia']
cudnn.benchmark = True
#-------------------- SETTINGS: NETWORK ARCHITECTURE, MODEL LOAD
if nnArchitecture == 'DENSE-NET-121': model = DenseNet121(nnClassCount, nnIsTrained).cuda()
elif nnArchitecture == 'DENSE-NET-169': model = DenseNet169(nnClassCount, nnIsTrained).cuda()
elif nnArchitecture == 'DENSE-NET-201': model = DenseNet201(nnClassCount, nnIsTrained).cuda()
# model = torch.nn.DataParallel(model).cuda()
modelCheckpoint = torch.load(pathModel)
model.load_state_dict(modelCheckpoint['state_dict'])
#-------------------- SETTINGS: DATA TRANSFORMS, TEN CROPS
normalize = transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
#-------------------- SETTINGS: DATASET BUILDERS
transformList = []
transformList.append(transforms.Resize(transResize))
transformList.append(transforms.TenCrop(transCrop))
transformList.append(transforms.Lambda(lambda crops: torch.stack([transforms.ToTensor()(crop) for crop in crops])))
transformList.append(transforms.Lambda(lambda crops: torch.stack([normalize(crop) for crop in crops])))
transformSequence=transforms.Compose(transformList)
datasetTest = DatasetGenerator(pathDirData, pathFileTest, transform=transformSequence)
dataLoaderTest = DataLoader(dataset=datasetTest, batch_size=trBatchSize, num_workers=8, shuffle=False, pin_memory=True)
outGT = torch.FloatTensor().cuda()
outPRED = torch.FloatTensor().cuda()
model.eval()
for i, (input, target) in enumerate(dataLoaderTest):
with torch.no_grad():
target = target.cuda()
outGT = torch.cat((outGT, target), 0)
bs, n_crops, c, h, w = input.size()
varInput = torch.autograd.Variable(input.view(-1, c, h, w).cuda())
out = model(varInput)
outMean = out.view(bs, n_crops, -1).mean(1)
outPRED = torch.cat((outPRED, outMean.data), 0)
# del varOutput, varTarget, varInput, target, input_
aurocIndividual = ChexnetTrainer.computeAUROC(outGT, outPRED, nnClassCount)
aurocMean = np.array(aurocIndividual).mean()
print ('AUROC mean ', aurocMean)
for i in range (0, len(aurocIndividual)):
print (CLASS_NAMES[i], ' ', aurocIndividual[i])
return
#--------------------------------------------------------------------------------