def _test_std(self): current_dir = osp.dirname(osp.realpath(__file__)) cfg_file = osp.join(current_dir, '..', 'configs', 'R-50_1x.yaml') merge_cfg_from_file(cfg_file) cfg.TEST.WEIGHTS = osp.join( current_dir, '..', 'outputs', 'train', 'coco_2014_train+coco_2014_valminusminival', 'R-50_1x', 'default', 'model_final.pkl') cfg.RETINANET.INFERENCE_TH = 0. dataset = JsonDataset('coco_2014_minival') roidb = dataset.get_roidb() model = model_builder.create(cfg.MODEL.TYPE, train=False, gpu_id=0) utils.net.initialize_gpu_from_weights_file(model, cfg.TEST.WEIGHTS, gpu_id=0) model_builder.add_inference_inputs(model) workspace.CreateNet(model.net) workspace.CreateNet(model.conv_body_net) num_images = len(roidb) num_classes = cfg.MODEL.NUM_CLASSES entry = roidb[0] im = cv2.imread(entry['image']) with utils.c2.NamedCudaScope(0): cls_boxes, cls_preds, cls_probs, box_preds, anchors, im_info = im_detect_bbox( model, im, debug=True) workspace.ResetWorkspace() return cls_preds, cls_probs, box_preds, anchors, im_info
def im_detect_all(model, im, box_proposals, timers=None): if timers is None: timers = defaultdict(Timer) # Handle RetinaNet testing separately for now if cfg.RETINANET.RETINANET_ON: cls_boxes = test_retinanet.im_detect_bbox(model, im, timers) return cls_boxes, None, None timers['im_detect_bbox'].tic() if cfg.TEST.BBOX_AUG.ENABLED: scores, boxes, im_scale = im_detect_bbox_aug(model, im, box_proposals) else: scores, boxes, im_scale = im_detect_bbox(model, im, cfg.TEST.SCALE, cfg.TEST.MAX_SIZE, boxes=box_proposals) timers['im_detect_bbox'].toc() # score and boxes are from the whole image after score thresholding and nms # (they are not separated by class) # cls_boxes boxes and scores are separated by class and in the format used # for evaluating results timers['misc_bbox'].tic() scores, boxes, cls_boxes = box_results_with_nms_and_limit(scores, boxes) timers['misc_bbox'].toc() if cfg.MODEL.MASK_ON and boxes.shape[0] > 0: timers['im_detect_mask'].tic() if cfg.TEST.MASK_AUG.ENABLED: masks = im_detect_mask_aug(model, im, boxes) else: masks = im_detect_mask(model, im_scale, boxes) timers['im_detect_mask'].toc() timers['misc_mask'].tic() cls_segms = segm_results(cls_boxes, masks, boxes, im.shape[0], im.shape[1]) timers['misc_mask'].toc() else: cls_segms = None if cfg.MODEL.KEYPOINTS_ON and boxes.shape[0] > 0: timers['im_detect_keypoints'].tic() if cfg.TEST.KPS_AUG.ENABLED: heatmaps = im_detect_keypoints_aug(model, im, boxes) else: heatmaps = im_detect_keypoints(model, im_scale, boxes) timers['im_detect_keypoints'].toc() timers['misc_keypoints'].tic() cls_keyps = keypoint_results(cls_boxes, heatmaps, boxes) timers['misc_keypoints'].toc() else: cls_keyps = None return cls_boxes, cls_segms, cls_keyps
def im_detect_all(model, im, box_proposals, timers=None): if timers is None: timers = defaultdict(Timer) # Handle RetinaNet testing separately for now if cfg.RETINANET.RETINANET_ON: cls_boxes = test_retinanet.im_detect_bbox(model, im, timers) return cls_boxes, None, None timers['im_detect_bbox'].tic() if cfg.TEST.BBOX_AUG.ENABLED: scores, boxes, im_scale = im_detect_bbox_aug(model, im, box_proposals) else: scores, boxes, im_scale = im_detect_bbox( model, im, cfg.TEST.SCALE, cfg.TEST.MAX_SIZE, boxes=box_proposals ) timers['im_detect_bbox'].toc() # score and boxes are from the whole image after score thresholding and nms # (they are not separated by class) # cls_boxes boxes and scores are separated by class and in the format used # for evaluating results timers['misc_bbox'].tic() scores, boxes, cls_boxes = box_results_with_nms_and_limit(scores, boxes) timers['misc_bbox'].toc() if cfg.MODEL.MASK_ON and boxes.shape[0] > 0: timers['im_detect_mask'].tic() if cfg.TEST.MASK_AUG.ENABLED: masks = im_detect_mask_aug(model, im, boxes) else: masks = im_detect_mask(model, im_scale, boxes) timers['im_detect_mask'].toc() timers['misc_mask'].tic() cls_segms = segm_results( cls_boxes, masks, boxes, im.shape[0], im.shape[1] ) timers['misc_mask'].toc() else: cls_segms = None if cfg.MODEL.KEYPOINTS_ON and boxes.shape[0] > 0: timers['im_detect_keypoints'].tic() if cfg.TEST.KPS_AUG.ENABLED: heatmaps = im_detect_keypoints_aug(model, im, boxes) else: heatmaps = im_detect_keypoints(model, im_scale, boxes) timers['im_detect_keypoints'].toc() timers['misc_keypoints'].tic() cls_keyps = keypoint_results(cls_boxes, heatmaps, boxes) timers['misc_keypoints'].toc() else: cls_keyps = None return cls_boxes, cls_segms, cls_keyps
def _test_std(self): current_dir = osp.dirname(osp.realpath(__file__)) cfg_file = osp.join(current_dir, '..', 'configs', 'R-50_1x.yaml') merge_cfg_from_file(cfg_file) cfg.TEST.WEIGHTS = osp.join( current_dir, '..', 'outputs', 'train', 'coco_2014_train+coco_2014_valminusminival', 'R-50_1x', 'default', 'model_final.pkl') cfg.RETINANET.INFERENCE_TH = 0. dataset = JsonDataset('coco_2014_minival') roidb = dataset.get_roidb() model = model_builder.create(cfg.MODEL.TYPE, train=False, gpu_id=0) utils.net.initialize_gpu_from_weights_file(model, cfg.TEST.WEIGHTS, gpu_id=0) model_builder.add_inference_inputs(model) workspace.CreateNet(model.net) workspace.CreateNet(model.conv_body_net) num_images = len(roidb) num_classes = cfg.MODEL.NUM_CLASSES entry = roidb[5] im = cv2.imread(entry['image']) with utils.c2.NamedCudaScope(0): cls_boxes, cls_preds, cls_probs, box_preds, anchors, im_info = im_detect_bbox( model, im, debug=True) cls_boxes = cls_boxes[:, :5] im_name = osp.splitext(osp.basename(entry['image']))[0] # utils.vis.vis_one_image(im[:, :, ::-1], # '{:s}-std-output'.format(im_name), # current_dir, # cls_boxes, # segms=None, # keypoints=None, # thresh=0., # box_alpha=0.8, # dataset=dataset, # show_class=False) workspace.ResetWorkspace() return cls_preds, cls_probs, box_preds, anchors, im_info, im, im_name, current_dir, dataset
def im_detect_all(model, im, box_proposals=None, timers=None): """Process the outputs of model for testing Args: model: the network module im_data: Pytorch variable. Input batch to the model. im_info: Pytorch variable. Input batch to the model. gt_boxes: Pytorch variable. Input batch to the model. num_boxes: Pytorch variable. Input batch to the model. args: arguments from command line. timer: record the cost of time for different steps The rest of inputs are of type pytorch Variables and either input to or output from the model. """ if timers is None: timers = defaultdict(Timer) # Handle RetinaNet testing separately for now if cfg.RETINANET.RETINANET_ON: timers['im_detect_bbox'].tic() cls_boxes = test_retinanet.im_detect_bbox(model, im, timers) timers['im_detect_bbox'].toc() return cls_boxes, None, None timers['im_detect_bbox'].tic() if cfg.TEST.BBOX_AUG.ENABLED: scores, boxes, im_scale, blob_conv = im_detect_bbox_aug( model, im, box_proposals) else: scores, boxes, im_scale, blob_conv = im_detect_bbox( model, im, cfg.TEST.SCALE, cfg.TEST.MAX_SIZE, box_proposals) timers['im_detect_bbox'].toc() # score and boxes are from the whole image after score thresholding and nms # (they are not separated by class) (numpy.ndarray) # cls_boxes boxes and scores are separated by class and in the format used # for evaluating results timers['misc_bbox'].tic() scores, boxes, cls_boxes = box_results_with_nms_and_limit(scores, boxes) timers['misc_bbox'].toc() if cfg.MODEL.MASK_ON and boxes.shape[0] > 0: timers['im_detect_mask'].tic() if cfg.TEST.MASK_AUG.ENABLED: masks = im_detect_mask_aug(model, im, boxes, im_scale, blob_conv) else: masks = im_detect_mask(model, im_scale, boxes, blob_conv) timers['im_detect_mask'].toc() timers['misc_mask'].tic() cls_segms = segm_results(cls_boxes, masks, boxes, im.shape[0], im.shape[1]) timers['misc_mask'].toc() else: cls_segms = None if cfg.MODEL.KEYPOINTS_ON and boxes.shape[0] > 0: timers['im_detect_keypoints'].tic() if cfg.TEST.KPS_AUG.ENABLED: heatmaps = im_detect_keypoints_aug(model, im, boxes, im_scale, blob_conv) else: heatmaps = im_detect_keypoints(model, im_scale, boxes, blob_conv) timers['im_detect_keypoints'].toc() timers['misc_keypoints'].tic() cls_keyps = keypoint_results(cls_boxes, heatmaps, boxes) timers['misc_keypoints'].toc() else: cls_keyps = None return cls_boxes, cls_segms, cls_keyps
def im_detect_all(model, im, box_proposals, timers=None): if timers is None: timers = defaultdict(Timer) # Handle RetinaNet testing separately for now if cfg.RETINANET.RETINANET_ON: cls_boxes = test_retinanet.im_detect_bbox(model, im, timers) return cls_boxes, None, None timers['im_detect_bbox'].tic() if cfg.TEST.BBOX_AUG.ENABLED: scores, boxes, im_scale = im_detect_bbox_aug(model, im, box_proposals) else: # scores, boxes, im_scale = im_detect_bbox( # model, im, cfg.TEST.SCALE, cfg.TEST.MAX_SIZE, boxes=box_proposals # ) scores, boxes, rois, im_scale, fc7_feats, roi_feats, im_info = im_detect_bbox( model, im, cfg.TEST.SCALE, cfg.TEST.MAX_SIZE, boxes=box_proposals ) timers['im_detect_bbox'].toc() # score and boxes are from the whole image after score thresholding and nms # (they are not separated by class) # cls_boxes boxes and scores are separated by class and in the format used # for evaluating results ##Edited by mohit raw_boxes = boxes raw_scores = scores timers['misc_bbox'].tic() scores, boxes, cls_boxes = box_results_with_nms_and_limit(scores, boxes) rpn_size = raw_scores.shape[0] ans_boxes = np.zeros((rpn_size,4)) ans_scores = np.zeros((rpn_size,3)) #choosing the class with max score, also saving background score for bi in range(rpn_size): box_num = bi j=np.argmax(raw_scores[box_num,1:]) j=j+1 box = raw_boxes[box_num,j * 4:(j + 1) * 4] ans_boxes[box_num,:] = box ans_scores[box_num,0] = j ans_scores[box_num,1] = np.max(raw_scores[box_num,1:]) ans_scores[box_num,2] = raw_scores[box_num,0] #Bckground score ans_cls_boxes = {} for ci in range(len(cls_boxes)): if len(cls_boxes[ci]) > 0: ans_cls_boxes[ci] = cls_boxes[ci] timers['misc_bbox'].toc() return ans_scores, ans_boxes, ans_cls_boxes, im_scale, fc7_feats, im_info #edit end if cfg.MODEL.MASK_ON and boxes.shape[0] > 0: timers['im_detect_mask'].tic() if cfg.TEST.MASK_AUG.ENABLED: masks = im_detect_mask_aug(model, im, boxes) else: masks = im_detect_mask(model, im_scale, boxes) timers['im_detect_mask'].toc() timers['misc_mask'].tic() cls_segms = segm_results( cls_boxes, masks, boxes, im.shape[0], im.shape[1] ) timers['misc_mask'].toc() else: cls_segms = None if cfg.MODEL.KEYPOINTS_ON and boxes.shape[0] > 0: timers['im_detect_keypoints'].tic() if cfg.TEST.KPS_AUG.ENABLED: heatmaps = im_detect_keypoints_aug(model, im, boxes) else: heatmaps = im_detect_keypoints(model, im_scale, boxes) timers['im_detect_keypoints'].toc() timers['misc_keypoints'].tic() cls_keyps = keypoint_results(cls_boxes, heatmaps, boxes) timers['misc_keypoints'].toc() else: cls_keyps = None return cls_boxes, cls_segms, cls_keyps
def im_detect_all(model, im, box_proposals, save_name, timers=None): if timers is None: timers = defaultdict(Timer) # Handle RetinaNet testing separately for now if cfg.RETINANET.RETINANET_ON: cls_boxes = test_retinanet.im_detect_bbox(model, im, timers) return cls_boxes, None, None timers['im_detect_bbox'].tic() if cfg.TEST.BBOX_AUG.ENABLED: scores, boxes, im_scale = im_detect_bbox_aug(model, im, box_proposals) else: scores, boxes, im_scale, fc6, roi_feat = im_detect_bbox( model, im, cfg.TEST.SCALE, cfg.TEST.MAX_SIZE, boxes=box_proposals) timers['im_detect_bbox'].toc() # score and boxes are from the whole image after score thresholding and nms # (they are not separated by class) # cls_boxes boxes and scores are separated by class and in the format used # for evaluating results timers['misc_bbox'].tic() scores, boxes, cls_boxes, fc6, roi_feat = box_results_with_nms_and_limit( scores, boxes, fc6, roi_feat) timers['misc_bbox'].toc() if cfg.MODEL.MASK_ON and boxes.shape[0] > 0: timers['im_detect_mask'].tic() if cfg.TEST.MASK_AUG.ENABLED: masks = im_detect_mask_aug(model, im, boxes) else: masks, masks_roi_feat, masks_fcn1, masks_fcn2, masks_fcn3, masks_fcn4 = im_detect_mask( model, im_scale, boxes) timers['im_detect_mask'].toc() timers['misc_mask'].tic() cls_segms, feat_masks = segm_results(cls_boxes, masks, boxes, im.shape[0], im.shape[1]) timers['misc_mask'].toc() else: cls_segms = None if cfg.MODEL.KEYPOINTS_ON and boxes.shape[0] > 0: timers['im_detect_keypoints'].tic() if cfg.TEST.KPS_AUG.ENABLED: heatmaps = im_detect_keypoints_aug(model, im, boxes) else: heatmaps = im_detect_keypoints(model, im_scale, boxes) timers['im_detect_keypoints'].toc() timers['misc_keypoints'].tic() cls_keyps = keypoint_results(cls_boxes, heatmaps, boxes) timers['misc_keypoints'].toc() else: cls_keyps = None feat_masks = feat_masks.astype('float32') masks_roi_feat = (masks_roi_feat * feat_masks).sum(2).sum(2) / ( feat_masks.sum(2).sum(2) + 0.0000001) masks_fcn1 = (masks_fcn1 * feat_masks).sum(2).sum(2) / ( feat_masks.sum(2).sum(2) + 0.0000001) masks_fcn2 = (masks_fcn2 * feat_masks).sum(2).sum(2) / ( feat_masks.sum(2).sum(2) + 0.0000001) masks_fcn3 = (masks_fcn3 * feat_masks).sum(2).sum(2) / ( feat_masks.sum(2).sum(2) + 0.0000001) masks_fcn4 = (masks_fcn4 * feat_masks).sum(2).sum(2) / ( feat_masks.sum(2).sum(2) + 0.0000001) np.savez(save_name, cls_segms = cls_segms, fc6 = fc6 , roi_feat =roi_feat, masks_roi_feat = masks_roi_feat, \ masks_fcn1 = masks_fcn1, masks_fcn2 = masks_fcn2, masks_fcn3 = masks_fcn3, masks_fcn4 = masks_fcn4 ) return cls_boxes, cls_segms, cls_keyps