-
Notifications
You must be signed in to change notification settings - Fork 1
/
ChexnetTrainer.py
442 lines (339 loc) · 19 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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
import os
import numpy as np
import time
import sys
import csv
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 import roc_auc_score
from DensenetModels import DenseNet121
from DensenetModels import DenseNet169
from DensenetModels import DenseNet201
from DatasetGenerator import DatasetGenerator
from DatasetGenerator_Imbalance import DatasetGeneratorforTraining
import matplotlib.pyplot as plt
# --------------------------------------------------------------------------------
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(14, nnIsTrained).cuda()
elif nnArchitecture == 'DENSE-NET-169': model = DenseNet169(nnClassCount, nnIsTrained).cuda()
elif nnArchitecture == 'DENSE-NET-201': model = DenseNet201(nnClassCount, nnIsTrained).cuda()
modelCheckpoint = torch.load('/home/stevenlai/Desktop/chexnet/original/models/m-25012018-123527.pth.tar')
model.load_state_dict(modelCheckpoint['state_dict'],False)
for param in model.parameters():
param.requires_grad = False
model.densenet121.classifier = nn.Sequential(nn.Linear(1024, 1), nn.Sigmoid())
#print(model)
model = torch.nn.DataParallel(model).to('cuda' if torch.cuda.is_available() else 'cpu')
#-------------------- 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 = DatasetGeneratorforTraining(pathImageDirectory=pathDirData, pathDatasetFile=pathFileTrain, transform=transformSequence)
datasetTrainAcc = DatasetGenerator(pathImageDirectory=pathDirData, pathDatasetFile=pathFileTrain, transform=transformSequence)
datasetVal = DatasetGenerator(pathImageDirectory=pathDirData, pathDatasetFile=pathFileVal, transform=transformSequence)
print('Train:',len(datasetTrain))
print('Train Acc:',len(datasetTrainAcc))
print('Val:',len(datasetVal))
dataLoaderTrain = DataLoader(dataset=datasetTrain, batch_size=trBatchSize, shuffle=True, num_workers=24, pin_memory=True)
dataLoaderTrainAcc = DataLoader(dataset=datasetTrainAcc, batch_size=trBatchSize, shuffle=True, num_workers=24, pin_memory=True)
dataLoaderVal = DataLoader(dataset=datasetVal, batch_size=trBatchSize, shuffle=False, num_workers=24, 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(model.parameters(), lr=0.01, momentum=0.9)
scheduler = ReduceLROnPlateau(optimizer, factor = 0.1, patience = 5, mode = 'min', verbose = 1)
#-------------------- SETTINGS: LOSS
loss = torch.nn.BCELoss(reduction = 'mean')
#---- Load checkpoint
if checkpoint != None:
modelCheckpoint = torch.load(checkpoint)
model.load_state_dict(modelCheckpoint['state_dict'])
optimizer.load_state_dict(modelCheckpoint['optimizer'])
#---- TRAIN THE NETWORK
lossMIN = 100000
trainacclist = []
trainlosslist = []
vallosslist = []
valacclist = []
for epochID in range (0, trMaxEpoch):
print('------- epoch:', epochID+1,'------------------------------------------------')
timestampTime = time.strftime("%H:%M:%S")
timestampDate = time.strftime("%Y_%m_%d")
timestampSTART = timestampDate + '-' + timestampTime
lossTrain, accTrain = ChexnetTrainer.epochTrain(model, dataLoaderTrain, dataLoaderTrainAcc, optimizer, scheduler, trMaxEpoch, nnClassCount, loss)
trainlosslist.append(lossTrain)
trainacclist.append(accTrain)
lossVal, accVal = ChexnetTrainer.epochVal(model, dataLoaderVal, optimizer, scheduler, trMaxEpoch, nnClassCount, loss)
vallosslist.append(lossVal)
valacclist.append(accVal)
timestampTime = time.strftime("%H:%M:%S")
timestampDate = time.strftime("%Y_%m_%d")
timestampEND = timestampDate + '-' + timestampTime
scheduler.step(lossVal)
if lossVal < lossMIN:
lossMIN = lossVal
torch.save({'epoch': epochID + 1, 'state_dict': model.state_dict(),
'best_loss': lossMIN, 'optimizer' : optimizer.state_dict()},
'/home/stevenlai/Desktop/chexnet/Full_set/model/' + launchTimestamp +'_fullset.pth.tar')
print ('[save] [' + timestampEND + ']')
print ('train acc = %.3f'%accTrain ,' train loss = %.3f'%float(lossTrain))
print ('val acc = %.3f'%accVal , ' val loss = %.3f'%float(lossVal))
print()
else:
print ( '[' + timestampEND + ']')
print ('train acc = %.3f'%accTrain ,' train loss = %.3f'%float(lossTrain))
print ('val acc = %.3f'%accVal ,' val loss = %.3f'%float(lossVal))
print()
plt.plot(trainacclist,label = 'training acc')
plt.plot(valacclist,label = 'val acc')
plt.legend(loc = 'lower right')
plt.title('acc')
plt.savefig('/home/stevenlai/Desktop/chexnet/Full_set/plot/'+ launchTimestamp + '_acc_fullset.png')
plt.close()
plt.plot(trainlosslist,label='training loss')
plt.plot(vallosslist,label = 'val loss')
plt.legend(loc = 'upper right')
plt.title('loss')
plt.savefig('/home/stevenlai/Desktop/chexnet/Full_set/plot/'+ launchTimestamp + '_loss_fullset.png')
plt.close()
#--------------------------------------------------------------------------------
def epochTrain (model, dataLoader,dataLoaderAcc, optimizer, scheduler, epochMax, classCount, loss):
model.train()
loss_add = 0
lossNorm = 0
# enumerate spend most time
for batchID, (input, target) in enumerate (dataLoader):
target = target.cuda(non_blocking = True)
varInput = input.to('cuda' if torch.cuda.is_available() else 'cpu')
varTarget = target.to('cuda' if torch.cuda.is_available() else 'cpu')
varOutput = model(varInput)
lossvalue = loss(varOutput, varTarget)
loss_add += lossvalue.item()
lossNorm += 1
optimizer.zero_grad()
lossvalue.backward()
optimizer.step()
outloss = loss_add/lossNorm
#--------------------------------------------------------------------------------
CLASS_NAMES = [ 'Non TB', 'TB']
cudnn.benchmark = True
outGT = torch.FloatTensor().to('cuda' if torch.cuda.is_available() else 'cpu')
#outPRED = torch.FloatTensor().cuda()
correct_count = 0
total_count = 0
model.eval()
with torch.no_grad():
for batchID, (input, target) in enumerate (dataLoaderAcc):
target = target.to('cuda' if torch.cuda.is_available() else 'cpu')
outGT = torch.cat((outGT, target), 0)
bs, c, h, w = input.size()
varInput = input.view(-1, c, h, w).to('cuda' if torch.cuda.is_available() else 'cpu')
out = model(varInput)
for k in range(len(out)):
if out[k][0]<0.5: out[k][0] = 0.
else: out[k][0] = 1.
if out[k][0] == target[k][0]:correct_count+=1
total_count += 1
acc = correct_count/total_count
return outloss, acc
#--------------------------------------------------------------------------------
def epochVal (model, dataLoader, optimizer, scheduler, epochMax, classCount, loss):
model.eval ()
lossVal = 0
lossValNorm = 0
CLASS_NAMES = [ 'Normal', 'Abnormal']
cudnn.benchmark = True
outGT = torch.FloatTensor().to('cuda' if torch.cuda.is_available() else 'cpu')
#outPRED = torch.FloatTensor().to('cuda' if torch.cuda.is_available() else 'cpu')
correct_count = 0
total_count = 0
losstensorMean = 0
with torch.no_grad():
for i, (input, target) in enumerate (dataLoader):
target = target.to('cuda' if torch.cuda.is_available() else 'cpu')
varInput = input.to('cuda' if torch.cuda.is_available() else 'cpu')
varTarget = target.to('cuda' if torch.cuda.is_available() else 'cpu')
varOutput = model(varInput)
losstensor = loss(varOutput, varTarget)
lossVal += losstensor.item()
lossValNorm += 1
outGT = torch.cat((outGT, target), 0)
bs, c, h, w = input.size()
varInput = input.view(-1, c, h, w).to('cuda' if torch.cuda.is_available() else 'cpu')
out = model(varInput)
for k in range(len(out)):
if out[k][0]<0.5: out[k][0] = 0.
else: out[k][0] = 1.
if out[k][0] == target[k][0]:correct_count+=1
total_count += 1
outLoss = lossVal / lossValNorm
acc = correct_count/total_count
return outLoss, acc
#--------------------------------------------------------------------------------
#---- Computes area under ROC curve
#---- dataGT - ground truth data
#---- dataPRED - predicted data
#---- classCount - number of classes
def computeAUROC (dataGT, dataPRED, classCount):
CLASS_NAMES = ['non-TB', 'TB']
outAUROC = []
datanpGT = dataGT.cpu().numpy()
datanpPRED = dataPRED.cpu().numpy()
#print('classCount:',classCount)
classCount -= 1
for i in range(classCount):
# outAUROC.append(roc_auc_score(datanpGT[:, i], datanpPRED[:, i]))
try:
#print('class:',CLASS_NAMES[i])
#print('datanpGT[:, i]',datanpGT[:, i])
#print('datanpPRED[:, i]',datanpPRED[:, i])
outAUROC.append(roc_auc_score(datanpGT[:, i], datanpPRED[:, i]))
except ValueError:
#print('Value Error:',CLASS_NAMES[i])
pass
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):
print('Testing default')
print('Batch size:',trBatchSize)
CLASS_NAMES = [ 'non-TB', 'TB']
gt = []
pred = []
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'],False)
print('model loaded:',pathModel)
#-------------------- 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, num_workers=8, shuffle=False, pin_memory=True)
print('Test:',len(datasetTest))
outGT = torch.FloatTensor().cuda()
outPRED = torch.FloatTensor().cuda()
model.eval()
a = np.array([[0,0],[0,0]])
print('Start Testing...')
with torch.no_grad():
for i, (input, target) in enumerate(dataLoaderTest):
# print('input',input.size()) (1,10,3,224,224)
# print('target',target.size()) (1,1)
target = target.cuda()
outGT = torch.cat((outGT, target), 0)
bs, n_crops, c, h, w = input.size()
varInput = (input.view(-1, c, h, w).cuda()).to('cuda')
out = model(varInput)
# print(out.size()) (10,1)
outMean = sum(out)/10
outPRED = torch.cat((outPRED, outMean.data), 0)
# print('outMean',outMean)
# print('outPRED', outPRED)
# Suppose they will be the same
if outMean < 0.5: outMean = 0.
else: outMean = 1.
# print('outMean',outMean)
# print('target', float(target[0][0]))
# Suppose they will be the same type
if outMean == 1. and float(target[0][0]) == 1.: a[0][0]+=1
if outMean == 0. and float(target[0][0]) == 0.: a[1][1]+=1
if outMean == 0. and float(target[0][0]) == 1.:
a[1][0]+=1
# print('TB mistaken into non TB')
if outMean == 1. and float(target[0][0]) == 0.:
a[0][1]+=1
# print('non TB mistaken into TB')
# print(a)
gt.append(float(target[0][0]))
pred.append(outMean)
total_correct = a[0][0] + a[1][1]
total = a[0][0] + a[0][1] + a[1][0] + a[1][1]
acc = total_correct/total
print('test acc = %.3f'%acc)
# Matrix
fig, ax = plt.subplots(figsize=(5,5))
a = a.T
ax.matshow(a.T, cmap=plt.cm.Blues, alpha=0.3)
for i in range(2):
for j in range(2):
ax.text(i, j, s = a[i, j], va='center', ha='center')
savename = (pathFileTest.split('/')[-1]).split('.')[0]
plt.title(savename + ' TB-nonTB matrix')
plt.xlabel('ground truth')
plt.ylabel('predict')
labels = ['TB','non-TB']
ax.set_xticklabels([''] + labels)
ax.set_yticklabels([''] + labels)
plt.savefig('/home/stevenlai/Desktop/chexnet/Full_set/plot/matrix_fullset_'+savename+'_'+launchTimeStamp+'.png')
plt.close()
#plt.show()
# csv
# save filename
name = []
txt = open(pathFileTest)
for filename in txt:
name.append(filename.split(' ')[0])
# output csv
outputdir = open('/home/stevenlai/Desktop/chexnet/Full_set/plot/result_'+savename+'_'+launchTimeStamp+'.csv','w')
csvwriter = csv.writer(outputdir)
csvwriter.writerow(['file','ground truth','predict'])
for i in range(len(name)):
csvwriter.writerow([name[i],gt[i],pred[i]])
outputdir.close()
#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 acc
#--------------------------------------------------------------------------------