forked from zoogzog/chexnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChexnetTrainer.py
386 lines (279 loc) · 17.3 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
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import os
import numpy as np
import time
import sys
import re
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
CLASS_NAMES = [ 'Atelectasis', 'Cardiomegaly', 'Effusion', 'Infiltration', 'Mass', 'Nodule', 'Pneumonia',
'Pneumothorax', 'Consolidation', 'Edema', 'Emphysema', 'Fibrosis', 'Pleural_Thickening', 'Hernia']
#--------------------------------------------------------------------------------
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)
#-------------------- SETTINGS: DATASET BUILDERS
datasetTrain = DatasetGenerator(pathImageDirectory=pathDirData, pathDatasetFile=pathFileTrain, transform=transformSequence)
datasetVal = DatasetGenerator(pathImageDirectory=pathDirData, pathDatasetFile=pathFileVal, transform=transformSequence)
dataLoaderTrain = DataLoader(dataset=datasetTrain, batch_size=trBatchSize, shuffle=True, pin_memory=True) # num_workers=N
dataLoaderVal = DataLoader(dataset=datasetVal, batch_size=trBatchSize, shuffle=False, pin_memory=True) # num_workers=N
#-------------------- SETTINGS: OPTIMIZER & SCHEDULER
optimizer = optim.Adam (model.parameters(), lr=0.0001, betas=(0.9, 0.999), eps=1e-08, weight_decay=1e-5)
scheduler = ReduceLROnPlateau(optimizer, factor = 0.1, patience = 5, mode = 'min')
#-------------------- SETTINGS: LOSS
loss = torch.nn.BCELoss(size_average = True)
#---- Load checkpoint
if checkpoint != None:
print("=> loading checkpoint")
modelCheckpoint = torch.load(checkpoint)
# Error when loading DenseNet model : Missing key(s) in state_dict
# https://github.com/KaiyangZhou/deep-person-reid/issues/23
# The error is caused by the mismatch in keys, e.g. layers were named 'norm.1', 'conv.1',
# but are now named 'norm1', 'conv1' (I trained the model with the old torchvision).
# modify:
# '.'s are no longer allowed in module names, but pervious _DenseLayer
# has keys 'norm.1', 'relu.1', 'conv.1', 'norm.2', 'relu.2', 'conv.2'.
# They are also in the checkpoints in model_urls.
# This pattern is used to find such keys.
# https://github.com/pytorch/vision/blob/50b2f910490a731c4cd50db5813b291860f02237/torchvision/models/densenet.py#L28
pattern = re.compile(
r'^(.*denselayer\d+\.(?:norm|relu|conv))\.((?:[12])\.(?:weight|bias|running_mean|running_var))$')
state_dict = modelCheckpoint['state_dict']
for key in list(state_dict.keys()):
res = pattern.match(key)
if res:
new_key = res.group(1) + res.group(2)
state_dict[new_key] = state_dict[key]
del state_dict[key]
model.load_state_dict(state_dict)
optimizer.load_state_dict(modelCheckpoint['optimizer'])
print("=> loaded checkpoint")
#---- TRAIN THE NETWORK
lossMIN = 100000
for epochID in range (0, 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)
lossVal, losstensor = 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.data[0])
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):
model.train()
for batchID, (input, target) in enumerate (dataLoader):
target = target.cuda(async = True)
varInput = torch.autograd.Variable(input)
varTarget = torch.autograd.Variable(target)
varOutput = model(varInput)
lossvalue = loss(varOutput, varTarget)
optimizer.zero_grad()
lossvalue.backward()
optimizer.step()
#--------------------------------------------------------------------------------
def epochVal (model, dataLoader, optimizer, scheduler, epochMax, classCount, loss):
model.eval ()
lossVal = 0
lossValNorm = 0
losstensorMean = 0
for i, (input, target) in enumerate (dataLoader):
target = target.cuda(async=True)
varInput = torch.autograd.Variable(input, volatile=True)
varTarget = torch.autograd.Variable(target, volatile=True)
varOutput = model(varInput)
losstensor = loss(varOutput, varTarget)
losstensorMean += losstensor
lossVal += losstensor.data[0]
lossValNorm += 1
outLoss = lossVal / lossValNorm
losstensorMean = losstensorMean / lossValNorm
return outLoss, losstensorMean
#--------------------------------------------------------------------------------
#---- 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
# redefine metric function to calculate auc and accuracy for one single class
def compute_metrics_class(gt, pred_proba, class_name):
"""Computes Area Under the Curve (AUC) and Accuracy from prediction scores for one single class.
Args:
gt: Pytorch tensor on GPU, shape = [n_samples, n_classes]
true binary labels.
pred: Pytorch tensor on GPU, shape = [n_samples, n_classes]
can either be probability estimates of the positive class,
confidence values, or binary decisions.
Returns:
Dictionary with metrics values.
"""
metrics = {}
index_class = CLASS_NAMES.index(class_name)
gt_class = gt.cpu().numpy()[:, index_class]
pred_proba_class = pred_proba.cpu().numpy()[:, index_class]
pred_class = np.zeros(len(pred_proba_class))
pred_class[pred_proba_class>=0.5] = 1
AUROC_class = roc_auc_score(gt_class, pred_proba_class)
metrics['AUROC'] = AUROC_class
accuracy_class = float(np.sum(gt_class == pred_class)) / float(len(pred_class))
metrics['accuracy'] = accuracy_class
return metrics
#--------------------------------------------------------------------------------
#---- 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):
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()
print("=> loading checkpoint")
modelCheckpoint = torch.load(pathModel)
# https://github.com/KaiyangZhou/deep-person-reid/issues/23
pattern = re.compile(
r'^(.*denselayer\d+\.(?:norm|relu|conv))\.((?:[12])\.(?:weight|bias|running_mean|running_var))$')
state_dict = modelCheckpoint['state_dict']
for key in list(state_dict.keys()):
res = pattern.match(key)
if res:
new_key = res.group(1) + res.group(2)
state_dict[new_key] = state_dict[key]
del state_dict[key]
model.load_state_dict(state_dict)
print("=> loaded checkpoint")
#-------------------- 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(pathImageDirectory=pathDirData, pathDatasetFile=pathFileTest, transform=transformSequence)
dataLoaderTest = DataLoader(dataset=datasetTest, batch_size=trBatchSize, shuffle=False, pin_memory=True) #num_workers=N
outGT = torch.FloatTensor().cuda()
outPRED = torch.FloatTensor().cuda()
model.eval()
for i, (input, target) in enumerate(dataLoaderTest):
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(), volatile=True)
out = model(varInput)
outMean = out.view(bs, n_crops, -1).mean(1)
outPRED = torch.cat((outPRED, outMean.data), 0)
metrics_pneumonia = ChexnetTrainer.compute_metrics_class(outGT, outPRED, 'Pneumonia')
print('The AUROC for Pneumonia is {}'.format(metrics_pneumonia['AUROC']))
print('The accuracy for Pneumonia is {}'.format(metrics_pneumonia['accuracy']))
return metrics_pneumonia
#-------------------------------------------------------------------------#
def predict (pathDirData, pathFileTest, pathModel, nnArchitecture, nnClassCount, nnIsTrained,
trBatchSize, transResize, transCrop, launchTimeStamp):
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)
# https://github.com/KaiyangZhou/deep-person-reid/issues/23
pattern = re.compile(
r'^(.*denselayer\d+\.(?:norm|relu|conv))\.((?:[12])\.(?:weight|bias|running_mean|running_var))$')
state_dict = modelCheckpoint['state_dict']
for key in list(state_dict.keys()):
res = pattern.match(key)
if res:
new_key = res.group(1) + res.group(2)
state_dict[new_key] = state_dict[key]
del state_dict[key]
model.load_state_dict(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(pathImageDirectory=pathDirData, pathDatasetFile=pathFileTest, transform=transformSequence)
dataLoaderTest = DataLoader(dataset=datasetTest, batch_size=trBatchSize, shuffle=False, pin_memory=True) #num_workers=N
outGT = torch.FloatTensor().cuda()
outPRED = torch.FloatTensor().cuda()
model.eval()
for i, (input, target) in enumerate(dataLoaderTest):
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(), volatile=True)
out = model(varInput)
outMean = out.view(bs, n_crops, -1).mean(1)
outPRED = torch.cat((outPRED, outMean.data), 0)
pneumonia_probas = []
for p in outPRED.cpu().data.numpy()[:, CLASS_NAMES.index('Pneumonia')]:
pneumonia_probas.append(p)
return pneumonia_probas
#--------------------------------------------------------------------------------