def do_reval(dataset_name, output_dir, args): dataset = JsonDataset(dataset_name) dets = load_object(os.path.join(output_dir, 'detections.pkl')) # Override config with the one saved in the detections file if args.cfg_file is not None: core_config.merge_cfg_from_cfg(core_config.load_cfg(dets['cfg'])) else: core_config._merge_a_into_b(core_config.load_cfg(dets['cfg']), cfg) # re-filter on score threshold: dets['all_boxes'] = \ [ [ im[im[:,4] > cfg.TEST.SCORE_THRESH,:] if len(im) != 0 else [] for im in cls ] for cls in dets['all_boxes'] ] results = task_evaluation.evaluate_all(dataset, dets['all_boxes'], dets['all_segms'], dets['all_keyps'], output_dir, use_matlab=args.matlab_eval) task_evaluation.log_copy_paste_friendly_results(results)
def main(args): logger = logging.getLogger(__name__) dummy_coco_dataset = dummy_datasets.get_coco_dataset() cfg_orig = load_cfg(envu.yaml_dump(cfg)) im = cv2.imread(args.im_file) if args.rpn_pkl is not None: proposal_boxes, _proposal_scores = get_rpn_box_proposals(im, args) workspace.ResetWorkspace() else: proposal_boxes = None cls_boxes, cls_segms, cls_keyps, cls_bodys = None, None, None, None for i in range(0, len(args.models_to_run), 2): pkl = args.models_to_run[i] yml = args.models_to_run[i + 1] cfg.immutable(False) merge_cfg_from_cfg(cfg_orig) merge_cfg_from_file(yml) if len(pkl) > 0: weights_file = pkl else: weights_file = cfg.TEST.WEIGHTS cfg.NUM_GPUS = 1 assert_and_infer_cfg(cache_urls=False) model = model_engine.initialize_model_from_cfg(weights_file) with c2_utils.NamedCudaScope(0): cls_boxes_, cls_segms_, cls_keyps_ , cls_bodys_= \ model_engine.im_detect_all(model, im, proposal_boxes) cls_boxes = cls_boxes_ if cls_boxes_ is not None else cls_boxes cls_segms = cls_segms_ if cls_segms_ is not None else cls_segms cls_keyps = cls_keyps_ if cls_keyps_ is not None else cls_keyps cls_bodys = cls_bodys_ if cls_bodys_ is not None else cls_bodys workspace.ResetWorkspace() out_name = os.path.join( args.output_dir, '{}'.format(os.path.basename(args.im_file) + '.pdf')) logger.info('Processing {} -> {}'.format(args.im_file, out_name)) with open('test_vis.pkl', 'w') as f: pickle.dump( { 'im': im, 'cls_boxes': np.array(cls_boxes), 'cls_bodys': np.array(cls_bodys) }, f) vis_utils.vis_one_image(im[:, :, ::-1], args.im_file, args.output_dir, cls_boxes, cls_segms, cls_keyps, cls_bodys, dataset=dummy_coco_dataset, box_alpha=0.3, show_class=True, thresh=0.7, kp_thresh=2)
def main(args): logger = logging.getLogger(__name__) dummy_nucoco_dataset = dummy_datasets.get_nucoco_dataset() cfg_orig = load_cfg(envu.yaml_dump(cfg)) ## Load image coco = COCO_PLUS(args.ann_file, args.imgs_dir) image_id = coco.dataset['images'][args.im_ind]['id'] img_path = os.path.join(args.imgs_dir, coco.imgs[image_id]["file_name"]) im = cv2.imread(img_path) ## Get the proposals for this image proposals = rrpn_loader(args.rpn_pkl) proposal_boxes = proposals[image_id]['boxes'] _proposal_scores = proposals[image_id]['scores'] workspace.ResetWorkspace() ## run models cls_boxes, cls_segms, cls_keyps = None, None, None for i in range(0, len(args.models_to_run), 2): pkl = args.models_to_run[i] yml = args.models_to_run[i + 1] cfg.immutable(False) merge_cfg_from_cfg(cfg_orig) merge_cfg_from_file(yml) if len(pkl) > 0: weights_file = pkl else: weights_file = cfg.TEST.WEIGHTS cfg.NUM_GPUS = 1 assert_and_infer_cfg(cache_urls=False) model = model_engine.initialize_model_from_cfg(weights_file) with c2_utils.NamedCudaScope(0): cls_boxes_, cls_segms_, cls_keyps_ = \ model_engine.im_detect_all(model, im, proposal_boxes) cls_boxes = cls_boxes_ if cls_boxes_ is not None else cls_boxes cls_segms = cls_segms_ if cls_segms_ is not None else cls_segms cls_keyps = cls_keyps_ if cls_keyps_ is not None else cls_keyps workspace.ResetWorkspace() out_name = os.path.join( args.output_dir, '{}'.format(os.path.basename(img_path) + '.pdf') ) logger.info('Processing {} -> {}'.format(img_path, out_name)) vis_utils.vis_one_image( im[:, :, ::-1], img_path, args.output_dir, cls_boxes, cls_segms, cls_keyps, dataset=dummy_nucoco_dataset, box_alpha=0.3, show_class=True, thresh=0.7, kp_thresh=2 )
def main(args): logger = logging.getLogger(__name__) dummy_coco_dataset = dummy_datasets.get_coco_dataset() cfg_orig = load_cfg(yaml.dump(cfg)) im = cv2.imread(args.im_file) if args.rpn_pkl is not None: proposal_boxes, _proposal_scores = get_rpn_box_proposals(im, args) workspace.ResetWorkspace() else: proposal_boxes = None cls_boxes, cls_segms, cls_keyps = None, None, None for i in range(0, len(args.models_to_run), 2): pkl = args.models_to_run[i] yml = args.models_to_run[i + 1] cfg.immutable(False) merge_cfg_from_cfg(cfg_orig) merge_cfg_from_file(yml) if len(pkl) > 0: weights_file = pkl else: weights_file = cfg.TEST.WEIGHTS cfg.NUM_GPUS = 1 assert_and_infer_cfg(cache_urls=False) model = model_engine.initialize_model_from_cfg(weights_file) with c2_utils.NamedCudaScope(0): cls_boxes_, cls_segms_, cls_keyps_ = \ model_engine.im_detect_all(model, im, proposal_boxes) cls_boxes = cls_boxes_ if cls_boxes_ is not None else cls_boxes cls_segms = cls_segms_ if cls_segms_ is not None else cls_segms cls_keyps = cls_keyps_ if cls_keyps_ is not None else cls_keyps workspace.ResetWorkspace() out_name = os.path.join( args.output_dir, '{}'.format(os.path.basename(args.im_file) + '.pdf') ) logger.info('Processing {} -> {}'.format(args.im_file, out_name)) vis_utils.vis_one_image( im[:, :, ::-1], args.im_file, args.output_dir, cls_boxes, cls_segms, cls_keyps, dataset=dummy_coco_dataset, box_alpha=0.3, show_class=True, thresh=0.7, kp_thresh=2 )
def get_detectron_result(im): """ 功能: 获取传入图像的检测结果 输入参数列表: im: BGR格式图片 返回参数列表: img: 检测结果图片 detectron_result_info: 检测结果字典 """ setup_logging(__name__) logger = logging.getLogger(__name__) dummy_coco_dataset = dummy_datasets.get_coco_dataset() cfg_orig = load_cfg(yaml.dump(cfg)) if rpn_pkl is not None: proposal_boxes, _proposal_scores = get_rpn_box_proposals(im) workspace.ResetWorkspace() else: proposal_boxes = None cls_boxes, cls_segms, cls_keyps = None, None, None pkl = rpn_pkl yml = rpn_cfg cfg.immutable(False) merge_cfg_from_cfg(cfg_orig) merge_cfg_from_file(yml) if len(pkl) > 0: weights_file = pkl else: weights_file = cfg.TEST.WEIGHTS cfg.NUM_GPUS = 1 assert_and_infer_cfg(cache_urls=False) model = model_engine.initialize_model_from_cfg(weights_file) with c2_utils.NamedCudaScope(0): cls_boxes_, cls_segms_, cls_keyps_ = \ model_engine.im_detect_all(model, im, proposal_boxes) cls_boxes = cls_boxes_ if cls_boxes_ is not None else cls_boxes cls_segms = cls_segms_ if cls_segms_ is not None else cls_segms cls_keyps = cls_keyps_ if cls_keyps_ is not None else cls_keyps workspace.ResetWorkspace() img, detectron_result_info = vis_utils.vis_one_image_opencv( im, cls_boxes, cls_segms, cls_keyps, dataset=dummy_coco_dataset, show_box=True, show_class=True) return img, detectron_result_info
def do_reval(dataset_name, output_dir, args): dataset = JsonDataset(dataset_name) dets = load_object(os.path.join(output_dir, 'detections.pkl')) # Override config with the one saved in the detections file if args.cfg_file is not None: core_config.merge_cfg_from_cfg(core_config.load_cfg(dets['cfg'])) else: core_config._merge_a_into_b(core_config.load_cfg(dets['cfg']), cfg) results = task_evaluation.evaluate_all(dataset, dets['all_boxes'], dets['all_segms'], dets['all_keyps'], output_dir, use_matlab=args.matlab_eval) task_evaluation.log_copy_paste_friendly_results(results)
def do_reval(dataset_name, output_dir, args): dataset = JsonDataset(dataset_name) dets = load_object(os.path.join(output_dir, 'detections.pkl')) # Override config with the one saved in the detections file if args.cfg_file is not None: core_config.merge_cfg_from_cfg(core_config.load_cfg(dets['cfg'])) else: core_config._merge_a_into_b(core_config.load_cfg(dets['cfg']), cfg) results = task_evaluation.evaluate_all( dataset, dets['all_boxes'], dets['all_segms'], dets['all_keyps'], output_dir, use_matlab=args.matlab_eval ) task_evaluation.log_copy_paste_friendly_results(results)
def inference(self, image_list): """Do an inference on the model with a set of inputs. # Arguments: image_list: The input image list Return the result of the inference. """ image = image_list[0] image = self.linear_to_srgb(image)*255. imcpy = image.copy() # Initialize the model out of the configuration and weights files if not hasattr(self, 'model'): workspace.ResetWorkspace() # Reset to default config merge_cfg_from_cfg(self.default_cfg) # Load mask rcnn configuration file merge_cfg_from_file(self.cfg_file) assert_and_infer_cfg(cache_urls=False, make_immutable=False) self.model = infer_engine.initialize_model_from_cfg(self.weights) # Save mask rcnn full configuration file self.mrcnn_cfg = copy.deepcopy(AttrDict(cfg)) # cfg from detectron.core.config else: # There is a global config file for all detectron models (Densepose, Mask RCNN..) # Check if current global config file is correct for mask rcnn if not dict_equal(self.mrcnn_cfg, cfg): # Free memory of previous workspace workspace.ResetWorkspace() # Load mask rcnn configuration file merge_cfg_from_cfg(self.mrcnn_cfg) assert_and_infer_cfg(cache_urls=False, make_immutable=False) self.model = infer_engine.initialize_model_from_cfg(self.weights) with c2_utils.NamedCudaScope(0): cls_boxes, cls_segms, cls_keyps, _ = infer_engine.im_detect_all( self.model, image[:, :, ::-1], None ) if self.binary_masks: res = vis_utils.vis_one_image_binary( imcpy, cls_boxes, cls_segms, thresh=self.thresh ) else: res = vis_utils.vis_one_image_opencv( imcpy, cls_boxes, cls_segms, cls_keyps, thresh=self.thresh, show_box=self.show_box, show_class=self.show_class, dataset=self.dummy_coco_dataset, alpha=self.alpha, show_border=self.show_border, border_thick=self.border_thick, bbox_thick=self.bbox_thick, font_scale=self.font_scale ) res = self.srgb_to_linear(res.astype(np.float32) / 255.) return [res]
def test_merge_cfg_from_cfg(self): # Test: merge from deepcopy s = 'dummy0' cfg2 = copy.deepcopy(cfg) cfg2.MODEL.TYPE = s core_config.merge_cfg_from_cfg(cfg2) assert cfg.MODEL.TYPE == s # Test: merge from yaml s = 'dummy1' cfg2 = core_config.load_cfg(yaml.dump(cfg)) cfg2.MODEL.TYPE = s core_config.merge_cfg_from_cfg(cfg2) assert cfg.MODEL.TYPE == s # Test: merge with a valid key s = 'dummy2' cfg2 = AttrDict() cfg2.MODEL = AttrDict() cfg2.MODEL.TYPE = s core_config.merge_cfg_from_cfg(cfg2) assert cfg.MODEL.TYPE == s # Test: merge with an invalid key s = 'dummy3' cfg2 = AttrDict() cfg2.FOO = AttrDict() cfg2.FOO.BAR = s with self.assertRaises(KeyError): core_config.merge_cfg_from_cfg(cfg2) # Test: merge with converted type cfg2 = AttrDict() cfg2.TRAIN = AttrDict() cfg2.TRAIN.SCALES = [1] core_config.merge_cfg_from_cfg(cfg2) assert type(cfg.TRAIN.SCALES) is tuple assert cfg.TRAIN.SCALES[0] == 1 # Test: merge with invalid type cfg2 = AttrDict() cfg2.TRAIN = AttrDict() cfg2.TRAIN.SCALES = 1 with self.assertRaises(ValueError): core_config.merge_cfg_from_cfg(cfg2)
def test_merge_cfg_from_cfg(self): # Test: merge from deepcopy s = 'dummy0' cfg2 = copy.deepcopy(cfg) cfg2.MODEL.TYPE = s core_config.merge_cfg_from_cfg(cfg2) assert cfg.MODEL.TYPE == s # Test: merge from yaml s = 'dummy1' cfg2 = core_config.load_cfg(envu.yaml_dump(cfg)) cfg2.MODEL.TYPE = s core_config.merge_cfg_from_cfg(cfg2) assert cfg.MODEL.TYPE == s # Test: merge with a valid key s = 'dummy2' cfg2 = AttrDict() cfg2.MODEL = AttrDict() cfg2.MODEL.TYPE = s core_config.merge_cfg_from_cfg(cfg2) assert cfg.MODEL.TYPE == s # Test: merge with an invalid key s = 'dummy3' cfg2 = AttrDict() cfg2.FOO = AttrDict() cfg2.FOO.BAR = s with self.assertRaises(KeyError): core_config.merge_cfg_from_cfg(cfg2) # Test: merge with converted type cfg2 = AttrDict() cfg2.TRAIN = AttrDict() cfg2.TRAIN.SCALES = [1] core_config.merge_cfg_from_cfg(cfg2) assert type(cfg.TRAIN.SCALES) is tuple assert cfg.TRAIN.SCALES[0] == 1 # Test: merge with invalid type cfg2 = AttrDict() cfg2.TRAIN = AttrDict() cfg2.TRAIN.SCALES = 1 with self.assertRaises(ValueError): core_config.merge_cfg_from_cfg(cfg2)
setup_logging(__name__) logger = logging.getLogger(__name__) cfg_orig = load_cfg(yaml.dump(cfg)) #Download model of choice from https://github.com/facebookresearch/Detectron/blob/master/MODEL_ZOO.md" #Yaml files available at https://github.com/facebookresearch/Detectron/tree/master/configs models_dir = os.path.join(os.path.dirname(__file__), 'model_files') pkl = models_dir + "/kps_R-50-FPN.pkl" yml = models_dir + "/kps_R-50-FPN.yaml" if not os.path.isfile(pkl): exit("MODEL FILES NOT FOUND (check kpdetection.py)") #Config Setup cfg.immutable(False) merge_cfg_from_cfg(cfg_orig) merge_cfg_from_file(yml) weights_file = pkl cfg.NUM_GPUS = 1 assert_and_infer_cfg(cache_urls=False) model = model_engine.initialize_model_from_cfg(weights_file) #Detect all keypoints in im (image/frame) def detect(im): proposal_boxes, cls_boxes, cls_segms, cls_keyps \ = None, None, None, None with c2_utils.NamedCudaScope(0): cls_boxes_, cls_segms_, cls_keyps_ = \
def inference(self, image_list): """Do an inference of the DensePose model with a set of image inputs. # Arguments: image_list: The input image list Return the result of the inference. """ # Directly return image when no inference options if not (self.show_human_index or self.show_uv or self.show_border or self.show_grid): return [image_list[0]] image = image_list[0] image = self.linear_to_srgb(image) * 255. imcpy = image.copy() # Initialize the model out of the configuration and weights files if not hasattr(self, 'model'): workspace.ResetWorkspace() # Reset to default config merge_cfg_from_cfg(self.default_cfg) # Load densepose configuration file merge_cfg_from_file(self.cfg_file) assert_and_infer_cfg(cache_urls=False, make_immutable=False) self.model = infer_engine.initialize_model_from_cfg(self.weights) # Save densepose full configuration file self.densepose_cfg = copy.deepcopy( AttrDict(cfg)) #cfg from detectron.core.config else: # There is a global config file for all detectron models (Densepose, Mask RCNN..) # Check if current global config file is correct for densepose if not dict_equal(self.densepose_cfg, cfg): # Free memory of previous workspace workspace.ResetWorkspace() # Load densepose configuration file merge_cfg_from_cfg(self.densepose_cfg) assert_and_infer_cfg(cache_urls=False, make_immutable=False) self.model = infer_engine.initialize_model_from_cfg( self.weights) # Compute the image inference with c2_utils.NamedCudaScope(0): # image in BGR format for inference cls_boxes, cls_segms, cls_keyps, cls_bodys = infer_engine.im_detect_all( self.model, image[:, :, ::-1], None) res = vis_utils.vis_densepose( imcpy, # image in RGB format for visualization cls_boxes, cls_bodys, show_human_index=self.show_human_index, show_uv=self.show_uv, show_grid=self.show_grid, show_border=self.show_border, border_thick=self.border_thick, alpha=self.alpha) res = self.srgb_to_linear(res.astype(np.float32) / 255.) return [res]