|
| 1 | +# Copyright (c) OpenMMLab. All rights reserved. |
| 2 | +# File modified from original file: https://github.com/open-mmlab/mmsegmentation/blob/main/tools/test.py |
| 3 | +# Modified to run 2D segmentation on the WildScenes dataset. |
| 4 | +import argparse |
| 5 | +import os |
| 6 | +import os.path as osp |
| 7 | +import sys |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +from mmengine.config import Config, DictAction |
| 11 | +from mmengine.runner import Runner |
| 12 | + |
| 13 | +root_dir = Path(__file__).parent.parent.parent |
| 14 | +sys.path.append(str(root_dir)) |
| 15 | + |
| 16 | +from wildscenes.mmseg_wildscenes.registry import RUNNERS |
| 17 | + |
| 18 | + |
| 19 | +def parse_args(): |
| 20 | + parser = argparse.ArgumentParser( |
| 21 | + description='MMSeg test (and eval) a model') |
| 22 | + parser.add_argument('config', help='train config file path') |
| 23 | + parser.add_argument('checkpoint', help='checkpoint file') |
| 24 | + parser.add_argument( |
| 25 | + '--work-dir', |
| 26 | + help=('if specified, the evaluation metric results will be dumped' |
| 27 | + 'into the directory as json')) |
| 28 | + parser.add_argument( |
| 29 | + '--out', |
| 30 | + type=str, |
| 31 | + help='The directory to save output prediction for offline evaluation') |
| 32 | + parser.add_argument( |
| 33 | + '--show', action='store_true', help='show prediction results') |
| 34 | + parser.add_argument( |
| 35 | + '--show-dir', |
| 36 | + help='directory where painted images will be saved. ' |
| 37 | + 'If specified, it will be automatically saved ' |
| 38 | + 'to the work_dir/timestamp/show_dir') |
| 39 | + parser.add_argument( |
| 40 | + '--wait-time', type=float, default=2, help='the interval of show (s)') |
| 41 | + parser.add_argument( |
| 42 | + '--cfg-options', |
| 43 | + nargs='+', |
| 44 | + action=DictAction, |
| 45 | + help='override some settings in the used config, the key-value pair ' |
| 46 | + 'in xxx=yyy format will be merged into config file. If the value to ' |
| 47 | + 'be overwritten is a list, it should be like key="[a,b]" or key=a,b ' |
| 48 | + 'It also allows nested list/tuple values, e.g. key="[(a,b),(c,d)]" ' |
| 49 | + 'Note that the quotation marks are necessary and that no white space ' |
| 50 | + 'is allowed.') |
| 51 | + parser.add_argument( |
| 52 | + '--launcher', |
| 53 | + choices=['none', 'pytorch', 'slurm', 'mpi'], |
| 54 | + default='none', |
| 55 | + help='job launcher') |
| 56 | + parser.add_argument( |
| 57 | + '--tta', action='store_true', help='Test time augmentation') |
| 58 | + # When using PyTorch version >= 2.0.0, the `torch.distributed.launch` |
| 59 | + # will pass the `--local-rank` parameter to `tools/train.py` instead |
| 60 | + # of `--local_rank`. |
| 61 | + parser.add_argument('--local_rank', '--local-rank', type=int, default=0) |
| 62 | + args = parser.parse_args() |
| 63 | + if 'LOCAL_RANK' not in os.environ: |
| 64 | + os.environ['LOCAL_RANK'] = str(args.local_rank) |
| 65 | + |
| 66 | + return args |
| 67 | + |
| 68 | + |
| 69 | +def trigger_visualization_hook(cfg, args): |
| 70 | + default_hooks = cfg.default_hooks |
| 71 | + if 'visualization' in default_hooks: |
| 72 | + visualization_hook = default_hooks['visualization'] |
| 73 | + # Turn on visualization |
| 74 | + visualization_hook['draw'] = True |
| 75 | + if args.show: |
| 76 | + visualization_hook['show'] = True |
| 77 | + visualization_hook['wait_time'] = args.wait_time |
| 78 | + if args.show_dir: |
| 79 | + visualizer = cfg.visualizer |
| 80 | + visualizer['save_dir'] = args.show_dir |
| 81 | + else: |
| 82 | + raise RuntimeError( |
| 83 | + 'VisualizationHook must be included in default_hooks.' |
| 84 | + 'refer to usage ' |
| 85 | + '"visualization=dict(type=\'VisualizationHook\')"') |
| 86 | + |
| 87 | + return cfg |
| 88 | + |
| 89 | + |
| 90 | +def main(): |
| 91 | + args = parse_args() |
| 92 | + |
| 93 | + # load config |
| 94 | + cfg = Config.fromfile(args.config) |
| 95 | + cfg.launcher = args.launcher |
| 96 | + if args.cfg_options is not None: |
| 97 | + cfg.merge_from_dict(args.cfg_options) |
| 98 | + |
| 99 | + # work_dir is determined in this priority: CLI > segment in file > filename |
| 100 | + if args.work_dir is not None: |
| 101 | + # update configs according to CLI args if args.work_dir is not None |
| 102 | + cfg.work_dir = args.work_dir |
| 103 | + elif cfg.get('work_dir', None) is None: |
| 104 | + # use config filename as default work_dir if cfg.work_dir is None |
| 105 | + cfg.work_dir = osp.join('./work_dirs', |
| 106 | + osp.splitext(osp.basename(args.config))[0]) |
| 107 | + |
| 108 | + cfg.load_from = args.checkpoint |
| 109 | + |
| 110 | + if args.show or args.show_dir: |
| 111 | + cfg = trigger_visualization_hook(cfg, args) |
| 112 | + |
| 113 | + if args.tta: |
| 114 | + cfg.test_dataloader.dataset.pipeline = cfg.tta_pipeline |
| 115 | + cfg.tta_model.module = cfg.model |
| 116 | + cfg.model = cfg.tta_model |
| 117 | + |
| 118 | + # add output_dir in metric |
| 119 | + if args.out is not None: |
| 120 | + cfg.test_evaluator['output_dir'] = args.out |
| 121 | + cfg.test_evaluator['keep_results'] = True |
| 122 | + |
| 123 | + # build the runner from config |
| 124 | + if 'runner_type' not in cfg: |
| 125 | + # build the default runner |
| 126 | + runner = Runner.from_cfg(cfg) |
| 127 | + else: |
| 128 | + # build customized runner from the registry if 'runner_type' is set in the cfg |
| 129 | + runner = RUNNERS.build(cfg) |
| 130 | + |
| 131 | + # start testing |
| 132 | + runner.test() |
| 133 | + |
| 134 | + |
| 135 | +if __name__ == '__main__': |
| 136 | + main() |
0 commit comments