def test_single_gpu_test_kie_novisual(cfg_file): curr_dir = os.path.abspath(os.path.dirname(os.path.dirname(__file__))) config_file = os.path.join(curr_dir, cfg_file) cfg = Config.fromfile(config_file) meta_keys = list(cfg.data.test.pipeline[-1]['meta_keys']) must_keys = ['img_norm_cfg', 'ori_filename', 'img_shape'] for key in must_keys: meta_keys.append(key) cfg.data.test.pipeline[-1]['meta_keys'] = tuple(meta_keys) with tempfile.TemporaryDirectory() as tmpdirname: out_dir = osp.join(tmpdirname, 'tmp') model, data_loader = gene_sdmgr_model_dataloader(cfg, out_dir, curr_dir, empty_img=True) results = single_gpu_test(model, data_loader, out_dir=out_dir, is_kie=True) assert check_argument.is_type_list(results, dict) model, data_loader = gene_sdmgr_model_dataloader( cfg, out_dir, curr_dir) results = single_gpu_test(model, data_loader, out_dir=out_dir, is_kie=True) assert check_argument.is_type_list(results, dict)
def test_single_gpu_test_kie(cfg_file): curr_dir = os.path.abspath(os.path.dirname(os.path.dirname(__file__))) config_file = os.path.join(curr_dir, cfg_file) cfg = Config.fromfile(config_file) with tempfile.TemporaryDirectory() as tmpdirname: out_dir = osp.join(tmpdirname, 'tmp') model, data_loader = gene_sdmgr_model_dataloader( cfg, out_dir, curr_dir) results = single_gpu_test(model, data_loader, out_dir=out_dir, is_kie=True) assert check_argument.is_type_list(results, dict)
def test_single_gpu_test_det(cfg_file): curr_dir = os.path.abspath(os.path.dirname(os.path.dirname(__file__))) config_file = os.path.join(curr_dir, cfg_file) cfg = Config.fromfile(config_file) model = build_model(cfg) img_prefix = 'data/toy_dataset/imgs' ann_file = 'data/toy_dataset/instances_test.json' data_loader = generate_sample_dataloader(cfg, curr_dir, img_prefix, ann_file) with tempfile.TemporaryDirectory() as tmpdirname: out_dir = osp.join(tmpdirname, 'tmp') results = single_gpu_test(model, data_loader, out_dir=out_dir) assert check_argument.is_type_list(results, dict)
def main(): args = parse_args() 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 ' 'results / save the results) with the argument "--out", "--eval"' ', "--format-only", "--show" or "--show-dir".') if args.eval and args.format_only: raise ValueError('--eval and --format_only cannot be both specified.') if args.out is not None and not args.out.endswith(('.pkl', '.pickle')): raise ValueError('The output file must be a pkl file.') cfg = Config.fromfile(args.config) if args.cfg_options is not None: cfg.merge_from_dict(args.cfg_options) setup_multi_processes(cfg) # set cudnn_benchmark if cfg.get('cudnn_benchmark', False): torch.backends.cudnn.benchmark = True if cfg.model.get('pretrained'): cfg.model.pretrained = None if cfg.model.get('neck'): if isinstance(cfg.model.neck, list): for neck_cfg in cfg.model.neck: if neck_cfg.get('rfp_backbone'): if neck_cfg.rfp_backbone.get('pretrained'): neck_cfg.rfp_backbone.pretrained = None elif cfg.model.neck.get('rfp_backbone'): if cfg.model.neck.rfp_backbone.get('pretrained'): cfg.model.neck.rfp_backbone.pretrained = None # in case the test dataset is concatenated samples_per_gpu = (cfg.data.get('test_dataloader', {})).get( 'samples_per_gpu', cfg.data.get('samples_per_gpu', 1)) if samples_per_gpu > 1: cfg = disable_text_recog_aug_test(cfg) cfg = replace_image_to_tensor(cfg) # init distributed env first, since logger depends on the dist info. if args.launcher == 'none': cfg.gpu_ids = [args.gpu_id] distributed = False else: distributed = True init_dist(args.launcher, **cfg.dist_params) # build the dataloader dataset = build_dataset(cfg.data.test, dict(test_mode=True)) # step 1: give default values and override (if exist) from cfg.data default_loader_cfg = { **dict(seed=cfg.get('seed'), drop_last=False, dist=distributed), **({} if torch.__version__ != 'parrots' else dict( prefetch_num=2, pin_memory=False, )) } default_loader_cfg.update({ k: v for k, v in cfg.data.items() if k not in [ 'train', 'val', 'test', 'train_dataloader', 'val_dataloader', 'test_dataloader' ] }) test_loader_cfg = { **default_loader_cfg, **dict(shuffle=False, drop_last=False), **cfg.data.get('test_dataloader', {}), **dict(samples_per_gpu=samples_per_gpu) } data_loader = build_dataloader(dataset, **test_loader_cfg) # build the model and load checkpoint cfg.model.train_cfg = None model = build_detector(cfg.model, test_cfg=cfg.get('test_cfg')) model = revert_sync_batchnorm(model) fp16_cfg = cfg.get('fp16', None) if fp16_cfg is not None: wrap_fp16_model(model) load_checkpoint(model, args.checkpoint, map_location='cpu') if args.fuse_conv_bn: model = fuse_conv_bn(model) if not distributed: model = MMDataParallel(model, device_ids=cfg.gpu_ids) is_kie = cfg.model.type in ['SDMGR'] outputs = single_gpu_test(model, data_loader, args.show, args.show_dir, is_kie, args.show_score_thr) else: model = MMDistributedDataParallel( model.cuda(), device_ids=[torch.cuda.current_device()], broadcast_buffers=False) outputs = multi_gpu_test(model, data_loader, args.tmpdir, args.gpu_collect) rank, _ = get_dist_info() if rank == 0: if args.out: print(f'\nwriting results to {args.out}') mmcv.dump(outputs, args.out) kwargs = {} if args.eval_options is None else args.eval_options if args.format_only: dataset.format_results(outputs, **kwargs) if args.eval: 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))