def main(): args = parse_args() cfg = Config.fromfile(args.config) # 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 if cfg.get('cudnn_benchmark', False): torch.backends.cudnn.benchmark = True cfg.model.pretrained = None cfg.data.test.test_mode = True # build the dataloader 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) dataset = build_dataset(cfg.data.test) data_loader = build_dataloader(dataset, samples_per_gpu=1, workers_per_gpu=cfg.data.workers_per_gpu, dist=False, shuffle=False) # build the model and load checkpoint cfg.model.train_cfg = None model = build_detector(cfg.model, test_cfg=cfg.get('test_cfg')) 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) model = MMDataParallel(model, device_ids=[0]) model.eval() # the first several iterations may be very slow so skip them num_warmup = 5 pure_inf_time = 0 # benchmark with 2000 image and take the average for i, data in enumerate(data_loader): torch.cuda.synchronize() start_time = time.perf_counter() with torch.no_grad(): model(return_loss=False, rescale=True, **data) torch.cuda.synchronize() elapsed = time.perf_counter() - start_time if i >= num_warmup: pure_inf_time += elapsed if (i + 1) % args.log_interval == 0: fps = (i + 1 - num_warmup) / pure_inf_time print(f'Done image [{i + 1:<3}/ 2000], fps: {fps:.1f} img / s') if (i + 1) == 2000: pure_inf_time += elapsed fps = (i + 1 - num_warmup) / pure_inf_time print(f'Overall fps: {fps:.1f} img / s') break
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) # set cudnn_benchmark if cfg.get('cudnn_benchmark', False): torch.backends.cudnn.benchmark = True cfg.model.pretrained = None if cfg.model.get('neck'): if cfg.model.neck.get('rfp_backbone'): if cfg.model.neck.rfp_backbone.get('pretrained'): cfg.model.neck.rfp_backbone.pretrained = None cfg.data.test.test_mode = True # init distributed env first, since logger depends on the dist info. if args.launcher == 'none': distributed = False else: distributed = True init_dist(args.launcher, **cfg.dist_params) # build the dataloader # TODO: support multiple images per gpu (only minor changes are needed) dataset = build_dataset(cfg.data.test) data_loader = build_dataloader( dataset, samples_per_gpu=1, workers_per_gpu=cfg.data.workers_per_gpu, dist=distributed, shuffle=False) # build the model and load checkpoint model = build_detector(cfg.model, train_cfg=None, test_cfg=cfg.test_cfg) fp16_cfg = cfg.get('fp16', None) if fp16_cfg is not None: wrap_fp16_model(model) checkpoint = load_checkpoint(model, args.checkpoint, map_location='cpu') if args.fuse_conv_bn: model = fuse_module(model) # old versions did not save class info in checkpoints, this walkaround is # for backward compatibility if 'CLASSES' in checkpoint['meta']: model.CLASSES = checkpoint['meta']['CLASSES'] else: model.CLASSES = dataset.CLASSES if not distributed: model = MMDataParallel(model, device_ids=[0]) outputs = single_gpu_test(model, data_loader, args.show, args.show_dir, 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.options is None else args.options if args.format_only: dataset.format_results(outputs, **kwargs) if args.eval: dataset.evaluate(outputs, args.eval, classwise=True, **kwargs)
def test_config_data_pipeline(): """Test whether the data pipeline is valid and can process corner cases. CommandLine: xdoctest -m tests/test_config.py test_config_build_data_pipeline """ from mmcv import Config from mmseg.datasets.pipelines import Compose import numpy as np config_dpath = _get_config_directory() print('Found config_dpath = {!r}'.format(config_dpath)) import glob config_fpaths = list(glob.glob(join(config_dpath, '**', '*.py'))) config_fpaths = [p for p in config_fpaths if p.find('_base_') == -1] config_names = [relpath(p, config_dpath) for p in config_fpaths] print('Using {} config files'.format(len(config_names))) for config_fname in config_names: config_fpath = join(config_dpath, config_fname) print( 'Building data pipeline, config_fpath = {!r}'.format(config_fpath)) config_mod = Config.fromfile(config_fpath) # remove loading pipeline load_img_pipeline = config_mod.train_pipeline.pop(0) to_float32 = load_img_pipeline.get('to_float32', False) config_mod.train_pipeline.pop(0) config_mod.test_pipeline.pop(0) train_pipeline = Compose(config_mod.train_pipeline) test_pipeline = Compose(config_mod.test_pipeline) img = np.random.randint(0, 255, size=(1024, 2048, 3), dtype=np.uint8) if to_float32: img = img.astype(np.float32) seg = np.random.randint(0, 255, size=(1024, 2048, 1), dtype=np.uint8) results = dict(filename='test_img.png', ori_filename='test_img.png', img=img, img_shape=img.shape, ori_shape=img.shape, gt_semantic_seg=seg) results['seg_fields'] = ['gt_semantic_seg'] print('Test training data pipeline: \n{!r}'.format(train_pipeline)) output_results = train_pipeline(results) assert output_results is not None results = dict( filename='test_img.png', ori_filename='test_img.png', img=img, img_shape=img.shape, ori_shape=img.shape, ) print('Test testing data pipeline: \n{!r}'.format(test_pipeline)) output_results = test_pipeline(results) assert output_results is not None
def main(): args = parse_args() 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 if cfg.get('cudnn_benchmark', False): torch.backends.cudnn.benchmark = True # work_dir is determined in this priority: CLI > segment in file > filename if args.work_dir is not None: # update configs according to CLI args if args.work_dir is not None cfg.work_dir = args.work_dir elif cfg.get('work_dir', None) is None: # use config filename as default work_dir if cfg.work_dir is None cfg.work_dir = osp.join('./work_dirs', osp.splitext(osp.basename(args.config))[0]) if args.resume_from is not None: cfg.resume_from = args.resume_from if args.load_from is not None: cfg.load_from = args.load_from if args.gpu_ids is not None: cfg.gpu_ids = args.gpu_ids else: cfg.gpu_ids = range(1) if args.gpus is None else range(args.gpus) # init distributed env first, since logger depends on the dist info. if args.launcher == 'none': distributed = False else: distributed = True 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 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 timestamp = time.strftime('%Y%m%d_%H%M%S', time.localtime()) log_file = osp.join(cfg.work_dir, f'{timestamp}.log') logger = get_root_logger(log_file=log_file, log_level=cfg.log_level) # init the meta dict to record some important information such as # environment info and seed, which will be logged meta = dict() # log env info env_info_dict = collect_env() env_info = '\n'.join([(f'{k}: {v}') for k, v in env_info_dict.items()]) dash_line = '-' * 60 + '\n' logger.info('Environment info:\n' + dash_line + env_info + '\n' + dash_line) meta['env_info'] = env_info meta['config'] = cfg.pretty_text # log some basic info logger.info(f'Distributed training: {distributed}') logger.info(f'Config:\n{cfg.pretty_text}') # set random seeds if args.seed is not None: logger.info(f'Set random seed to {args.seed}, ' f'deterministic: {args.deterministic}') set_random_seed(args.seed, deterministic=args.deterministic) cfg.seed = args.seed meta['seed'] = args.seed meta['exp_name'] = osp.basename(args.config) model = build_detector(cfg.model, train_cfg=cfg.train_cfg, test_cfg=cfg.test_cfg) datasets = [build_dataset(cfg.data.train)] if len(cfg.workflow) == 2: val_dataset = copy.deepcopy(cfg.data.val) val_dataset.pipeline = cfg.data.train.pipeline datasets.append(build_dataset(val_dataset)) if cfg.checkpoint_config is not None: # save mmdet version, config file content and class names in # checkpoints as meta data cfg.checkpoint_config.meta = dict(mmdet_version=__version__ + get_git_hash()[:7], CLASSES=datasets[0].CLASSES) # add an attribute for visualization convenience model.CLASSES = datasets[0].CLASSES train_detector(model, datasets, cfg, distributed=distributed, validate=(not args.no_validate), timestamp=timestamp, meta=meta)
def main(): args = parse_args() assert args.out or args.eval or args.format_only or args.show, \ ('Please specify at least one operation (save/eval/format/show the ' 'results) with the argument "--out", "--eval", "--format_only" ' 'or "--show"') 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) # set cudnn_benchmark if cfg.get('cudnn_benchmark', False): torch.backends.cudnn.benchmark = True cfg.model.pretrained = None cfg.data.test.test_mode = True # set random seeds if args.seed is not None: set_random_seed(args.seed, deterministic=args.deterministic) # build the dataloader samples_per_gpu = cfg.data.test.pop('samples_per_gpu', 1) dataset = build_dataset(cfg.data.mini_test) data_loader = build_dataloader(dataset, samples_per_gpu=samples_per_gpu, workers_per_gpu=cfg.data.workers_per_gpu, dist=False, shuffle=False) # data_batch = iter(data_loader).next() # print(len(data_batch['seg_label'][0].data[0])) # print(type(data_batch['seg_label'][0].data[0][0])) # print(data_batch['seg_label'][0].data[0][0].shape) # build the model and load checkpoint model = build_detector(cfg.model, train_cfg=None, test_cfg=cfg.test_cfg) fp16_cfg = cfg.get('fp16', None) if fp16_cfg is not None: wrap_fp16_model(model) checkpoint = load_checkpoint(model, args.checkpoint, map_location='cpu') if args.fuse_conv_bn: model = fuse_module(model) # old versions did not save class info in checkpoints, this walkaround is # for backward compatibility if 'CLASSES' in checkpoint['meta']: model.CLASSES = checkpoint['meta']['CLASSES'] else: model.CLASSES = dataset.CLASSES model = MMDataParallel(model, device_ids=[0]) outputs = mmda_single_gpu_test(model, data_loader, args.show, args.show_dir) 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.options is None else args.options if args.format_only: dataset.format_results(outputs, **kwargs) if args.eval: dataset.evaluate(outputs, args.eval, jsonfile_prefix="./checkpoints/box_eval", **kwargs)
parser.add_argument('--checkpoint', help='the checkpoint file to resume from') parser.add_argument('--gpus', type=int, default=1, help='number of gpus to use ' '(only applicable to non-distributed training)') parser.add_argument('--batchsize', type=int, default=6) parser.add_argument('--seed', type=int, default=7, help='random seed') parser.add_argument('--progress', action='store_true') args = parser.parse_args() return args if __name__ == '__main__': args = parse_args() cfg = Config.fromfile(args.cfg) # update configs according to CLI args cfg.gpus = args.gpus cfg.data.videos_per_gpu = args.batchsize if 'pretrained' in cfg['model']['backbone']: cfg['model']['backbone']['pretrained'] = None if args.work_dir is not None: cfg.work_dir = args.work_dir if args.data_dir is not None: if 'test' in cfg.data: cfg.data.test.root_dir = args.data_dir if args.checkpoint is not None: chkpt_list = [args.checkpoint] else: chkpt_list = [os.path.join(cfg.work_dir, fn) for fn in os.listdir(cfg.work_dir) if fn.endswith('.pth')]
def main(): args = parse_args() cfg = Config.fromfile(args.config) # set cudnn_benchmark if cfg.get('cudnn_benchmark', False): torch.backends.cudnn.benchmark = True # update configs according to CLI args if args.work_dir is not None: cfg.work_dir = args.work_dir if args.resume_from is not None: cfg.resume_from = args.resume_from cfg.gpus = args.gpus if args.autoscale_lr: # apply the linear scaling rule (https://arxiv.org/abs/1706.02677) cfg.optimizer['lr'] = cfg.optimizer['lr'] * cfg.gpus / 8 # init distributed env first, since logger depends on the dist info. if args.launcher == 'none': distributed = False else: distributed = True init_dist(args.launcher, **cfg.dist_params) # create work_dir mmcv.mkdir_or_exist(osp.abspath(cfg.work_dir)) # init the logger before other steps timestamp = time.strftime('%Y%m%d_%H%M%S', time.localtime()) log_file = osp.join(cfg.work_dir, '{}.log'.format(timestamp)) logger = get_root_logger(log_file=log_file, log_level=cfg.log_level) # init the meta dict to record some important information such as # environment info and seed, which will be logged meta = dict() # log env info env_info_dict = collect_env() env_info = '\n'.join([('{}: {}'.format(k, v)) for k, v in env_info_dict.items()]) dash_line = '-' * 60 + '\n' logger.info('Environment info:\n' + dash_line + env_info + '\n' + dash_line) meta['env_info'] = env_info # log some basic info logger.info('Distributed training: {}'.format(distributed)) logger.info('Config:\n{}'.format(cfg.text)) # set random seeds if args.seed is not None: logger.info('Set random seed to {}, deterministic: {}'.format( args.seed, args.deterministic)) set_random_seed(args.seed, deterministic=args.deterministic) cfg.seed = args.seed meta['seed'] = args.seed model = build_detector(cfg.model, train_cfg=cfg.train_cfg, test_cfg=cfg.test_cfg) datasets = [build_dataset(cfg.data.train)] if len(cfg.workflow) == 2: val_dataset = copy.deepcopy(cfg.data.val) val_dataset.pipeline = cfg.data.train.pipeline datasets.append(build_dataset(val_dataset)) if cfg.checkpoint_config is not None: # save mmdet version, config file content and class names in # checkpoints as meta data cfg.checkpoint_config.meta = dict(mmdet_version=__version__, config=cfg.text, CLASSES=datasets[0].CLASSES) # add an attribute for visualization convenience model.CLASSES = datasets[0].CLASSES train_detector(model, datasets, cfg, distributed=distributed, validate=args.validate, timestamp=timestamp, meta=meta)
def main(): args = parse_args() 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 if cfg.get('cudnn_benchmark', False): torch.backends.cudnn.benchmark = True cfg.model.pretrained = None cfg.data.test.test_mode = True if isinstance(cfg.data.test, dict): # specify manual img_size if args.img_size != 0: print("Scaling images to ", args.img_size) test_pipeline = cfg.data.test.pipeline for d in test_pipeline: if 'img_scale' in d: d['img_scale'] = (args.img_size, args.img_size) cfg.data.test.pipeline = test_pipeline # modify path # for split in ['train', 'val', 'test']: # if split in cfg.data: # for key in ['root', 'img_prefix']: # if key in cfg.data[split]: # cfg.data[split][key] = join(args.ssd_dir, cfg.data[split][key]) # for key in ['ann_file']: # if key in cfg.data[split]: # cfg.data[split][key] = join(args.data_dir, cfg.data[split][key]) # build the dataloader 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) dataset = build_dataset(cfg.data.test) data_loader = build_dataloader(dataset, samples_per_gpu=1, workers_per_gpu=cfg.data.workers_per_gpu, dist=False, shuffle=False) # build the model and load checkpoint model = build_detector(cfg.model, train_cfg=None, test_cfg=cfg.get('test_cfg')) load_checkpoint(model, args.checkpoint, map_location='cpu') if args.convert_repvgg: print("Converting repvgg model") cfg.model.backbone['deploy'] = True deploy_model = build_detector(cfg.model, test_cfg=cfg.get('test_cfg')) model = repvgg_det_model_convert(model, deploy_model) if args.fuse_conv_bn: model = fuse_conv_bn(model) fp16_cfg = cfg.get('fp16', None) if fp16_cfg is not None: wrap_fp16_model(model) # print(model) # @time_this() # def forward(self, *inputs, **kwargs): # """Override the original forward function. # The main difference lies in the CPU inference where the datas in # :class:`DataContainers` will still be gathered. # """ # if not self.device_ids: # # We add the following line thus the module could gather and # # convert data containers as those in GPU inference # inputs, kwargs = self.scatter(inputs, kwargs, [-1]) # return self.module(*inputs[0], **kwargs[0]) # else: # return super(MMDataParallel, self).forward(*inputs, **kwargs) # MMDataParallel.forward = forward # model = MMDataParallel(model, device_ids=[0]) model = model.to('cuda') def process_dc(data): n = len(data['img_metas']) for i in range(n): data['img_metas'][i] = data['img_metas'][i].data[0] return data model.eval() # the first several iterations may be very slow so skip them num_warmup = 5 model_time = [] encoding_time = [] results = [] for i, data in enumerate(data_loader): data = process_dc(data) data['img'][0] = data['img'][0].to('cuda') torch.cuda.synchronize() t1 = time.perf_counter() with torch.no_grad(): result = model(return_loss=False, rescale=True, **data) torch.cuda.synchronize() t2 = time.perf_counter() model_time.append(t2 - t1) if i >= num_warmup: if (i + 1) % args.log_interval == 0: print( f'Done image [{i + 1:<3}/ {args.n_sample}], runtime: {1e3*(sum(model_time[num_warmup:])/(len(model_time) - num_warmup)):.6g}ms, fps: {(i + 1 - num_warmup)/ sum(model_time[num_warmup:])} img / s' ) if (i + 1) == args.n_sample: fps = (i + 1 - num_warmup) / sum(model_time[num_warmup:]) print(f'Overall fps: {fps:.1f} img / s') break s2ms = lambda x: 1e3 * x print_stats(model_time[num_warmup:], 'Model time (ms)', cvt=s2ms) if args.eval_out: with open(args.eval_out, "a") as eval_out: print("\n") print(args.config, "img_size=", args.img_size if args.img_size != 0 else "1333,800", file=eval_out) print(f'fps: {args.n_sample/sum(model_time[num_warmup:])}', file=eval_out) print_stats(model_time[num_warmup:], 'Model time (ms)', cvt=s2ms, file=eval_out) print("\n", file=eval_out) # export_timings(model, args.timing_path, warmup=num_warmup) del results
def parse_results(config_file, resultfile, dstpath, type): cfg = Config.fromfile(config_file) data_test = cfg.data['test'] dataset = get_dataset(data_test) outputs = mmcv.load(resultfile) if type == 'OBB': # dota1 has tested obb_results_dict = OBBDetComp4(dataset, outputs) current_thresh = 0.1 elif type == 'HBB': # dota1 has tested hbb_results_dict = HBBDet2Comp4(dataset, outputs) elif type == 'HBBOBB': # dota1 has tested # dota2 hbb_results_dict, obb_results_dict = HBBOBB2Comp4(dataset, outputs) current_thresh = 0.3 elif type == 'Mask': # TODO: dota1 did not pass # dota2, hbb has passed, obb has passed hbb_results_dict, obb_results_dict = HBBSeg2Comp4(dataset, outputs) current_thresh = 0.3 dataset_type = cfg.dataset_type if 'obb_results_dict' in vars(): if not os.path.exists(os.path.join(dstpath, 'Task1_results')): os.makedirs(os.path.join(dstpath, 'Task1_results')) for cls in obb_results_dict: with open(os.path.join(dstpath, 'Task1_results', cls + '.txt'), 'w') as obb_f_out: for index, outline in enumerate(obb_results_dict[cls]): if index != (len(obb_results_dict[cls]) - 1): obb_f_out.write(outline + '\n') else: obb_f_out.write(outline) if not os.path.exists(os.path.join(dstpath, 'Task1_results_nms')): os.makedirs(os.path.join(dstpath, 'Task1_results_nms')) mergebypoly_multiprocess(os.path.join(dstpath, 'Task1_results'), os.path.join(dstpath, 'Task1_results_nms'), nms_type=r'py_cpu_nms_poly_fast', o_thresh=current_thresh) OBB2HBB(os.path.join(dstpath, 'Task1_results_nms'), os.path.join(dstpath, 'Transed_Task2_results_nms')) if 'hbb_results_dict' in vars(): if not os.path.exists(os.path.join(dstpath, 'Task2_results')): os.makedirs(os.path.join(dstpath, 'Task2_results')) for cls in hbb_results_dict: with open(os.path.join(dstpath, 'Task2_results', cls + '.txt'), 'w') as f_out: for index, outline in enumerate(hbb_results_dict[cls]): if index != (len(hbb_results_dict[cls]) - 1): f_out.write(outline + '\n') else: f_out.write(outline) if not os.path.exists(os.path.join(dstpath, 'Task2_results_nms')): os.makedirs(os.path.join(dstpath, 'Task2_results_nms')) mergebyrec(os.path.join(dstpath, 'Task2_results'), os.path.join(dstpath, 'Task2_results_nms'))
def main(): args = parse_args() # 参数读取:读取configs/*.py文件,并检测mmcv.config对象用于后续模型训练参数设置 cfg = Config.fromfile(args.config) # set cudnn_benchmark # 在图片输入尺度固定时开启,可以加速,一般都是关的,只有在固定尺度的网络如SSD512中才开启 if cfg.get('cudnn_benchmark', False): torch.backends.cudnn.benchmark = True # update configs according to CLI args if args.work_dir is not None: cfg.work_dir = args.work_dir if args.resume_from is not None: cfg.resume_from = args.resume_from cfg.gpus = args.gpus if args.autoscale_lr: # apply the linear scaling rule (https://arxiv.org/abs/1706.02677) cfg.optimizer['lr'] = cfg.optimizer['lr'] * cfg.gpus / 8 # init distributed env first, since logger depends on the dist info. if args.launcher == 'none': distributed = False else: distributed = True # print(cfg.dist_params) init_dist(args.launcher, **cfg.dist_params) # init logger before other steps logger = get_root_logger(cfg.log_level) logger.info('Distributed training: {}'.format(distributed)) logger.info('MMDetection Version: {}'.format(__version__)) logger.info('Config: {}'.format(cfg.text)) # set random seeds if args.seed is not None: logger.info('Set random seed to {}'.format(args.seed)) set_random_seed(args.seed) # 构建模型:调用mmdet.models.build_detector,从config中读取必要的模型参数构建模型,包括模型参数+训练参数+测试参数 model = build_detector(cfg.model, train_cfg=cfg.train_cfg, test_cfg=cfg.test_cfg) # 数据集读取:调用mmdet.datasets.build_dataset,根据从配置文件中读取训练测试数据集的信息,建立数据集对象 datasets = [build_dataset(cfg.data.train)] if len(cfg.workflow) == 2: datasets.append(build_dataset(cfg.data.val)) # 可以支持train+val训练 if cfg.checkpoint_config is not None: # save mmdet version, config file content and class names in # checkpoints as meta data cfg.checkpoint_config.meta = dict(mmdet_version=__version__, config=cfg.text, CLASSES=datasets[0].CLASSES) # add an attribute for visualization convenience model.CLASSES = datasets[0].CLASSES # 开始训练 train_detector(model, datasets, cfg, distributed=distributed, validate=args.validate, logger=logger)
def evaluate(MODEL_PATH, CFG_PATH): filenames = readlines("../mono/datasets/splits/nyu_labeled/test_files.txt") cfg = Config.fromfile(CFG_PATH) dataset = NYULabeledDataset(cfg.data['in_path'], filenames, cfg.data['height'], cfg.data['width'], [0], is_train=False) dataloader = DataLoader(dataset, 1, shuffle=False, num_workers=4, pin_memory=True, drop_last=False) cfg.model['imgs_per_gpu'] = 12 model = MONO.module_dict[cfg.model['name']](cfg.model) checkpoint = torch.load(MODEL_PATH) model.load_state_dict(checkpoint['state_dict'], strict=True) model.cuda() model.eval() pred_disps = [] with torch.no_grad(): for batch_idx, inputs in enumerate(dataloader): for key, ipt in inputs.items(): inputs[key] = ipt.cuda() outputs = model(inputs) disp = outputs[("disp", 0, 0)] pred_disp, _ = disp_to_depth(disp, 0.1, 100) pred_disp = pred_disp.cpu()[:, 0].numpy() pred_disps.append(pred_disp) pred_disps = np.concatenate(pred_disps) print("-> Evaluating") if cfg.data['stereo_scale']: print('using baseline') else: print('using mean scaling') errors = [] ratios = [] for i in range(pred_disps.shape[0]): gt_depth = dataset.get_depth(None, i) gt_height, gt_width = gt_depth.shape[:2] pred_disp = pred_disps[i] pred_disp = cv2.resize(pred_disp, (gt_width, gt_height)) pred_depth = 1 / pred_disp mask = np.logical_and(gt_depth > MIN_DEPTH, gt_depth < MAX_DEPTH) crop = np.array([0.40810811 * gt_height, 0.99189189 * gt_height, 0.03594771 * gt_width, 0.96405229 * gt_width]).astype(np.int32) crop_mask = np.zeros(mask.shape) crop_mask[crop[0]:crop[1], crop[2]:crop[3]] = 1 mask = np.logical_and(mask, crop_mask) pred_depth = pred_depth[mask] gt_depth = gt_depth[mask] ratio = np.median(gt_depth) / np.median(pred_depth) ratios.append(ratio) if cfg.data['stereo_scale']: ratio = STEREO_SCALE_FACTOR pred_depth *= ratio pred_depth[pred_depth < MIN_DEPTH] = MIN_DEPTH pred_depth[pred_depth > MAX_DEPTH] = MAX_DEPTH errors.append(compute_errors(gt_depth, pred_depth)) ratios = np.array(ratios) med = np.median(ratios) mean_errors = np.array(errors).mean(0) print("Scaling ratios | med: {:0.3f} | std: {:0.3f}".format(med, np.std(ratios / med))) print("\n" + ("{:>}| " * 7).format("abs_rel", "sq_rel", "rmse", "rmse_log", "a1", "a2", "a3")) print(("&{:.3f} " * 7).format(*mean_errors.tolist()) + "\\\\") print("\n-> Done!")
coarse_attr_prob, coarse_attr_predictor.attr_idx2name) cats = jsonResult(cate_prob, cate_predictor.cate_idx2name) colors = colorgram.extract(file, 5) color_names = [get_color_name(x.rgb)[1] for x in colors] resultDict = {} resultDict['attributes'] = coarse_attrs resultDict['categories'] = cats resultDict['colors'] = color_names return jsonify(resultDict) if __name__ == '__main__': cfg_fine = Config.fromfile( './configs/category_attribute_predict/global_predictor_vgg.py') cfg_coarse = Config.fromfile( './configs/attribute_predict_coarse/global_predictor_resnet_attr.py') cfg_ret = Config.fromfile('configs/retriever_in_shop/global_retriever_vgg_loss_id.py') # global attribute predictor will not use landmarks # just set a default value landmark_tensor = torch.zeros(8) model_fine = build_predictor(cfg_fine.model) load_checkpoint(model_fine, './checkpoint/vgg16_fine_global.pth', map_location='cpu') model_coarse = build_predictor(cfg_coarse.model) load_checkpoint(model_coarse, './checkpoint/resnet_coarse_global.pth', map_location='cpu')
def main(): opt_level = 'O1' args = parse_args() cfg = Config.fromfile(args.config) torch.backends.cudnn.benchmark = True if args.work_dir is not None: cfg.work_dir = args.work_dir if args.resume_from is not None: cfg.resume_from = args.resume_from cfg.gpus = args.gpus if args.autoscale_lr: cfg.optimizer['lr'] = cfg.optimizer['lr'] * cfg.gpus / 8 init_dist(args.launcher, **cfg.dist_params) # init logger before other steps logger = get_root_logger(cfg.log_level) logger.info('MMDetection Version: {}'.format(__version__)) logger.info('Config: {}'.format(cfg.text)) # set random seeds if args.seed is not None: logger.info('Set random seed to {}'.format(args.seed)) set_random_seed(args.seed) model = build_detector(cfg.model, train_cfg=cfg.train_cfg, test_cfg=cfg.test_cfg).cuda() model = convert_syncbn_model(model) dataset = [build_dataset(cfg.data.train)] if len(cfg.workflow) == 2: dataset.append(build_dataset(cfg.data.val)) if cfg.checkpoint_config is not None: # save mmdet version, config file content and class names in # checkpoints as meta data cfg.checkpoint_config.meta = dict(mmdet_version=__version__, config=cfg.text, CLASSES=dataset[0].CLASSES) # add an attribute for visualization convenience model.CLASSES = dataset[0].CLASSES # prepare data loaders data_loaders = [ build_dataloader(ds, cfg.data.imgs_per_gpu, cfg.data.workers_per_gpu, dist=True) for ds in dataset ] # After amp.initialize, wrap the model with apex.parallel.DistributedDataParallel. optimizer = build_optimizer(model, cfg.optimizer) model, optimizer = amp.initialize(model, optimizer, opt_level=opt_level) model = ApexDistributedDataParallel(model, message_size=1e8, delay_allreduce=True) # build runner runner = ApexRunner(model, batch_processor, amp, optimizer, cfg.work_dir, cfg.log_level) optimizer_config = ApexOptimizerHook(opt_level=opt_level, amp=amp, **cfg.optimizer_config) # register hooks runner.register_training_hooks(cfg.lr_config, optimizer_config, cfg.checkpoint_config, cfg.log_config) runner.register_hook(DistSamplerSeedHook()) if hasattr(cfg, 'extra_hooks'): import detector_utils.pytorch.utils.mmcv_custom_hooks for hook_args in cfg.extra_hooks: hook_type_name = hook_args['type'] del hook_args['type'] assert hasattr(detector_utils.pytorch.utils.mmcv_custom_hooks, hook_type_name), \ f"Unknown hook name: {hook_type_name}" hook_type = getattr(detector_utils.pytorch.utils.mmcv_custom_hooks, hook_type_name) hook = hook_type(**hook_args) runner.register_hook(hook) # register eval hooks if args.validate: val_dataset_cfg = cfg.data.val eval_cfg = cfg.get('evaluation', {}) runner.register_hook(CocoDistEvalmAPHook(val_dataset_cfg, **eval_cfg)) if cfg.resume_from: runner.resume(cfg.resume_from) elif cfg.load_from: runner.load_checkpoint(cfg.load_from) runner.run(data_loaders, cfg.workflow, cfg.total_epochs)
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) # 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 if cfg.get('cudnn_benchmark', False): torch.backends.cudnn.benchmark = True 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 if isinstance(cfg.data.test, dict): cfg.data.test.test_mode = True elif isinstance(cfg.data.test, list): for ds_cfg in cfg.data.test: ds_cfg.test_mode = True # init distributed env first, since logger depends on the dist info. if args.launcher == 'none': distributed = False else: distributed = True init_dist(args.launcher, **cfg.dist_params) # build the dataloader 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) dataset = build_dataset(cfg.data.test) data_loader = build_dataloader(dataset, samples_per_gpu=samples_per_gpu, workers_per_gpu=cfg.data.workers_per_gpu, dist=distributed, shuffle=False) # build the model and load checkpoint model = build_detector(cfg.model, train_cfg=None, test_cfg=cfg.test_cfg) fp16_cfg = cfg.get('fp16', None) if fp16_cfg is not None: wrap_fp16_model(model) checkpoint = load_checkpoint(model, args.checkpoint, map_location='cpu') if args.fuse_conv_bn: model = fuse_conv_bn(model) # old versions did not save class info in checkpoints, this walkaround is # for backward compatibility if 'CLASSES' in checkpoint['meta']: model.CLASSES = checkpoint['meta']['CLASSES'] else: model.CLASSES = dataset.CLASSES if not distributed: model = MMDataParallel(model, device_ids=[0]) outputs = single_gpu_test(model, data_loader, args.show, args.show_dir, 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) # outputs = mmcv.load(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))
def main(): args = parse_args() cfg = Config.fromfile(args.config) cfg.merge_from_dict(args.cfg_options) # Load output_config from cfg output_config = cfg.get('output_config', {}) if args.out: # Overwrite output_config from args.out output_config = Config._merge_a_into_b(dict(out=args.out), output_config) # Load eval_config from cfg eval_config = cfg.get('eval_config', {}) if args.eval: # Overwrite eval_config from args.eval eval_config = Config._merge_a_into_b(dict(metrics=args.eval), eval_config) if args.eval_options: # Add options from args.eval_options eval_config = Config._merge_a_into_b(args.eval_options, eval_config) assert output_config or eval_config, \ ('Please specify at least one operation (save or eval the ' 'results) with the argument "--out" or "--eval"') dataset_type = cfg.data.test.type if output_config.get('out', None): if 'output_format' in output_config: # ugly workround to make recognition and localization the same warnings.warn( 'Skip checking `output_format` in localization task.') else: out = output_config['out'] # make sure the dirname of the output path exists mmcv.mkdir_or_exist(osp.dirname(out)) _, suffix = osp.splitext(out) if dataset_type == 'AVADataset': assert suffix[1:] == 'csv', ('For AVADataset, the format of ' 'the output file should be csv') else: assert suffix[1:] in file_handlers, ( 'The format of the output ' 'file should be json, pickle or yaml') # set cudnn benchmark if cfg.get('cudnn_benchmark', False): torch.backends.cudnn.benchmark = True cfg.data.test.test_mode = True if args.average_clips is not None: # You can set average_clips during testing, it will override the # original setting if cfg.model.get('test_cfg') is None and cfg.get('test_cfg') is None: cfg.model.setdefault('test_cfg', dict(average_clips=args.average_clips)) else: if cfg.model.get('test_cfg') is not None: cfg.model.test_cfg.average_clips = args.average_clips else: cfg.test_cfg.average_clips = args.average_clips # init distributed env first, since logger depends on the dist info. if args.launcher == 'none': distributed = False else: distributed = True init_dist(args.launcher, **cfg.dist_params) # The flag is used to register module's hooks cfg.setdefault('module_hooks', []) # build the dataloader dataset = build_dataset(cfg.data.test, dict(test_mode=True)) dataloader_setting = dict(videos_per_gpu=cfg.data.get('videos_per_gpu', 1), workers_per_gpu=cfg.data.get( 'workers_per_gpu', 1), dist=distributed, shuffle=False) dataloader_setting = dict(dataloader_setting, **cfg.data.get('test_dataloader', {})) data_loader = build_dataloader(dataset, **dataloader_setting) # remove redundant pretrain steps for testing turn_off_pretrained(cfg.model) # build the model and load checkpoint model = build_model(cfg.model, train_cfg=None, test_cfg=cfg.get('test_cfg')) if len(cfg.module_hooks) > 0: register_module_hooks(model, cfg.module_hooks) 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=[0]) outputs = single_gpu_test(model, data_loader) 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 output_config.get('out', None): out = output_config['out'] print(f'\nwriting results to {out}') dataset.dump_results(outputs, **output_config) if eval_config: eval_res = dataset.evaluate(outputs, **eval_config) for name, val in eval_res.items(): print(f'{name}: {val:.04f}')
def set_configuration( self, cfg_in ): cfg = self.get_configuration() cfg.merge_config( cfg_in ) self._config_file = str( cfg.get_value( "config_file" ) ) self._seed_weights = str( cfg.get_value( "seed_weights" ) ) self._train_directory = str( cfg.get_value( "train_directory" ) ) self._output_directory = str( cfg.get_value( "output_directory" ) ) self._gpu_count = int( cfg.get_value( "gpu_count" ) ) self._integer_labels = strtobool( cfg.get_value( "integer_labels" ) ) self._launcher = str( cfg.get_value( "launcher" ) ) self._validate = strtobool( cfg.get_value( "validate" ) ) self._training_data = [] from mmcv import Config self._cfg = Config.fromfile( self._config_file ) if self._cfg.get( 'cudnn_benchmark', False ): torch.backends.cudnn.benchmark = True if self._train_directory is not None: self._cfg.work_dir = self._train_directory self._groundtruth_store = os.path.join( self._train_directory, self._tmp_annotation_file ) if not os.path.exists( self._train_directory ): os.mkdir( self._train_directory ) else: self._groundtruth_store = self._tmp_annotation_file if self._seed_weights is not None: self._cfg.resume_from = self._seed_weights if self._gpu_count > 0: self._cfg.gpus = self._gpu_count else: self._cfg.gpus = torch.cuda.device_count() if self._cfg.checkpoint_config is not None: from mmdet import __version__ self._cfg.checkpoint_config.meta = dict( mmdet_version=__version__, config=self._cfg.text ) if self._launcher == 'none': self._distributed = False else: self._distributed = True from mmdet.apis import init_dist init_dist( self._launcher, **self._cfg.dist_params ) from mmdet.apis import get_root_logger self._logger = get_root_logger( self._cfg.log_level ) self._logger.info( 'Distributed training: {}'.format( self._distributed ) ) if self._random_seed is not "none": logger.info( 'Set random seed to {}'.format( self._random_seed ) ) from mmdet.apis import set_random_seed set_random_seed( int( self._random_seed ) ) from mmdet.models import build_detector self._model = build_detector( self._cfg.model, train_cfg=self._cfg.train_cfg, test_cfg=self._cfg.test_cfg )
def main(): args = parse_args() cfg = Config.fromfile(args.config) if args.cfg_options is not None: cfg.merge_from_dict(args.cfg_options) # import modules from plguin/xx, registry will be updated if hasattr(cfg, 'plugin') & cfg.plugin: import importlib _module_dir = os.path.dirname(args.config) _module_dir = _module_dir.split('/') _module_path = _module_dir[0] for m in _module_dir[1:]: _module_path = _module_path + '.' + m print(_module_path) plg_lib = importlib.import_module(_module_path) # 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 if cfg.get('cudnn_benchmark', False): torch.backends.cudnn.benchmark = True cfg.model.pretrained = None # 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. if args.launcher == 'none': distributed = False else: distributed = True init_dist(args.launcher, **cfg.dist_params) # set random seeds if args.seed is not None: set_random_seed(args.seed, deterministic=args.deterministic) # build the dataloader dataset = build_dataset(cfg.data.test) data_loader = build_dataloader(dataset, samples_per_gpu=samples_per_gpu, workers_per_gpu=cfg.data.workers_per_gpu, dist=distributed, shuffle=False) # build the model and load checkpoint cfg.model.train_cfg = None model = build_detector(cfg.model, test_cfg=cfg.get('test_cfg')) fp16_cfg = cfg.get('fp16', None) if fp16_cfg is not None: wrap_fp16_model(model) checkpoint = 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=[0]) outputs = single_gpu_test(model, data_loader, args.show, args.show_dir) 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))