-
Notifications
You must be signed in to change notification settings - Fork 11
/
train_target.py
240 lines (196 loc) · 10.1 KB
/
train_target.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
#!/usr/bin/env python
import argparse
import os
import os.path as osp
import torch.nn.functional as F
# import matplotlib
# matplotlib.use('TkAgg')
# import matplotlib.pyplot as plt
import torch
from torch.autograd import Variable
import tqdm
from dataloaders import fundus_dataloader as DL
from torch.utils.data import DataLoader
from dataloaders import custom_transforms as tr
from torchvision import transforms
# from scipy.misc import imsave
from matplotlib.pyplot import imsave
from utils.Utils import *
from utils.metrics import *
from datetime import datetime
import pytz
import networks.deeplabv3 as netd
import networks.deeplabv3_eval as netd_eval
import cv2
import torch.backends.cudnn as cudnn
import random
from tensorboardX import SummaryWriter
bceloss = torch.nn.BCELoss(reduction='none')
seed = 3377
savefig = False
get_hd = True
model_save = True
if True:
cudnn.benchmark = False
cudnn.deterministic = True
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--model-file', type=str, default='./logs/source/source_model.pth.tar')
parser.add_argument('--dataset', type=str, default='Domain2')
parser.add_argument('--source', type=str, default='Domain3')
parser.add_argument('-g', '--gpu', type=int, default=0)
parser.add_argument('--data-dir', default='../../../../Data/Fundus/')
parser.add_argument('--out-stride',type=int,default=16)
parser.add_argument('--sync-bn',type=bool,default=True)
parser.add_argument('--freeze-bn',type=bool,default=False)
args = parser.parse_args()
os.environ['CUDA_VISIBLE_DEVICES'] = str(args.gpu)
model_file = args.model_file
# 1. dataset
composed_transforms_train = transforms.Compose([
tr.Resize(512),
tr.add_salt_pepper_noise(),
tr.adjust_light(),
tr.eraser(),
tr.Normalize_tf(),
tr.ToTensor()
])
composed_transforms_test = transforms.Compose([
tr.Resize(512),
tr.Normalize_tf(),
tr.ToTensor()
])
db_train = DL.FundusSegmentation(base_dir=args.data_dir, dataset=args.dataset, split='train/ROIs', transform=composed_transforms_train)
db_test = DL.FundusSegmentation(base_dir=args.data_dir, dataset=args.dataset, split='test/ROIs', transform=composed_transforms_test)
db_source = DL.FundusSegmentation(base_dir=args.data_dir, dataset=args.source, split='train/ROIs', transform=composed_transforms_test)
train_loader = DataLoader(db_train, batch_size=8, shuffle=False, num_workers=1)
test_loader = DataLoader(db_test, batch_size=1, shuffle=False, num_workers=1)
source_loader = DataLoader(db_source, batch_size=1, shuffle=False, num_workers=1)
# 2. model
model = netd.DeepLab(num_classes=2, backbone='mobilenet', output_stride=args.out_stride, sync_bn=args.sync_bn, freeze_bn=args.freeze_bn)
model_eval = netd_eval.DeepLab(num_classes=2, backbone='mobilenet', output_stride=args.out_stride, sync_bn=args.sync_bn, freeze_bn=args.freeze_bn).cuda()
if torch.cuda.is_available():
model = model.cuda()
print('==> Loading %s model file: %s' %
(model.__class__.__name__, model_file))
checkpoint = torch.load(model_file)
model.load_state_dict(checkpoint['model_state_dict'])
model.train()
if args.dataset=="Domain2":
npfilename = './generate_pseudo/pseudolabel_D2.npz'
elif args.dataset=="Domain1":
npfilename = './generate_pseudo/pseudolabel_D1.npz'
npdata = np.load(npfilename, allow_pickle=True)
pseudo_label_dic = npdata['arr_0'].item()
uncertain_dic = npdata['arr_1'].item()
proto_pseudo_dic = npdata['arr_2'].item()
var_list = model.named_parameters()
optim_gen = torch.optim.Adam(model.parameters(), lr=0.002, betas=(0.9, 0.99))
best_val_cup_dice = 0.0;
best_val_disc_dice = 0.0;
best_avg = 0.0
iter_num = 0
for epoch_num in tqdm.tqdm(range(2), ncols=70):
model.train()
for batch_idx, (sample) in enumerate(train_loader):
data, target, img_name = sample['image'], sample['map'], sample['img_name']
if torch.cuda.is_available():
data, target = data.cuda(), target.cuda()
data, target = Variable(data), Variable(target)
prediction, _, feature = model(data)
prediction = torch.sigmoid(prediction)
pseudo_label = [pseudo_label_dic.get(key) for key in img_name]
uncertain_map = [uncertain_dic.get(key) for key in img_name]
proto_pseudo = [proto_pseudo_dic.get(key) for key in img_name]
pseudo_label = torch.from_numpy(np.asarray(pseudo_label)).float().cuda()
uncertain_map = torch.from_numpy(np.asarray(uncertain_map)).float().cuda()
proto_pseudo = torch.from_numpy(np.asarray(proto_pseudo)).float().cuda()
for param in model.parameters():
param.requires_grad = True
optim_gen.zero_grad()
target_0_obj = F.interpolate(pseudo_label[:,0:1,...], size=feature.size()[2:], mode='nearest')
target_1_obj = F.interpolate(pseudo_label[:, 1:, ...], size=feature.size()[2:], mode='nearest')
target_0_bck = 1.0 - target_0_obj;target_1_bck = 1.0 - target_1_obj
mask_0_obj = torch.zeros([pseudo_label.shape[0], 1, pseudo_label.shape[2], pseudo_label.shape[3]]).cuda()
mask_0_bck = torch.zeros([pseudo_label.shape[0], 1, pseudo_label.shape[2], pseudo_label.shape[3]]).cuda()
mask_1_obj = torch.zeros([pseudo_label.shape[0], 1, pseudo_label.shape[2], pseudo_label.shape[3]]).cuda()
mask_1_bck = torch.zeros([pseudo_label.shape[0], 1, pseudo_label.shape[2], pseudo_label.shape[3]]).cuda()
mask_0_obj[uncertain_map[:, 0:1, ...] < 0.05] = 1.0
mask_0_bck[uncertain_map[:, 0:1, ...] < 0.05] = 1.0
mask_1_obj[uncertain_map[:, 1:, ...] < 0.05] = 1.0
mask_1_bck[uncertain_map[:, 1:, ...] < 0.05] = 1.0
mask = torch.cat((mask_0_obj*pseudo_label[:,0:1,...] + mask_0_bck*(1.0-pseudo_label[:,0:1,...]), mask_1_obj*pseudo_label[:,1:,...] + mask_1_bck*(1.0-pseudo_label[:,1:,...])), dim=1)
mask_proto = torch.zeros([data.shape[0], 2, data.shape[2], data.shape[3]]).cuda()
mask_proto[pseudo_label==proto_pseudo] = 1.0
mask = mask*mask_proto
loss_seg_pixel = bceloss(prediction, pseudo_label)
loss_seg = torch.sum(mask * loss_seg_pixel) / torch.sum(mask)
loss_seg.backward()
optim_gen.step()
iter_num = iter_num + 1
#test
model_eval.train()
pretrained_dict = model.state_dict()
model_dict = model_eval.state_dict()
pretrained_dict = {k: v for k, v in pretrained_dict.items() if k in model_dict}
model_eval.load_state_dict(pretrained_dict)
val_cup_dice = 0.0;val_disc_dice = 0.0;datanum_cnt = 0.0
cup_hd = 0.0; disc_hd = 0.0;datanum_cnt_cup = 0.0;datanum_cnt_disc = 0.0
with torch.no_grad():
for batch_idx, (sample) in enumerate(test_loader):
data, target, img_name = sample['image'], sample['map'], sample['img_name']
if torch.cuda.is_available():
data, target = data.cuda(), target.cuda()
data, target = Variable(data), Variable(target)
prediction, boundary, _ = model_eval(data)
prediction = torch.sigmoid(prediction)
target_numpy = target.data.cpu()
prediction = prediction.data.cpu()
prediction[prediction>0.75] = 1;prediction[prediction <= 0.75] = 0
cup_dice = dice_coefficient_numpy(prediction[:,0, ...], target_numpy[:, 0, ...])
disc_dice = dice_coefficient_numpy(prediction[:,1, ...], target_numpy[:, 1, ...])
for i in range(prediction.shape[0]):
hd_tmp = hd_numpy(prediction[i, 0, ...], target_numpy[i, 0, ...], get_hd)
if np.isnan(hd_tmp):
datanum_cnt_cup -= 1.0
else:
cup_hd += hd_tmp
hd_tmp = hd_numpy(prediction[i, 1, ...], target_numpy[i, 1, ...], get_hd)
if np.isnan(hd_tmp):
datanum_cnt_disc -= 1.0
else:
disc_hd += hd_tmp
val_cup_dice += np.sum(cup_dice)
val_disc_dice += np.sum(disc_dice)
datanum_cnt += float(prediction.shape[0])
datanum_cnt_cup += float(prediction.shape[0])
datanum_cnt_disc += float(prediction.shape[0])
val_cup_dice /= datanum_cnt
val_disc_dice /= datanum_cnt
cup_hd /= datanum_cnt_cup
disc_hd /= datanum_cnt_disc
if (val_cup_dice+val_disc_dice)/2.0>best_avg:
best_val_cup_dice = val_cup_dice; best_val_disc_dice = val_disc_dice; best_avg = (val_cup_dice+val_disc_dice)/2.0
best_cup_hd = cup_hd; best_disc_hd = disc_hd; best_avg_hd = (best_cup_hd+best_disc_hd)/2.0
if not os.path.exists('./logs/train_target'):
os.mkdir('./logs/train_target')
if args.dataset == 'Domain1':
savefile = './logs/train_target/' + 'D1_' + 'checkpoint_%d.pth.tar' % epoch_num
elif args.dataset == 'Domain2':
savefile = './logs/train_target/' + 'D2_' + 'checkpoint_%d.pth.tar' % epoch_num
if model_save:
torch.save({
'model_state_dict': model.state_dict(),
'best_mean_dice': best_avg,
'best_cup_dice': best_val_cup_dice,
'best_disc_dice': best_val_disc_dice,
}, savefile)
print("cup: %.4f disc: %.4f avg: %.4f cup: %.4f disc: %.4f avg: %.4f" %
(val_cup_dice, val_disc_dice, (val_cup_dice+val_disc_dice)/2.0, cup_hd, disc_hd, (cup_hd+disc_hd)/2.0))
print("best cup: %.4f best disc: %.4f best avg: %.4f best cup: %.4f best disc: %.4f best avg: %.4f" %
(best_val_cup_dice, best_val_disc_dice, best_avg, best_cup_hd, best_disc_hd, best_avg_hd))
model.train()