-
Notifications
You must be signed in to change notification settings - Fork 333
/
train_erfnet.py
291 lines (236 loc) · 11.1 KB
/
train_erfnet.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
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 as tf
import numpy as np
import models
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.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
elif args.dataset == 'CULane':
num_class = 5
ignore_label = 255
else:
raise ValueError('Unknown dataset ' + args.dataset)
model = models.ERFNet(num_class)
input_mean = model.input_mean
input_std = model.input_std
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()
ckpt_name = []
cnt = 0
for name, param in state_dict.items():
if name not in list(own_state.keys()) or 'output_conv' in name:
ckpt_name.append(name)
continue
own_state[name].copy_(param)
cnt += 1
print('#reused param: {}'.format(cnt))
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']
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("CULane", "VOCAug") + 'DataSet')(data_list=args.train_list, transform=torchvision.transforms.Compose([
tf.GroupRandomScale(size=(0.595, 0.621), interpolation=(cv2.INTER_LINEAR, cv2.INTER_NEAREST)),
tf.GroupRandomCropRatio(size=(args.img_width, args.img_height)),
tf.GroupRandomRotation(degree=(-1, 1), 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)
val_loader = torch.utils.data.DataLoader(
getattr(ds, args.dataset.replace("CULane", "VOCAug") + 'DataSet')(data_list=args.val_list, transform=torchvision.transforms.Compose([
tf.GroupRandomScale(size=(0.595, 0.621), interpolation=(cv2.INTER_LINEAR, cv2.INTER_NEAREST)),
tf.GroupRandomCropRatio(size=(args.img_width, args.img_height)),
tf.GroupNormalize(mean=(input_mean, (0, )), std=(input_std, (1, ))),
])), batch_size=args.batch_size, shuffle=False, num_workers=args.workers, pin_memory=False)
# define loss function (criterion) optimizer and evaluator
weights = [1.0 for _ in range(5)]
weights[0] = 0.4
class_weights = torch.FloatTensor(weights).cuda()
criterion = torch.nn.NLLLoss(ignore_index=ignore_label, weight=class_weights).cuda()
criterion_exist = torch.nn.BCEWithLogitsLoss().cuda()
optimizer = torch.optim.SGD(model.parameters(), args.lr, momentum=args.momentum, weight_decay=args.weight_decay)
evaluator = EvalSegmentation(num_class, ignore_label)
args.evaluate = True
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, criterion_exist, 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, criterion_exist, optimizer, epoch):
batch_time = AverageMeter()
data_time = AverageMeter()
losses = AverageMeter()
losses_exist = AverageMeter()
# switch to train mode
model.train()
end = time.time()
for i, (input, target, target_exist) in enumerate(train_loader):
# measure data loading time
data_time.update(time.time() - end)
target = target.cuda()
target_exist = target_exist.float().cuda()
input_var = torch.autograd.Variable(input)
target_var = torch.autograd.Variable(target)
target_exist_var = torch.autograd.Variable(target_exist)
# compute output
output, output_exist = model(input_var) # output_mid
loss = criterion(torch.nn.functional.log_softmax(output, dim=1), target_var)
# print(output_exist.data.cpu().numpy().shape)
loss_exist = criterion_exist(output_exist, target_exist_var)
loss_tot = loss + loss_exist * 0.1
# measure accuracy and record loss
losses.update(loss.data.item(), input.size(0))
losses_exist.update(loss_exist.item(), 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' 'Loss_exist {loss_exist.val:.4f} ({loss_exist.avg:.4f})\t'.format(
epoch, i, len(train_loader), batch_time=batch_time, data_time=data_time, loss=losses,
loss_exist=losses_exist, 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, target_exist) in enumerate(val_loader):
target = target.cuda()
input_var = torch.autograd.Variable(input, volatile=True)
target_var = torch.autograd.Variable(target)
# compute output
output, _ = model(input_var)
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.item(), 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('trained'):
os.makedirs('trained')
filename = os.path.join('trained', '_'.join((args.snapshot_pref, args.method.lower(), filename)))
torch.save(state, filename)
if is_best:
best_name = os.path.join('trained', '_'.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['weight_decay'] = decay
if __name__ == '__main__':
main()