-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.py
95 lines (82 loc) · 3.19 KB
/
main.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
import os
import misc
import torch
from mmcv import Config
from mmdet3d_plugin import *
import pytorch_lightning as pl
from argparse import ArgumentParser
from LightningTools.pl_model import pl_model
from LightningTools.dataset_dm import DataModule
from pytorch_lightning import loggers as pl_loggers
from pytorch_lightning.profiler import SimpleProfiler
from pytorch_lightning.strategies.ddp import DDPStrategy
from pytorch_lightning.callbacks import ModelCheckpoint, LearningRateMonitor
def parse_config():
parser = ArgumentParser()
parser.add_argument('--config_path', default='./configs/semantic_kitti.py')
parser.add_argument('--ckpt_path', default=None)
parser.add_argument('--seed', type=int, default=7240, help='random seed point')
parser.add_argument('--log_folder', default='semantic_kitti')
parser.add_argument('--save_path', default=None)
parser.add_argument('--test_mapping', action='store_true')
parser.add_argument('--submit', action='store_true')
parser.add_argument('--eval', action='store_true')
parser.add_argument('--log_every_n_steps', type=int, default=1000)
parser.add_argument('--check_val_every_n_epoch', type=int, default=1)
parser.add_argument('--pretrain', action='store_true')
args = parser.parse_args()
cfg = Config.fromfile(args.config_path)
cfg.update(vars(args))
return args, cfg
if __name__ == '__main__':
args, config = parse_config()
log_folder = os.path.join('logs', config['log_folder'])
misc.check_path(log_folder)
misc.check_path(os.path.join(log_folder, 'tensorboard'))
tb_logger = pl_loggers.TensorBoardLogger(
save_dir=log_folder,
name='tensorboard'
)
config.dump(os.path.join(log_folder, 'config.py'))
profiler = SimpleProfiler(dirpath=log_folder, filename="profiler.txt")
seed = config.seed
pl.seed_everything(seed)
num_gpu = torch.cuda.device_count()
model = pl_model(config)
data_dm = DataModule(config)
checkpoint_callback = ModelCheckpoint(
monitor='val/mIoU',
mode='max',
save_last=True,
filename='best')
if not config.eval:
trainer = pl.Trainer(
devices=[i for i in range(num_gpu)],
strategy=DDPStrategy(
accelerator='gpu',
find_unused_parameters=False
),
max_steps=config.training_steps,
resume_from_checkpoint=None,
callbacks=[
checkpoint_callback,
LearningRateMonitor(logging_interval='step')
],
logger=tb_logger,
profiler=profiler,
sync_batchnorm=True,
log_every_n_steps=config['log_every_n_steps'],
check_val_every_n_epoch=config['check_val_every_n_epoch']
)
trainer.fit(model=model, datamodule=data_dm)
else:
trainer = pl.Trainer(
devices=[i for i in range(num_gpu)],
strategy=DDPStrategy(
accelerator='gpu',
find_unused_parameters=False
),
logger=tb_logger,
profiler=profiler
)
trainer.test(model=model, datamodule=data_dm, ckpt_path=config['ckpt_path'])