def _build2(self, s_in: Shape, s_out: Shape) -> ShapeList: """ build the network """ self.input_shape = s_in self.net = build_detector(self._mmdet_model) self._backbone_interface = get_backbone_interface( self.net.backbone) self._build_heads() return self.get_network_output_shapes()
def main(): args = parse_args() assert args.out or args.show, \ ('Please specify at least one operation (save or show the results) ' 'with the argument "--out" or "--show"') if args.out is not None and not args.out.endswith(('.pkl', '.pickle')): raise ValueError('The output file must be a pkl file.') cfg = mmcv.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 # 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, imgs_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') # old versions did not save class info in checkpoints, this walkaround is # for backward compatibility model.CLASSES = dataset.CLASSES if not distributed: model = MMDataParallel(model, device_ids=[0]) outputs = single_gpu_test(model, data_loader, args.show) else: model = MMDistributedDataParallel(model.cuda()) outputs = multi_gpu_test(model, data_loader, args.tmpdir) rank, _ = get_dist_info() if rank == 0: mmcv.dump(outputs, args.out) results2json(dataset, outputs, args.out)
def build_model(cfg, train_cfg=None, test_cfg=None): """Build model.""" args = cfg.copy() obj_type = args.pop('type') if obj_type in LOCALIZERS: return build_localizer(cfg) if obj_type in RECOGNIZERS: return build_recognizer(cfg, train_cfg, test_cfg) if obj_type in DETECTORS: return build_detector(cfg, train_cfg, test_cfg) raise ValueError(f'{obj_type} is not registered in ' 'LOCALIZERS, RECOGNIZERS or DETECTORS')
def __init__(self): super(Net3, self).__init__() cfg = mmcv.Config.fromfile('/home/liuziming/mmdetection/configs/rpn_r50_fpn_1x.py') # set cudnn_benchmark if cfg.get('cudnn_benchmark', False): torch.backends.cudnn.benchmark = True cfg.model.pretrained = None cfg.data.test.test_mode = True self.RPN = builder.build_detector(cfg.model, train_cfg=None, test_cfg=cfg.test_cfg) self.backbone = ResNet(50,4,frozen_stages=1,) self.init_weights(pretrained='modelzoo://resnet50') self.relation = SelfAttention(2,256,256,256) self.fc = nn.Linear(256*2,40) self.avgpool = nn.AdaptiveAvgPool2d((1,1))
def build_model(cfg, train_cfg=None, test_cfg=None): """Build model.""" args = cfg.copy() obj_type = args.pop('type') if obj_type in LOCALIZERS: return build_localizer(cfg) if obj_type in RECOGNIZERS: return build_recognizer(cfg, train_cfg, test_cfg) if obj_type in DETECTORS: return build_detector(cfg, train_cfg, test_cfg) model_in_mmdet = ['FastRCNN'] if obj_type in model_in_mmdet: raise ImportError( 'Please install mmdet for spatial temporal detection tasks.') raise ValueError(f'{obj_type} is not registered in ' 'LOCALIZERS, RECOGNIZERS or DETECTORS')
def build_model(cfg, gpu): """ Return model wrapped in MMDataParallel. TODO: support multi-GPU """ if type(gpu) != list: gpu = [gpu] model_type = cfg["model"].pop("config") train_cfg = cfg["model"].pop("train_cfg") test_cfg = cfg["model"].pop("test_cfg") mmdet_cfg = Config.fromfile(osp.join(_PATH, "models", "{}.py".format(model_type))) # Change model parameters if cfg["model"] is not None: if type(cfg["model"]["bbox_head"]) == list: assert len(cfg["model"]["bbox_head"]) == len(mmdet_cfg.model.bbox_head) bbox_head_cfg = cfg["model"].pop("bbox_head") for ind, i in enumerate(bbox_head_cfg): if i is None: continue assert type(i) is dict assert type(mmdet_cfg.model.bbox_head[ind]) is ConfigDict mmdet_cfg.model.bbox_head[ind].update(i) mmdet_cfg.model.update(cfg["model"]) if train_cfg is not None: if type(train_cfg["rcnn"]) == list: assert len(train_cfg["rcnn"]) == len(mmdet_cfg.train_cfg.rcnn) rcnn_cfg = train_cfg.pop("rcnn") for ind, i in enumerate(rcnn_cfg): if i is None: continue assert type(i) is dict assert type(mmdet_cfg.train_cfg.rcnn[ind]) is ConfigDict mmdet_cfg.train_cfg.rcnn[ind].update(i) mmdet_cfg.train_cfg.update(train_cfg) if test_cfg is not None: mmdet_cfg.test_cfg.update(test_cfg) model = build_detector( mmdet_cfg.model, train_cfg=mmdet_cfg.train_cfg, test_cfg=mmdet_cfg.test_cfg ) model = MMDataParallel(model, device_ids=gpu) return model
def build_model(cfg, gpu): ''' Return model wrapped in MMDataParallel. TODO: support multi-GPU ''' if type(gpu) != list: gpu = [gpu] model_type = cfg['model'].pop('config') train_cfg = cfg['model'].pop('train_cfg') test_cfg = cfg['model'].pop('test_cfg') mmdet_cfg = Config.fromfile(osp.join(_PATH, 'models', '{}.py'.format(model_type))) # Change model parameters if cfg['model'] is not None: if type(cfg['model']['bbox_head']) == list: assert len(cfg['model']['bbox_head']) == len(mmdet_cfg.model.bbox_head) bbox_head_cfg = cfg['model'].pop('bbox_head') for ind, i in enumerate(bbox_head_cfg): if i is None: continue assert type(i) is dict assert type(mmdet_cfg.model.bbox_head[ind]) is ConfigDict mmdet_cfg.model.bbox_head[ind].update(i) mmdet_cfg.model.update(cfg['model']) if train_cfg is not None: if type(train_cfg['rcnn']) == list: assert len(train_cfg['rcnn']) == len(mmdet_cfg.train_cfg.rcnn) rcnn_cfg = train_cfg.pop('rcnn') for ind, i in enumerate(rcnn_cfg): if i is None: continue assert type(i) is dict assert type(mmdet_cfg.train_cfg.rcnn[ind]) is ConfigDict mmdet_cfg.train_cfg.rcnn[ind].update(i) mmdet_cfg.train_cfg.update(train_cfg) if test_cfg is not None: mmdet_cfg.test_cfg.update(test_cfg) model = build_detector(mmdet_cfg.model, train_cfg=mmdet_cfg.train_cfg, test_cfg=mmdet_cfg.test_cfg) model = MMDataParallel(model, device_ids=gpu) return model
def build_model(cfg, train_cfg=None, test_cfg=None): """Build model.""" args = cfg.copy() obj_type = args.pop('type') if obj_type in LOCALIZERS: return build_localizer(cfg) if obj_type in RECOGNIZERS: return build_recognizer(cfg, train_cfg, test_cfg) if obj_type in DETECTORS: if train_cfg is not None or test_cfg is not None: warnings.warn( 'train_cfg and test_cfg is deprecated, ' 'please specify them in model. Details see this ' 'PR: https://github.com/open-mmlab/mmaction2/pull/629', UserWarning) return build_detector(cfg, train_cfg, test_cfg) model_in_mmdet = ['FastRCNN'] if obj_type in model_in_mmdet: raise ImportError( 'Please install mmdet for spatial temporal detection tasks.') raise ValueError(f'{obj_type} is not registered in ' 'LOCALIZERS, RECOGNIZERS or DETECTORS')
target_stds=[0.1, 0.1, 0.2, 0.2]), reg_class_agnostic=False, loss_cls=dict(type='CrossEntropyLoss', use_sigmoid=False, loss_weight=1.0), loss_bbox=dict(type='L1Loss', loss_weight=1.0)))) # model training and testing settings train_cfg = dict(rcnn=dict(assigner=dict(type='MaxIoUAssigner', pos_iou_thr=0.5, neg_iou_thr=0.5, min_pos_iou=0.5, match_low_quality=False, ignore_iof_thr=-1), sampler=dict(type='RandomSampler', num=512, pos_fraction=0.25, neg_pos_ub=-1, add_gt_as_proposals=True), pos_weight=-1, debug=False)) test_cfg = dict(rcnn=dict(score_thr=0.05, nms=dict(type='nms', iou_threshold=0.5), max_per_img=100)) if __name__ == '__main__': detector = build_detector(config.model, Config(config.train_cfg), Config(config.test_cfg)) print(detector)
import numpy as np import torch from mmcv.utils.config import Config, ConfigDict from mmcv.parallel import MMDataParallel from mmdet.models.builder import build_detector from factory import builder mmdet_cfg = Config.fromfile('factory/models/SERetinaNeXt50.py') model = build_detector(mmdet_cfg.model, mmdet_cfg.train_cfg, mmdet_cfg.test_cfg) model = MMDataParallel(model, device_ids=[0]) X = np.ones(([2, 3, 512, 512])) X = torch.from_numpy(X).float() img_meta = {0: {'img_shape': (3, 512, 512), 'scale_factor': 1., 'flip': False}} y_pred = [] for x in X: y_pred.append( model([x.unsqueeze(0)], img_meta=[img_meta], return_loss=False))
def validate(self, checkpoint_file_path, output_file): """Runs validation with QualitAI metrics.""" print('Loading model...') self.cfg.data.test.test_mode = True self.cfg.model.pretrained = None model = build_detector(self.cfg.model, train_cfg=None, test_cfg=self.cfg.test_cfg) fp16_cfg = self.cfg.get('fp16', None) if fp16_cfg is not None: wrap_fp16_model(model) checkpoint = load_checkpoint(model, checkpoint_file_path, map_location='cpu') if 'CLASSES' in checkpoint['meta']: model.CLASSES = checkpoint['meta']['CLASSES'] else: model.CLASSES = self.dataset.CLASSES model = MMDataParallel(model, device_ids=[0]).cuda() model.eval() print('Done!') print('Starting inference run...') # Do inference results = [] bool_preds = [] bool_targets = [] prog_bar = ProgressBar(len(self.loader.dataset)) file_id_lookup = self.get_ids_of_files(self.dataset.coco) for i, data in enumerate(self.loader): with torch.no_grad(): result = model(return_loss=False, rescale=True, **data) results.append(result) img_shape = data['img_meta'][0].data[0][0]['ori_shape'] bool_pred = self.transform_preds_to_boolean( img_shape[0:2], result[0]) bool_preds.append(bool_pred) path, img_name = split(data['img_meta'][0].data[0][0]['filename']) if img_name in file_id_lookup: img_id = file_id_lookup[img_name] else: img_name = join(split(path)[1], img_name) if img_name in file_id_lookup: img_id = file_id_lookup[img_name] else: raise KeyError(img_name) bool_target = self.transform_targets_to_boolean( self.dataset.coco, img_id, img_shape[0:2]) bool_targets.append(bool_target) target_img = np.zeros(img_shape, dtype='uint8') target_img[bool_target] = [0, 255, 0] target_img = Image.fromarray(target_img) pred_img = np.zeros(img_shape, dtype='uint8') pred_img[bool_pred] = [255, 0, 0] pred_img = Image.fromarray(pred_img) intersection_img = np.zeros(img_shape, dtype='uint8') intersection_img[bool_target * bool_pred] = [0, 0, 255] intersection_img = Image.fromarray(intersection_img) target_img.save('/workspace/outputs/{}-target.png'.format(i)) pred_img.save('/workspace/outputs/{}-pred.png'.format(i)) intersection_img.save( '/workspace/outputs/{}-intersection.png'.format(i)) prog_bar.update() # Dump out result files if not isinstance(results[0], dict): results2json(self.dataset, results, output_file) else: for name in results[0]: results_ = [result[name] for result in results] result_file = output_file + '.{}'.format(name) results2json(self.dataset, results_, result_file) # Calculate values "\nWriting out results..." print('\nStarting evaluation according to QualitAI metrics...') accuracy = 0. precision = 0. recall = 0. num_imgs = 0. for target, pred in zip(bool_targets, bool_preds): accuracy += self.calculate_accuracy(target, pred) precision += self.calculate_precision(target, pred) recall += self.calculate_recall(target, pred) num_imgs += 1. accuracy /= num_imgs precision /= num_imgs recall /= num_imgs print('Done!') print("\nResults:") print("======================") print("Num imgs: {}".format(int(num_imgs))) print("Accuracy: {:.7f}".format(accuracy)) print("Precision: {:.7f}".format(precision)) print("Recall: {:.7f}".format(recall))