-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtrain.py
372 lines (303 loc) · 14.2 KB
/
train.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
# encoding: utf-8
import re
import sys
import os
import cv2
import time
import numpy as np
import torch
import torch.nn as nn
import torch.backends.cudnn as cudnn
from torch.optim import lr_scheduler
import torch.optim as optim
import torchvision
import torchvision.transforms as transforms
from torch.utils.data import DataLoader
from read_data import ChestXrayDataSet
from sklearn.metrics import roc_auc_score
from skimage.measure import label
from model import Densenet121_AG, Fusion_Branch
from PIL import Image
#np.set_printoptions(threshold = np.nan)
CKPT_PATH = ''
CKPT_PATH_G = 'previous_models/AG_CNN_Global_epoch_1.pkl'
CKPT_PATH_L = 'previous_models/AG_CNN_Local_epoch_2.pkl'
CKPT_PATH_F = 'previous_models/AG_CNN_Fusion_epoch_23.pkl'
N_CLASSES = 14
CLASS_NAMES = [ 'Atelectasis', 'Cardiomegaly', 'Effusion', 'Infiltration', 'Mass', 'Nodule', 'Pneumonia',
'Pneumothorax', 'Consolidation', 'Edema', 'Emphysema', 'Fibrosis', 'Pleural_Thickening', 'Hernia']
DATA_DIR = 'input/images'
TRAIN_IMAGE_LIST = 'labels/train_list.txt'
VAL_IMAGE_LIST = 'labels/val_list.txt'
save_model_path = 'model-AG-CNN/'
save_model_name = 'AG_CNN'
LR_G = 1e-8
LR_L = 1e-8
LR_F = 1e-3
num_epochs = 50
BATCH_SIZE = 32
normalize = transforms.Normalize(
mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]
)
preprocess = transforms.Compose([
transforms.Resize((256,256)),
transforms.CenterCrop(224),
transforms.ToTensor(),
normalize,
])
def Attention_gen_patchs(ori_image, fm_cuda):
# fm => mask =>(+ ori-img) => crop = patchs
feature_conv = fm_cuda.data.cpu().numpy()
size_upsample = (224, 224)
bz, nc, h, w = feature_conv.shape
patchs_cuda = torch.FloatTensor()
for i in range(0, bz):
feature = feature_conv[i]
cam = feature.reshape((nc, h*w))
cam = cam.sum(axis=0)
cam = cam.reshape(h,w)
cam = cam - np.min(cam)
cam_img = cam / np.max(cam)
cam_img = np.uint8(255 * cam_img)
heatmap_bin = binImage(cv2.resize(cam_img, size_upsample))
heatmap_maxconn = selectMaxConnect(heatmap_bin)
heatmap_mask = heatmap_bin * heatmap_maxconn
ind = np.argwhere(heatmap_mask != 0)
minh = min(ind[:,0])
minw = min(ind[:,1])
maxh = max(ind[:,0])
maxw = max(ind[:,1])
# to ori image
image = ori_image[i].numpy().reshape(224,224,3)
image = image[int(224*0.334):int(224*0.667),int(224*0.334):int(224*0.667),:]
image = cv2.resize(image, size_upsample)
image_crop = image[minh:maxh,minw:maxw,:] * 256 # because image was normalized before
image_crop = preprocess(Image.fromarray(image_crop.astype('uint8')).convert('RGB'))
img_variable = torch.autograd.Variable(image_crop.reshape(3,224,224).unsqueeze(0))
patchs_cuda = torch.cat((patchs_cuda,img_variable),0)
return patchs_cuda
def binImage(heatmap):
_, heatmap_bin = cv2.threshold(heatmap , 0 , 255 , cv2.THRESH_BINARY+cv2.THRESH_OTSU)
# t in the paper
#_, heatmap_bin = cv2.threshold(heatmap , 178 , 255 , cv2.THRESH_BINARY)
return heatmap_bin
def selectMaxConnect(heatmap):
labeled_img, num = label(heatmap, connectivity=2, background=0, return_num=True)
max_label = 0
max_num = 0
for i in range(1, num+1):
if np.sum(labeled_img == i) > max_num:
max_num = np.sum(labeled_img == i)
max_label = i
lcc = (labeled_img == max_label)
if max_num == 0:
lcc = (labeled_img == -1)
lcc = lcc + 0
return lcc
def main():
print("[Info]: Loading Data ...")
normalize = transforms.Normalize([0.485, 0.456, 0.406],
[0.229, 0.224, 0.225])
train_dataset = ChestXrayDataSet(data_dir=DATA_DIR,
image_list_file=TRAIN_IMAGE_LIST,
transform=transforms.Compose([
transforms.Resize(224),
transforms.CenterCrop(224),
transforms.ToTensor(),
normalize,
]))
train_loader = DataLoader(dataset=train_dataset, batch_size=BATCH_SIZE,
shuffle=True, num_workers=4, pin_memory=True)
test_dataset = ChestXrayDataSet(data_dir=DATA_DIR,
image_list_file=VAL_IMAGE_LIST,
transform=transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
normalize,
]))
test_loader = DataLoader(dataset=test_dataset, batch_size=128,
shuffle=False, num_workers=4, pin_memory=True)
print("[Info]: Data has been loaded ...")
print("[Info]: Loading Model ...")
# initialize and load the model
Global_Branch_model = Densenet121_AG(pretrained = False, num_classes = N_CLASSES).cuda()
Local_Branch_model = Densenet121_AG(pretrained = False, num_classes = N_CLASSES).cuda()
Fusion_Branch_model = Fusion_Branch(input_size = 2048, output_size = N_CLASSES).cuda()
if os.path.isfile(CKPT_PATH):
print("[Info]: Loading checkpoint")
checkpoint = torch.load(CKPT_PATH)
# to load state
# Code modified from torchvision densenet source for loading from pre .4 densenet weights.
state_dict = checkpoint['state_dict']
remove_data_parallel = True # Change if you don't want to use nn.DataParallel(model)
pattern = re.compile(
r'^(.*denselayer\d+\.(?:norm|relu|conv))\.((?:[12])\.(?:weight|bias|running_mean|running_var))$')
for key in list(state_dict.keys()):
ori_key = key
key = key.replace('densenet121.','')
#print('key',key)
match = pattern.match(key)
new_key = match.group(1) + match.group(2) if match else key
new_key = new_key[7:] if remove_data_parallel else new_key
#print('new_key',new_key)
if '.0.' in new_key:
new_key = new_key.replace('0.','')
state_dict[new_key] = state_dict[ori_key]
# Delete old key only if modified.
if match or remove_data_parallel:
del state_dict[ori_key]
Global_Branch_model.load_state_dict(state_dict)
Local_Branch_model.load_state_dict(state_dict)
print("[Info]: Loaded baseline checkpoint")
else:
print("[Info]: No previous checkpoint found ...")
if os.path.isfile(CKPT_PATH_G):
checkpoint = torch.load(CKPT_PATH_G)
Global_Branch_model.load_state_dict(checkpoint)
print("[Info]: Loaded Global_Branch_model checkpoint")
if os.path.isfile(CKPT_PATH_L):
checkpoint = torch.load(CKPT_PATH_L)
Local_Branch_model.load_state_dict(checkpoint)
print("[Info]: Loaded Local_Branch_model checkpoint")
if os.path.isfile(CKPT_PATH_F):
checkpoint = torch.load(CKPT_PATH_F)
Fusion_Branch_model.load_state_dict(checkpoint)
print("[Info]: Loaded Fusion_Branch_model checkpoint")
cudnn.benchmark = True
criterion = nn.BCELoss()
optimizer_global = optim.Adam(Global_Branch_model.parameters(), lr=LR_G, betas=(0.9, 0.999), eps=1e-08, weight_decay=1e-5)
lr_scheduler_global = lr_scheduler.StepLR(optimizer_global , step_size = 10, gamma = 1)
optimizer_local = optim.Adam(Local_Branch_model.parameters(), lr=LR_L, betas=(0.9, 0.999), eps=1e-08, weight_decay=1e-5)
lr_scheduler_local = lr_scheduler.StepLR(optimizer_local , step_size = 10, gamma = 1)
optimizer_fusion = optim.Adam(Fusion_Branch_model.parameters(), lr=LR_F, betas=(0.9, 0.999), eps=1e-08, weight_decay=1e-5)
lr_scheduler_fusion = lr_scheduler.StepLR(optimizer_fusion , step_size = 15, gamma = 0.1)
print("[Info]: Model has been loaded ...")
print("[Info]: Starting training ...")
for epoch in range(num_epochs):
since = time.time()
print('Epoch: {}/{}'.format(epoch , num_epochs - 1))
print('-' * 10)
#set the mode of model
lr_scheduler_global.step() #about lr and gamma
lr_scheduler_local.step()
lr_scheduler_fusion.step()
Global_Branch_model.train() #set model to training mode
Local_Branch_model.train()
Fusion_Branch_model.train()
running_loss = 0.0
#Iterate over data
for i, (input, target) in enumerate(train_loader):
input_var = torch.autograd.Variable(input.cuda())
target_var = torch.autograd.Variable(target.cuda())
optimizer_global.zero_grad()
optimizer_local.zero_grad()
optimizer_fusion.zero_grad()
# compute output
output_global, fm_global, pool_global = Global_Branch_model(input_var)
patchs_var = Attention_gen_patchs(input,fm_global)
torch.cuda.empty_cache()
output_local, _, pool_local = Local_Branch_model(patchs_var)
#print(fusion_var.shape)
output_fusion = Fusion_Branch_model(pool_global, pool_local)
#
torch.cuda.empty_cache()
# loss
loss1 = criterion(output_global, target_var)
loss2 = criterion(output_local, target_var)
loss3 = criterion(output_fusion, target_var)
#
loss = loss1*0.8 + loss2*0.1 + loss3*0.1
if (i%500) == 0:
print('step: {} totalloss: {loss:.3f} loss1: {loss1:.3f} loss2: {loss2:.3f} loss3: {loss3:.3f}'.format(i, loss = loss, loss1 = loss1, loss2 = loss2, loss3 = loss3))
loss.backward()
optimizer_global.step()
optimizer_local.step()
optimizer_fusion.step()
#print(loss.data.item())
running_loss += loss.data.item()
#break
'''
if i == 40:
print('break')
break
'''
epoch_loss = float(running_loss) / float(i)
print(' Epoch over Loss: {:.5f}'.format(epoch_loss))
print("[Info]: Starting testing ...")
test(Global_Branch_model, Local_Branch_model, Fusion_Branch_model,test_loader)
#break
#save
if epoch % 1 == 0:
save_path = save_model_path
torch.save(Global_Branch_model.state_dict(), save_path+save_model_name+'_Global'+'_epoch_'+str(epoch)+'.pkl')
print('Global_Branch_model already save!')
torch.save(Local_Branch_model.state_dict(), save_path+save_model_name+'_Local'+'_epoch_'+str(epoch)+'.pkl')
print('Local_Branch_model already save!')
torch.save(Fusion_Branch_model.state_dict(), save_path+save_model_name+'_Fusion'+'_epoch_'+str(epoch)+'.pkl')
print('Fusion_Branch_model already save!')
time_elapsed = time.time() - since
print('Training one epoch complete in {:.0f}m {:.0f}s'.format(time_elapsed // 60 , time_elapsed % 60))
def test(model_global, model_local, model_fusion, test_loader):
# initialize the ground truth and output tensor
gt = torch.FloatTensor().cuda()
pred_global = torch.FloatTensor().cuda()
pred_local = torch.FloatTensor().cuda()
pred_fusion = torch.FloatTensor().cuda()
# switch to evaluate mode
model_global.eval()
model_local.eval()
model_fusion.eval()
cudnn.benchmark = True
for i, (inp, target) in enumerate(test_loader):
with torch.no_grad():
if i % 2000 == 0:
print('testing process:',i)
target = target.cuda()
gt = torch.cat((gt, target), 0)
input_var = torch.autograd.Variable(inp.cuda())
#output = model_global(input_var)
output_global, fm_global, pool_global = model_global(input_var)
patchs_var = Attention_gen_patchs(inp,fm_global)
output_local, _, pool_local = model_local(patchs_var)
output_fusion = model_fusion(pool_global,pool_local)
pred_global = torch.cat((pred_global, output_global.data), 0)
pred_local = torch.cat((pred_local, output_local.data), 0)
pred_fusion = torch.cat((pred_fusion, output_fusion.data), 0)
AUROCs_g = compute_AUCs(gt, pred_global)
AUROC_avg = np.array(AUROCs_g).mean()
print('Global branch: The average AUROC is {AUROC_avg:.3f}'.format(AUROC_avg=AUROC_avg))
for i in range(N_CLASSES):
print('The AUROC of {} is {}'.format(CLASS_NAMES[i], AUROCs_g[i]))
AUROCs_l = compute_AUCs(gt, pred_local)
AUROC_avg = np.array(AUROCs_l).mean()
print('\n')
print('Local branch: The average AUROC is {AUROC_avg:.3f}'.format(AUROC_avg=AUROC_avg))
for i in range(N_CLASSES):
print('The AUROC of {} is {}'.format(CLASS_NAMES[i], AUROCs_l[i]))
AUROCs_f = compute_AUCs(gt, pred_fusion)
AUROC_avg = np.array(AUROCs_f).mean()
print('\n')
print('Fusion branch: The average AUROC is {AUROC_avg:.3f}'.format(AUROC_avg=AUROC_avg))
for i in range(N_CLASSES):
print('The AUROC of {} is {}'.format(CLASS_NAMES[i], AUROCs_f[i]))
def compute_AUCs(gt, pred):
"""Computes Area Under the Curve (AUC) from prediction scores.
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:
List of AUROCs of all classes.
"""
AUROCs = []
gt_np = gt.cpu().numpy()
pred_np = pred.cpu().numpy()
for i in range(N_CLASSES):
AUROCs.append(roc_auc_score(gt_np[:, i], pred_np[:, i]))
return AUROCs
if __name__ == '__main__':
main()