-
Notifications
You must be signed in to change notification settings - Fork 2
/
train.py
444 lines (379 loc) · 19.7 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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
import os
import random
import time
import cv2
import numpy as np
import logging
import argparse
import torch
import torch.backends.cudnn as cudnn
import torch.nn as nn
import torch.nn.functional as F
import torch.nn.parallel
import torch.optim
import torch.utils.data
import torch.multiprocessing as mp
import torch.distributed as dist
from tensorboardX import SummaryWriter
from model.IPMTnetwork import IPMTnetwork
from util import dataset
from util import transform, config
from util.util import AverageMeter, poly_learning_rate, step_learning_rate, intersectionAndUnionGPU
cv2.ocl.setUseOpenCL(False)
cv2.setNumThreads(0)
def get_parser():
parser = argparse.ArgumentParser(description='PyTorch Semantic Segmentation')
parser.add_argument('--config', type=str, required=True, help='config file')
parser.add_argument('--opts', default=None, nargs=argparse.REMAINDER)
cfg = config.load_cfg_from_cfg_file('config/pascal/pascal_split1_resnet50.yaml')
return cfg
def get_logger():
logger_name = "main-logger"
logger = logging.getLogger(logger_name)
logger.setLevel(logging.INFO)
handler = logging.StreamHandler()
fmt = "[%(asctime)s %(levelname)s %(filename)s line %(lineno)d %(process)d] %(message)s"
handler.setFormatter(logging.Formatter(fmt))
logger.addHandler(handler)
return logger
def worker_init_fn(worker_id):
random.seed(args.manual_seed + worker_id)
def main_process():
return not args.multiprocessing_distributed or (args.multiprocessing_distributed and args.rank % args.ngpus_per_node == 0)
def main():
args = get_parser()
assert args.classes > 1
assert (args.train_h - 1) % 8 == 0 and (args.train_w - 1) % 8 == 0
# os.environ["CUDA_VISIBLE_DEVICES"] = ','.join(str(x) for x in args.train_gpu)
if args.manual_seed is not None:
cudnn.benchmark = False
cudnn.deterministic = True
torch.cuda.manual_seed(args.manual_seed)
np.random.seed(args.manual_seed)
torch.manual_seed(args.manual_seed)
torch.cuda.manual_seed_all(args.manual_seed)
random.seed(args.manual_seed)
### multi-processing training is deprecated
if args.dist_url == "env://" and args.world_size == -1:
args.world_size = int(os.environ["WORLD_SIZE"])
args.distributed = args.world_size > 1 or args.multiprocessing_distributed
args.ngpus_per_node = len(args.train_gpu)
if len(args.train_gpu) == 1:
args.sync_bn = False # sync_bn is deprecated
args.distributed = False
args.multiprocessing_distributed = False
if args.multiprocessing_distributed:
args.world_size = args.ngpus_per_node * args.world_size
mp.spawn(main_worker, nprocs=args.ngpus_per_node, args=(args.ngpus_per_node, args))
else:
main_worker(args.train_gpu, args.ngpus_per_node, args)
def main_worker(argss):
global args
args = argss
model = IPMTnetwork(layers=args.layers, shot=args.shot, \
reduce_dim=args.hidden_dims, with_transformer=args.with_transformer)
param_dicts = [
{"params": [p for n, p in model.named_parameters() if "transformer" not in n
and p.requires_grad]},
]
transformer_param_dicts = [
{
"params": [p for n, p in model.named_parameters() if "transformer" in n and "bias" not in n and p.requires_grad],
"lr": 1e-4,
"weight_decay": 1e-2,
},
{
"params": [p for n, p in model.named_parameters() if "transformer" in n and "bias" in n and p.requires_grad],
"lr": 1e-4,
"weight_decay": 0,
}
]
optimizer = torch.optim.SGD(
param_dicts,
lr=args.base_lr, momentum=args.momentum, weight_decay=args.weight_decay)
base_lrs = [pg['lr'] for pg in optimizer.param_groups]
transformer_optimizer = torch.optim.AdamW(transformer_param_dicts, lr=1e-4, weight_decay=1e-4)
global logger, writer
logger = get_logger()
writer = SummaryWriter(args.save_path)
logger.info("=> creating model ...")
logger.info("Classes: {}".format(args.classes))
logger.info(model)
print(args)
model = torch.nn.DataParallel(model.cuda())
if args.weight:
if os.path.isfile(args.weight):
logger.info("=> loading weight '{}'".format(args.weight))
checkpoint = torch.load(args.weight)
model.load_state_dict(checkpoint['state_dict'])
logger.info("=> loaded weight '{}'".format(args.weight))
else:
logger.info("=> no weight found at '{}'".format(args.weight))
if args.resume:
if os.path.isfile(args.resume):
logger.info("=> loading checkpoint '{}'".format(args.resume))
checkpoint = torch.load(args.resume, map_location=lambda storage, loc: storage.cuda())
args.start_epoch = checkpoint['epoch']
model.load_state_dict(checkpoint['state_dict'])
optimizer.load_state_dict(checkpoint['optimizer'])
logger.info("=> loaded checkpoint '{}' (epoch {})".format(args.resume, checkpoint['epoch']))
else:
logger.info("=> no checkpoint found at '{}'".format(args.resume))
value_scale = 255
mean = [0.485, 0.456, 0.406]
mean = [item * value_scale for item in mean]
std = [0.229, 0.224, 0.225]
std = [item * value_scale for item in std]
assert args.split in [0, 1, 2, 3, 999]
train_transform = [
transform.RandScale([args.scale_min, args.scale_max]),
transform.RandRotate([args.rotate_min, args.rotate_max], padding=mean, ignore_label=args.padding_label),
transform.RandomGaussianBlur(),
transform.RandomHorizontalFlip(),
transform.Crop([args.train_h, args.train_w], crop_type='rand', padding=mean, ignore_label=args.padding_label),
transform.ToTensor(),
transform.Normalize(mean=mean, std=std)]
train_transform = transform.Compose(train_transform)
train_data = dataset.SemData(split=args.split, shot=args.shot, data_root=args.data_root, \
data_list=args.train_list, transform=train_transform, mode='train', \
use_coco=args.use_coco, use_split_coco=args.use_split_coco)
train_sampler = None
train_loader = torch.utils.data.DataLoader(train_data, batch_size=args.batch_size, shuffle=(train_sampler is None), num_workers=args.workers, pin_memory=True, sampler=train_sampler, drop_last=True)
if args.evaluate:
if args.resized_val:
val_transform = transform.Compose([
transform.Resize(size=args.val_size),
transform.ToTensor(),
transform.Normalize(mean=mean, std=std)])
else:
val_transform = transform.Compose([
transform.test_Resize(size=args.val_size),
transform.ToTensor(),
transform.Normalize(mean=mean, std=std)])
val_data = dataset.SemData(split=args.split, shot=args.shot, data_root=args.data_root, \
data_list=args.val_list, transform=val_transform, mode='val', \
use_coco=args.use_coco, use_split_coco=args.use_split_coco)
val_sampler = None
val_loader = torch.utils.data.DataLoader(val_data, batch_size=args.batch_size_val, shuffle=False, num_workers=args.workers, pin_memory=True, sampler=val_sampler)
max_iou = 0.
filename = ''
for epoch in range(args.start_epoch, args.epochs):
if args.fix_random_seed_val:
torch.cuda.manual_seed(args.manual_seed + epoch)
np.random.seed(args.manual_seed + epoch)
torch.manual_seed(args.manual_seed + epoch)
torch.cuda.manual_seed_all(args.manual_seed + epoch)
random.seed(args.manual_seed + epoch)
epoch_log = epoch + 1
loss_train, mIoU_train, mAcc_train, allAcc_train = train(train_loader, model, optimizer, transformer_optimizer, epoch, base_lrs)
if main_process():
writer.add_scalar('loss_train', loss_train, epoch_log)
writer.add_scalar('mIoU_train', mIoU_train, epoch_log)
writer.add_scalar('mAcc_train', mAcc_train, epoch_log)
writer.add_scalar('allAcc_train', allAcc_train, epoch_log)
if args.evaluate and epoch>70:
loss_val, mIoU_val, mAcc_val, allAcc_val, class_miou = validate(val_loader, model)
if main_process():
writer.add_scalar('loss_val', loss_val, epoch_log)
writer.add_scalar('mIoU_val', mIoU_val, epoch_log)
writer.add_scalar('mAcc_val', mAcc_val, epoch_log)
writer.add_scalar('class_miou_val', class_miou, epoch_log)
writer.add_scalar('allAcc_val', allAcc_val, epoch_log)
if class_miou > max_iou:
max_iou = class_miou
if os.path.exists(filename):
os.remove(filename)
filename = args.save_path + '/train_epoch_' + str(epoch) + '_'+str(max_iou)+'.pth'
logger.info('Saving checkpoint to: ' + filename)
torch.save({'epoch': epoch, 'state_dict': model.state_dict(), 'optimizer': optimizer.state_dict()}, filename)
filename = args.save_path + '/final.pth'
logger.info('Saving checkpoint to: ' + filename)
torch.save({'epoch': args.epochs, 'state_dict': model.state_dict(), 'optimizer': optimizer.state_dict()}, filename)
def train(train_loader, model, optimizer, transformer_optimizer, epoch, base_lrs):
batch_time = AverageMeter()
data_time = AverageMeter()
main_loss_meter = AverageMeter()
aux_loss_meter = AverageMeter()
loss_meter = AverageMeter()
intersection_meter = AverageMeter()
union_meter = AverageMeter()
target_meter = AverageMeter()
model.train()
end = time.time()
max_iter = args.epochs * len(train_loader)
vis_key = 0
print('Warmup: {}'.format(args.warmup))
for i, (input, target, s_input, s_mask, subcls) in enumerate(train_loader):
data_time.update(time.time() - end)
current_iter = epoch * len(train_loader) + i + 1
index_split = -1
if args.base_lr > 1e-6:
poly_learning_rate(optimizer, base_lrs, current_iter, max_iter, power=args.power, index_split=index_split, warmup=args.warmup, warmup_step=len(train_loader))
s_input = s_input.cuda(non_blocking=True)
s_mask = s_mask.cuda(non_blocking=True)
input = input.cuda(non_blocking=True)
target = target.cuda(non_blocking=True)
output, main_loss, aux_loss = model(s_x=s_input, s_y=s_mask, x=input, y=target)
if not args.multiprocessing_distributed:
main_loss, aux_loss = torch.mean(main_loss), torch.mean(aux_loss)
loss = main_loss + args.aux_weight * aux_loss
optimizer.zero_grad()
transformer_optimizer.zero_grad()
loss.backward()
optimizer.step()
transformer_optimizer.step()
n = input.size(0)
if args.multiprocessing_distributed:
main_loss, aux_loss, loss = main_loss.detach() * n, aux_loss * n, loss * n
count = target.new_tensor([n], dtype=torch.long)
dist.all_reduce(main_loss), dist.all_reduce(aux_loss), dist.all_reduce(loss), dist.all_reduce(count)
n = count.item()
main_loss, aux_loss, loss = main_loss / n, aux_loss / n, loss / n
intersection, union, target = intersectionAndUnionGPU(output, target, args.classes, args.ignore_label)
if args.multiprocessing_distributed:
dist.all_reduce(intersection), dist.all_reduce(union), dist.all_reduce(target)
intersection, union, target = intersection.cpu().numpy(), union.cpu().numpy(), target.cpu().numpy()
intersection_meter.update(intersection), union_meter.update(union), target_meter.update(target)
accuracy = sum(intersection_meter.val) / (sum(target_meter.val) + 1e-10)
main_loss_meter.update(main_loss.item(), n)
aux_loss_meter.update(aux_loss.item(), n)
loss_meter.update(loss.item(), n)
batch_time.update(time.time() - end)
end = time.time()
remain_iter = max_iter - current_iter
remain_time = remain_iter * batch_time.avg
t_m, t_s = divmod(remain_time, 60)
t_h, t_m = divmod(t_m, 60)
remain_time = '{:02d}:{:02d}:{:02d}'.format(int(t_h), int(t_m), int(t_s))
if (i + 1) % args.print_freq == 0 and main_process():
logger.info('Epoch: [{}/{}][{}/{}] '
'Data {data_time.val:.3f} ({data_time.avg:.3f}) '
'Batch {batch_time.val:.3f} ({batch_time.avg:.3f}) '
'Remain {remain_time} '
'MainLoss {main_loss_meter.val:.4f} '
'AuxLoss {aux_loss_meter.val:.4f} '
'Loss {loss_meter.val:.4f} '
'Accuracy {accuracy:.4f}.'.format(epoch+1, args.epochs, i + 1, len(train_loader),
batch_time=batch_time,
data_time=data_time,
remain_time=remain_time,
main_loss_meter=main_loss_meter,
aux_loss_meter=aux_loss_meter,
loss_meter=loss_meter,
accuracy=accuracy))
if main_process():
writer.add_scalar('loss_train_batch', main_loss_meter.val, current_iter)
writer.add_scalar('mIoU_train_batch', np.mean(intersection / (union + 1e-10)), current_iter)
writer.add_scalar('mAcc_train_batch', np.mean(intersection / (target + 1e-10)), current_iter)
writer.add_scalar('allAcc_train_batch', accuracy, current_iter)
iou_class = intersection_meter.sum / (union_meter.sum + 1e-10)
accuracy_class = intersection_meter.sum / (target_meter.sum + 1e-10)
mIoU = np.mean(iou_class)
mAcc = np.mean(accuracy_class)
allAcc = sum(intersection_meter.sum) / (sum(target_meter.sum) + 1e-10)
if main_process():
logger.info('Train result at epoch [{}/{}]: mIoU/mAcc/allAcc {:.4f}/{:.4f}/{:.4f}.'.format(epoch, args.epochs, mIoU, mAcc, allAcc))
for i in range(args.classes):
logger.info('Class_{} Result: iou/accuracy {:.4f}/{:.4f}.'.format(i, iou_class[i], accuracy_class[i]))
return main_loss_meter.avg, mIoU, mAcc, allAcc
def validate(val_loader, model, criterion=nn.CrossEntropyLoss(ignore_index=255)):
if main_process():
logger.info('>>>>>>>>>>>>>>>> Start Evaluation >>>>>>>>>>>>>>>>')
batch_time = AverageMeter()
model_time = AverageMeter()
data_time = AverageMeter()
loss_meter = AverageMeter()
intersection_meter = AverageMeter()
union_meter = AverageMeter()
target_meter = AverageMeter()
if args.use_coco:
split_gap = 20
else:
split_gap = 5
class_intersection_meter = [0]*split_gap
class_union_meter = [0]*split_gap
if args.manual_seed is not None and args.fix_random_seed_val:
torch.cuda.manual_seed(args.manual_seed)
np.random.seed(args.manual_seed)
torch.manual_seed(args.manual_seed)
torch.cuda.manual_seed_all(args.manual_seed)
random.seed(args.manual_seed)
model.eval()
end = time.time()
if args.split != 999:
if args.use_coco:
test_num = 5000
else:
test_num = 1000
else:
test_num = len(val_loader)
assert test_num % args.batch_size_val == 0
iter_num = 0
total_time = 0
for e in range(10):
for i, (input, target, s_input, s_mask, subcls, ori_label) in enumerate(val_loader):
if (iter_num-1) * args.batch_size_val >= test_num:
break
iter_num += 1
data_time.update(time.time() - end)
input = input.cuda(non_blocking=True)
target = target.cuda(non_blocking=True)
ori_label = ori_label.cuda(non_blocking=True)
start_time = time.time()
output = model(s_x=s_input, s_y=s_mask, x=input, y=target)
total_time = total_time + 1
model_time.update(time.time() - start_time)
if args.ori_resize:
longerside = max(ori_label.size(1), ori_label.size(2))
backmask = torch.ones(ori_label.size(0), longerside, longerside).cuda()*255
backmask[0, :ori_label.size(1), :ori_label.size(2)] = ori_label
target = backmask.clone().long()
output = F.interpolate(output, size=target.size()[1:], mode='bilinear', align_corners=True)
loss = criterion(output, target)
n = input.size(0)
loss = torch.mean(loss)
output = output.max(1)[1]
intersection, union, new_target = intersectionAndUnionGPU(output, target, args.classes, args.ignore_label)
intersection, union, target, new_target = intersection.cpu().numpy(), union.cpu().numpy(), target.cpu().numpy(), new_target.cpu().numpy()
intersection_meter.update(intersection), union_meter.update(union), target_meter.update(new_target)
subcls = subcls[0].cpu().numpy()[0]
class_intersection_meter[(subcls-1)%split_gap] += intersection[1]
class_union_meter[(subcls-1)%split_gap] += union[1]
accuracy = sum(intersection_meter.val) / (sum(target_meter.val) + 1e-10)
loss_meter.update(loss.item(), input.size(0))
batch_time.update(time.time() - end)
end = time.time()
if ((i + 1) % (test_num/100) == 0) and main_process():
logger.info('Test: [{}/{}] '
'Data {data_time.val:.3f} ({data_time.avg:.3f}) '
'Batch {batch_time.val:.3f} ({batch_time.avg:.3f}) '
'Loss {loss_meter.val:.4f} ({loss_meter.avg:.4f}) '
'Accuracy {accuracy:.4f}.'.format(iter_num* args.batch_size_val, test_num,
data_time=data_time,
batch_time=batch_time,
loss_meter=loss_meter,
accuracy=accuracy))
iou_class = intersection_meter.sum / (union_meter.sum + 1e-10)
accuracy_class = intersection_meter.sum / (target_meter.sum + 1e-10)
mIoU = np.mean(iou_class)
mAcc = np.mean(accuracy_class)
allAcc = sum(intersection_meter.sum) / (sum(target_meter.sum) + 1e-10)
class_iou_class = []
class_miou = 0
for i in range(len(class_intersection_meter)):
class_iou = class_intersection_meter[i]/(class_union_meter[i]+ 1e-10)
class_iou_class.append(class_iou)
class_miou += class_iou
class_miou = class_miou*1.0 / len(class_intersection_meter)
logger.info('meanIoU---Val result: mIoU {:.4f}.'.format(class_miou))
for i in range(split_gap):
logger.info('Class_{} Result: iou {:.4f}.'.format(i+1, class_iou_class[i]))
if main_process():
logger.info('FBIoU---Val result: mIoU/mAcc/allAcc {:.4f}/{:.4f}/{:.4f}.'.format(mIoU, mAcc, allAcc))
for i in range(args.classes):
logger.info('Class_{} Result: iou/accuracy {:.4f}/{:.4f}.'.format(i, iou_class[i], accuracy_class[i]))
logger.info('<<<<<<<<<<<<<<<<< End Evaluation <<<<<<<<<<<<<<<<<')
print('avg inference time: {:.4f}, count: {}'.format(model_time.avg, test_num))
return loss_meter.avg, mIoU, mAcc, allAcc, class_miou
if __name__ == '__main__':
main()