-
Notifications
You must be signed in to change notification settings - Fork 10
/
train.py
executable file
·81 lines (66 loc) · 2.56 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
from ast import expr_context
from lib2to3.refactor import get_all_fix_names
import os.path as osp
from argparse import ArgumentParser
# import wandb
from mmcv import Config
from pytorch_lightning import Trainer, seed_everything
from pytorch_lightning.callbacks import ModelCheckpoint
from torch.utils.data import DataLoader
from datasets import build_dataset
from models import MODELS
import random
import numpy as np
import torch
from tensorboardX import SummaryWriter
from datasets.nusc_test import Testloader
from datasets.common import NUSCENES_ROOT, OX_VAL_ROOT, CARLAEPE_ROOT
def parse_args():
parser = ArgumentParser(description='Training with DDP.')
parser.add_argument('--config', type=str)
parser.add_argument('--gpus', type=int)
parser.add_argument('--work_dir', type=str, default='checkpoints')
parser.add_argument('--seed', type=int, default=1024)
parser.add_argument('--test', type=int, default=False)
args = parser.parse_args()
return args
def main():
# parse args
args = parse_args()
cfg = Config.fromfile(osp.join(f'configs/{args.config}.yaml'))
cfg.test = args.test
cfg.seed = args.seed
# show information
print(f'Now training with {args.config}...')
# configure seed
random.seed(args.seed)
np.random.seed(args.seed)
torch.manual_seed(args.seed)
torch.cuda.manual_seed(args.seed)
seed_everything(args.seed)
# prepare data loader
dataset = build_dataset(cfg.dataset)
loader = DataLoader(dataset, cfg.imgs_per_gpu, shuffle=False, num_workers=cfg.workers_per_gpu, drop_last=True)
if cfg.model.name == 'rnw':
cfg.data_link = dataset
# define model
model = MODELS.build(name=cfg.model.name, option=cfg)
# define trainer
work_dir = osp.join(args.work_dir, args.config)
# save checkpoint every 'cfg.checkpoint_epoch_interval' epochs
checkpoint_callback = ModelCheckpoint(dirpath=work_dir,
save_weights_only=True,
save_top_k=-1,
filename='checkpoint_{epoch}',
every_n_epochs=1)
trainer = Trainer(accelerator='ddp',
default_root_dir=work_dir,
gpus=args.gpus,
num_nodes=1,
max_epochs=cfg.total_epochs,
callbacks=[checkpoint_callback],
deterministic=False)
# training
trainer.fit(model, train_dataloaders=loader)
if __name__ == '__main__':
main()