def main(): args = parse_args() cfg = Config.fromfile(args.config) # set cudnn_benchmark if cfg.get('cudnn_benchmark', False): torch.backends.cudnn.benchmark = True # set random seeds if args.seed is not None: print('set random seed to', args.seed) set_random_seed(args.seed, deterministic=args.deterministic) # build the model and load checkpoint model = build_model(cfg.model, train_cfg=cfg.train_cfg, test_cfg=cfg.test_cfg) _ = load_checkpoint(model, args.checkpoint, map_location='cpu') # sanity check for models without ema if not model.use_ema: args.sample_model = 'orig' if args.sample_model == 'ema': generator = model.generator_ema else: generator = model.generator mmcv.print_log(f'Sampling model: {args.sample_model}', 'mmgen') mmcv.print_log(f'Show mode: {args.show_mode}', 'mmgen') mmcv.print_log(f'Samples path: {args.samples_path}', 'mmgen') generator.eval() if not args.use_cpu: generator = generator.cuda() # if given proj_latent, reset args.endpoint if args.proj_latent is not None: mmcv.print_log(f'Load projected latent: {args.proj_latent}', 'mmgen') proj_file = torch.load(args.proj_latent) proj_n = len(proj_file) setattr(args, 'endpoint', proj_n) assert args.space == 'w', 'Projected latent are w or w-plus latent.' noise_batch = [] for img_path in proj_file: noise_batch.append(proj_file[img_path]['latent'].unsqueeze(0)) noise_batch = torch.cat(noise_batch, dim=0).cuda() if args.use_cpu: noise_batch = noise_batch.to('cpu') if args.show_mode == 'sequence': assert args.endpoint >= 2 else: assert args.endpoint >= 2 and args.endpoint % 2 == 0,\ '''We need paired images in group mode, so keep endpoint an even number''' kwargs = dict(max_batch_size=args.batch_size) if args.sample_cfg is None: args.sample_cfg = dict() kwargs.update(args.sample_cfg) # remind users to fixed injected noise if kwargs.get('randomize_noise', 'True'): mmcv.print_log( '''Hint: For Style-Based GAN, you can add `--sample-cfg randomize_noise=False` to fix injected noises''', 'mmgen') # get noises corresponding to each endpoint if not args.proj_latent: noise_batch = batch_inference( generator, None, num_batches=args.endpoint, dict_key='noise_batch' if args.space == 'z' else 'latent', return_noise=True, **kwargs) if args.space == 'w': kwargs['truncation_latent'] = generator.get_mean_latent() kwargs['input_is_latent'] = True if args.show_mode == 'sequence': results = sample_from_path(generator, noise_batch[:-1, ], noise_batch[1:, ], args.interval, args.interp_mode, args.space, **kwargs) else: results = sample_from_path(generator, noise_batch[::2, ], noise_batch[1::2, ], args.interval, args.interp_mode, args.space, **kwargs) # reorder results results = torch.stack(results).permute(1, 0, 2, 3, 4) _, _, ch, h, w = results.shape results = results.reshape(-1, ch, h, w) # rescale value range to [0, 1] results = ((results + 1) / 2) results = results[:, [2, 1, 0], ...] results = results.clamp_(0, 1) # save image mmcv.mkdir_or_exist(args.samples_path) if args.show_mode == 'sequence': if args.export_video: # render video. video_out = imageio.get_writer(os.path.join( args.samples_path, 'lerp.mp4'), mode='I', fps=60, codec='libx264', bitrate='12M') video_out = layout_grid(video_out, results) video_out.close() else: for i in range(results.shape[0]): image = results[i:i + 1] save_image( image, os.path.join(args.samples_path, '{:0>5d}'.format(i) + '.png')) else: if args.export_video: # render video. video_out = imageio.get_writer(os.path.join( args.samples_path, 'lerp.mp4'), mode='I', fps=60, codec='libx264', bitrate='12M') n_pair = args.endpoint // 2 grid_w, grid_h = crack_integer(n_pair) video_out = layout_grid(video_out, results, grid_h=grid_h, grid_w=grid_w) video_out.close() else: save_image(results, os.path.join(args.samples_path, 'group.png'), nrow=args.interval)
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_start_time = time.time() cfg = Config.fromfile(args.config) cfg_last = time.time() - cfg_start_time print('cfg time:', cfg_last) # 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_start_time = time.time() 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=False, shuffle=False) dataset_last = time.time() - dataset_start_time print('dataset & dataloader time:', dataset_last) # data_batch = iter(data_loader).next() # print(len(data_batch['points'][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_start_time = time.time() 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_time = time.time() - model_start_time print('model time:', model_time) model = MMDataParallel(model, device_ids=[0]) outputs = mmda_single_gpu_test(model, data_loader, args.show, args.show_dir) # len(outputs)=len(dataset) 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, pkl_path=cfg.data.test.ann_file, jsonfile_prefix=args.json, **kwargs)
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 if hasattr(cfg.model, 'detector'): cfg.model.detector.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 model = build_model(cfg.model) fp16_cfg = cfg.get('fp16', None) if fp16_cfg is not None: wrap_fp16_model(model) if args.checkpoint is not None: 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() 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) # 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) if args.data_phase != 'train': cfg.data.train = cfg.data.get(args.data_phase) 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() 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 # 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, 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('Env 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: 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 # ipdb.set_trace(context=10) train_detector(model=model, dataset=datasets, cfg=cfg, distributed=distributed, validate=args.validate, timestamp=timestamp, meta=meta)
def _format_bbox(self, results, jsonfile_prefix=None): """Convert the results to the standard format. Args: results (list[dict]): Testing results of the dataset. jsonfile_prefix (str): The prefix of the output jsonfile. You can specify the output directory/filename by modifying the jsonfile_prefix. Default: None. Returns: str: Path of the output json file. """ nusc_annos = {} mapped_class_names = self.CLASSES print('Start to convert detection format...') CAM_NUM = 6 for sample_id, det in enumerate(mmcv.track_iter_progress(results)): if sample_id % CAM_NUM == 0: boxes_per_frame = [] attrs_per_frame = [] # need to merge results from images of the same sample annos = [] boxes, attrs = output_to_nusc_box(det) sample_token = self.data_infos[sample_id]['token'] boxes, attrs = cam_nusc_box_to_global(self.data_infos[sample_id], boxes, attrs, mapped_class_names, self.eval_detection_configs, self.eval_version) boxes_per_frame.extend(boxes) attrs_per_frame.extend(attrs) # Remove redundant predictions caused by overlap of images if (sample_id + 1) % CAM_NUM != 0: continue boxes = global_nusc_box_to_cam( self.data_infos[sample_id + 1 - CAM_NUM], boxes_per_frame, mapped_class_names, self.eval_detection_configs, self.eval_version) cam_boxes3d, scores, labels = nusc_box_to_cam_box3d(boxes) # box nms 3d over 6 images in a frame # TODO: move this global setting into config nms_cfg = dict(use_rotate_nms=True, nms_across_levels=False, nms_pre=4096, nms_thr=0.05, score_thr=0.01, min_bbox_size=0, max_per_frame=500) from mmcv import Config nms_cfg = Config(nms_cfg) cam_boxes3d_for_nms = xywhr2xyxyr(cam_boxes3d.bev) boxes3d = cam_boxes3d.tensor # generate attr scores from attr labels attrs = labels.new_tensor([attr for attr in attrs_per_frame]) boxes3d, scores, labels, attrs = box3d_multiclass_nms( boxes3d, cam_boxes3d_for_nms, scores, nms_cfg.score_thr, nms_cfg.max_per_frame, nms_cfg, mlvl_attr_scores=attrs) cam_boxes3d = CameraInstance3DBoxes(boxes3d, box_dim=9) det = bbox3d2result(cam_boxes3d, scores, labels, attrs) boxes, attrs = output_to_nusc_box(det) boxes, attrs = cam_nusc_box_to_global( self.data_infos[sample_id + 1 - CAM_NUM], boxes, attrs, mapped_class_names, self.eval_detection_configs, self.eval_version) for i, box in enumerate(boxes): name = mapped_class_names[box.label] attr = self.get_attr_name(attrs[i], name) nusc_anno = dict(sample_token=sample_token, translation=box.center.tolist(), size=box.wlh.tolist(), rotation=box.orientation.elements.tolist(), velocity=box.velocity[:2].tolist(), detection_name=name, detection_score=box.score, attribute_name=attr) annos.append(nusc_anno) # other views results of the same frame should be concatenated if sample_token in nusc_annos: nusc_annos[sample_token].extend(annos) else: nusc_annos[sample_token] = annos nusc_submissions = { 'meta': self.modality, 'results': nusc_annos, } mmcv.mkdir_or_exist(jsonfile_prefix) res_path = osp.join(jsonfile_prefix, 'results_nusc.json') print('Results writes to', res_path) mmcv.dump(nusc_submissions, res_path) return res_path
from mmcv import Config cfg = Config.fromfile( './configs/recognition/tsn/tsn_r50_video_1x1x8_100e_kinetics400_rgb.py') from mmcv.runner import set_random_seed # Modify dataset type and path # cfg.dataset_type = 'VideoDataset' # cfg.data_root = 'kinetics400_tiny/train/' # cfg.data_root_val = 'kinetics400_tiny/val/' # cfg.ann_file_train = 'kinetics400_tiny/kinetics_tiny_train_video.txt' # cfg.ann_file_val = 'kinetics400_tiny/kinetics_tiny_val_video.txt' # cfg.ann_file_test = 'kinetics400_tiny/kinetics_tiny_val_video.txt' # cfg.data.test.type = 'VideoDataset' # cfg.data.test.ann_file = 'kinetics400_tiny/kinetics_tiny_val_video.txt' # cfg.data.test.data_prefix = 'kinetics400_tiny/val/' # cfg.data.train.type = 'VideoDataset' # cfg.data.train.ann_file = 'kinetics400_tiny/kinetics_tiny_train_video.txt' # cfg.data.train.data_prefix = 'kinetics400_tiny/train/' # cfg.data.val.type = 'VideoDataset' # cfg.data.val.ann_file = 'kinetics400_tiny/kinetics_tiny_val_video.txt' # cfg.data.val.data_prefix = 'kinetics400_tiny/val/' # The flag is used to determine whether it is omnisource training cfg.setdefault('omnisource', False) # Modify num classes of the model in cls_head cfg.model.cls_head.num_classes = 3 # We can use the pre-trained TSN model
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, f'{timestamp}.log') logger = get_root_logger(log_file=log_file, log_level=cfg.log_level) # log env info env_info_dict = collect_env.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) # log some basic info logger.info('Distributed training: {}'.format(distributed)) logger.info('mmedit Version: {}'.format(__version__)) 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 model = build_model(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 version, config file content and class names in # checkpoints as meta data cfg.checkpoint_config.meta = dict( mmedit_version=__version__, config=cfg.text, ) # meta information meta = dict() if cfg.get('exp_name', None) is None: cfg['exp_name'] = osp.splitext(osp.basename(cfg.work_dir))[0] meta['exp_name'] = cfg.exp_name meta['mmedit Version'] = __version__ meta['seed'] = args.seed meta['env_info'] = env_info # add an attribute for visualization convenience train_model(model, datasets, cfg, distributed=distributed, validate=(not args.no_validate), timestamp=timestamp, meta=meta)
def test_reserved_key(): cfg_file = osp.join(data_path, 'config/g.py') with pytest.raises(KeyError): Config.fromfile(cfg_file)
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 cfg.get('USE_MMDET', False): from mmdet.apis import multi_gpu_test, single_gpu_test from mmdet.models import build_detector as build_model from mmdet.datasets import build_dataloader else: from qdtrack.apis import multi_gpu_test, single_gpu_test from qdtrack.models import build_model from qdtrack.datasets import build_dataloader if args.cfg_options is not None: cfg.merge_from_dict(args.cfg_options) # set cudnn_benchmark if cfg.get('cudnn_benchmark', False): torch.backends.cudnn.benchmark = True cfg.model.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 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_model(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) 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) 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']: eval_kwargs.pop(key, None) eval_kwargs.update(dict(metric=args.eval, **kwargs)) # print(dataset.evaluate(outputs, **eval_kwargs)) # debugging using lvis, remove the track results first because lvis expects only the detection results det_outputs = outputs['bbox_result'] print(dataset.evaluate(det_outputs, **eval_kwargs))
def test_construct(): cfg = Config() assert cfg.filename is None assert cfg.text == '' assert len(cfg) == 0 assert cfg._cfg_dict == {} with pytest.raises(TypeError): Config([0, 1]) cfg_dict = dict(item1=[1, 2], item2=dict(a=0), item3=True, item4='test') # test a.py cfg_file = osp.join(data_path, 'config/a.py') cfg = Config(cfg_dict, filename=cfg_file) assert isinstance(cfg, Config) assert cfg.filename == cfg_file assert cfg.text == open(cfg_file, 'r').read() assert cfg.dump() == cfg.pretty_text with tempfile.TemporaryDirectory() as temp_config_dir: dump_file = osp.join(temp_config_dir, 'a.py') cfg.dump(dump_file) assert cfg.dump() == open(dump_file, 'r').read() assert Config.fromfile(dump_file) # test b.json cfg_file = osp.join(data_path, 'config/b.json') cfg = Config(cfg_dict, filename=cfg_file) assert isinstance(cfg, Config) assert cfg.filename == cfg_file assert cfg.text == open(cfg_file, 'r').read() assert cfg.dump() == json.dumps(cfg_dict) with tempfile.TemporaryDirectory() as temp_config_dir: dump_file = osp.join(temp_config_dir, 'b.json') cfg.dump(dump_file) assert cfg.dump() == open(dump_file, 'r').read() assert Config.fromfile(dump_file) # test c.yaml cfg_file = osp.join(data_path, 'config/c.yaml') cfg = Config(cfg_dict, filename=cfg_file) assert isinstance(cfg, Config) assert cfg.filename == cfg_file assert cfg.text == open(cfg_file, 'r').read() assert cfg.dump() == yaml.dump(cfg_dict) with tempfile.TemporaryDirectory() as temp_config_dir: dump_file = osp.join(temp_config_dir, 'c.yaml') cfg.dump(dump_file) assert cfg.dump() == open(dump_file, 'r').read() assert Config.fromfile(dump_file) # test h.py cfg_file = osp.join(data_path, 'config/h.py') cfg_dict = dict(item1='h.py', item2=f'{osp.dirname(__file__)}/data/config', item3='abc_h') cfg = Config(cfg_dict, filename=cfg_file) assert isinstance(cfg, Config) assert cfg.filename == cfg_file assert cfg.text == open(cfg_file, 'r').read() assert cfg.dump() == cfg.pretty_text with tempfile.TemporaryDirectory() as temp_config_dir: dump_file = osp.join(temp_config_dir, 'h.py') cfg.dump(dump_file) assert cfg.dump() == open(dump_file, 'r').read() assert Config.fromfile(dump_file) assert Config.fromfile(dump_file)['item1'] == cfg_dict['item1'] assert Config.fromfile(dump_file)['item2'] == cfg_dict['item2'] assert Config.fromfile(dump_file)['item3'] == cfg_dict['item3'] # test no use_predefined_variable cfg_dict = dict(item1='{{fileBasename}}', item2='{{ fileDirname}}', item3='abc_{{ fileBasenameNoExtension }}') assert Config.fromfile(cfg_file, False) assert Config.fromfile(cfg_file, False)['item1'] == cfg_dict['item1'] assert Config.fromfile(cfg_file, False)['item2'] == cfg_dict['item2'] assert Config.fromfile(cfg_file, False)['item3'] == cfg_dict['item3'] # test p.yaml cfg_file = osp.join(data_path, 'config/p.yaml') cfg_dict = dict(item1=f'{osp.dirname(__file__)}/data/config') cfg = Config(cfg_dict, filename=cfg_file) assert isinstance(cfg, Config) assert cfg.filename == cfg_file assert cfg.text == open(cfg_file, 'r').read() assert cfg.dump() == yaml.dump(cfg_dict) with tempfile.TemporaryDirectory() as temp_config_dir: dump_file = osp.join(temp_config_dir, 'p.yaml') cfg.dump(dump_file) assert cfg.dump() == open(dump_file, 'r').read() assert Config.fromfile(dump_file) assert Config.fromfile(dump_file)['item1'] == cfg_dict['item1'] # test no use_predefined_variable assert Config.fromfile(cfg_file, False) assert Config.fromfile(cfg_file, False)['item1'] == '{{ fileDirname }}' # test o.json cfg_file = osp.join(data_path, 'config/o.json') cfg_dict = dict(item1=f'{osp.dirname(__file__)}/data/config') cfg = Config(cfg_dict, filename=cfg_file) assert isinstance(cfg, Config) assert cfg.filename == cfg_file assert cfg.text == open(cfg_file, 'r').read() assert cfg.dump() == json.dumps(cfg_dict) with tempfile.TemporaryDirectory() as temp_config_dir: dump_file = osp.join(temp_config_dir, 'o.json') cfg.dump(dump_file) assert cfg.dump() == open(dump_file, 'r').read() assert Config.fromfile(dump_file) assert Config.fromfile(dump_file)['item1'] == cfg_dict['item1'] # test no use_predefined_variable assert Config.fromfile(cfg_file, False) assert Config.fromfile(cfg_file, False)['item1'] == '{{ fileDirname }}'
def main(): args = parse_args() cfg = Config.fromfile(args.config) if args.cfg_options is not None: cfg.merge_from_dict(args.cfg_options) # set cudnn_benchmark if cfg.get('cudnn_benchmark', False): torch.backends.cudnn.benchmark = 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) rank, _ = get_dist_info() dirname = os.path.dirname(args.checkpoint) ckpt = os.path.basename(args.checkpoint) if 'http' in args.checkpoint: log_path = None else: log_name = ckpt.split('.')[0] + '_eval_log' + '.txt' log_path = os.path.join(dirname, log_name) logger = get_root_logger(log_file=log_path, log_level=cfg.log_level, file_mode='a') logger.info('evaluation') # set random seeds if args.seed is not None: if rank == 0: mmcv.print_log(f'set random seed to {args.seed}', 'mmgen') set_random_seed(args.seed, deterministic=args.deterministic) # build the model and load checkpoint model = build_model(cfg.model, train_cfg=cfg.train_cfg, test_cfg=cfg.test_cfg) # sanity check for models without ema if not model.use_ema: args.sample_model = 'orig' mmcv.print_log(f'Sampling model: {args.sample_model}', 'mmgen') model.eval() if not distributed: _ = load_checkpoint(model, args.checkpoint, map_location='cpu') model = MMDataParallel(model, device_ids=[0]) # build metrics if args.eval: if args.eval[0] == 'none': # only sample images metrics = [] assert args.num_samples is not None and args.num_samples > 0 else: metrics = [ build_metric(cfg.metrics[metric]) for metric in args.eval ] else: metrics = [ build_metric(cfg.metrics[metric]) for metric in cfg.metrics ] basic_table_info = dict(train_cfg=os.path.basename(cfg._filename), ckpt=ckpt, sample_model=args.sample_model) if len(metrics) == 0: basic_table_info['num_samples'] = args.num_samples data_loader = None else: basic_table_info['num_samples'] = -1 # build the dataloader if cfg.data.get('test', None) and cfg.data.test.get( 'imgs_root', None): dataset = build_dataset(cfg.data.test) elif cfg.data.get('val', None) and cfg.data.val.get( 'imgs_root', None): dataset = build_dataset(cfg.data.val) elif cfg.data.get('train', None): # we assume that the train part should work well dataset = build_dataset(cfg.data.train) else: raise RuntimeError('There is no valid dataset config to run, ' 'please check your dataset configs.') data_loader = build_dataloader(dataset, samples_per_gpu=args.batch_size, workers_per_gpu=cfg.data.get( 'val_workers_per_gpu', cfg.data.workers_per_gpu), dist=distributed, shuffle=True) if args.sample_cfg is None: args.sample_cfg = dict() # online mode will not save samples if args.online and len(metrics) > 0: single_gpu_online_evaluation(model, data_loader, metrics, logger, basic_table_info, args.batch_size, **args.sample_cfg) else: single_gpu_evaluation(model, data_loader, metrics, logger, basic_table_info, args.batch_size, args.samples_path, **args.sample_cfg) else: raise NotImplementedError("We hasn't implemented multi gpu eval yet.")
def main(): args = parse_args() if args.tensorrt and args.onnx: raise ValueError( 'Cannot set onnx mode and tensorrt mode at the same time.') 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 # 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) if args.tensorrt: outputs = inference_tensorrt(args.checkpoint, distributed, data_loader, dataloader_setting['videos_per_gpu']) elif args.onnx: outputs = inference_onnx(args.checkpoint, distributed, data_loader, dataloader_setting['videos_per_gpu']) else: outputs = inference_pytorch(args, cfg, distributed, data_loader) 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 test_vid_fgfa_style_forward(cfg_file): config = Config.fromfile(cfg_file) model = copy.deepcopy(config.model) model.pretrains = None model.detector.pretrained = None from mmtrack.models import build_model detector = build_model(model) # Test forward train with a non-empty truth batch input_shape = (1, 3, 256, 256) mm_inputs = _demo_mm_inputs(input_shape, num_items=[10]) imgs = mm_inputs.pop('imgs') img_metas = mm_inputs.pop('img_metas') img_metas[0]['is_video_data'] = True gt_bboxes = mm_inputs['gt_bboxes'] gt_labels = mm_inputs['gt_labels'] gt_masks = mm_inputs['gt_masks'] ref_input_shape = (2, 3, 256, 256) ref_mm_inputs = _demo_mm_inputs(ref_input_shape, num_items=[9, 11]) ref_img = ref_mm_inputs.pop('imgs')[None] ref_img_metas = ref_mm_inputs.pop('img_metas') ref_img_metas[0]['is_video_data'] = True ref_img_metas[1]['is_video_data'] = True ref_gt_bboxes = ref_mm_inputs['gt_bboxes'] ref_gt_labels = ref_mm_inputs['gt_labels'] ref_gt_masks = ref_mm_inputs['gt_masks'] losses = detector.forward(img=imgs, img_metas=img_metas, gt_bboxes=gt_bboxes, gt_labels=gt_labels, ref_img=ref_img, ref_img_metas=[ref_img_metas], ref_gt_bboxes=ref_gt_bboxes, ref_gt_labels=ref_gt_labels, gt_masks=gt_masks, ref_gt_masks=ref_gt_masks, return_loss=True) assert isinstance(losses, dict) loss, _ = detector._parse_losses(losses) loss.requires_grad_(True) assert float(loss.item()) > 0 loss.backward() # Test forward train with an empty truth batch mm_inputs = _demo_mm_inputs(input_shape, num_items=[0]) imgs = mm_inputs.pop('imgs') img_metas = mm_inputs.pop('img_metas') img_metas[0]['is_video_data'] = True gt_bboxes = mm_inputs['gt_bboxes'] gt_labels = mm_inputs['gt_labels'] gt_masks = mm_inputs['gt_masks'] ref_mm_inputs = _demo_mm_inputs(ref_input_shape, num_items=[0, 0]) ref_imgs = ref_mm_inputs.pop('imgs')[None] ref_img_metas = ref_mm_inputs.pop('img_metas') ref_img_metas[0]['is_video_data'] = True ref_img_metas[1]['is_video_data'] = True ref_gt_bboxes = ref_mm_inputs['gt_bboxes'] ref_gt_labels = ref_mm_inputs['gt_labels'] ref_gt_masks = ref_mm_inputs['gt_masks'] losses = detector.forward(img=imgs, img_metas=img_metas, gt_bboxes=gt_bboxes, gt_labels=gt_labels, ref_img=ref_imgs, ref_img_metas=[ref_img_metas], ref_gt_bboxes=ref_gt_bboxes, ref_gt_labels=ref_gt_labels, gt_masks=gt_masks, ref_gt_masks=ref_gt_masks, return_loss=True) assert isinstance(losses, dict) loss, _ = detector._parse_losses(losses) loss.requires_grad_(True) assert float(loss.item()) > 0 loss.backward() # Test forward test with frame_stride=1 and frame_range=[-1,0] with torch.no_grad(): imgs = torch.cat([imgs, imgs.clone()], dim=0) img_list = [g[None, :] for g in imgs] img_metas.extend(copy.deepcopy(img_metas)) for i in range(len(img_metas)): img_metas[i]['frame_id'] = i img_metas[i]['num_left_ref_imgs'] = 1 img_metas[i]['frame_stride'] = 1 ref_imgs = [ref_imgs.clone(), imgs[[0]][None].clone()] ref_img_metas = [ copy.deepcopy(ref_img_metas), copy.deepcopy([img_metas[0]]) ] results = defaultdict(list) for one_img, one_meta, ref_img, ref_img_meta in zip( img_list, img_metas, ref_imgs, ref_img_metas): result = detector.forward([one_img], [[one_meta]], ref_img=[ref_img], ref_img_metas=[[ref_img_meta]], return_loss=False) for k, v in result.items(): results[k].append(v)
left_margin = int(round((w - tw) / 2.)) im = im.crop((left_margin, top_margin, left_margin + tw, top_margin + th)) gt = np.ascontiguousarray(gt, dtype=np.float32) / 256.0 gt = gt.reshape((h, w)) gt = gt[top_margin:top_margin + th, left_margin:left_margin + tw] processed = get_transform(augment=False) im = processed(im) sample = {} sample['leftImage'] = im sample['left_gt'] = gt if self.transform is not None: sample = self.transform(sample) return sample def name(self): return 'KittiRaw' if __name__ == "__main__": from mmcv import Config cfg = '../../config/bts_cfg.py' cfg = Config.fromfile(cfg) kitti_dataset = KittiRaw(cfg, 'train') print(kitti_dataset[0]['left_gt'].max()) print("*")
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) # 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) # 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=False, shuffle=False) if args.backend == 'onnxruntime': from mmdet.core.export.model_wrappers import ONNXRuntimeDetector model = ONNXRuntimeDetector(args.model, class_names=dataset.CLASSES, device_id=0) elif args.backend == 'tensorrt': from mmdet.core.export.model_wrappers import TensorRTDetector model = TensorRTDetector(args.model, class_names=dataset.CLASSES, device_id=0) model = MMDataParallel(model, device_ids=[0]) outputs = single_gpu_test(model, data_loader, args.show, args.show_dir, args.show_score_thr) 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))
def main(): parser = ArgumentParser() # parser.add_argument("pcd", help="Point cloud file") parser.add_argument("config", help="Config file") parser.add_argument("checkpoint", help="Checkpoint file") parser.add_argument("--device", default="cuda:0", help="Device used for inference") parser.add_argument("--score-thr", type=float, default=0.6, help="bbox score threshold") parser.add_argument("--out-dir", type=str, default="demo", help="dir to save results") args = parser.parse_args() cfg = Config.fromfile(args.config) # build the model from a config file and a checkpoint file model = init_detector(args.config, args.checkpoint, device=args.device) # model = MMDataParallel(model, device_ids=[0]) device = next(model.parameters()).device dataset = build_dataset(cfg.data.test) # print(model) # test a single image # result, data = inference_detector(model, args.pcd) data = dataset[10] data = collate([data], samples_per_gpu=1) if next(model.parameters()).is_cuda: # scatter to specified GPU data = scatter(data, [device.index])[0] else: # this is a workaround to avoid the bug of MMDataParallel data["img_metas"] = data["img_metas"][0].data data["points"] = data["points"][0].data # print(data) with torch.no_grad(): result = model(return_loss=False, rescale=True, **data) # show the results # print(result) model.show_results(data, result, args.out_dir) with torch.cuda.device(0): input_data = data input_data["return_loss"] = False input_data["rescale"] = True macs, params = get_model_profile( model=model, # model input_res=(1, 60000, 4), # input shape or input to the input_constructor input_constructor=lambda x: input_data, # if specified, a constructor taking input_res is used as input to the model print_profile= True, # prints the model graph with the measured profile attached to each module detailed=True, # print the detailed profile module_depth= -1, # depth into the nested modules with -1 being the inner most modules top_modules= 3, # the number of top modules to print aggregated profile warm_up= 10, # the number of warm-ups before measuring the time of each module as_string= True, # print raw numbers (e.g. 1000) or as human-readable strings (e.g. 1k) ignore_modules= None, # the list of modules to ignore in the profiling )
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.resume_from is not None: cfg.resume_from = args.resume_from cfg.gpus = args.gpus 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) # init logger before other steps logger = get_root_logger(cfg.log_level) logger.info('Distributed training: {}'.format(distributed)) # set random seeds if args.seed is not None: logger.info('Set random seed to {}'.format(args.seed)) set_random_seed(args.seed) datasets = [build_dataset(cfg.data.train)] model = build_detector(cfg.model, train_cfg=cfg.train_cfg, test_cfg=cfg.test_cfg) if cfg.load_from: checkpoint = load_checkpoint(model, cfg.load_from, map_location='cpu') model.CLASSES = datasets[0].CLASSES if cfg.load_from: checkpoint = load_checkpoint(model, cfg.load_from, map_location='cpu') model.CLASSES = datasets[0].CLASSES 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) data_loader = build_dataloader(datasets[0], imgs_per_gpu=cfg.data.imgs_per_gpu, workers_per_gpu=cfg.data.workers_per_gpu, num_gpus=cfg.gpus, dist=False, shuffle=False) # put model on gpus model = MMDataParallel(model, device_ids=range(cfg.gpus)).cuda() model.train() if hasattr(model, 'module'): model_load = model.module optimizer = obj_from_dict(cfg.optimizer, torch.optim, dict(params=model_load.parameters())) check_video = None start_epoch = 0 meta = None epoch = start_epoch for e in range(cfg.total_epochs): i = 0 print(data_loader.__len__()) for i, data in enumerate(data_loader): reference_id = (data['img_meta'].data[0][0]['filename'].split('/') [-1]).split('.')[0] video_id = data['img_meta'].data[0][0]['filename'].split('/')[-2] losses = model(return_loss=True, **data) losses, log_vars = parse_losses(losses) optimizer.zero_grad() losses.backward() optimizer.step() # print('transform kernel check',model.module.agg.trans_kernel.sum().item()) print('epoch:',epoch,'i:',i,'video_id:',video_id,'reference_id:',reference_id, \ 'loss_cls:',log_vars['loss_cls'],'loss_init_box:',log_vars['loss_pts_init'], \ 'loss_refine_box:',log_vars['loss_pts_refine']) if i % 1000 == 0: if meta is None: meta = dict(epoch=epoch + 1, iter=i) else: meta.update(epoch=epoch + 1, iter=i) checkpoint = { 'meta': meta, 'state_dict': weights_to_cpu(model.state_dict()) } if optimizer is not None: checkpoint['optimizer'] = optimizer.state_dict() if not os.path.exists(cfg.work_dir): os.mkdir(cfg.work_dir) filename = os.path.join(cfg.work_dir, 'epoch_{}_{}.pth'.format(epoch, i)) torch.save(checkpoint, filename) if epoch % 1 == 0: if meta is None: meta = dict(epoch=epoch + 1, iter=i) else: meta.update(epoch=epoch + 1, iter=i) checkpoint = { 'meta': meta, 'state_dict': weights_to_cpu(model.state_dict()) } if optimizer is not None: checkpoint['optimizer'] = optimizer.state_dict() if not os.path.exists(cfg.work_dir): os.mkdir(cfg.work_dir) filename = os.path.join(cfg.work_dir, 'epoch_{}.pth'.format(epoch)) torch.save(checkpoint, filename) epoch += 1
def emb2img(self, features, exp_shape): for i, x in enumerate(features): B, P, E = x.shape #(batch_size, latent_dim, emb_dim) x = x.transpose(1, 2).reshape(B, E, exp_shape[i][2], exp_shape[i][3]) features[i] = x return features def init_weights(self, pretrained=None): pass from mmcv import Config cfg = Config.fromfile('./configs/detectors/detectors_htc_r50_1x_coco.py') from mmdet.apis import set_random_seed cfg.dataset_type = 'CocoDataset' cfg.data_root = args.train_data_root cfg.model.roi_head.bbox_head[0].num_classes = 1 cfg.model.roi_head.bbox_head[1].num_classes = 1 cfg.model.roi_head.bbox_head[2].num_classes = 1 cfg.model.roi_head.mask_head[0].num_classes = 1 cfg.model.roi_head.mask_head[1].num_classes = 1 cfg.model.roi_head.mask_head[2].num_classes = 1 cfg.model.roi_head.semantic_head.num_classes = 1 cfg.data.train.type = 'CocoDataset'
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) ### config_name = os.path.basename(args.config) dst_config_dir = os.path.join(cfg.work_dir, config_name) if not os.path.isfile(dst_config_dir): if not os.path.isdir(cfg.work_dir): os.makedirs(cfg.work_dir) import shutil shutil.copy(args.config, dst_config_dir) # init logger before other steps logger = get_root_logger(cfg.log_level) logger.info('Distributed training: {}'.format(distributed)) if cfg.SEED is not None: args.seed = cfg.SEED # 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) datasets = [build_dataset(cfg.data.train)] if len(cfg.workflow) == 2: datasets.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=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 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.') print ("!!!!!!!!!!!!!!!!!!!!!!!!!!",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 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.train)#<================================================================ 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 print (cfg.model,"\n",cfg.test_cfg) model = build_detector(cfg.model, train_cfg=cfg.train_cfg, 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]) #print("!!!!!!!!!!!!! here") #print (model) outputs = single_gpu_test(model, data_loader, args.show, args.show_dir, args.show_score_thr,return_loss=True) 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']: eval_kwargs.pop(key, None) eval_kwargs.update(dict(metric=args.eval, **kwargs)) print(dataset.evaluate(outputs, **eval_kwargs))
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) # set multi-process settings setup_multi_processes(cfg) # 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 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) if args.gpu_ids is not None: cfg.gpu_ids = args.gpu_ids[0:1] warnings.warn('`--gpu-ids` is deprecated, please use `--gpu-id`. ' 'Because we only support single GPU mode in ' 'non-distributed testing. Use the first GPU ' 'in `gpu_ids` now.') else: cfg.gpu_ids = [args.gpu_id] # 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) rank, _ = get_dist_info() # allows not to create if args.work_dir is not None and rank == 0: mmcv.mkdir_or_exist(osp.abspath(args.work_dir)) timestamp = time.strftime('%Y%m%d_%H%M%S', time.localtime()) json_file = osp.join(args.work_dir, f'eval_{timestamp}.json') # 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) # old versions did not save class info in checkpoints, this walkaround is # for backward compatibility if 'CLASSES' in checkpoint.get('meta', {}): model.CLASSES = checkpoint['meta']['CLASSES'] else: model.CLASSES = dataset.CLASSES if not distributed: model = MMDataParallel(model, device_ids=cfg.gpu_ids) 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.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', 'dynamic_intervals' ]: eval_kwargs.pop(key, None) eval_kwargs.update(dict(metric=args.eval, **kwargs)) metric = dataset.evaluate(outputs, **eval_kwargs) print(metric) metric_dict = dict(config=args.config, metric=metric) if args.work_dir is not None and rank == 0: mmcv.dump(metric_dict, json_file)
parser.add_argument( '--path_mat', default='/mnt/lustre/geyongtao/dataset/AFLW/AFLWinfo_release.mat', type=str, help='aflw dataset mat path') parser.add_argument('--data_root', default='/mnt/lustre/geyongtao/dataset/AFLW/data/flickr/', type=str, help='aflw dataset root path') parser.add_argument('--size_img', default=256, type=int, help='image size') args = parser.parse_args() if __name__ == '__main__': # net and model from mmcv import Config cfg = Config.fromfile(args.cfg_file) save_folder = os.path.join('./eval/', args.dataset, cfg['test_cfg']['save_folder']) import models net = models.__dict__[cfg['net_cfg']['net_name']](phase='test', cfg=cfg['net_cfg']) net = load_model(net, args.trained_model) net.eval() print('Finished loading model!') print(net) if args.cuda: net = net.cuda() cudnn.benchmark = True else:
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 cfg.get('USE_MMDET', False): from mmdet.apis import multi_gpu_test, single_gpu_test from mmdet.models import build_detector as build_model from mmdet.datasets import build_dataloader else: from mmtrack.apis import multi_gpu_test, single_gpu_test from mmtrack.models import build_model from mmtrack.datasets import build_dataloader if args.cfg_options is not None: cfg.merge_from_dict(args.cfg_options) # set cudnn_benchmark if cfg.get('cudnn_benchmark', False): torch.backends.cudnn.benchmark = True # cfg.model.pretrains = None if hasattr(cfg.model, 'detector'): cfg.model.detector.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 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) logger = get_logger('ParamsSearcher', log_file=args.log) # get all cases search_params = get_search_params(cfg.model.tracker, logger=logger) combinations = [p for p in product(*search_params.values())] search_cfgs = [] for c in combinations: search_cfg = dotty(cfg.model.tracker.copy()) for i, k in enumerate(search_params.keys()): search_cfg[k] = c[i] search_cfgs.append(dict(search_cfg)) print_log(f'Totally {len(search_cfgs)} cases.', logger) # init with the first one cfg.model.tracker = search_cfgs[0].copy() # build the model and load checkpoint if cfg.get('test_cfg', False): model = build_model(cfg.model, train_cfg=cfg.train_cfg, test_cfg=cfg.test_cfg) else: model = build_model(cfg.model) fp16_cfg = cfg.get('fp16', None) if fp16_cfg is not None: wrap_fp16_model(model) if args.checkpoint is not None: checkpoint = load_checkpoint(model, args.checkpoint, map_location='cpu') if 'CLASSES' in checkpoint['meta']: model.CLASSES = checkpoint['meta']['CLASSES'] if not hasattr(model, 'CLASSES'): model.CLASSES = dataset.CLASSES if args.fuse_conv_bn: model = fuse_conv_bn(model) if not distributed: model = MMDataParallel(model, device_ids=[0]) else: model = MMDistributedDataParallel( model.cuda(), device_ids=[torch.cuda.current_device()], broadcast_buffers=False) print_log(f'Record {cfg.search_metrics}.', logger) for i, search_cfg in enumerate(search_cfgs): if not distributed: model.tracker = build_tracker(search_cfg) outputs = single_gpu_test(model, data_loader, args.show, args.show_dir, args.show_score_thr) else: model.module.tracker = build_tracker(search_cfg) 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']: eval_kwargs.pop(key, None) eval_kwargs.update(dict(metric=args.eval, **kwargs)) results = dataset.evaluate(outputs, **eval_kwargs) _records = [] for k in cfg.search_metrics: if isinstance(results[k], float): _records.append(f'{(results[k]):.3f}') else: _records.append(f'{(results[k])}') print_log(f'{combinations[i]}: {_records}', logger)
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']) # import modules from plguin/xx, registry will be updated if hasattr(cfg, 'plugin') & cfg.plugin: import importlib if hasattr(cfg, 'plugin_dir'): plugin_dir = cfg.plugin_dir _module_dir = os.path.dirname(plugin_dir) _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) else: # import dir is the dirpath for the config file _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) # 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.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) if args.autoscale_lr: # apply the linear scaling rule (https://arxiv.org/abs/1706.02677) cfg.optimizer['lr'] = cfg.optimizer['lr'] * len(cfg.gpu_ids) / 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) # 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) # add a logging filter logging_filter = logging.Filter('mmdet') logging_filter.filter = lambda record: record.find('mmdet') != -1 # 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.get('train_cfg'), test_cfg=cfg.get('test_cfg')) logger.info(f'Model:\n{model}') datasets = [build_dataset(cfg.data.train)] if len(cfg.workflow) == 2: val_dataset = copy.deepcopy(cfg.data.val) # in case we use a dataset wrapper if 'dataset' in cfg.data.train: val_dataset.pipeline = cfg.data.train.dataset.pipeline else: val_dataset.pipeline = cfg.data.train.pipeline # set test_mode=False here in deep copied config # which do not affect AP/AR calculation later # refer to https://mmdetection3d.readthedocs.io/en/latest/tutorials/customize_runtime.html#customize-workflow # noqa val_dataset.test_mode = False 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.pretty_text, CLASSES=datasets[0].CLASSES) # add an attribute for visualization convenience model.CLASSES = datasets[0].CLASSES #print('calling train_detector') train_detector(model, datasets, cfg, distributed=distributed, validate=(not args.no_validate), timestamp=timestamp, meta=meta)
def main(): args = parse_args() cfg = Config.fromfile(args.config) # set cudnn_benchmark if cfg.get('cudnn_benchmark', False): torch.backends.cudnn.benchmark = True # set random seeds if args.seed is not None: print('set random seed to', args.seed) set_random_seed(args.seed, deterministic=args.deterministic) # build the model and load checkpoint model = build_model(cfg.model, train_cfg=cfg.train_cfg, test_cfg=cfg.test_cfg) _ = load_checkpoint(model, args.checkpoint, map_location='cpu') # sanity check for models without ema if not model.use_ema: args.sample_model = 'orig' if args.sample_model == 'ema': generator = model.generator_ema else: generator = model.generator mmcv.print_log(f'Sampling model: {args.sample_model}', 'mmgen') mmcv.print_log(f'Show mode: {args.show_mode}', 'mmgen') mmcv.print_log(f'Samples path: {args.samples_path}', 'mmgen') generator.eval() if not args.use_cpu: generator = generator.cuda() if args.show_mode == 'sequence': assert args.endpoint >= 2 else: assert args.endpoint >= 2 and args.endpoint % 2 == 0 kwargs = dict(max_batch_size=args.batch_size) if args.sample_cfg is None: args.sample_cfg = dict() kwargs.update(args.sample_cfg) # get noises corresponding to each endpoint noise_batch = batch_inference( generator, None, num_batches=args.endpoint, dict_key='noise_batch' if args.space == 'z' else 'latent', return_noise=True, **kwargs) if args.space == 'w': kwargs['truncation_latent'] = generator.get_mean_latent() kwargs['input_is_latent'] = True if args.show_mode == 'sequence': results = sample_from_path(generator, noise_batch[:-1, ], noise_batch[1:, ], args.interval, args.interp_mode, args.space, **kwargs) else: results = sample_from_path(generator, noise_batch[::2, ], noise_batch[1::2, ], args.interval, args.interp_mode, args.space, **kwargs) # reorder results results = torch.stack(results).permute(1, 0, 2, 3, 4) _, _, ch, h, w = results.shape results = results.reshape(-1, ch, h, w) # rescale value range to [0, 1] results = ((results + 1) / 2) results = results[:, [2, 1, 0], ...] results = results.clamp_(0, 1) # save image mmcv.mkdir_or_exist(args.samples_path) if args.show_mode == 'sequence': for i in range(results.shape[0]): image = results[i:i + 1] save_image( image, os.path.join(args.samples_path, '{:0>5d}'.format(i) + '.png')) else: save_image(results, os.path.join(args.samples_path, 'group.png'), nrow=args.interval)
def test_two_stage_inpaintor(): model = dict( disc_input_with_mask=True, encdec=dict(type='DeepFillEncoderDecoder'), disc=dict(type='DeepFillv1Discriminators', global_disc_cfg=dict(type='MultiLayerDiscriminator', in_channels=3, max_channels=256, fc_in_channels=256 * 16 * 16, fc_out_channels=1, num_convs=4, norm_cfg=None, act_cfg=dict(type='ELU'), out_act_cfg=dict(type='LeakyReLU', negative_slope=0.2)), local_disc_cfg=dict(type='MultiLayerDiscriminator', in_channels=3, max_channels=512, fc_in_channels=512 * 8 * 8, fc_out_channels=1, num_convs=4, norm_cfg=None, act_cfg=dict(type='ELU'), out_act_cfg=dict(type='LeakyReLU', negative_slope=0.2))), stage1_loss_type=('loss_l1_hole', 'loss_l1_valid'), stage2_loss_type=('loss_l1_hole', 'loss_l1_valid', 'loss_gan'), loss_gan=dict( type='GANLoss', gan_type='hinge', loss_weight=1, ), loss_l1_hole=dict( type='L1Loss', loss_weight=1.0, ), loss_l1_valid=dict( type='L1Loss', loss_weight=1.0, ), pretrained=None) train_cfg = Config(dict(disc_step=1, local_size=(128, 128))) test_cfg = Config(dict(metrics=['l1'])) tsinpaintor = DeepFillv1Inpaintor(**model, train_cfg=train_cfg, test_cfg=test_cfg) # check architecture assert tsinpaintor.stage1_loss_type == ('loss_l1_hole', 'loss_l1_valid') assert tsinpaintor.stage2_loss_type == ('loss_l1_hole', 'loss_l1_valid', 'loss_gan') assert tsinpaintor.with_l1_hole_loss assert tsinpaintor.with_l1_valid_loss assert not tsinpaintor.with_composed_percep_loss assert not tsinpaintor.with_out_percep_loss assert tsinpaintor.with_gan if torch.cuda.is_available(): # prepare data gt_img = torch.rand((2, 3, 256, 256)).cuda() mask = torch.zeros((2, 1, 256, 256)).cuda() mask[..., 50:180, 60:170] = 1. masked_img = gt_img * (1. - mask) bbox_tensor = torch.tensor([[50, 60, 110, 110], [50, 60, 110, 110]]).cuda() data_batch = dict(gt_img=gt_img, mask=mask, masked_img=masked_img, mask_bbox=bbox_tensor) # prepare model and optimizer tsinpaintor.cuda() optimizers_config = dict(generator=dict(type='Adam', lr=0.0001), disc=dict(type='Adam', lr=0.0001)) optims = build_optimizers(tsinpaintor, optimizers_config) # check train_step with standard deepfillv2 model outputs = tsinpaintor.train_step(data_batch, optims) assert outputs['num_samples'] == 2 log_vars = outputs['log_vars'] assert 'real_loss_global' in log_vars assert 'stage1_loss_l1_hole' in log_vars assert 'stage1_loss_l1_valid' in log_vars assert 'stage2_loss_l1_hole' in log_vars assert 'stage2_loss_l1_valid' in log_vars assert 'stage1_fake_res' in outputs['results'] assert 'stage2_fake_res' in outputs['results'] assert outputs['results']['stage1_fake_res'].size() == (2, 3, 256, 256) # check train step w/o disc step tsinpaintor.train_cfg.disc_step = 0 outputs = tsinpaintor.train_step(data_batch, optims) assert outputs['num_samples'] == 2 log_vars = outputs['log_vars'] assert 'real_loss_global' not in log_vars assert 'stage1_loss_l1_hole' in log_vars assert 'stage1_loss_l1_valid' in log_vars assert 'stage2_loss_l1_hole' in log_vars assert 'stage2_loss_l1_valid' in log_vars assert 'stage1_fake_res' in outputs['results'] assert 'stage2_fake_res' in outputs['results'] assert outputs['results']['stage1_fake_res'].size() == (2, 3, 256, 256) tsinpaintor.train_cfg.disc_step = 1 # check train step w/ multiple disc step tsinpaintor.train_cfg.disc_step = 5 outputs = tsinpaintor.train_step(data_batch, optims) assert outputs['num_samples'] == 2 log_vars = outputs['log_vars'] assert 'real_loss_global' in log_vars assert 'stage1_loss_l1_hole' not in log_vars assert outputs['results']['fake_res'].size() == (2, 3, 256, 256) tsinpaintor.train_cfg.disc_step = 1 # test forward test w/o save image outputs = tsinpaintor.forward_test(masked_img[0:1], mask[0:1], gt_img=gt_img[0:1, ...]) assert 'eval_result' in outputs assert outputs['eval_result']['l1'] > 0 # test forward test w/o eval metrics tsinpaintor.test_cfg = dict() tsinpaintor.eval_with_metrics = False outputs = tsinpaintor.forward_test(masked_img[0:1], mask[0:1]) for key in [ 'stage1_fake_res', 'stage2_fake_res', 'fake_res', 'fake_img' ]: assert outputs[key].size() == (1, 3, 256, 256) # test forward test w/ save image with tempfile.TemporaryDirectory() as tmpdir: outputs = tsinpaintor.forward_test( masked_img[0:1], mask[0:1], save_image=True, save_path=tmpdir, iteration=4396, meta=[dict(gt_img_path='igccc.png')]) assert os.path.exists(os.path.join(tmpdir, 'igccc_4396.png')) # test forward test w/ save image w/ gt_img with tempfile.TemporaryDirectory() as tmpdir: outputs = tsinpaintor.forward_test( masked_img[0:1], mask[0:1], save_image=True, save_path=tmpdir, meta=[dict(gt_img_path='igccc.png')], gt_img=gt_img[0:1, ...]) assert os.path.exists(os.path.join(tmpdir, 'igccc.png')) with pytest.raises(AssertionError): outputs = tsinpaintor.forward_test(masked_img[0:1], mask[0:1], save_image=True, save_path=tmpdir, iteration=4396, gt_img=gt_img[0:1, ...]) with pytest.raises(AssertionError): outputs = tsinpaintor.forward_test( masked_img[0:1], mask[0:1], save_image=True, save_path=None, iteration=4396, meta=[dict(gt_img_path='igccc.png')], gt_img=gt_img[0:1, ...]) # check train_step with not implemented loss type with pytest.raises(NotImplementedError): model_ = copy.deepcopy(model) model_['stage1_loss_type'] = ('igccc', ) tsinpaintor = DeepFillv1Inpaintor(**model_, train_cfg=train_cfg, test_cfg=test_cfg).cuda() outputs = tsinpaintor.train_step(data_batch, optims) # test input w/o ones and disc input w/o mask model_ = dict( disc_input_with_mask=False, input_with_ones=False, encdec=dict( type='DeepFillEncoderDecoder', stage1=dict(type='GLEncoderDecoder', encoder=dict(type='DeepFillEncoder', in_channels=4), decoder=dict(type='DeepFillDecoder', in_channels=128), dilation_neck=dict(type='GLDilationNeck', in_channels=128, act_cfg=dict(type='ELU'))), stage2=dict(type='DeepFillRefiner', encoder_attention=dict( type='DeepFillEncoder', encoder_type='stage2_attention', in_channels=4), encoder_conv=dict(type='DeepFillEncoder', encoder_type='stage2_conv', in_channels=4)), ), disc=dict( type='DeepFillv1Discriminators', global_disc_cfg=dict(type='MultiLayerDiscriminator', in_channels=3, max_channels=256, fc_in_channels=256 * 16 * 16, fc_out_channels=1, num_convs=4, norm_cfg=None, act_cfg=dict(type='ELU'), out_act_cfg=dict(type='LeakyReLU', negative_slope=0.2)), local_disc_cfg=dict(type='MultiLayerDiscriminator', in_channels=3, max_channels=512, fc_in_channels=512 * 8 * 8, fc_out_channels=1, num_convs=4, norm_cfg=None, act_cfg=dict(type='ELU'), out_act_cfg=dict(type='LeakyReLU', negative_slope=0.2))), stage1_loss_type=('loss_l1_hole', 'loss_l1_valid'), stage2_loss_type=('loss_l1_hole', 'loss_l1_valid', 'loss_gan'), loss_gan=dict( type='GANLoss', gan_type='hinge', loss_weight=1, ), loss_l1_hole=dict( type='L1Loss', loss_weight=1.0, ), loss_gp=dict(type='GradientPenaltyLoss', loss_weight=10.), loss_tv=dict( type='MaskedTVLoss', loss_weight=0.1, ), loss_l1_valid=dict( type='L1Loss', loss_weight=1.0, ), loss_disc_shift=dict(type='DiscShiftLoss'), pretrained=None) tsinpaintor = DeepFillv1Inpaintor(**model_, train_cfg=train_cfg, test_cfg=test_cfg).cuda() outputs = tsinpaintor.train_step(data_batch, optims) assert outputs['num_samples'] == 2 log_vars = outputs['log_vars'] assert 'real_loss_global' in log_vars assert 'stage1_loss_l1_hole' in log_vars assert 'stage1_loss_l1_valid' in log_vars assert 'stage2_loss_l1_hole' in log_vars assert 'stage2_loss_l1_valid' in log_vars assert 'stage1_fake_res' in outputs['results'] assert 'stage2_fake_res' in outputs['results'] assert outputs['results']['stage1_fake_res'].size() == (2, 3, 256, 256) outputs = tsinpaintor.forward_test(masked_img[0:1], mask[0:1], gt_img=gt_img[0:1, ...]) assert 'eval_result' in outputs assert outputs['eval_result']['l1'] > 0 # test w/o stage1 loss model_ = copy.deepcopy(model) model_['stage1_loss_type'] = None tsinpaintor = DeepFillv1Inpaintor(**model_, train_cfg=train_cfg, test_cfg=test_cfg).cuda() outputs = tsinpaintor.train_step(data_batch, optims) assert outputs['num_samples'] == 2 log_vars = outputs['log_vars'] assert 'real_loss_global' in log_vars assert 'stage1_loss_l1_hole' not in log_vars assert 'stage1_loss_l1_valid' not in log_vars assert 'stage2_loss_l1_hole' in log_vars assert 'stage2_loss_l1_valid' in log_vars assert 'stage1_fake_res' in outputs['results'] assert 'stage2_fake_res' in outputs['results'] assert outputs['results']['stage1_fake_res'].size() == (2, 3, 256, 256) # test w/o stage2 loss model_ = copy.deepcopy(model) model_['stage2_loss_type'] = None tsinpaintor = DeepFillv1Inpaintor(**model_, train_cfg=train_cfg, test_cfg=test_cfg).cuda() outputs = tsinpaintor.train_step(data_batch, optims) assert outputs['num_samples'] == 2 log_vars = outputs['log_vars'] assert 'real_loss_global' in log_vars assert 'stage1_loss_l1_hole' in log_vars assert 'stage1_loss_l1_valid' in log_vars assert 'stage2_loss_l1_hole' not in log_vars assert 'stage2_loss_l1_valid' not in log_vars assert 'stage1_fake_res' in outputs['results'] assert 'stage2_fake_res' in outputs['results'] assert outputs['results']['stage1_fake_res'].size() == (2, 3, 256, 256)
def test_pconv_inpaintor(init_weights): cfg = Config.fromfile( 'tests/data/inpaintor_config/pconv_inpaintor_test.py') if torch.cuda.is_available(): pconv_inpaintor = build_model(cfg.model, train_cfg=cfg.train_cfg, test_cfg=cfg.test_cfg) assert pconv_inpaintor.__class__.__name__ == 'PConvInpaintor' pconv_inpaintor.cuda() gt_img = torch.randn((1, 3, 256, 256)).cuda() mask = torch.zeros_like(gt_img) mask[..., 50:160, 100:210] = 1. masked_img = gt_img * (1. - mask) data_batch = dict(gt_img=gt_img, mask=mask, masked_img=masked_img) optim_g = torch.optim.SGD(pconv_inpaintor.generator.parameters(), lr=0.1) optim_dict = dict(generator=optim_g) outputs = pconv_inpaintor.train_step(data_batch, optim_dict) assert outputs['results']['fake_res'].shape == (1, 3, 256, 256) assert outputs['results']['final_mask'].shape == (1, 3, 256, 256) assert 'loss_l1_hole' in outputs['log_vars'] assert 'loss_l1_valid' in outputs['log_vars'] assert 'loss_tv' in outputs['log_vars'] # test forward dummy res = pconv_inpaintor.forward_dummy( torch.cat([masked_img, mask], dim=1)) assert res.shape == (1, 3, 256, 256) # test forward test w/o save image outputs = pconv_inpaintor.forward_test(masked_img[0:1], mask[0:1], gt_img=gt_img[0:1, ...]) assert 'eval_results' in outputs assert outputs['eval_results']['l1'] > 0 assert outputs['eval_results']['psnr'] > 0 assert outputs['eval_results']['ssim'] > 0 # test forward test w/o eval metrics pconv_inpaintor.test_cfg = dict() pconv_inpaintor.eval_with_metrics = False outputs = pconv_inpaintor.forward_test(masked_img[0:1], mask[0:1]) for key in ['fake_res', 'fake_img']: assert outputs[key].size() == (1, 3, 256, 256) # test forward test w/ save image with tempfile.TemporaryDirectory() as tmpdir: outputs = pconv_inpaintor.forward_test( masked_img[0:1], mask[0:1], save_image=True, save_path=tmpdir, iteration=4396, meta=[dict(gt_img_path='igccc.png')]) assert os.path.exists(os.path.join(tmpdir, 'igccc_4396.png')) # test forward test w/ save image w/ gt_img with tempfile.TemporaryDirectory() as tmpdir: outputs = pconv_inpaintor.forward_test( masked_img[0:1], mask[0:1], save_image=True, save_path=tmpdir, meta=[dict(gt_img_path='igccc.png')], gt_img=gt_img[0:1, ...]) assert os.path.exists(os.path.join(tmpdir, 'igccc.png')) with pytest.raises(AssertionError): outputs = pconv_inpaintor.forward_test(masked_img[0:1], mask[0:1], save_image=True, save_path=tmpdir, iteration=4396, gt_img=gt_img[0:1, ...]) with pytest.raises(AssertionError): outputs = pconv_inpaintor.forward_test( masked_img[0:1], mask[0:1], save_image=True, save_path=None, iteration=4396, meta=[dict(gt_img_path='igccc.png')], gt_img=gt_img[0:1, ...]) # reset mock to clear some memory usage init_weights.reset_mock()
except ImportError: from engineer.SPPE.src.utils.img import transformBox_batch import os from torch.utils.data import DataLoader import torch.nn as nn from engineer.datasets.loader.build_loader import train_loader_collate_fn, test_loader_collate_fn from engineer.datasets.builder import build_dataset from engineer.models.builder import build_generator, build_backbone from engineer.core.train import train_epochs from utils import group_weight if __name__ == "__main__": args = opt assert args.config is not None, "you must give your model config" cfg = Config.fromfile(args.config) # train_data_set = TrainSingerDataset(cfg.data.json_file, transfer=transfer,img_dir = cfg.data.img_dir,black_list=cfg.data.black_list) train_data_set = build_dataset(cfg.data.train) train_loader = DataLoader(train_data_set, batch_size=opt.trainBatch, shuffle=True, num_workers=4, collate_fn=train_loader_collate_fn) test_data_set = build_dataset(cfg.data.test) test_loader = DataLoader(test_data_set, batch_size=opt.validBatch, shuffle=False, num_workers=4, collate_fn=test_loader_collate_fn)
def main(): args = parse_args() cfg = Config.fromfile(args.config) cfg.merge_from_dict(args.cfg_options) # set cudnn_benchmark if cfg.get('cudnn_benchmark', False): torch.backends.cudnn.benchmark = True # work_dir is determined in this priority: # CLI > config file > default (base 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.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) # The flag is used to determine whether it is omnisource training cfg.setdefault('omnisource', False) # 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 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 # log some basic info logger.info(f'Distributed training: {distributed}') logger.info(f'Config: {cfg.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['config_name'] = osp.basename(args.config) meta['work_dir'] = osp.basename(cfg.work_dir.rstrip('/\\')) model = build_model(cfg.model, train_cfg=cfg.train_cfg, test_cfg=cfg.test_cfg) if cfg.omnisource: # If omnisource flag is set, cfg.data.train should be a list assert type(cfg.data.train) is list datasets = [build_dataset(dataset) for dataset in cfg.data.train] else: datasets = [build_dataset(cfg.data.train)] if len(cfg.workflow) == 2: # For simplicity, omnisource is not compatiable with val workflow, # we recommend you to use `--validate` assert not cfg.omnisource if args.validate: warnings.warn('val workflow is duplicated with `--validate`, ' 'it is recommended to use `--validate`. see ' 'https://github.com/open-mmlab/mmaction2/pull/123') val_dataset = copy.deepcopy(cfg.data.val) datasets.append(build_dataset(val_dataset)) if cfg.checkpoint_config is not None: # save mmaction version, config file content and class names in # checkpoints as meta data cfg.checkpoint_config.meta = dict(mmaction_version=__version__ + get_git_hash(digits=7), config=cfg.text) train_model(model, datasets, cfg, distributed=distributed, validate=args.validate, timestamp=timestamp, meta=meta)
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 )