def test_cls_net(weights_file, dataset_name, proposal_file, output_dir, ind_range=None, gpu_id=0): """Run inference on all images in a dataset or over an index range of images in a dataset using a single GPU. """ assert weights_file != '', \ 'TEST.WEIGHTS must be set to the model file to test' assert not cfg.MODEL.RPN_ONLY, \ 'Use rpn_generate to generate proposals from RPN-only models' assert dataset_name != '', \ 'TEST.DATASET must be set to the dataset name to test' roidb, dataset, start_ind, end_ind, total_num_images = get_roidb_and_dataset( dataset_name, proposal_file, ind_range,gt_cls=True ) model = initialize_model_from_cfg(weights_file, gpu_id=gpu_id) num_images = len(roidb) num_classes = cfg.MODEL.NUM_CLASSES all_cls = [] all_labs = [] timers = defaultdict(Timer) for i, entry in enumerate(roidb): im = cv2.imread(entry['image']) with c2_utils.NamedCudaScope(gpu_id): cls_scores, _, _ = im_detect_all( model, im, None, timers ) all_cls.append(np.argmax(cls_scores)) all_labs.append(entry["gt_classes"][0]) if i % 10 == 0: # Reduce log file size ave_total_time = np.sum([t.average_time for t in timers.values()]) eta_seconds = ave_total_time * (num_images - i - 1) eta = str(datetime.timedelta(seconds=int(eta_seconds))) cls_time = ( timers['classify_im'].average_time ) logger.info( ( 'im_detect: range [{:d}, {:d}] of {:d}: ' '{:d}/{:d} {:.3f}s (eta: {})' ).format( start_ind + 1, end_ind, total_num_images, start_ind + i + 1, start_ind + num_images, cls_time, eta ) ) correct_pred = np.equal(np.array(all_cls), np.array(all_labs)) acc = np.mean(correct_pred.astype(np.float32)) return acc
def detect(): im = cv2.imread(File) entry = {} entry['id'] = File.split('/')[-1].split('.')[0] entry['image'] = File print(entry) timers = defaultdict(Timer) with c2_utils.NamedCudaScope(gpu_id): cls_boxes_i, cls_segms_i, cls_keyps_i = im_detect_all( model, im, None, timers, entry) mask_res_top = test_res_top.res_top_result(model, entry) resize_pred = cv2.resize(mask_res_top.argmax(0), (160, 300), interpolation=cv2.INTER_NEAREST) cv2.imwrite(cache_file, resize_pred) global png global png_pred png = Image.open(cache_file).convert('P') png.putpalette(cmap) png_pred = ImageTk.PhotoImage(png) # canvas.image = filename # <--- keep reference of your image canvas.create_image(170, 0, anchor='nw', image=png_pred) print('done')
def test_net( weights_file, dataset_name, proposal_file, output_dir, ind_range=None, gpu_id=0 ): """Run inference on all images in a dataset or over an index range of images in a dataset using a single GPU. """ assert not cfg.MODEL.RPN_ONLY, \ 'Use rpn_generate to generate proposals from RPN-only models' roidb, dataset, start_ind, end_ind, total_num_images = get_roidb_and_dataset( dataset_name, proposal_file, ind_range ) model = initialize_model_from_cfg(weights_file, gpu_id=gpu_id) # pose_pred_model = generate_poseJPPNet_pred_model() num_images = len(roidb) num_classes = cfg.MODEL.NUM_CLASSES all_boxes, all_segms, all_keyps = empty_results(num_classes, num_images) timers = defaultdict(Timer) for i, entry in enumerate(roidb): if cfg.TEST.PRECOMPUTED_PROPOSALS: # The roidb may contain ground-truth rois (for example, if the roidb # comes from the training or val split). We only want to evaluate # detection on the *non*-ground-truth rois. We select only the rois # that have the gt_classes field set to 0, which means there's no # ground truth. box_proposals = entry['boxes'][entry['gt_classes'] == 0] if len(box_proposals) == 0: continue else: # Faster R-CNN type models generate proposals on-the-fly with an # in-network RPN; 1-stage models don't require proposals. box_proposals = None im = cv2.imread(entry['image']) with c2_utils.NamedCudaScope(gpu_id): cls_boxes_i, cls_segms_i, cls_keyps_i = im_detect_all( model, im, box_proposals, timers, entry ) # test_res_top.feedBlob_run(model, im, entry) mask_res_top = test_res_top.res_top_result(model, entry) extend_results(i, all_boxes, cls_boxes_i) if cls_segms_i is not None: extend_results(i, all_segms, cls_segms_i) if cls_keyps_i is not None: extend_results(i, all_keyps, cls_keyps_i) if i % 10 == 0: # Reduce log file size ave_total_time = np.sum([t.average_time for t in timers.values()]) eta_seconds = ave_total_time * (num_images - i - 1) eta = str(datetime.timedelta(seconds=int(eta_seconds))) det_time = ( timers['im_detect_bbox'].average_time + timers['im_detect_mask'].average_time + timers['im_detect_keypoints'].average_time ) misc_time = ( timers['misc_bbox'].average_time + timers['misc_mask'].average_time + timers['misc_keypoints'].average_time ) logger.info( ( 'im_detect: range [{:d}, {:d}] of {:d}: ' '{:d}/{:d} {:.3f}s + {:.3f}s (eta: {})' ).format( start_ind + 1, end_ind, total_num_images, start_ind + i + 1, start_ind + num_images, det_time, misc_time, eta ) ) if cfg.VIS: im_name = os.path.splitext(os.path.basename(entry['image']))[0] mask_png_20 = vis_utils.vis_one_image( im[:, :, ::-1], '{:d}_{:s}'.format(i, im_name), os.path.join(output_dir, 'vis_9k'), cls_boxes_i, segms=cls_segms_i, keypoints=cls_keyps_i, thresh=cfg.VIS_TH, box_alpha=0.8, dataset=dataset, show_class=True ) # fusion mask_fusion = mask_png_20 + mask_res_top mask_fusion_out = os.path.join(output_dir, 'fusion') if not os.path.exists(mask_fusion_out): os.makedirs(mask_fusion_out) # logger.info('fusion save to {}'.format(mask_fusion_out)) cv2.imwrite(os.path.join(mask_fusion_out, entry['id']+'.png'), mask_fusion.argmax(0)) cfg_yaml = yaml.dump(cfg) if ind_range is not None: det_name = 'detection_range_%s_%s.pkl' % tuple(ind_range) else: det_name = 'detections.pkl' det_file = os.path.join(output_dir, det_name) save_object( dict( all_boxes=all_boxes, all_segms=all_segms, all_keyps=all_keyps, cfg=cfg_yaml ), det_file ) logger.info('Wrote detections to: {}'.format(os.path.abspath(det_file))) return all_boxes, all_segms, all_keyps
def detect_im(weights_file, roidb, gamma, idxs=None, gpu_id=0): '''detect the unlabeled samples''' roidb = [roidb[i] for i in idxs] model = infer_engine.initialize_model_from_cfg(weights_file, gpu_id=gpu_id) thresh = gamma allBoxes = [] allScore = [] allY = [] eps = 0 al_idx = [] allClass = [] ALScore = [] timers = defaultdict(Timer) dummy_coco_dataset = dummy_datasets.get_coco_dataset() for i, entry in enumerate(roidb): box_proposals = None im = cv2.imread(entry['image']) with c2_utils.NamedCudaScope(gpu_id): cls_boxes_i, cls_segms_i, cls_keyps_i = im_detect_all( model, im, box_proposals, timers) # scores, boxes, im_scale = im_detect_bbox_aug(model, im, box_proposals) ## print('scores:{},boxes:{}'.format(scores.shape,boxes.shape)) # # scores_i, boxes_i, cls_boxes_i = box_results_with_nms_and_limit(scores, boxes) # cls_segms_i = None;cls_keyps_i = None # output_dir = './'+str(gamma) # if True: # im_name = os.path.splitext(os.path.basename(entry['image']))[0] # vis_utils.vis_one_image( # im[:, :, ::-1], # '{:d}_{:s}'.format(i, im_name), # os.path.join(output_dir, 'vis'), # cls_boxes_i, # segms=None, # keypoints=None, # thresh=0.9, # box_alpha=0.8, # dataset=dummy_coco_dataset, # show_class=True # ) if isinstance(cls_boxes_i, list): boxes, segms, keypoints, classes = convert_from_cls_format( cls_boxes_i, cls_segms_i, cls_keyps_i) if boxes is None or boxes.shape[0] == 0 or max(boxes[:, 4]) < thresh: # al process al_idx.append(idxs[i]) if boxes is not None and boxes.shape[0] != 0: ALScore.append(np.mean(boxes[:, 4])) else: ALScore.append(0) continue # print('scores_i:{},boxes_i:{},boxes:{},cls_boxes_i:{}'.format(scores_i, boxes_i,boxes, cls_boxes_i)) areas = (boxes[:, 2] - boxes[:, 0]) * (boxes[:, 3] - boxes[:, 1]) sorted_inds = np.argsort(-areas) BBox = [] Score = [] Y = [] Class = [] for i in sorted_inds: bbox = boxes[i, :4] score = boxes[i, -1] # add self-supervised process if score < thresh: continue BBox.append(list(bbox)) Score.append(score) # only one class score ?? Class.append(classes[i]) allBoxes.append(BBox) allClass.append(Class) allScore.append(Score) return allBoxes, allClass, allScore, al_idx, ALScore
def test_net( weights_file, dataset_name, proposal_file, output_dir, ind_range=None, gpu_id=0 ): """Run inference on all images in a dataset or over an index range of images in a dataset using a single GPU. """ assert not cfg.MODEL.RPN_ONLY, \ 'Use rpn_generate to generate proposals from RPN-only models' roidb, dataset, start_ind, end_ind, total_num_images = get_roidb_and_dataset( dataset_name, proposal_file, ind_range ) model = initialize_model_from_cfg(weights_file, gpu_id=gpu_id) num_images = len(roidb) num_classes = cfg.MODEL.NUM_CLASSES all_boxes, all_segms, all_keyps, all_bodys = \ empty_results(num_classes, num_images) timers = defaultdict(Timer) for i, entry in enumerate(roidb): if 'has_no_densepose' in entry.keys(): pass else: if cfg.TEST.PRECOMPUTED_PROPOSALS: # The roidb may contain ground-truth rois (for example, if the roidb # comes from the training or val split). We only want to evaluate # detection on the *non*-ground-truth rois. We select only the rois # that have the gt_classes field set to 0, which means there's no # ground truth. box_proposals = entry['boxes'][entry['gt_classes'] == 0] if len(box_proposals) == 0: continue else: # Faster R-CNN type models generate proposals on-the-fly with an # in-network RPN; 1-stage models don't require proposals. box_proposals = None im = cv2.imread(entry['image']) with c2_utils.NamedCudaScope(gpu_id): cls_boxes_i, cls_segms_i, cls_keyps_i,cls_bodys_i = \ im_detect_all(model, im, box_proposals, timers) extend_results(i, all_boxes, cls_boxes_i) if cls_segms_i is not None: extend_results(i, all_segms, cls_segms_i) if cls_keyps_i is not None: extend_results(i, all_keyps, cls_keyps_i) if cls_bodys_i is not None: extend_results(i, all_bodys, cls_bodys_i) if i % 10 == 0: # Reduce log file size ave_total_time = np.sum([t.average_time for t in timers.values()]) eta_seconds = ave_total_time * (num_images - i - 1) eta = str(datetime.timedelta(seconds=int(eta_seconds))) det_time = ( timers['im_detect_bbox'].average_time + timers['im_detect_mask'].average_time + timers['im_detect_keypoints'].average_time + timers['im_detect_body_uv'].average_time ) misc_time = ( timers['misc_bbox'].average_time + timers['misc_mask'].average_time + timers['misc_keypoints'].average_time + timers['misc_body_uv'].average_time ) logger.info( ( 'im_detect: range [{:d}, {:d}] of {:d}: ' '{:d}/{:d} {:.3f}s + {:.3f}s (eta: {})' ).format( start_ind + 1, end_ind, total_num_images, start_ind + i + 1, start_ind + num_images, det_time, misc_time, eta ) ) if cfg.VIS: im_name = os.path.splitext(os.path.basename(entry['image']))[0] vis_utils.vis_one_image( im[:, :, ::-1], '{:d}_{:s}'.format(i, im_name), os.path.join(output_dir, 'vis'), cls_boxes_i, segms=cls_segms_i, keypoints=cls_keyps_i, thresh=cfg.VIS_TH, box_alpha=0.8, dataset=dataset, show_class=True ) cfg_yaml = yaml.dump(cfg) if ind_range is not None: det_name = 'detection_range_%s_%s.pkl' % tuple(ind_range) else: det_name = 'detections.pkl' det_file = os.path.join(output_dir, det_name) save_object( dict( all_boxes=all_boxes, all_segms=all_segms, all_keyps=all_keyps, all_bodys=all_bodys, cfg=cfg_yaml ), det_file ) logger.info('Wrote detections to: {}'.format(os.path.abspath(det_file))) return all_boxes, all_segms, all_keyps, all_bodys
def test_net( weights_file, dataset_name, proposal_file, output_dir, ind_range=None, gpu_id=0 ): """Run inference on all images in a dataset or over an index range of images in a dataset using a single GPU. """ assert not cfg.MODEL.RPN_ONLY, \ 'Use rpn_generate to generate proposals from RPN-only models' if cfg.TEST.IMS_PER_BATCH != 1: return test_net_batch(weights_file, dataset_name, proposal_file, output_dir, ind_range, gpu_id) roidb, dataset, start_ind, end_ind, total_num_images = get_roidb_and_dataset( dataset_name, proposal_file, ind_range ) # roidb = roidb[:500] # roidb = [{'image': im} for im in glob.glob('/staging/leuven/stg_00027/imob/detectron/lib/datasets/data/coco/coco_val2014/*.png')][:500] model = initialize_model_from_cfg(weights_file, gpu_id=gpu_id) num_images = len(roidb) num_classes = cfg.MODEL.NUM_CLASSES all_boxes, all_segms, all_keyps = empty_results(num_classes, num_images) timers = defaultdict(Timer) for i, entry in enumerate(roidb): if cfg.TEST.PRECOMPUTED_PROPOSALS: # The roidb may contain ground-truth rois (for example, if the roidb # comes from the training or val split). We only want to evaluate # detection on the *non*-ground-truth rois. We select only the rois # that have the gt_classes field set to 0, which means there's no # ground truth. box_proposals = entry['boxes'][entry['gt_classes'] == 0] if len(box_proposals) == 0: continue else: # Faster R-CNN type models generate proposals on-the-fly with an # in-network RPN; 1-stage models don't require proposals. box_proposals = None im = cv2.imread(entry['image']) with c2_utils.NamedCudaScope(gpu_id): cls_boxes_i, cls_segms_i, cls_keyps_i = im_detect_all( model, im, box_proposals, timers ) extend_results(i, all_boxes, cls_boxes_i) if cls_segms_i is not None: extend_results(i, all_segms, cls_segms_i) if cls_keyps_i is not None: extend_results(i, all_keyps, cls_keyps_i) if i % 10 == 0: # Reduce log file size ave_total_time = np.sum([t.average_time for t in timers.values()]) eta_seconds = ave_total_time * (num_images - i - 1) eta = str(datetime.timedelta(seconds=int(eta_seconds))) det_time = ( timers['im_detect_bbox'].average_time + timers['im_detect_mask'].average_time + timers['im_detect_keypoints'].average_time ) misc_time = ( timers['misc_bbox'].average_time + timers['misc_mask'].average_time + timers['misc_keypoints'].average_time ) logger.info( ( 'im_detect: range [{:d}, {:d}] of {:d}: ' '{:d}/{:d} {:.3f}s + {:.3f}s (eta: {})' ).format( start_ind + 1, end_ind, total_num_images, start_ind + i + 1, start_ind + num_images, det_time, misc_time, eta ) ) if cfg.VIS: im_name = os.path.splitext(os.path.basename(entry['image']))[0] vis_utils.vis_one_image( im[:, :, ::-1], '{:d}_{:s}'.format(i, im_name), os.path.join(output_dir, 'vis'), cls_boxes_i, segms=cls_segms_i, keypoints=cls_keyps_i, thresh=cfg.VIS_TH, box_alpha=0.8, dataset=dataset, show_class=True ) cfg_yaml = envu.yaml_dump(cfg) if ind_range is not None: det_name = 'detection_range_%s_%s.pkl' % tuple(ind_range) else: det_name = 'detections.pkl' det_file = os.path.join(output_dir, det_name) save_object( dict( all_boxes=all_boxes, all_segms=all_segms, all_keyps=all_keyps, cfg=cfg_yaml ), det_file ) logger.info('Wrote detections to: {}'.format(os.path.abspath(det_file))) return all_boxes, all_segms, all_keyps
def next_batch(self): data_blob = np.zeros( (self.batch_size, input_size[0], input_size[1], 40), dtype=np.float32) gt_blob = np.zeros((self.batch_size, input_size[0], input_size[1]), dtype=np.int32) batch_id = 0 #for i in range(self.batch_size): while True: # print("start loading data batch {}".format(batch_id)) entry = self.roidb[self.cur] self.cur += 1 if self.cur >= self.num_samples: self.cur = 0 if self.is_train: # shuffle random.shuffle(self.seq) im = cv2.imread(entry['image']) label = cv2.imread(entry['label'], 0) # model test create human timers = defaultdict(Timer) with c2_utils.NamedCudaScope(gpu_id): cls_boxes_i, cls_segms_i, cls_keyps_i = im_detect_all( self.test_model, im, None, timers, entry) mask_res_top = test_res_top.res_top_result(self.test_model, entry, save_png=False) im_name = os.path.splitext(os.path.basename(entry['image']))[0] mask_png_20 = vis_utils.vis_one_image(im[:, :, ::-1], '{:d}_{:s}'.format( self.cur, im_name), output_dir, cls_boxes_i, segms=cls_segms_i, keypoints=cls_keyps_i, thresh=cfg.VIS_TH, box_alpha=0.8, dataset=None, show_class=False, save_png=False) if len(mask_png_20.shape) == 2: print("data next continue") continue #resize #print("mask_res_top shape:", mask_res_top.shape) mask_restop_resize = cv2.resize(mask_res_top.transpose([1, 2, 0]), (input_size[0], input_size[1]), interpolation=cv2.INTER_LINEAR) #print("mask_restop_resize: ", mask_restop_resize.shape) data_blob[batch_id, :, :, 0:20] = mask_restop_resize #print("data_blob:", data_blob.shape) # print("mask_png_20 shape", mask_png_20.shape) mask_png_20 = mask_png_20.transpose((1, 2, 0)) #print("mask_png_20 shape:", mask_png_20.shape) #print("data_blob shape:", data_blob.shape) data_blob[batch_id, :, :, 20:] = cv2.resize(mask_png_20, (input_size[0], input_size[1]), interpolation=cv2.INTER_LINEAR) gt_blob[batch_id] = cv2.resize(label, (input_size[0], input_size[1]), interpolation=cv2.INTER_NEAREST) # find c==4 sunglasses index = np.where(gt_blob[batch_id] == 4) if len(index[0]): print('have sunglass, pixles:', len(index[0])) # print("loaded data batch {}".format(batch_id)) batch_id += 1 if batch_id >= self.batch_size: break data_blob = np.transpose(data_blob, [0, 3, 1, 2]) # NHWC-> NCHW return data_blob, gt_blob
def test_net(weights_file, dataset_name, proposal_file, output_dir, ind_range=None, gpu_id=0): """Run inference on all images in a dataset or over an index range of images in a dataset using a single GPU. """ assert not cfg.MODEL.RPN_ONLY, \ 'Use rpn_generate to generate proposals from RPN-only models' roidb, dataset, start_ind, end_ind, total_num_images = get_roidb_and_dataset( dataset_name, proposal_file, ind_range) model = initialize_model_from_cfg(weights_file, gpu_id=gpu_id) num_images = len(roidb) num_classes = cfg.MODEL.NUM_CLASSES all_boxes, all_segms, all_keyps, all_personmasks, all_parss, all_bodys = \ empty_results(num_classes, num_images) timers = defaultdict(Timer) txt_all = [] parsing_i_png = None for i, entry in enumerate(roidb): if 'has_no_densepose' in entry.keys(): pass else: if cfg.TEST.PRECOMPUTED_PROPOSALS: # The roidb may contain ground-truth rois (for example, if the roidb # comes from the training or val split). We only want to evaluate # detection on the *non*-ground-truth rois. We select only the rois # that have the gt_classes field set to 0, which means there's no # ground truth. box_proposals = entry['boxes'][entry['gt_classes'] == 0] if len(box_proposals) == 0: continue else: # Faster R-CNN type models generate proposals on-the-fly with an # in-network RPN; 1-stage models don't require proposals. box_proposals = None im = cv2.imread(entry['image']) with c2_utils.NamedCudaScope(gpu_id): cls_boxes_i, cls_segms_i, cls_keyps_i, cls_personmask_i, cls_parss_i, cls_bodys_i = \ im_detect_all(model, im, box_proposals, timers) if cfg.MODEL.PARSING_ON: parsing_i_png, txt_result = parsing_utils.parsing2png( cls_boxes_i, cls_parss_i, output_dir, entry['image'], im.shape[:2]) ''' person_mask_i_png, txt_result_pm = parsing_utils.parsing2png( cls_boxes_i, cls_personmask_i, output_dir, entry['image'], im.shape[:2],flag_pm=True, ) ''' txt_all.append(txt_result) extend_results(i, all_boxes, cls_boxes_i) if cls_segms_i is not None: extend_results(i, all_segms, cls_segms_i) if cls_keyps_i is not None: extend_results(i, all_keyps, cls_keyps_i) if cls_personmask_i is not None: extend_results(i, all_personmasks, cls_personmask_i) if cls_parss_i is not None: extend_results(i, all_parss, cls_parss_i) if cls_bodys_i is not None: extend_results(i, all_bodys, cls_bodys_i) if i % 10 == 0: # Reduce log file size ave_total_time = np.sum([t.average_time for t in timers.values()]) eta_seconds = ave_total_time * (num_images - i - 1) eta = str(datetime.timedelta(seconds=int(eta_seconds))) det_time = (timers['im_detect_bbox'].average_time + timers['im_detect_mask'].average_time + timers['im_detect_keypoints'].average_time + timers['im_detect_body_uv'].average_time) misc_time = (timers['misc_bbox'].average_time + timers['misc_mask'].average_time + timers['misc_keypoints'].average_time + timers['misc_body_uv'].average_time) logger.info(('im_detect: range [{:d}, {:d}] of {:d}: ' '{:d}/{:d} {:.3f}s + {:.3f}s (eta: {})').format( start_ind + 1, end_ind, total_num_images, start_ind + i + 1, start_ind + num_images, det_time, misc_time, eta)) if cfg.VIS and not 'has_no_densepose' in entry.keys(): im_name = os.path.splitext(os.path.basename(entry['image']))[0] vis_utils.vis_one_image(im[:, :, ::-1], '{:d}_{:s}'.format(i, im_name), os.path.join(output_dir, 'vis'), cls_boxes_i, segms=cls_personmask_i, keypoints=cls_keyps_i, body_uv=cls_bodys_i, part_segms=parsing_i_png, thresh=cfg.VIS_TH, box_alpha=0.8, dataset=dataset, show_class=True) cfg_yaml = yaml.dump(cfg) if ind_range is not None: det_name = 'detection_range_%s_%s.pkl' % tuple(ind_range) else: det_name = 'detections.pkl' det_file = os.path.join(output_dir, det_name) save_object( dict(all_boxes=all_boxes, all_segms=all_segms, all_keyps=all_keyps, all_personmasks=all_personmasks, all_parss=all_parss, all_bodys=all_bodys, cfg=cfg_yaml), det_file) logger.info('Wrote detections to: {}'.format(os.path.abspath(det_file))) return all_boxes, all_segms, all_keyps, all_personmasks, all_parss, all_bodys
def test_net(weights_file, dataset_name, proposal_file, output_dir, ind_range=None, gpu_id=0, subset_pointer=None): """Run inference on all images in a dataset or over an index range of images in a dataset using a single GPU. """ assert not cfg.MODEL.RPN_ONLY, \ 'Use rpn_generate to generate proposals from RPN-only models' # determine file name if ind_range is not None: det_name = 'detection_range_%s_%s.pkl' % tuple(ind_range) else: det_name = 'detections.pkl' det_file = os.path.join(output_dir, det_name) # load results if already present if os.path.exists(det_file): res = load_object(det_file) all_boxes, all_segms, all_keyps = res['all_boxes'], res[ 'all_segms'], res['all_keyps'] else: roidb, dataset, start_ind, end_ind, total_num_images = get_roidb_and_dataset( dataset_name, proposal_file, ind_range) if subset_pointer is not None: voc_subset = subset_pointer.subset this_sub = voc_subset[:len(roidb)] # subset_pointer.subset = voc_subset[len(roidb):] # filter roidb: roidb = [roi for taking, roi in zip(this_sub, roidb) if taking] total_num_images = len(roidb) end_ind = total_num_images model = initialize_model_from_cfg(weights_file, gpu_id=gpu_id) num_images = len(roidb) num_classes = cfg.MODEL.NUM_CLASSES all_boxes, all_segms, all_keyps = empty_results( num_classes, num_images) if cfg.TEST.COLLECT_ALL: all_feats = [] all_class_weights = np.empty(shape=(num_images, num_classes), dtype=np.float32) timers = defaultdict(Timer) for i, entry in enumerate(roidb): if cfg.TEST.PRECOMPUTED_PROPOSALS: # The roidb may contain ground-truth rois (for example, if the roidb # comes from the training or val split). We only want to evaluate # detection on the *non*-ground-truth rois. We select only the rois # that have the gt_classes field set to 0, which means there's no # ground truth. box_proposals = entry['boxes'][entry['gt_classes'] == 0] if len(box_proposals) == 0: continue else: # Faster R-CNN type models generate proposals on-the-fly with an # in-network RPN; 1-stage models don't require proposals. box_proposals = None im = cv2.imread(entry['image']) with c2_utils.NamedCudaScope(gpu_id): cls_boxes_i, cls_segms_i, cls_keyps_i, sum_softmax, topk_feats = im_detect_all( model, im, box_proposals, timers, return_feats=cfg.TEST.COLLECT_ALL) # print('nfeats:', topk_feats.shape[0]) # print(topk_feats) extend_results(i, all_boxes, cls_boxes_i) if cls_segms_i is not None: extend_results(i, all_segms, cls_segms_i) if cls_keyps_i is not None: extend_results(i, all_keyps, cls_keyps_i) if cfg.TEST.COLLECT_ALL: all_class_weights[i] = sum_softmax all_feats.append( topk_feats ) # will accumulate about 9 Gb of feats on COCO train set (118K imgs) if i % 10 == 0: # Reduce log file size ave_total_time = np.sum( [t.average_time for t in timers.values()]) eta_seconds = ave_total_time * (num_images - i - 1) eta = str(datetime.timedelta(seconds=int(eta_seconds))) det_time = (timers['im_detect_bbox'].average_time + timers['im_detect_mask'].average_time + timers['im_detect_keypoints'].average_time) misc_time = (timers['misc_bbox'].average_time + timers['misc_mask'].average_time + timers['misc_keypoints'].average_time) logger.info(('im_detect: range [{:d}, {:d}] of {:d}: ' '{:d}/{:d} {:.3f}s + {:.3f}s (eta: {})').format( start_ind + 1, end_ind, total_num_images, start_ind + i + 1, start_ind + num_images, det_time, misc_time, eta)) if cfg.VIS: im_name = os.path.splitext(os.path.basename(entry['image']))[0] vis_utils.vis_one_image(im[:, :, ::-1], '{:d}_{:s}'.format(i, im_name), os.path.join(output_dir, 'vis'), cls_boxes_i, segms=cls_segms_i, keypoints=cls_keyps_i, thresh=cfg.VIS_TH, box_alpha=0.8, dataset=dataset, show_class=True) cfg_yaml = envu.yaml_dump(cfg) save_object( dict(all_boxes=all_boxes, all_segms=all_segms, all_keyps=all_keyps, cfg=cfg_yaml), det_file) logger.info('Wrote detections to: {}'.format( os.path.abspath(det_file))) if cfg.TEST.COLLECT_ALL: save_object(all_class_weights, os.path.join(output_dir, 'class_weights.pkl')) save_object(all_feats, os.path.join(output_dir, 'feature_vectors.pkl')) logger.info( 'Wrote class weights and feature vectors to output folder') return all_boxes, all_segms, all_keyps
def test_net(weights_file, dataset_name, proposal_file, output_dir, ind_range=None, gpu_id=0): """Run inference on all images in a dataset or over an index range of images in a dataset using a single GPU. """ assert not cfg.MODEL.RPN_ONLY, \ 'Use rpn_generate to generate proposals from RPN-only models' fp32_ws_name = "__fp32_ws__" int8_ws_name = "__int8_ws__" roidb, dataset, start_ind, end_ind, total_num_images = get_roidb_and_dataset( dataset_name, proposal_file, ind_range) model1 = None if os.environ.get('COSIM'): workspace.SwitchWorkspace(int8_ws_name, True) model, ob, ob_mask, ob_keypoint = initialize_model_from_cfg(weights_file, gpu_id=gpu_id) if os.environ.get('COSIM'): workspace.SwitchWorkspace(fp32_ws_name, True) model1, _, _, _ = initialize_model_from_cfg(weights_file, gpu_id=gpu_id, int8=False) num_images = len(roidb) num_classes = cfg.MODEL.NUM_CLASSES all_boxes, all_segms, all_keyps = empty_results(num_classes, num_images) timers = defaultdict(Timer) # for kl_divergence calibration, we use the first 100 images to get # the min and max values, and the remaing images are applied to compute the hist. # if the len(images) <= 100, we extend the images with themselves. if os.environ.get('INT8INFO') == "1" and os.environ.get( 'INT8CALIB') == "kl_divergence": kl_iter_num_for_range = int(os.environ.get('INT8KLNUM')) if not kl_iter_num_for_range: kl_iter_num_for_range = 100 while (len(roidb) < 2 * kl_iter_num_for_range): roidb += roidb if os.environ.get('EPOCH2') == "1": for i, entry in enumerate(roidb): if cfg.TEST.PRECOMPUTED_PROPOSALS: # The roidb may contain ground-truth rois (for example, if the roidb # comes from the training or val split). We only want to evaluate # detection on the *non*-ground-truth rois. We select only the rois # that have the gt_classes field set to 0, which means there's no # ground truth. box_proposals = entry['boxes'][entry['gt_classes'] == 0] if len(box_proposals) == 0: continue else: # Faster R-CNN type models generate proposals on-the-fly with an # in-network RPN; 1-stage models don't require proposals. box_proposals = None im = [] im.append(cv2.imread(entry['image'])) print("im is {} and i is {} ".format(entry['image'], i)) with c2_utils.NamedCudaScope(gpu_id): cls_boxes_i, cls_segms_i, cls_keyps_i = im_detect_all( model, im, box_proposals, timers, model1) extend_results(i, all_boxes, cls_boxes_i[0]) if cls_segms_i is not None: extend_results(i, all_segms, cls_segms_i[0]) if cls_keyps_i is not None: extend_results(i, all_keyps, cls_keyps_i[0]) all_boxes, all_segms, all_keyps = empty_results( num_classes, num_images) logging.warning("begin to run benchmark") for i, entry in enumerate(roidb): if cfg.TEST.PRECOMPUTED_PROPOSALS: # The roidb may contain ground-truth rois (for example, if the roidb # comes from the training or val split). We only want to evaluate # detection on the *non*-ground-truth rois. We select only the rois # that have the gt_classes field set to 0, which means there's no # ground truth. box_proposals = entry['boxes'][entry['gt_classes'] == 0] if len(box_proposals) == 0: continue else: # Faster R-CNN type models generate proposals on-the-fly with an # in-network RPN; 1-stage models don't require proposals. box_proposals = None im = [] im.append(cv2.imread(entry['image'])) print("im is {} and i is {} ".format(entry['image'], i)) with c2_utils.NamedCudaScope(gpu_id): cls_boxes_i, cls_segms_i, cls_keyps_i = im_detect_all( model, im, box_proposals, timers, model1) if os.environ.get('DPROFILE') == "1" and ob != None: logging.warning("enter profile log") logging.warning("net observer time = {}".format(ob.average_time())) logging.warning("net observer time = {}".format( ob.average_time_children())) if os.environ.get('DPROFILE') == "1" and ob_mask != None: logging.warning("mask net observer time = {}".format( ob_mask.average_time())) logging.warning("mask net observer time = {}".format( ob_mask.average_time_children())) if os.environ.get('DPROFILE') == "1" and ob_mask != None: logging.warning("keypoint net observer time = {}".format( ob_keypoint.average_time())) logging.warning("keypoint net observer time = {}".format( ob_keypoint.average_time_children())) extend_results(i, all_boxes, cls_boxes_i[0]) if cls_segms_i is not None: extend_results(i, all_segms, cls_segms_i[0]) if cls_keyps_i is not None: extend_results(i, all_keyps, cls_keyps_i[0]) if i % 10 == 0: # Reduce log file size ave_total_time = np.sum([t.average_time for t in timers.values()]) eta_seconds = ave_total_time * (num_images - i - 1) eta = str(datetime.timedelta(seconds=int(eta_seconds))) det_time = (timers['im_detect_bbox'].average_time + timers['im_detect_mask'].average_time + timers['im_detect_keypoints'].average_time) misc_time = (timers['misc_bbox'].average_time + timers['misc_mask'].average_time + timers['misc_keypoints'].average_time) logger.info(('im_detect: range [{:d}, {:d}] of {:d}: ' '{:d}/{:d} {:.3f}s + {:.3f}s (eta: {})').format( start_ind + 1, end_ind, total_num_images, start_ind + i + 1, start_ind + num_images, det_time, misc_time, eta)) if cfg.VIS: im_name = os.path.splitext(os.path.basename(entry['image']))[0] vis_utils.vis_one_image(im[:, :, ::-1], '{:d}_{:s}'.format(i, im_name), os.path.join(output_dir, 'vis'), cls_boxes_i[0], segms=cls_segms_i[0], keypoints=cls_keyps_i[0], thresh=cfg.VIS_TH, box_alpha=0.8, dataset=dataset, show_class=True) for key, value in timers.items(): logger.info('{} : {}'.format(key, value.average_time)) #remove observer if ob != None: model.net.RemoveObserver(ob) if ob_mask != None: model.mask_net.RemoveObserver(ob_mask) if ob_keypoint != None: model.keypoint_net.RemoveObserver(ob_keypoint) if os.environ.get('INT8INFO') == "1": def save_net(net_def, init_def): if net_def is None or init_def is None: return if net_def.name is None or init_def.name is None: return if os.environ.get('INT8PTXT') == "1": with open(net_def.name + '_predict_int8.pbtxt', 'wb') as n: n.write(str(net_def)) with open(net_def.name + '_init_int8.pbtxt', 'wb') as n: n.write(str(init_def)) else: with open(net_def.name + '_predict_int8.pb', 'wb') as n: n.write(net_def.SerializeToString()) with open(net_def.name + '_init_int8.pb', 'wb') as n: n.write(init_def.SerializeToString()) algorithm = AbsmaxCalib() kind = os.environ.get('INT8CALIB') if kind == "moving_average": ema_alpha = 0.5 algorithm = EMACalib(ema_alpha) elif kind == "kl_divergence": algorithm = KLCalib(kl_iter_num_for_range) calib = Calibrator(algorithm) if model.net: predict_quantized, init_quantized = calib.DepositQuantizedModule( workspace, model.net.Proto()) save_net(predict_quantized, init_quantized) if cfg.MODEL.MASK_ON: predict_quantized, init_quantized = calib.DepositQuantizedModule( workspace, model.mask_net.Proto()) save_net(predict_quantized, init_quantized) if cfg.MODEL.KEYPOINTS_ON: predict_quantized, init_quantized = calib.DepositQuantizedModule( workspace, model.keypoint_net.Proto()) save_net(predict_quantized, init_quantized) cfg_yaml = yaml.dump(cfg) if ind_range is not None: det_name = 'detection_range_%s_%s.pkl' % tuple(ind_range) else: det_name = 'detections.pkl' det_file = os.path.join(output_dir, det_name) save_object( dict(all_boxes=all_boxes, all_segms=all_segms, all_keyps=all_keyps, cfg=cfg_yaml), det_file) logger.info('Wrote detections to: {}'.format(os.path.abspath(det_file))) return all_boxes, all_segms, all_keyps
cfg.TEST.WEIGHTS = weights_file cfg.TRAIN.WEIGHTS = weights_file assert_and_infer_cfg() logger.info('Testing with config:') logger.info(pprint.pformat(cfg)) model = test_engine.initialize_model_from_cfg(cfg.TEST.WEIGHTS, gpu_id=gpu_id) im = cv2.imread(File) entry = {} entry['id'] = File.split('/')[-1].split('.')[0] entry['image'] = File print(entry) timers = defaultdict(Timer) with c2_utils.NamedCudaScope(gpu_id): cls_boxes_i, cls_segms_i, cls_keyps_i = im_detect_all( model, im, None, timers, entry) # fetch blob # hg_pred_NCHW_8, fpn_inner_res3_3_sum_lateral, fpn_inner_res3_3_sum_lateral_pose_concat_conv with c2_utils.NamedCudaScope(gpu_id): pose_map = workspace.FetchBlob( core.ScopedName('hg_pred_NCHW_16'))[0, :, :, :] res_feat = workspace.FetchBlob( core.ScopedName('fpn_inner_res4_5_sum_lateral'))[0, :, :, :] concat_conv_relu = workspace.FetchBlob( core.ScopedName('fpn_inner_res5_lateral_pose_conv'))[0, :, :, :] blobs = [pose_map, res_feat, concat_conv_relu] blobs_name = ['pose_map', 'res_feat', 'concat_conv_relu'] save_path = '/home/gaomingda/Documents/maskrcnn_fcn/demo/features/997_15' #feat1 = pose_map[0, :, :]
def test_net(weights_file, dataset_name, proposal_file, output_dir, ind_range=None, gpu_id=0): """Run inference on all images in a dataset or over an index range of images in a dataset using a single GPU. """ assert not cfg.MODEL.RPN_ONLY, \ 'Use rpn_generate to generate proposals from RPN-only models' roidb, dataset, start_ind, end_ind, total_num_images = get_roidb_and_dataset( dataset_name, proposal_file, ind_range) model = initialize_model_from_cfg(weights_file, gpu_id=gpu_id) num_images = len(roidb) num_classes = cfg.MODEL.NUM_CLASSES all_boxes, all_segms, all_keyps, all_track = empty_results( num_classes, num_images) box_proposals_prev = None im_prev = None fpn_res_sum_prev = None cls_boxes_prev = None cls_segms_prev = None cls_keyps_prev = None boxes_prev = None im_scale_prev = None color_inds_prev = None colors = vis_utils.distinct_colors(20) timers = defaultdict(Timer) for i, entry in enumerate(roidb): if cfg.TEST.PRECOMPUTED_PROPOSALS: # The roidb may contain ground-truth rois (for example, if the roidb # comes from the training or val split). We only want to evaluate # detection on the *non*-ground-truth rois. We select only the rois # that have the gt_classes field set to 0, which means there's no # ground truth. box_proposals = entry['boxes'][entry['gt_classes'] == 0] if len(box_proposals) == 0: continue else: # Faster R-CNN type models generate proposals on-the-fly with an # in-network RPN; 1-stage models don't require proposals. box_proposals = None im = cv2.imread(entry['image']) with c2_utils.NamedCudaScope(gpu_id): if cfg.MODEL.TRACKING_ON: # Single image inference on the first frame if i == 0: im_prev = im box_proposals_prev = box_proposals cls_boxes_list, cls_segms_list, cls_keyps_list, track_mat_i, extras = multi_im_detect_all( model, [im], [box_proposals], timers, tracking=False) im_scale_list, boxes_list, fpn_res_sum_list = extras cls_boxes_i = cls_boxes_list[0] cls_segms_i = cls_segms_list[0] cls_keyps_i = cls_keyps_list[0] im_scale_prev = im_scale_list[0] boxes_prev = boxes_list[0] fpn_res_sum_prev = fpn_res_sum_list[0] # Pairwise image inference on the second frame elif i == 1: cls_boxes_list, cls_segms_list, cls_keyps_list, track_mat_i, extras = multi_im_detect_all( model, [im_prev, im], [box_proposals_prev, box_proposals], timers) boxes_list, im_scale_list, fpn_res_sum_list = extras cls_boxes_i = cls_boxes_list[1] cls_segms_i = cls_segms_list[1] cls_keyps_i = cls_keyps_list[1] boxes_prev = boxes_list[1] im_scale_prev = im_scale_list[1] fpn_res_sum_prev = fpn_res_sum_list[1] # Sequential image inference after the second frame else: cls_boxes_i, cls_segms_i, cls_keyps_i, track_mat_i, extras = im_detect_all_seq( model, im, box_proposals, (cls_boxes_i, fpn_res_sum_prev, boxes_prev, im_scale_prev), timers) boxes_prev, im_scale_prev, fpn_res_sum_prev = extras else: cls_boxes_i, cls_segms_i, cls_keyps_i = im_detect_all( model, im, box_proposals, timers) extend_results(i, all_boxes, cls_boxes_i) if cls_segms_i is not None: extend_results(i, all_segms, cls_segms_i) if cls_keyps_i is not None: extend_results(i, all_keyps, cls_keyps_i) if track_mat_i is not None: all_track[i] = track_mat_i if i % 10 == 0: # Reduce log file size ave_total_time = np.sum([t.average_time for t in timers.values()]) eta_seconds = ave_total_time * (num_images - i - 1) eta = str(datetime.timedelta(seconds=int(eta_seconds))) det_time = (timers['im_detect_bbox'].average_time + timers['im_detect_mask'].average_time + timers['im_detect_keypoints'].average_time + timers['im_detect_tracking'].average_time) misc_time = (timers['misc_bbox'].average_time + timers['misc_mask'].average_time + timers['misc_keypoints'].average_time + timers['misc_tracking'].average_time) logger.info(('im_detect: range [{:d}, {:d}] of {:d}: ' '{:d}/{:d} {:.3f}s + {:.3f}s (eta: {})').format( start_ind + 1, end_ind, total_num_images, start_ind + i + 1, start_ind + num_images, det_time, misc_time, eta)) if cfg.VIS: im_name = os.path.splitext(os.path.basename(entry['image']))[0] if MODEL.TRACKING_ON: # Pairwise visualization for the first image pair if i == 1: _, _, _, _, color_inds_prev = vis_utils.vis_image_pair_opencv( im_list, cls_boxes_list, cls_segms_list, cls_keyps_list, track_mat_i, dataset=dataset, show_track=True, show_box=True, ) # Sequential visualization after the first image pair else: _, _, _, color_inds_prev = vis_utils.vis_image_pair_opencv( [None, im], [cls_boxes_prev, cls_boxes], [cls_segms_prev, cls_segms], [cls_keyps_prev, cls_keyps], cls_track, dataset=dataset, show_track=True, show_box=True, color_inds_list=[color_inds_prev, None]) else: vis_utils.vis_one_image(im[:, :, ::-1], '{:d}_{:s}'.format(i, im_name), os.path.join(output_dir, 'vis'), cls_boxes_i, segms=cls_segms_i, keypoints=cls_keyps_i, thresh=cfg.VIS_TH, box_alpha=0.8, dataset=dataset, show_class=True) im_prev = im box_proposals_prev = box_proposals cfg_yaml = envu.yaml_dump(cfg) if ind_range is not None: det_name = 'detection_range_%s_%s.pkl' % tuple(ind_range) else: det_name = 'detections.pkl' det_file = os.path.join(output_dir, det_name) save_object( dict(all_boxes=all_boxes, all_segms=all_segms, all_keyps=all_keyps, all_track=all_track, cfg=cfg_yaml), det_file) logger.info('Wrote detections to: {}'.format(os.path.abspath(det_file))) return all_boxes, all_segms, all_keyps, all_track