-
Notifications
You must be signed in to change notification settings - Fork 19
/
train.py
403 lines (355 loc) · 16.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
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
import time, argparse, os.path as osp, os
import torch, numpy as np
import torch.distributed as dist
from copy import deepcopy
import mmcv
from mmengine import Config
from mmengine.runner import set_random_seed
from mmengine.optim import build_optim_wrapper
from mmengine.logging import MMLogger
from mmengine.utils import symlink
from mmseg.models import build_segmentor
from timm.scheduler import CosineLRScheduler, MultiStepLRScheduler
import warnings
warnings.filterwarnings("ignore")
def pass_print(*args, **kwargs):
pass
def main(local_rank, args):
# global settings
set_random_seed(args.seed)
torch.backends.cudnn.deterministic = False
torch.backends.cudnn.benchmark = True
# load config
cfg = Config.fromfile(args.py_config)
cfg.work_dir = args.work_dir
# init DDP
if args.gpus > 1:
distributed = True
ip = os.environ.get("MASTER_ADDR", "127.0.0.1")
port = os.environ.get("MASTER_PORT", "20506")
hosts = int(os.environ.get("WORLD_SIZE", 1)) # number of nodes
rank = int(os.environ.get("RANK", 0)) # node id
gpus = torch.cuda.device_count() # gpus per node
print(f"tcp://{ip}:{port}")
dist.init_process_group(
backend="nccl", init_method=f"tcp://{ip}:{port}",
world_size=hosts * gpus, rank=rank * gpus + local_rank)
world_size = dist.get_world_size()
cfg.gpu_ids = range(world_size)
torch.cuda.set_device(local_rank)
if local_rank != 0:
import builtins
builtins.print = pass_print
else:
distributed = False
world_size = 1
if local_rank == 0:
# from torch.utils.tensorboard import SummaryWriter
os.makedirs(args.work_dir, exist_ok=True)
cfg.dump(osp.join(args.work_dir, osp.basename(args.py_config)))
# writer = SummaryWriter(log_dir=osp.join(args.work_dir, 'tf'))
from utils.tb_wrapper import WrappedTBWriter
writer = WrappedTBWriter('selfocc', log_dir=osp.join(args.work_dir, 'tf'))
WrappedTBWriter._instance_dict['selfocc'] = writer
else:
writer = None
timestamp = time.strftime('%Y%m%d_%H%M%S', time.localtime())
log_file = osp.join(args.work_dir, f'{timestamp}.log')
logger = MMLogger('selfocc', log_file=log_file)
MMLogger._instance_dict['selfocc'] = logger
logger.info(f'Config:\n{cfg.pretty_text}')
# build model
import model
from dataset import get_dataloader
from loss import OPENOCC_LOSS
from utils.feat_tools import multi2single_scale
my_model = build_segmentor(cfg.model)
my_model.init_weights()
n_parameters = sum(p.numel() for p in my_model.parameters() if p.requires_grad)
logger.info(f'Number of params: {n_parameters}')
if distributed:
if cfg.get('syncBN', True):
my_model = torch.nn.SyncBatchNorm.convert_sync_batchnorm(my_model)
logger.info('converted sync bn.')
find_unused_parameters = cfg.get('find_unused_parameters', False)
ddp_model_module = torch.nn.parallel.DistributedDataParallel
my_model = ddp_model_module(
my_model.cuda(),
device_ids=[torch.cuda.current_device()],
broadcast_buffers=False,
find_unused_parameters=find_unused_parameters)
raw_model = my_model.module
else:
my_model = my_model.cuda()
raw_model = my_model
logger.info('done ddp model')
if args.dataset == 'nuscenes' and cfg.get('sem', False):
from utils.openseed_utils import build_openseed_model, forward_openseed_model
openseed_model = build_openseed_model()
train_dataset_loader, val_dataset_loader = get_dataloader(
cfg.train_dataset_config,
cfg.val_dataset_config,
cfg.train_wrapper_config,
cfg.val_wrapper_config,
cfg.train_loader,
cfg.val_loader,
cfg.nusc,
dist=distributed,
iter_resume=args.iter_resume)
# get optimizer, loss, scheduler
optimizer = build_optim_wrapper(my_model, cfg.optimizer)
# cfg.loss.update({'writer': writer})
loss_func = OPENOCC_LOSS.build(cfg.loss).cuda()
max_num_epochs = cfg.max_epochs
if cfg.get('multisteplr', False):
scheduler = MultiStepLRScheduler(
optimizer,
**cfg.multisteplr_config
)
else:
scheduler = CosineLRScheduler(
optimizer,
t_initial=len(train_dataset_loader) * max_num_epochs,
lr_min=1e-6,
warmup_t=cfg.get('warmup_iters', 500),
warmup_lr_init=1e-6,
t_in_epochs=False)
amp = cfg.get('amp', False)
if amp:
scaler = torch.cuda.amp.GradScaler()
os.environ['amp'] = 'true'
else:
os.environ['amp'] = 'false'
# resume and load
epoch = 0
global_iter = 0
last_iter = 0
cfg.resume_from = ''
if osp.exists(osp.join(args.work_dir, 'latest.pth')):
cfg.resume_from = osp.join(args.work_dir, 'latest.pth')
if args.resume_from:
cfg.resume_from = args.resume_from
logger.info('resume from: ' + cfg.resume_from)
logger.info('work dir: ' + args.work_dir)
if cfg.resume_from and osp.exists(cfg.resume_from):
map_location = 'cpu'
ckpt = torch.load(cfg.resume_from, map_location=map_location)
print(raw_model.load_state_dict(ckpt['state_dict'], strict=False))
optimizer.load_state_dict(ckpt['optimizer'])
scheduler.load_state_dict(ckpt['scheduler'])
epoch = ckpt['epoch']
global_iter = ckpt['global_iter']
last_iter = ckpt['last_iter'] if 'last_iter' in ckpt else 0
if hasattr(train_dataset_loader.sampler, 'set_last_iter'):
train_dataset_loader.sampler.set_last_iter(last_iter)
print(f'successfully resumed from epoch {epoch}')
elif cfg.load_from:
ckpt = torch.load(cfg.load_from, map_location='cpu')
if 'state_dict' in ckpt:
state_dict = ckpt['state_dict']
else:
state_dict = ckpt
print(raw_model.load_state_dict(state_dict, strict=False))
# training
print_freq = cfg.print_freq
first_run = True
grad_accumulation = args.gradient_accumulation
# assert grad_accumulation * world_size == 8
grad_norm = 0
if args.depth_metric:
from utils.metric_util import DepthMetric
if args.dataset == 'kitti':
camera_names = ['front']
elif args.dataset == 'nuscenes':
camera_names = ['front', 'front_right', 'front_left', \
'back', 'back_left', 'back_right']
depth_metric = DepthMetric(
camera_names=camera_names).cuda()
depth_metric._reset()
while epoch < max_num_epochs:
my_model.train()
os.environ['eval'] = 'false'
if hasattr(train_dataset_loader.sampler, 'set_epoch'):
train_dataset_loader.sampler.set_epoch(epoch)
loss_list = []
time.sleep(10)
data_time_s = time.time()
time_s = time.time()
for i_iter, (
input_imgs, curr_imgs, prev_imgs, next_imgs, color_imgs,
img_metas, curr_aug, prev_aug, next_aug) in enumerate(train_dataset_loader):
if first_run:
i_iter = i_iter + last_iter
input_imgs = input_imgs.cuda()
curr_imgs = curr_imgs.cuda()
prev_imgs = prev_imgs.cuda()
next_imgs = next_imgs.cuda()
color_imgs = color_imgs.cuda()
data_time_e = time.time()
with torch.cuda.amp.autocast(amp):
# forward + backward + optimize
if args.dataset == 'nuscenes' and cfg.get('sem', False):
sem_map = forward_openseed_model(openseed_model, curr_imgs[0] * 256., cfg.img_size)
img_metas[0].update({'sem': sem_map})
curr_feats, prev_feats, next_feats = curr_imgs, prev_imgs, next_imgs
result_dict = my_model(
imgs=input_imgs, metas=img_metas, global_iter=global_iter)
loss_input = {
'curr_imgs': curr_imgs,
'prev_imgs': prev_imgs,
'next_imgs': next_imgs,
'curr_feats': curr_feats,
'prev_feats': prev_feats,
'next_feats': next_feats,
'metas': img_metas,
'color_imgs': color_imgs,
}
for loss_input_key, loss_input_val in cfg.loss_input_convertion.items():
loss_input.update({
loss_input_key: result_dict[loss_input_val]})
loss, loss_dict = loss_func(loss_input)
loss = loss / grad_accumulation
if not amp:
loss.backward()
if (global_iter + 1) % grad_accumulation == 0:
grad_norm = torch.nn.utils.clip_grad_norm_(my_model.parameters(), cfg.grad_max_norm)
optimizer.step()
optimizer.zero_grad()
else:
scaler.scale(loss).backward()
if (global_iter + 1) % grad_accumulation == 0:
scaler.unscale_(optimizer)
grad_norm = torch.nn.utils.clip_grad_norm_(my_model.parameters(), cfg.grad_max_norm)
scaler.step(optimizer)
scaler.update()
optimizer.zero_grad()
loss_list.append(loss.detach().cpu().item())
scheduler.step_update(global_iter)
time_e = time.time()
global_iter += 1
if i_iter % print_freq == 0 and local_rank == 0:
lr = optimizer.param_groups[0]['lr']
logger.info('[TRAIN] Epoch %d Iter %5d/%d: Loss: %.3f (%.3f), grad_norm: %.3f, lr: %.7f, time: %.3f (%.3f)'%(
epoch, i_iter, len(train_dataset_loader),
loss.item(), np.mean(loss_list), grad_norm, lr,
time_e - time_s, data_time_e - data_time_s))
detailed_loss = []
for loss_name, loss_value in loss_dict.items():
detailed_loss.append(f'{loss_name}: {loss_value:.5f}')
detailed_loss = ', '.join(detailed_loss)
logger.info(detailed_loss)
loss_list = []
data_time_s = time.time()
time_s = time.time()
if args.iter_resume:
if (i_iter + 1) % 50 == 0 and local_rank == 0:
dict_to_save = {
'state_dict': raw_model.state_dict(),
'optimizer': optimizer.state_dict(),
'scheduler': scheduler.state_dict(),
'epoch': epoch,
'global_iter': global_iter,
'last_iter': i_iter + 1,
}
save_file_name = os.path.join(os.path.abspath(args.work_dir), 'iter.pth')
torch.save(dict_to_save, save_file_name)
dst_file = osp.join(args.work_dir, 'latest.pth')
symlink(save_file_name, dst_file)
logger.info(f'iter ckpt {i_iter + 1} saved!')
# save checkpoint
if local_rank == 0:
dict_to_save = {
'state_dict': raw_model.state_dict(),
'optimizer': optimizer.state_dict(),
'scheduler': scheduler.state_dict(),
'epoch': epoch + 1,
'global_iter': global_iter,
}
save_file_name = os.path.join(os.path.abspath(args.work_dir), f'epoch_{epoch+1}.pth')
torch.save(dict_to_save, save_file_name)
dst_file = osp.join(args.work_dir, 'latest.pth')
symlink(save_file_name, dst_file)
epoch += 1
first_run = False
# eval
if epoch % cfg.get('eval_every_epochs', 1) != 0:
continue
my_model.eval()
os.environ['eval'] = 'true'
val_loss_list = []
with torch.no_grad():
for i_iter_val, (input_imgs, curr_imgs, prev_imgs, next_imgs, color_imgs, \
img_metas, curr_aug, prev_aug, next_aug) in enumerate(val_dataset_loader):
input_imgs = input_imgs.cuda()
curr_imgs = curr_imgs.cuda()
prev_imgs = prev_imgs.cuda()
next_imgs = next_imgs.cuda()
color_imgs = color_imgs.cuda()
with torch.cuda.amp.autocast(amp):
if args.dataset == 'nuscenes' and cfg.get('sem', False):
sem_map = forward_openseed_model(openseed_model, curr_imgs[0] * 256., cfg.img_size)
img_metas[0].update({'sem': sem_map})
curr_feats, prev_feats, next_feats = curr_imgs, prev_imgs, next_imgs
result_dict = my_model(imgs=input_imgs, metas=img_metas)
loss_input = {
'curr_imgs': curr_imgs,
'prev_imgs': prev_imgs,
'next_imgs': next_imgs,
'curr_feats': curr_feats,
'prev_feats': prev_feats,
'next_feats': next_feats,
'metas': img_metas,
'color_imgs': color_imgs,
}
for loss_input_key, loss_input_val in cfg.loss_input_convertion.items():
loss_input.update({
loss_input_key: result_dict[loss_input_val]
})
loss, loss_dict = loss_func(loss_input)
if args.depth_metric:
ms_depths = result_dict['ms_depths'][0]
ms_depths = ms_depths.unflatten(-1, cfg.num_rays)
depth_loc = ms_depths.new_tensor(img_metas[0]['depth_loc'])
depth_gt = ms_depths.new_tensor(img_metas[0]['depth_gt'])
depth_mask = torch.from_numpy(img_metas[0]['depth_mask']).cuda()
# depth_pred = ms_depths[0, :(ms_depths.shape[1] // 2)]
depth_pred = ms_depths[0]
depth_metric._after_step(depth_loc, depth_gt, depth_mask, depth_pred)
val_loss_list.append(loss.detach().cpu().numpy())
if i_iter_val % print_freq == 0 and local_rank == 0:
logger.info('[EVAL] Epoch %d Iter %5d: Loss: %.3f (%.3f)'%(
epoch, i_iter_val, loss.item(), np.mean(val_loss_list)))
detailed_loss = []
for loss_name, loss_value in loss_dict.items():
detailed_loss.append(f'{loss_name}: {loss_value:.5f}')
detailed_loss = ', '.join(detailed_loss)
logger.info(detailed_loss)
if args.depth_metric:
depth_metric._after_epoch()
depth_metric._reset()
logger.info('Current val loss is %.3f' %
(np.mean(val_loss_list)))
if writer is not None:
writer.close()
if __name__ == '__main__':
# Training settings
parser = argparse.ArgumentParser(description='')
parser.add_argument('--py-config', default='config/tpv_lidarseg.py')
parser.add_argument('--work-dir', type=str, default='./out/tpv_lidarseg')
parser.add_argument('--resume-from', type=str, default='')
parser.add_argument('--hfai', action='store_true', default=False)
parser.add_argument('--iter-resume', action='store_true', default=False)
parser.add_argument('--seed', type=int, default=42)
parser.add_argument('--gradient-accumulation', type=int, default=1)
parser.add_argument('--depth-metric', action='store_true', default=False)
parser.add_argument('--dataset', type=str, default='nuscenes')
args = parser.parse_args()
ngpus = torch.cuda.device_count()
args.gpus = ngpus
print(args)
if args.hfai:
os.environ['HFAI'] = 'true'
if ngpus > 1:
torch.multiprocessing.spawn(main, args=(args,), nprocs=args.gpus)
else:
main(0, args)