forked from emma-sjwang/BEAL
-
Notifications
You must be signed in to change notification settings - Fork 1
/
train.py
executable file
·235 lines (206 loc) · 7.84 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
from datetime import datetime
import os
import os.path as osp
# PyTorch includes
import torch
from torchvision import transforms
from torch.utils.data import DataLoader
import argparse
import yaml
from train_process import Trainer
# Custom includes
from dataloaders import fundus_dataloader as DL
from dataloaders import custom_transforms as tr
from networks.deeplabv3 import *
from networks.GAN import BoundaryDiscriminator, UncertaintyDiscriminator
here = osp.dirname(osp.abspath(__file__))
def main():
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument('-g', '--gpu', type=int, default=0, help='gpu id')
parser.add_argument('--resume', default=None, help='checkpoint path')
# configurations (same configuration as original work)
# https://github.com/shelhamer/fcn.berkeleyvision.org
parser.add_argument(
'--datasetS', type=str, default='refuge', help='test folder id contain images ROIs to test'
)
parser.add_argument(
'--datasetT', type=str, default='Drishti-GS', help='refuge / Drishti-GS/ RIM-ONE_r3'
)
parser.add_argument(
'--batch-size', type=int, default=8, help='batch size for training the model'
)
parser.add_argument(
'--group-num', type=int, default=1, help='group number for group normalization'
)
parser.add_argument(
'--max-epoch', type=int, default=200, help='max epoch'
)
parser.add_argument(
'--stop-epoch', type=int, default=200, help='stop epoch'
)
parser.add_argument(
'--warmup-epoch', type=int, default=-1, help='warmup epoch begin train GAN'
)
parser.add_argument(
'--interval-validate', type=int, default=10, help='interval epoch number to valide the model'
)
parser.add_argument(
'--lr-gen', type=float, default=1e-3, help='learning rate',
)
parser.add_argument(
'--lr-dis', type=float, default=2.5e-5, help='learning rate',
)
parser.add_argument(
'--lr-decrease-rate', type=float, default=0.1, help='ratio multiplied to initial lr',
)
parser.add_argument(
'--weight-decay', type=float, default=0.0005, help='weight decay',
)
parser.add_argument(
'--momentum', type=float, default=0.99, help='momentum',
)
parser.add_argument(
'--data-dir',
default='/home/sjwang/ssd1T/fundus/domain_adaptation/',
help='data root path'
)
parser.add_argument(
'--pretrained-model',
default='../../../models/pytorch/fcn16s_from_caffe.pth',
help='pretrained model of FCN16s',
)
parser.add_argument(
'--out-stride',
type=int,
default=16,
help='out-stride of deeplabv3+',
)
parser.add_argument(
'--sync-bn',
type=bool,
default=True,
help='sync-bn in deeplabv3+',
)
parser.add_argument(
'--freeze-bn',
type=bool,
default=False,
help='freeze batch normalization of deeplabv3+',
)
args = parser.parse_args()
args.model = 'FCN8s'
now = datetime.now()
args.out = osp.join(here, 'logs', args.datasetT, now.strftime('%Y%m%d_%H%M%S.%f'))
os.makedirs(args.out)
with open(osp.join(args.out, 'config.yaml'), 'w') as f:
yaml.safe_dump(args.__dict__, f, default_flow_style=False)
os.environ['CUDA_VISIBLE_DEVICES'] = str(args.gpu)
cuda = torch.cuda.is_available()
torch.manual_seed(1337)
if cuda:
torch.cuda.manual_seed(1337)
# 1. dataset
composed_transforms_tr = transforms.Compose([
tr.RandomScaleCrop(512),
tr.RandomRotate(),
tr.RandomFlip(),
tr.elastic_transform(),
tr.add_salt_pepper_noise(),
tr.adjust_light(),
tr.eraser(),
tr.Normalize_tf(),
tr.ToTensor()
])
composed_transforms_ts = transforms.Compose([
tr.RandomCrop(512),
tr.Normalize_tf(),
tr.ToTensor()
])
domain = DL.FundusSegmentation(base_dir=args.data_dir, dataset=args.datasetS, split='train',
transform=composed_transforms_tr)
domain_loaderS = DataLoader(domain, batch_size=args.batch_size, shuffle=True, num_workers=2, pin_memory=True)
domain_T = DL.FundusSegmentation(base_dir=args.data_dir, dataset=args.datasetT, split='train',
transform=composed_transforms_tr)
domain_loaderT = DataLoader(domain_T, batch_size=args.batch_size, shuffle=False, num_workers=2, pin_memory=True)
domain_val = DL.FundusSegmentation(base_dir=args.data_dir, dataset=args.datasetT, split='train',
transform=composed_transforms_ts)
domain_loader_val = DataLoader(domain_val, batch_size=args.batch_size, shuffle=False, num_workers=2, pin_memory=True)
# 2. model
model_gen = DeepLab(num_classes=2, backbone='mobilenet', output_stride=args.out_stride,
sync_bn=args.sync_bn, freeze_bn=args.freeze_bn).cuda()
model_dis = BoundaryDiscriminator().cuda()
model_dis2 = UncertaintyDiscriminator().cuda()
start_epoch = 0
start_iteration = 0
# 3. optimizer
optim_gen = torch.optim.Adam(
model_gen.parameters(),
lr=args.lr_gen,
betas=(0.9, 0.99)
)
optim_dis = torch.optim.SGD(
model_dis.parameters(),
lr=args.lr_dis,
momentum=args.momentum,
weight_decay=args.weight_decay
)
optim_dis2 = torch.optim.SGD(
model_dis2.parameters(),
lr=args.lr_dis,
momentum=args.momentum,
weight_decay=args.weight_decay
)
if args.resume:
checkpoint = torch.load(args.resume)
pretrained_dict = checkpoint['model_state_dict']
model_dict = model_gen.state_dict()
# 1. filter out unnecessary keys
pretrained_dict = {k: v for k, v in pretrained_dict.items() if k in model_dict}
# 2. overwrite entries in the existing state dict
model_dict.update(pretrained_dict)
# 3. load the new state dict
model_gen.load_state_dict(model_dict)
pretrained_dict = checkpoint['model_dis_state_dict']
model_dict = model_dis.state_dict()
pretrained_dict = {k: v for k, v in pretrained_dict.items() if k in model_dict}
model_dict.update(pretrained_dict)
model_dis.load_state_dict(model_dict)
pretrained_dict = checkpoint['model_dis2_state_dict']
model_dict = model_dis2.state_dict()
pretrained_dict = {k: v for k, v in pretrained_dict.items() if k in model_dict}
model_dict.update(pretrained_dict)
model_dis2.load_state_dict(model_dict)
start_epoch = checkpoint['epoch'] + 1
start_iteration = checkpoint['iteration'] + 1
optim_gen.load_state_dict(checkpoint['optim_state_dict'])
optim_dis.load_state_dict(checkpoint['optim_dis_state_dict'])
optim_dis2.load_state_dict(checkpoint['optim_dis2_state_dict'])
optim_adv.load_state_dict(checkpoint['optim_adv_state_dict'])
trainer = Trainer.Trainer(
cuda=cuda,
model_gen=model_gen,
model_dis=model_dis,
model_uncertainty_dis=model_dis2,
optimizer_gen=optim_gen,
optimizer_dis=optim_dis,
optimizer_uncertainty_dis=optim_dis2,
lr_gen=args.lr_gen,
lr_dis=args.lr_dis,
lr_decrease_rate=args.lr_decrease_rate,
val_loader=domain_loader_val,
domain_loaderS=domain_loaderS,
domain_loaderT=domain_loaderT,
out=args.out,
max_epoch=args.max_epoch,
stop_epoch=args.stop_epoch,
interval_validate=args.interval_validate,
batch_size=args.batch_size,
warmup_epoch=args.warmup_epoch,
)
trainer.epoch = start_epoch
trainer.iteration = start_iteration
trainer.train()
if __name__ == '__main__':
main()