forked from CSAILVision/unifiedparsing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
339 lines (280 loc) · 13.1 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
# System libs
import os
import time
# import math
import random
import argparse
from distutils.version import LooseVersion
# Numerical libs
import numpy as np
import torch
import torch.nn as nn
# Our libs
from dataset import TrainDataset
from models import ModelBuilder, SegmentationModule
from utils import AverageMeter
from lib.nn import UserScatteredDataParallel, user_scattered_collate, patch_replication_callback
import lib.utils.data as torchdata
from broden_dataset_utils.joint_dataset import broden_dataset
# train one epoch
def train(segmentation_module, iterator, optimizers, history, epoch, args):
batch_time = AverageMeter()
data_time = AverageMeter()
names = ['object', 'part', 'scene', 'material']
ave_losses = {n: AverageMeter() for n in names}
ave_metric = {n: AverageMeter() for n in names}
ave_losses['total'] = AverageMeter()
segmentation_module.train(not args.fix_bn)
# main loop
tic = time.time()
for i in range(args.epoch_iters):
batch_data, src_idx = next(iterator)
data_time.update(time.time() - tic)
segmentation_module.zero_grad()
# forward pass
ret = segmentation_module(batch_data)
# Backward
loss = ret['loss']['total'].mean()
loss.backward()
for optimizer in optimizers:
optimizer.step()
# measure elapsed time
batch_time.update(time.time() - tic)
tic = time.time()
# measure losses
for name in ret['loss'].keys():
ave_losses[name].update(ret['loss'][name].mean().item())
# measure metrics
# NOTE: scene metric will be much lower than benchmark
for name in ret['metric'].keys():
ave_metric[name].update(ret['metric'][name].mean().item())
# calculate accuracy, and display
if i % args.disp_iter == 0:
loss_info = "Loss: total {:.4f}, ".format(ave_losses['total'].average())
loss_info += ", ".join(["{} {:.2f}".format(
n[0], ave_losses[n].average()
if ave_losses[n].average() is not None else 0) for n in names])
acc_info = "Accuracy: " + ", ".join(["{} {:4.2f}".format(
n[0], ave_metric[n].average()
if ave_metric[n].average() is not None else 0) for n in names])
print('Epoch: [{}][{}/{}], Time: {:.2f}, Data: {:.2f}, '
'LR: encoder {:.6f}, decoder {:.6f}, {}, {}'
.format(epoch, i, args.epoch_iters,
batch_time.average(), data_time.average(),
args.running_lr_encoder, args.running_lr_decoder,
acc_info, loss_info))
fractional_epoch = epoch - 1 + 1. * i / args.epoch_iters
history['train']['epoch'].append(fractional_epoch)
history['train']['loss'].append(loss.item())
# adjust learning rate
cur_iter = i + (epoch - 1) * args.epoch_iters
adjust_learning_rate(optimizers, cur_iter, args)
def checkpoint(nets, history, args, epoch_num):
print('Saving checkpoints...')
(net_encoder, net_decoder) = nets
suffix_latest = 'epoch_{}.pth'.format(epoch_num)
dict_encoder = net_encoder.state_dict()
dict_decoder = net_decoder.state_dict()
# dict_encoder_save = {k: v for k, v in dict_encoder.items() if not (k.endswith('_tmp_running_mean') or k.endswith('tmp_running_var'))}
# dict_decoder_save = {k: v for k, v in dict_decoder.items() if not (k.endswith('_tmp_running_mean') or k.endswith('tmp_running_var'))}
torch.save(history,
'{}/history_{}'.format(args.ckpt, suffix_latest))
torch.save(dict_encoder,
'{}/encoder_{}'.format(args.ckpt, suffix_latest))
torch.save(dict_decoder,
'{}/decoder_{}'.format(args.ckpt, suffix_latest))
def group_weight(module):
group_decay = []
group_no_decay = []
for m in module.modules():
if isinstance(m, nn.Linear):
group_decay.append(m.weight)
if m.bias is not None:
group_no_decay.append(m.bias)
elif isinstance(m, nn.modules.conv._ConvNd):
group_decay.append(m.weight)
if m.bias is not None:
group_no_decay.append(m.bias)
elif isinstance(m, nn.modules.batchnorm._BatchNorm):
if m.weight is not None:
group_no_decay.append(m.weight)
if m.bias is not None:
group_no_decay.append(m.bias)
assert len(list(module.parameters())) == len(group_decay) + len(group_no_decay)
groups = [dict(params=group_decay), dict(params=group_no_decay, weight_decay=.0)]
return groups
def create_optimizers(nets, args):
(net_encoder, net_decoder) = nets
optimizer_encoder = torch.optim.SGD(
group_weight(net_encoder),
lr=args.lr_encoder,
momentum=args.beta1,
weight_decay=args.weight_decay)
optimizer_decoder = torch.optim.SGD(
group_weight(net_decoder),
lr=args.lr_decoder,
momentum=args.beta1,
weight_decay=args.weight_decay)
return (optimizer_encoder, optimizer_decoder)
def adjust_learning_rate(optimizers, cur_iter, args):
scale_running_lr = ((1. - float(cur_iter) / args.max_iters) ** args.lr_pow)
args.running_lr_encoder = args.lr_encoder * scale_running_lr
args.running_lr_decoder = args.lr_decoder * scale_running_lr
(optimizer_encoder, optimizer_decoder) = optimizers
for param_group in optimizer_encoder.param_groups:
param_group['lr'] = args.running_lr_encoder
for param_group in optimizer_decoder.param_groups:
param_group['lr'] = args.running_lr_decoder
def create_multi_source_train_data_loader(args):
training_records = broden_dataset.record_list['train']
# 0: object, part, scene
# 1: material
multi_source_iters = []
for idx_source in range(len(training_records)):
dataset = TrainDataset(training_records[idx_source], idx_source, args,
batch_per_gpu=args.batch_size_per_gpu)
loader_object_part_scene = torchdata.DataLoader(
dataset,
batch_size=args.num_gpus, # we have modified data_parallel
shuffle=False, # we do not use this param
collate_fn=user_scattered_collate,
num_workers=int(args.workers),
drop_last=True,
pin_memory=True)
multi_source_iters.append(iter(loader_object_part_scene))
# sample from multi source
nr_record = [len(records) for records in training_records]
sample_prob = np.asarray(nr_record) / np.sum(nr_record)
while True: # TODO(LYC):: set random seed.
source_idx = np.random.choice(len(training_records), 1, p=sample_prob)[0]
yield next(multi_source_iters[source_idx]), source_idx
def main(args):
# Network Builders
builder = ModelBuilder()
net_encoder = builder.build_encoder(
arch=args.arch_encoder,
fc_dim=args.fc_dim,
weights=args.weights_encoder)
net_decoder = builder.build_decoder(
arch=args.arch_decoder,
fc_dim=args.fc_dim,
nr_classes=args.nr_classes,
weights=args.weights_decoder)
# TODO(LYC):: move criterion outside model.
# crit = nn.NLLLoss(ignore_index=-1)
if args.arch_decoder.endswith('deepsup'):
segmentation_module = SegmentationModule(
net_encoder, net_decoder, args.deep_sup_scale)
else:
segmentation_module = SegmentationModule(
net_encoder, net_decoder)
print('1 Epoch = {} iters'.format(args.epoch_iters))
# create loader iterator
iterator_train = create_multi_source_train_data_loader(args=args)
# load nets into gpu
if args.num_gpus > 1:
segmentation_module = UserScatteredDataParallel(
segmentation_module,
device_ids=range(args.num_gpus))
# For sync bn
patch_replication_callback(segmentation_module)
segmentation_module.cuda()
# Set up optimizers
nets = (net_encoder, net_decoder)
optimizers = create_optimizers(nets, args)
# Main loop
history = {'train': {'epoch': [], 'loss': [], 'acc': []}}
for epoch in range(args.start_epoch, args.num_epoch + 1):
train(segmentation_module, iterator_train, optimizers, history, epoch, args)
# checkpointing
checkpoint(nets, history, args, epoch)
print('Training Done!')
if __name__ == '__main__':
assert LooseVersion(torch.__version__) >= LooseVersion('0.4.0'), \
'PyTorch>=0.4.0 is required'
parser = argparse.ArgumentParser()
# Model related arguments
parser.add_argument('--id', default='baseline',
help="a name for identifying the model")
parser.add_argument('--arch_encoder', default='resnet50',
help="architecture of net_encoder")
parser.add_argument('--arch_decoder', default='upernet',
help="architecture of net_decoder")
parser.add_argument('--weights_encoder', default='',
help="weights to finetune net_encoder")
parser.add_argument('--weights_decoder', default='',
help="weights to finetune net_decoder")
parser.add_argument('--fc_dim', default=2048, type=int,
help='number of features between encoder and decoder')
# optimization related arguments
parser.add_argument('--num_gpus', default=8, type=int,
help='number of gpus to use')
parser.add_argument('--batch_size_per_gpu', default=2, type=int,
help='input batch size')
parser.add_argument('--num_epoch', default=40, type=int,
help='epochs to train for')
parser.add_argument('--start_epoch', default=1, type=int,
help='epoch to start training. useful if continue from a checkpoint')
parser.add_argument('--epoch_iters', default=5000, type=int,
help='iterations of each epoch (irrelevant to batch size)')
parser.add_argument('--optim', default='SGD', help='optimizer')
parser.add_argument('--lr_encoder', default=2e-2, type=float, help='LR')
parser.add_argument('--lr_decoder', default=2e-2, type=float, help='LR')
parser.add_argument('--lr_pow', default=0.9, type=float,
help='power in poly to drop LR')
parser.add_argument('--beta1', default=0.9, type=float,
help='momentum for sgd, beta1 for adam')
parser.add_argument('--weight_decay', default=1e-4, type=float,
help='weights regularizer')
parser.add_argument('--fix_bn', default=0, type=int,
help='fix bn params')
# Data related arguments
parser.add_argument('--workers', default=16, type=int,
help='number of data loading workers')
parser.add_argument('--imgSize', default=[300,375,450,525,600], nargs='+', type=int,
help='input image size of short edge (int or list)')
parser.add_argument('--imgMaxSize', default=1000, type=int,
help='maximum input image size of long edge')
parser.add_argument('--padding_constant', default=32, type=int,
help='maxmimum downsampling rate of the network')
parser.add_argument('--segm_downsampling_rate', default=4, type=int,
help='downsampling rate of the segmentation label')
parser.add_argument('--random_flip', default=True, type=bool,
help='if horizontally flip images when training')
# Misc arguments
parser.add_argument('--seed', default=304, type=int, help='manual seed')
parser.add_argument('--ckpt', default='./ckpt',
help='folder to output checkpoints')
parser.add_argument('--disp_iter', type=int, default=20,
help='frequency to display')
args = parser.parse_args()
print("Input arguments:")
for key, val in vars(args).items():
print("{:16} {}".format(key, val))
args.batch_size = args.num_gpus * args.batch_size_per_gpu
args.max_iters = args.epoch_iters * args.num_epoch
args.running_lr_encoder = args.lr_encoder
args.running_lr_decoder = args.lr_decoder
args.id += '-' + str(args.arch_encoder)
args.id += '-' + str(args.arch_decoder)
args.id += '-ngpus' + str(args.num_gpus)
args.id += '-batchSize' + str(args.batch_size)
args.id += '-imgMaxSize' + str(args.imgMaxSize)
args.id += '-paddingConst' + str(args.padding_constant)
args.id += '-segmDownsampleRate' + str(args.segm_downsampling_rate)
args.id += '-LR_encoder' + str(args.lr_encoder)
args.id += '-LR_decoder' + str(args.lr_decoder)
args.id += '-epoch' + str(args.num_epoch)
args.id += '-decay' + str(args.weight_decay)
args.id += '-fixBN' + str(args.fix_bn)
print('Model ID: {}'.format(args.id))
nr_classes = broden_dataset.nr.copy()
nr_classes['part'] = sum(
[len(parts) for obj, parts in broden_dataset.object_part.items()])
args.nr_classes = nr_classes
args.ckpt = os.path.join(args.ckpt, args.id)
if not os.path.isdir(args.ckpt):
os.makedirs(args.ckpt)
random.seed(args.seed)
torch.manual_seed(args.seed)
main(args)