"...csrc/git@developer.sourcefind.cn:OpenDAS/vision.git" did not exist on "c4dcfb06ff7c83a8701343de602acb3861c36619"
Unverified Commit 8b131f52 authored by Wenhao Wu's avatar Wenhao Wu Committed by GitHub
Browse files

Refactor train.py/test.py (#343)

* refactor

* refactor train/test

* refactor train/test

* refactor train.py/test.py

* refactor train.py/test.py --- specify default of seed to 0 & save mmdet version instead of mmdet3d git hash

* refactor train.py/test.py --- specify default of seed to 0 & save mmdet version instead of mmdet3d git hash
parent b65f9b0f
...@@ -2,16 +2,18 @@ import argparse ...@@ -2,16 +2,18 @@ import argparse
import mmcv import mmcv
import os import os
import torch import torch
import warnings
from mmcv import Config, DictAction from mmcv import Config, DictAction
from mmcv.cnn import fuse_conv_bn
from mmcv.parallel import MMDataParallel, MMDistributedDataParallel from mmcv.parallel import MMDataParallel, MMDistributedDataParallel
from mmcv.runner import get_dist_info, init_dist, load_checkpoint from mmcv.runner import (get_dist_info, init_dist, load_checkpoint,
wrap_fp16_model)
from mmdet3d.apis import single_gpu_test from mmdet3d.apis import single_gpu_test
from mmdet3d.datasets import build_dataloader, build_dataset from mmdet3d.datasets import build_dataloader, build_dataset
from mmdet3d.models import build_detector from mmdet3d.models import build_detector
from mmdet.apis import multi_gpu_test, set_random_seed from mmdet.apis import multi_gpu_test, set_random_seed
from mmdet.core import wrap_fp16_model from mmdet.datasets import replace_ImageToTensor
from tools.fuse_conv_bn import fuse_module
def parse_args(): def parse_args():
...@@ -47,14 +49,35 @@ def parse_args(): ...@@ -47,14 +49,35 @@ def parse_args():
parser.add_argument( parser.add_argument(
'--tmpdir', '--tmpdir',
help='tmp directory used for collecting results from multiple ' help='tmp directory used for collecting results from multiple '
'workers, available when gpu_collect is not specified') 'workers, available when gpu-collect is not specified')
parser.add_argument('--seed', type=int, default=0, help='random seed') parser.add_argument('--seed', type=int, default=0, help='random seed')
parser.add_argument( parser.add_argument(
'--deterministic', '--deterministic',
action='store_true', action='store_true',
help='whether to set deterministic options for CUDNN backend.') help='whether to set deterministic options for CUDNN backend.')
parser.add_argument( parser.add_argument(
'--options', nargs='+', action=DictAction, help='custom options') '--cfg-options',
nargs='+',
action=DictAction,
help='override some settings in the used config, the key-value pair '
'in xxx=yyy format will be merged into config file. If the value to '
'be overwritten is a list, it should be like key="[a,b]" or key=a,b '
'It also allows nested list/tuple values, e.g. key="[(a,b),(c,d)]" '
'Note that the quotation marks are necessary and that no white space '
'is allowed.')
parser.add_argument(
'--options',
nargs='+',
action=DictAction,
help='custom options for evaluation, the key-value pair in xxx=yyy '
'format will be kwargs for dataset.evaluate() function (deprecate), '
'change to --eval-options instead.')
parser.add_argument(
'--eval-options',
nargs='+',
action=DictAction,
help='custom options for evaluation, the key-value pair in xxx=yyy '
'format will be kwargs for dataset.evaluate() function')
parser.add_argument( parser.add_argument(
'--launcher', '--launcher',
choices=['none', 'pytorch', 'slurm', 'mpi'], choices=['none', 'pytorch', 'slurm', 'mpi'],
...@@ -64,16 +87,25 @@ def parse_args(): ...@@ -64,16 +87,25 @@ def parse_args():
args = parser.parse_args() args = parser.parse_args()
if 'LOCAL_RANK' not in os.environ: if 'LOCAL_RANK' not in os.environ:
os.environ['LOCAL_RANK'] = str(args.local_rank) os.environ['LOCAL_RANK'] = str(args.local_rank)
if args.options and args.eval_options:
raise ValueError(
'--options and --eval-options cannot be both specified, '
'--options is deprecated in favor of --eval-options')
if args.options:
warnings.warn('--options is deprecated in favor of --eval-options')
args.eval_options = args.options
return args return args
def main(): def main():
args = parse_args() args = parse_args()
assert args.out or args.eval or args.format_only or args.show, \ assert args.out or args.eval or args.format_only or args.show \
or args.show_dir, \
('Please specify at least one operation (save/eval/format/show the ' ('Please specify at least one operation (save/eval/format/show the '
'results) with the argument "--out", "--eval", "--format_only" ' 'results / save the results) with the argument "--out", "--eval"'
'or "--show"') ', "--format-only", "--show" or "--show-dir"')
if args.eval and args.format_only: if args.eval and args.format_only:
raise ValueError('--eval and --format_only cannot be both specified') raise ValueError('--eval and --format_only cannot be both specified')
...@@ -82,12 +114,34 @@ def main(): ...@@ -82,12 +114,34 @@ def main():
raise ValueError('The output file must be a pkl file.') raise ValueError('The output file must be a pkl file.')
cfg = Config.fromfile(args.config) cfg = Config.fromfile(args.config)
if args.cfg_options is not None:
cfg.merge_from_dict(args.cfg_options)
# import modules from string list.
if cfg.get('custom_imports', None):
from mmcv.utils import import_modules_from_strings
import_modules_from_strings(**cfg['custom_imports'])
# set cudnn_benchmark # set cudnn_benchmark
if cfg.get('cudnn_benchmark', False): if cfg.get('cudnn_benchmark', False):
torch.backends.cudnn.benchmark = True torch.backends.cudnn.benchmark = True
cfg.model.pretrained = None cfg.model.pretrained = None
cfg.data.test.test_mode = True # in case the test dataset is concatenated
samples_per_gpu = 1
if isinstance(cfg.data.test, dict):
cfg.data.test.test_mode = True
samples_per_gpu = cfg.data.test.pop('samples_per_gpu', 1)
if samples_per_gpu > 1:
# Replace 'ImageToTensor' to 'DefaultFormatBundle'
cfg.data.test.pipeline = replace_ImageToTensor(
cfg.data.test.pipeline)
elif isinstance(cfg.data.test, list):
for ds_cfg in cfg.data.test:
ds_cfg.test_mode = True
samples_per_gpu = max(
[ds_cfg.pop('samples_per_gpu', 1) for ds_cfg in cfg.data.test])
if samples_per_gpu > 1:
for ds_cfg in cfg.data.test:
ds_cfg.pipeline = replace_ImageToTensor(ds_cfg.pipeline)
# init distributed env first, since logger depends on the dist info. # init distributed env first, since logger depends on the dist info.
if args.launcher == 'none': if args.launcher == 'none':
...@@ -101,7 +155,6 @@ def main(): ...@@ -101,7 +155,6 @@ def main():
set_random_seed(args.seed, deterministic=args.deterministic) set_random_seed(args.seed, deterministic=args.deterministic)
# build the dataloader # build the dataloader
samples_per_gpu = cfg.data.test.pop('samples_per_gpu', 1)
dataset = build_dataset(cfg.data.test) dataset = build_dataset(cfg.data.test)
data_loader = build_dataloader( data_loader = build_dataloader(
dataset, dataset,
...@@ -118,10 +171,10 @@ def main(): ...@@ -118,10 +171,10 @@ def main():
wrap_fp16_model(model) wrap_fp16_model(model)
checkpoint = load_checkpoint(model, args.checkpoint, map_location='cpu') checkpoint = load_checkpoint(model, args.checkpoint, map_location='cpu')
if args.fuse_conv_bn: if args.fuse_conv_bn:
model = fuse_module(model) model = fuse_conv_bn(model)
# old versions did not save class info in checkpoints, this walkaround is # old versions did not save class info in checkpoints, this walkaround is
# for backward compatibility # for backward compatibility
if 'CLASSES' in checkpoint['meta']: if 'CLASSES' in checkpoint.get('meta', {}):
model.CLASSES = checkpoint['meta']['CLASSES'] model.CLASSES = checkpoint['meta']['CLASSES']
else: else:
model.CLASSES = dataset.CLASSES model.CLASSES = dataset.CLASSES
...@@ -142,11 +195,19 @@ def main(): ...@@ -142,11 +195,19 @@ def main():
if args.out: if args.out:
print(f'\nwriting results to {args.out}') print(f'\nwriting results to {args.out}')
mmcv.dump(outputs, args.out) mmcv.dump(outputs, args.out)
kwargs = {} if args.options is None else args.options kwargs = {} if args.eval_options is None else args.eval_options
if args.format_only: if args.format_only:
dataset.format_results(outputs, **kwargs) dataset.format_results(outputs, **kwargs)
if args.eval: if args.eval:
dataset.evaluate(outputs, args.eval, **kwargs) eval_kwargs = cfg.get('evaluation', {}).copy()
# hard-code way to remove EvalHook args
for key in [
'interval', 'tmpdir', 'start', 'gpu_collect', 'save_best',
'rule'
]:
eval_kwargs.pop(key, None)
eval_kwargs.update(dict(metric=args.eval, **kwargs))
print(dataset.evaluate(outputs, **eval_kwargs))
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -7,8 +7,9 @@ import mmcv ...@@ -7,8 +7,9 @@ import mmcv
import os import os
import time import time
import torch import torch
import warnings
from mmcv import Config, DictAction from mmcv import Config, DictAction
from mmcv.runner import init_dist from mmcv.runner import get_dist_info, init_dist
from os import path as osp from os import path as osp
from mmdet3d import __version__ from mmdet3d import __version__
...@@ -46,7 +47,22 @@ def parse_args(): ...@@ -46,7 +47,22 @@ def parse_args():
action='store_true', action='store_true',
help='whether to set deterministic options for CUDNN backend.') help='whether to set deterministic options for CUDNN backend.')
parser.add_argument( parser.add_argument(
'--options', nargs='+', action=DictAction, help='arguments in dict') '--options',
nargs='+',
action=DictAction,
help='override some settings in the used config, the key-value pair '
'in xxx=yyy format will be merged into config file (deprecate), '
'change to --cfg-options instead.')
parser.add_argument(
'--cfg-options',
nargs='+',
action=DictAction,
help='override some settings in the used config, the key-value pair '
'in xxx=yyy format will be merged into config file. If the value to '
'be overwritten is a list, it should be like key="[a,b]" or key=a,b '
'It also allows nested list/tuple values, e.g. key="[(a,b),(c,d)]" '
'Note that the quotation marks are necessary and that no white space '
'is allowed.')
parser.add_argument( parser.add_argument(
'--launcher', '--launcher',
choices=['none', 'pytorch', 'slurm', 'mpi'], choices=['none', 'pytorch', 'slurm', 'mpi'],
...@@ -61,6 +77,14 @@ def parse_args(): ...@@ -61,6 +77,14 @@ def parse_args():
if 'LOCAL_RANK' not in os.environ: if 'LOCAL_RANK' not in os.environ:
os.environ['LOCAL_RANK'] = str(args.local_rank) os.environ['LOCAL_RANK'] = str(args.local_rank)
if args.options and args.cfg_options:
raise ValueError(
'--options and --cfg-options cannot be both specified, '
'--options is deprecated in favor of --cfg-options')
if args.options:
warnings.warn('--options is deprecated in favor of --cfg-options')
args.cfg_options = args.options
return args return args
...@@ -68,8 +92,12 @@ def main(): ...@@ -68,8 +92,12 @@ def main():
args = parse_args() args = parse_args()
cfg = Config.fromfile(args.config) cfg = Config.fromfile(args.config)
if args.options is not None: if args.cfg_options is not None:
cfg.merge_from_dict(args.options) cfg.merge_from_dict(args.cfg_options)
# import modules from string list.
if cfg.get('custom_imports', None):
from mmcv.utils import import_modules_from_strings
import_modules_from_strings(**cfg['custom_imports'])
# set cudnn_benchmark # set cudnn_benchmark
if cfg.get('cudnn_benchmark', False): if cfg.get('cudnn_benchmark', False):
...@@ -100,9 +128,14 @@ def main(): ...@@ -100,9 +128,14 @@ def main():
else: else:
distributed = True distributed = True
init_dist(args.launcher, **cfg.dist_params) init_dist(args.launcher, **cfg.dist_params)
# re-set gpu_ids with distributed training mode
_, world_size = get_dist_info()
cfg.gpu_ids = range(world_size)
# create work_dir # create work_dir
mmcv.mkdir_or_exist(osp.abspath(cfg.work_dir)) mmcv.mkdir_or_exist(osp.abspath(cfg.work_dir))
# dump config
cfg.dump(osp.join(cfg.work_dir, osp.basename(args.config)))
# init the logger before other steps # init the logger before other steps
timestamp = time.strftime('%Y%m%d_%H%M%S', time.localtime()) timestamp = time.strftime('%Y%m%d_%H%M%S', time.localtime())
log_file = osp.join(cfg.work_dir, f'{timestamp}.log') log_file = osp.join(cfg.work_dir, f'{timestamp}.log')
...@@ -122,6 +155,7 @@ def main(): ...@@ -122,6 +155,7 @@ def main():
logger.info('Environment info:\n' + dash_line + env_info + '\n' + logger.info('Environment info:\n' + dash_line + env_info + '\n' +
dash_line) dash_line)
meta['env_info'] = env_info meta['env_info'] = env_info
meta['config'] = cfg.pretty_text
# log some basic info # log some basic info
logger.info(f'Distributed training: {distributed}') logger.info(f'Distributed training: {distributed}')
...@@ -134,6 +168,7 @@ def main(): ...@@ -134,6 +168,7 @@ def main():
set_random_seed(args.seed, deterministic=args.deterministic) set_random_seed(args.seed, deterministic=args.deterministic)
cfg.seed = args.seed cfg.seed = args.seed
meta['seed'] = args.seed meta['seed'] = args.seed
meta['exp_name'] = osp.basename(args.config)
model = build_detector( model = build_detector(
cfg.model, cfg.model,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment