-
Notifications
You must be signed in to change notification settings - Fork 24
/
train_erfnet_vanilla.py
307 lines (246 loc) · 12.2 KB
/
train_erfnet_vanilla.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
import os
import time
import shutil
import torch
import torchvision
import torch.nn.parallel
import torch.backends.cudnn as cudnn
import torch.optim
import cv2
import utils.transforms_train as tf
import numpy as np
import models
from models import sync_bn
import dataset as ds
from options.options import parser
best_mIoU = 0
def main():
global args, best_mIoU
args = parser.parse_args()
os.environ["CUDA_VISIBLE_DEVICES"] = ','.join(str(gpu) for gpu in args.gpus)
args.gpus = len(args.gpus)
if args.no_partialbn:
sync_bn.Synchronize.init(args.gpus)
if args.dataset == 'VOCAug' or args.dataset == 'VOC2012' or args.dataset == 'COCO':
num_class = 21
ignore_label = 255
scale_series = [10, 20, 30, 60]
elif args.dataset == 'Cityscapes':
num_class = 19
ignore_label = 255 # 0
scale_series = [15, 30, 45, 90]
elif args.dataset == 'ApolloScape':
num_class = 37 # merge the noise and ignore labels
ignore_label = 255 # 0
else:
raise ValueError('Unknown dataset ' + args.dataset)
model = models.ERFNet(num_class, partial_bn=not args.no_partialbn) # models.PSPNet(num_class, base_model=args.arch, dropout=args.dropout, partial_bn=not args.no_partialbn)
input_mean = model.input_mean
input_std = model.input_std
# policies = model.get_optim_policies()
model = torch.nn.DataParallel(model, device_ids=range(args.gpus)).cuda()
def load_my_state_dict(model, state_dict): #custom function to load model when not all dict elements
own_state = model.state_dict()
# print(own_state.keys())
ckpt_name = []
cnt = 0
for name, param in state_dict.items():
if name.replace('module.features', 'module') not in list(own_state.keys()):
ckpt_name.append(name)
continue
own_state[name.replace('module.features', 'module')].copy_(param)
# print(cnt)
cnt += 1
print('#reused param: {}'.format(cnt))
# print(ckpt_name)
return model
if args.resume:
if os.path.isfile(args.resume):
print(("=> loading checkpoint '{}'".format(args.resume)))
checkpoint = torch.load(args.resume)
args.start_epoch = checkpoint['epoch']
# weightspath = args.resume
# best_mIoU = checkpoint['best_mIoU']
# model = load_my_state_dict(model, checkpoint['state_dict'])
torch.nn.Module.load_state_dict(model, checkpoint['state_dict'])
print(("=> loaded checkpoint '{}' (epoch {})".format(args.evaluate, checkpoint['epoch'])))
else:
print(("=> no checkpoint found at '{}'".format(args.resume)))
cudnn.benchmark = True
cudnn.fastest = True
# Data loading code
train_loader = torch.utils.data.DataLoader(
getattr(ds, args.dataset.replace("ApolloScape", "VOCAug") + 'DataSet_train')(data_list=args.train_list, transform=torchvision.transforms.Compose([
tf.GroupRandomScale(size=(0.5, 0.5), interpolation=(cv2.INTER_LINEAR, cv2.INTER_NEAREST)),
# tf.GroupRandomScaleRatio(size=(args.train_size, args.train_size + 20, int(args.train_size * 1 / 3), int(args.train_size * 1 / 3) + 20), interpolation=(cv2.INTER_LINEAR, cv2.INTER_NEAREST)),
# tf.GroupRandomRotation(degree=(-10, 10), interpolation=(cv2.INTER_LINEAR, cv2.INTER_NEAREST), padding=(input_mean, (ignore_label, ))),
tf.GroupRandomCropRatio(size=(args.train_size, int(args.train_size * 1 / 3))),
tf.GroupRandomRotation(degree=(-10, 10), interpolation=(cv2.INTER_LINEAR, cv2.INTER_NEAREST), padding=(input_mean, (ignore_label, ))),
tf.GroupNormalize(mean=(input_mean, (0, )), std=(input_std, (1, ))),
])), batch_size=args.batch_size, shuffle=True, num_workers=args.workers, pin_memory=False, drop_last=True) # pin_memory=True
val_loader = torch.utils.data.DataLoader(
getattr(ds, args.dataset.replace("ApolloScape", "VOCAug") + 'DataSet_train')(data_list=args.val_list, transform=torchvision.transforms.Compose([
tf.GroupRandomScale(size=(0.5, 0.5), interpolation=(cv2.INTER_LINEAR, cv2.INTER_NEAREST)),
# tf.GroupRandomScaleRatio(size=(args.test_size, args.test_size, int(args.test_size * 1 / 3), int(args.test_size * 1 / 3)), interpolation=(cv2.INTER_LINEAR, cv2.INTER_NEAREST)),
tf.GroupRandomCropRatio(size=(args.train_size, int(args.train_size * 1 / 3))),
tf.GroupNormalize(mean=(input_mean, (0, )), std=(input_std, (1, ))),
])), batch_size=args.batch_size, shuffle=False, num_workers=args.workers, pin_memory=False) # pin_memory=True
# define loss function (criterion) optimizer and evaluator
weights = [1.0 for _ in range(37)]
weights[0] = 0.05
weights[36] = 0.05
class_weights = torch.FloatTensor(weights).cuda()
criterion = torch.nn.NLLLoss(ignore_index=ignore_label, weight=class_weights).cuda()
'''for group in policies:
print(('group: {} has {} params, lr_mult: {}, decay_mult: {}'.format(group['name'], len(group['params']), group['lr_mult'], group['decay_mult'])))'''
optimizer = torch.optim.SGD(model.parameters(), args.lr, momentum=args.momentum, weight_decay=args.weight_decay)
evaluator = EvalSegmentation(num_class, ignore_label)
if args.evaluate:
validate(val_loader, model, criterion, 0, evaluator)
return
for epoch in range(args.epochs): # args.start_epoch
adjust_learning_rate(optimizer, epoch, args.lr_steps)
# train for one epoch
train(train_loader, model, criterion, optimizer, epoch)
# evaluate on validation set
if (epoch + 1) % args.eval_freq == 0 or epoch == args.epochs - 1:
mIoU = validate(val_loader, model, criterion, (epoch + 1) * len(train_loader), evaluator)
# remember best mIoU and save checkpoint
is_best = mIoU > best_mIoU
best_mIoU = max(mIoU, best_mIoU)
save_checkpoint({
'epoch': epoch + 1,
'arch': args.arch,
'state_dict': model.state_dict(),
'best_mIoU': best_mIoU,
}, is_best)
def train(train_loader, model, criterion, optimizer, epoch):
batch_time = AverageMeter()
data_time = AverageMeter()
losses = AverageMeter()
if args.no_partialbn:
model.module.partialBN(False)
sync_bn.convert_bn(model)
else:
model.module.partialBN(True)
# switch to train mode
model.train()
end = time.time()
for i, (input, target) in enumerate(train_loader):
# measure data loading time
data_time.update(time.time() - end)
# print(np.unique(target.numpy()))
target = target.cuda(async=True)
input_var = torch.autograd.Variable(input)
target_var = torch.autograd.Variable(target)
# compute output
output = model(input_var) # output_mid
loss = criterion(torch.nn.functional.log_softmax(output, dim=1), target_var)
# loss_mid = criterion(torch.nn.functional.log_softmax(output_mid, dim=1), target_var)
loss_tot = loss # + loss_mid * 0.4
# measure accuracy and record loss
losses.update(loss.data[0], input.size(0))
# compute gradient and do SGD step
optimizer.zero_grad()
loss_tot.backward()
optimizer.step()
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
if (i + 1) % args.print_freq == 0:
print(('Epoch: [{0}][{1}/{2}], lr: {lr:.5f}\t' 'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t' 'Data {data_time.val:.3f} ({data_time.avg:.3f})\t' 'Loss {loss.val:.4f} ({loss.avg:.4f})\t'.format(epoch, i, len(train_loader), batch_time=batch_time, data_time=data_time, loss=losses, lr=optimizer.param_groups[-1]['lr'])))
batch_time.reset()
data_time.reset()
losses.reset()
def flip(x, dim):
xsize = x.size()
dim = x.dim() + dim if dim < 0 else dim
x = x.view(-1, *xsize[dim:])
x = x.view(x.size(0), x.size(1), -1)[:, getattr(torch.arange(x.size(1) - 1, -1, -1), ('cpu', 'cuda')[x.is_cuda])().long(), :]
return x.view(xsize)
def validate(val_loader, model, criterion, iter, evaluator, logger=None):
batch_time = AverageMeter()
losses = AverageMeter()
IoU = AverageMeter()
# switch to evaluate mode
model.eval()
end = time.time()
for i, (input, target) in enumerate(val_loader):
target = target.cuda(async=True)
input_var = torch.autograd.Variable(input, volatile=True)
target_var = torch.autograd.Variable(target)
# compute output
output = model(input_var) # [0]
# compute output flip
# output_flip = flip(model(flip(input_var, -1)), -1)
# output = (output + output_flip) / 2.0
loss = criterion(torch.nn.functional.log_softmax(output, dim=1), target_var)
# measure accuracy and record loss
pred = output.data.cpu().numpy().transpose(0, 2, 3, 1)
pred = np.argmax(pred, axis=3).astype(np.uint8)
IoU.update(evaluator(pred, target.cpu().numpy()))
losses.update(loss.data[0], input.size(0))
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
if (i + 1) % args.print_freq == 0:
acc = np.sum(np.diag(IoU.sum)) / float(np.sum(IoU.sum))
mIoU = np.diag(IoU.sum) / (1e-20 + IoU.sum.sum(1) + IoU.sum.sum(0) - np.diag(IoU.sum))
mIoU = np.sum(mIoU) / len(mIoU)
print(('Test: [{0}/{1}]\t' 'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t' 'Loss {loss.val:.4f} ({loss.avg:.4f})\t' 'Pixels Acc {acc:.3f}\t' 'mIoU {mIoU:.3f}'.format(i, len(val_loader), batch_time=batch_time, loss=losses, acc=acc, mIoU=mIoU)))
acc = np.sum(np.diag(IoU.sum)) / float(np.sum(IoU.sum))
mIoU = np.diag(IoU.sum) / (1e-20 + IoU.sum.sum(1) + IoU.sum.sum(0) - np.diag(IoU.sum))
mIoU = np.sum(mIoU) / len(mIoU)
print(('Testing Results: Pixels Acc {acc:.3f}\tmIoU {mIoU:.3f} ({bestmIoU:.4f})\tLoss {loss.avg:.5f}'.format(acc=acc, mIoU=mIoU, bestmIoU=max(mIoU, best_mIoU), loss=losses)))
return mIoU
def save_checkpoint(state, is_best, filename='checkpoint.pth.tar'):
if not os.path.exists('outputs_erfnet_new'):
os.makedirs('outputs_erfnet_new')
filename = os.path.join('outputs_erfnet_new', '_'.join((args.snapshot_pref, args.method.lower(), filename)))
torch.save(state, filename)
if is_best:
best_name = os.path.join('outputs_erfnet_new', '_'.join((args.snapshot_pref, args.method.lower(), 'model_best.pth.tar')))
shutil.copyfile(filename, best_name)
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = None
self.avg = None
self.sum = None
self.count = None
def update(self, val, n=1):
if self.val is None:
self.val = val
self.sum = val * n
self.count = n
self.avg = self.sum / self.count
else:
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
class EvalSegmentation(object):
def __init__(self, num_class, ignore_label=None):
self.num_class = num_class
self.ignore_label = ignore_label
def __call__(self, pred, gt):
assert (pred.shape == gt.shape)
gt = gt.flatten().astype(int)
pred = pred.flatten().astype(int)
locs = (gt != self.ignore_label)
sumim = gt + pred * self.num_class
hs = np.bincount(sumim[locs], minlength=self.num_class**2).reshape(self.num_class, self.num_class)
return hs
def adjust_learning_rate(optimizer, epoch, lr_steps):
"""Sets the learning rate to the initial LR decayed by 10 every 30 epochs"""
# decay = 0.1**(sum(epoch >= np.array(lr_steps)))
decay = ((1 - float(epoch) / args.epochs)**(0.9))
lr = args.lr * decay
decay = args.weight_decay
for param_group in optimizer.param_groups:
param_group['lr'] = lr # * param_group['lr_mult']
param_group['weight_decay'] = decay # * param_group['decay_mult']
if __name__ == '__main__':
main()