-
Notifications
You must be signed in to change notification settings - Fork 22
/
train_unet.py
218 lines (174 loc) · 7.13 KB
/
train_unet.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
import argparse
import os
import shutil
import time
import logging
import random
import torch
import torch.nn.functional as F
import torch.backends.cudnn as cudnn
import torch.optim
from torch.utils.data import DataLoader
cudnn.benchmark = True
import numpy as np
import models
from models import criterions
from data import datasets
from data.sampler import CycleSampler
from data.data_utils import add_mask, init_fn
from utils import Parser
from predict_unet import validate, AverageMeter
parser = argparse.ArgumentParser()
#parser.add_argument('-cfg', '--cfg', default='unet_all', type=str)
#parser.add_argument('-cfg', '--cfg', default='unet_dice2_c25', type=str)
#parser.add_argument('-cfg', '--cfg', default='unet_dice2_redo', type=str)
parser.add_argument('-cfg', '--cfg', default='unet_ce_hard', type=str)
#parser.add_argument('-cfg', '--cfg', default='unet_bce_hard_per_im', type=str)
#parser.add_argument('-cfg', '--cfg', default='unet_bce_mean', type=str)
parser.add_argument('-gpu', '--gpu', default='1', type=str)
parser.add_argument('-out', '--out', default='', type=str)
path = os.path.dirname(__file__)
## parse arguments
args = parser.parse_args()
args = Parser(args.cfg, log='train').add_args(args)
args.gpu = str(args.gpu)
ckpts = args.makedir()
resume = os.path.join(ckpts, 'model_last.tar')
#resume = ''
if not args.resume and os.path.exists(resume):
args.resume = resume
def main():
# setup environments and seeds
os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu
torch.manual_seed(args.seed)
torch.cuda.manual_seed(args.seed)
random.seed(args.seed)
np.random.seed(args.seed)
# setup networks
Network = getattr(models, args.net)
model = Network(**args.net_params)
model = model.cuda()
optimizer = getattr(torch.optim, args.opt)(
model.parameters(), **args.opt_params)
criterion = getattr(criterions, args.criterion)
msg = ''
# optionally resume from a checkpoint
if args.resume:
if os.path.isfile(args.resume):
print("=> loading checkpoint '{}'".format(args.resume))
checkpoint = torch.load(args.resume)
args.start_iter = checkpoint['iter']
model.load_state_dict(checkpoint['state_dict'])
optimizer.load_state_dict(checkpoint['optim_dict'])
msg = ("=> loaded checkpoint '{}' (iter {})"
.format(args.resume, checkpoint['iter']))
else:
msg = "=> no checkpoint found at '{}'".format(args.resume)
else:
msg = '-------------- New training session ----------------'
msg += '\n' + str(args)
logging.info(msg)
# Data loading code
Dataset = getattr(datasets, args.dataset)
train_list = os.path.join(args.data_dir, args.train_list)
train_set = Dataset(train_list, root=args.data_dir, for_train=True,
transforms=args.train_transforms)
num_iters = args.num_iters or (len(train_set) * args.num_epochs) // args.batch_size
num_iters -= args.start_iter
train_sampler = CycleSampler(len(train_set), num_iters*args.batch_size)
train_loader = DataLoader(
train_set,
batch_size=args.batch_size,
collate_fn=train_set.collate, sampler=train_sampler,
num_workers=args.workers, pin_memory=True, worker_init_fn=init_fn)
if args.valid_list:
valid_list = os.path.join(args.data_dir, args.valid_list)
valid_set = Dataset(valid_list, root=args.data_dir,
for_train=False, transforms=args.test_transforms)
valid_loader = DataLoader(
valid_set, batch_size=1, shuffle=False,
collate_fn=valid_set.collate,
num_workers=4, pin_memory=True)
train_valid_set = Dataset(train_list, root=args.data_dir,
for_train=False, transforms=args.test_transforms)
train_valid_loader = DataLoader(
train_valid_set, batch_size=1, shuffle=False,
collate_fn=train_valid_set.collate,
num_workers=4, pin_memory=True)
start = time.time()
enum_batches = len(train_set)/float(args.batch_size)
args.schedule = {int(k*enum_batches): v for k, v in args.schedule.items()}
args.save_freq = int(enum_batches * args.save_freq)
args.valid_freq = int(enum_batches * args.valid_freq)
losses = AverageMeter()
torch.set_grad_enabled(True)
for i, data in enumerate(train_loader, args.start_iter):
## validation
#if args.valid_list and (i % args.valid_freq) == 0:
# logging.info('-'*50)
# msg = 'Iter {}, Epoch {:.4f}, {}'.format(i, i/enum_batches, 'validation')
# logging.info(msg)
# with torch.no_grad():
# validate(valid_loader, model, names=valid_set.names)
# actual training
adjust_learning_rate(optimizer, i)
data = [t.cuda(non_blocking=True) for t in data]
x, target = data[:2]
if len(data) > 2: # has mask
x = add_mask(x, data.pop(), 1)
logging.info(x.shape)
logging.info(torch.max(x))
logging.info(torch.min(x))
logging.info(target.shape)
logging.info(torch.max(target))
logging.info(torch.min(target))
# compute output
output = model(x) # nx5x128x128x128, target nx128x128x128
loss = criterion(output, target, args.alpha)
# measure accuracy and record loss
losses.update(loss.item(), target.numel())
# compute gradient and do SGD step
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (i+1) % args.save_freq == 0:
epoch = int((i+1) // enum_batches)
file_name = os.path.join(ckpts, 'model_epoch_{}.tar'.format(epoch))
torch.save({
'iter': i+1,
'state_dict': model.state_dict(),
'optim_dict': optimizer.state_dict(),
},
file_name)
msg = 'Iter {0:}, Epoch {1:.4f}, Loss {2:.4f}'.format(
i+1, (i+1)/enum_batches, losses.avg)
logging.info(msg)
losses.reset()
i = num_iters + args.start_iter
file_name = os.path.join(ckpts, 'model_last.tar')
torch.save({
'iter': i,
'state_dict': model.state_dict(),
'optim_dict': optimizer.state_dict(),
},
file_name)
if args.valid_list:
logging.info('-'*50)
msg = 'Iter {}, Epoch {:.4f}, {}'.format(i, i/enum_batches, 'validate validation data')
logging.info(msg)
with torch.no_grad():
validate(valid_loader, model, names=valid_set.names, out_dir=args.out)
#logging.info('-'*50)
#msg = 'Iter {}, Epoch {:.4f}, {}'.format(i, i/enum_batches, 'validate training data')
#logging.info(msg)
#with torch.no_grad():
# validate(train_valid_loader, model, names=train_valid_set.names, verbose=False)
msg = 'total time: {:.4f} minutes'.format((time.time() - start)/60)
logging.info(msg)
def adjust_learning_rate(optimizer, epoch):
# reduce learning rate by a factor of 10
if epoch+1 in args.schedule:
for param_group in optimizer.param_groups:
param_group['lr'] *= 0.1
if __name__ == '__main__':
main()