def box_results_with_nms_and_limit(scores, boxes): # NOTE: support single-batch """Returns bounding-box detection results by thresholding on scores and applying non-maximum suppression (NMS). `boxes` has shape (#detections, 4 * #classes), where each row represents a list of predicted bounding boxes for each of the object classes in the dataset (including the background class). The detections in each row originate from the same object proposal. `scores` has shape (#detection, #classes), where each row represents a list of object detection confidence scores for each of the object classes in the dataset (including the background class). `scores[i, j]`` corresponds to the box at `boxes[i, j * 4:(j + 1) * 4]`. """ num_classes = cfg.MODEL.NUM_CLASSES cls_boxes = [[] for _ in range(num_classes)] # Apply threshold on detection probabilities and apply NMS # Skip j = 0, because it's the background class for j in range(1, num_classes): inds = np.where(scores[:, j] > cfg.TEST.SCORE_THRESH)[0] scores_j = scores[inds, j] boxes_j = boxes[inds, j * 4:(j + 1) * 4] dets_j = np.hstack((boxes_j, scores_j[:, np.newaxis])).astype(np.float32, copy=False) if cfg.TEST.USE_GT_PROPOSALS: nms_dets = dets_j elif cfg.TEST.SOFT_NMS.ENABLED: nms_dets, _ = box_utils.soft_nms(dets_j, sigma=cfg.TEST.SOFT_NMS.SIGMA, overlap_thresh=cfg.TEST.NMS, score_thresh=0.0001, method=cfg.TEST.SOFT_NMS.METHOD) else: keep = box_utils.nms(dets_j, cfg.TEST.NMS) nms_dets = dets_j[keep, :] # Refine the post-NMS boxes using bounding-box voting if cfg.TEST.BBOX_VOTE.ENABLED: nms_dets = box_utils.box_voting( nms_dets, dets_j, cfg.TEST.BBOX_VOTE.VOTE_TH, scoring_method=cfg.TEST.BBOX_VOTE.SCORING_METHOD) cls_boxes[j] = nms_dets # Limit to max_per_image detections **over all classes** if cfg.TEST.DETECTIONS_PER_IM > 0: image_scores = np.hstack( [cls_boxes[j][:, -1] for j in range(1, num_classes)]) if len(image_scores) > cfg.TEST.DETECTIONS_PER_IM: image_thresh = np.sort(image_scores)[-cfg.TEST.DETECTIONS_PER_IM] for j in range(1, num_classes): keep = np.where(cls_boxes[j][:, -1] >= image_thresh)[0] cls_boxes[j] = cls_boxes[j][keep, :] im_results = np.vstack([cls_boxes[j] for j in range(1, num_classes)]) boxes = im_results[:, :-1] scores = im_results[:, -1] return scores, boxes, cls_boxes
def box_results_with_nms_and_limit(scores, boxes): # NOTE: support single-batch """Returns bounding-box detection results by thresholding on scores and applying non-maximum suppression (NMS). `boxes` has shape (#detections, 4 * #classes), where each row represents a list of predicted bounding boxes for each of the object classes in the dataset (including the background class). The detections in each row originate from the same object proposal. `scores` has shape (#detection, #classes), where each row represents a list of object detection confidence scores for each of the object classes in the dataset (including the background class). `scores[i, j]`` corresponds to the box at `boxes[i, j * 4:(j + 1) * 4]`. """ num_classes = cfg.MODEL.NUM_CLASSES cls_boxes = [[] for _ in range(num_classes)] # Apply threshold on detection probabilities and apply NMS # Skip j = 0, because it's the background class for j in range(1, num_classes): inds = np.where(scores[:, j] > cfg.TEST.SCORE_THRESH)[0] scores_j = scores[inds, j] boxes_j = boxes[inds, j * 4:(j + 1) * 4] dets_j = np.hstack((boxes_j, scores_j[:, np.newaxis])).astype(np.float32, copy=False) if cfg.TEST.SOFT_NMS.ENABLED: nms_dets, _ = box_utils.soft_nms( dets_j, sigma=cfg.TEST.SOFT_NMS.SIGMA, overlap_thresh=cfg.TEST.NMS, score_thresh=0.0001, method=cfg.TEST.SOFT_NMS.METHOD ) else: keep = box_utils.nms(dets_j, cfg.TEST.NMS) nms_dets = dets_j[keep, :] # Refine the post-NMS boxes using bounding-box voting if cfg.TEST.BBOX_VOTE.ENABLED: nms_dets = box_utils.box_voting( nms_dets, dets_j, cfg.TEST.BBOX_VOTE.VOTE_TH, scoring_method=cfg.TEST.BBOX_VOTE.SCORING_METHOD ) cls_boxes[j] = nms_dets # Limit to max_per_image detections **over all classes** if cfg.TEST.DETECTIONS_PER_IM > 0: image_scores = np.hstack( [cls_boxes[j][:, -1] for j in range(1, num_classes)] ) if len(image_scores) > cfg.TEST.DETECTIONS_PER_IM: image_thresh = np.sort(image_scores)[-cfg.TEST.DETECTIONS_PER_IM] for j in range(1, num_classes): keep = np.where(cls_boxes[j][:, -1] >= image_thresh)[0] cls_boxes[j] = cls_boxes[j][keep, :] im_results = np.vstack([cls_boxes[j] for j in range(1, num_classes)]) boxes = im_results[:, :-1] scores = im_results[:, -1] return scores, boxes, cls_boxes
def box_results_with_nms_and_limit(self, scores, boxes, score_thresh=cfg.TEST.SCORE_THRESH): num_classes = cfg.MODEL.NUM_CLASSES cls_boxes = [[] for _ in range(num_classes)] # Apply threshold on detection probabilities and apply NMS # Skip j = 0, because it's the background class for j in range(1, num_classes): inds = np.where(scores[:, j] > score_thresh)[0] scores_j = scores[inds, j] boxes_j = boxes[inds, j * 4:(j + 1) * 4] dets_j = np.hstack( (boxes_j, scores_j[:, np.newaxis])).astype(np.float32, copy=False) if cfg.TEST.SOFT_NMS.ENABLED: nms_dets, _ = box_utils.soft_nms( dets_j, sigma=cfg.TEST.SOFT_NMS.SIGMA, overlap_thresh=cfg.TEST.NMS, score_thresh=0.0001, method=cfg.TEST.SOFT_NMS.METHOD) else: keep = box_utils.nms(dets_j, cfg.TEST.NMS) nms_dets = dets_j[keep, :] # add labels label_j = np.ones((nms_dets.shape[0], 1), dtype=np.float32) * j nms_dets = np.hstack((nms_dets, label_j)) # Refine the post-NMS boxes using bounding-box voting if cfg.TEST.BBOX_VOTE.ENABLED: nms_dets = box_utils.box_voting( nms_dets, dets_j, cfg.TEST.BBOX_VOTE.VOTE_TH, scoring_method=cfg.TEST.BBOX_VOTE.SCORING_METHOD) cls_boxes[j] = nms_dets # Limit to max_per_image detections **over all classes** if cfg.TEST.DETECTIONS_PER_IM > 0: image_scores = np.hstack( [cls_boxes[j][:, -2] for j in range(1, num_classes)]) if len(image_scores) > cfg.TEST.DETECTIONS_PER_IM: image_thresh = np.sort( image_scores)[-cfg.TEST.DETECTIONS_PER_IM] for j in range(1, num_classes): keep = np.where(cls_boxes[j][:, -2] >= image_thresh)[0] cls_boxes[j] = cls_boxes[j][keep, :] im_results = np.vstack([cls_boxes[j] for j in range(1, num_classes)]) boxes = im_results[:, :-2] scores = im_results[:, -2] labels = im_results[:, -1] return scores, boxes, labels
def iou_box_nms_and_limit(stage1_box, stage1_iou, dets_cls, scores): num_classes = cfg.MODEL.NUM_CLASSES cls_boxes = [[] for _ in range(num_classes)] for j in range(1, num_classes): inds = np.array(dets_cls[str(j)], dtype=np.int) if not inds.tolist(): boxes_j = np.empty((0, 4), dtype=np.float32) else: boxes_j = stage1_box[inds] iou_j = stage1_iou[inds] score_j = scores[inds] dets_j = np.hstack((boxes_j, iou_j[:, np.newaxis])).astype(np.float32, copy=False) if cfg.TEST.SOFT_NMS.ENABLED: nms_dets, _ = box_utils.soft_nms(dets_j, sigma=cfg.TEST.SOFT_NMS.SIGMA, overlap_thresh=cfg.TEST.NMS, score_thresh=0.0001, method=cfg.TEST.SOFT_NMS.METHOD) else: keep = box_utils.nms(dets_j, cfg.TEST.NMS) boxes_j = boxes_j[keep] score_j = score_j[keep] nms_dets = np.hstack( (boxes_j, score_j[:, np.newaxis])).astype(np.float32, copy=False) # Refine the post-NMS boxes using bounding-box voting if cfg.TEST.BBOX_VOTE.ENABLED: nms_dets = box_utils.box_voting( nms_dets, dets_j, cfg.TEST.BBOX_VOTE.VOTE_TH, scoring_method=cfg.TEST.BBOX_VOTE.SCORING_METHOD) cls_boxes[j] = nms_dets # Limit to max_per_image detections **over all classes** if cfg.TEST.DETECTIONS_PER_IM > 0: image_scores = np.hstack( [cls_boxes[j][:, -1] for j in range(1, num_classes)]) if len(image_scores) > cfg.TEST.DETECTIONS_PER_IM: image_thresh = np.sort(image_scores)[-cfg.TEST.DETECTIONS_PER_IM] for j in range(1, num_classes): keep = np.where(cls_boxes[j][:, -1] >= image_thresh)[0] cls_boxes[j] = cls_boxes[j][keep, :] im_results = np.vstack([cls_boxes[j] for j in range(1, num_classes)]) boxes = im_results[:, :-1] scores = im_results[:, -1] return scores, boxes, cls_boxes
def box_results_with_nms_and_limit(self, scores, boxes, score_thresh=cfg.TEST.SCORE_THRESH): num_classes = cfg.MODEL.NUM_CLASSES cls_boxes = [[] for _ in range(num_classes)] # Apply threshold on detection probabilities and apply NMS # Skip j = 0, because it's the background class nms_mask = np.zeros_like(scores) for j in range(1, num_classes): inds = np.where(scores[:, j] > score_thresh)[0] scores_j = scores[inds, j] boxes_j = boxes[inds, j * 4:(j + 1) * 4] dets_j = np.hstack((boxes_j, scores_j[:, np.newaxis])).astype(np.float32, copy=False) if cfg.TEST.SOFT_NMS.ENABLED: nms_dets, keep = box_utils.soft_nms( dets_j, sigma=cfg.TEST.SOFT_NMS.SIGMA, overlap_thresh=cfg.TEST.NMS, score_thresh=0.0001, method=cfg.TEST.SOFT_NMS.METHOD ) else: keep = box_utils.nms(dets_j, cfg.TEST.NMS) nms_dets = dets_j[keep, :] nms_mask[:, j][keep] = 1.0 # add labels # Refine the post-NMS boxes using bounding-box voting dists_all = nms_mask * scores scores_pre, labels_pre = dists_all.max(-1), dists_all.argmax(-1) inds_all = np.where(scores_pre > 0)[0] labels_all = labels_pre[inds_all] scores_all = scores_pre[inds_all] idx = np.argsort(-scores_all) if cfg.TEST.DETECTIONS_PER_IM < idx.shape[0]: idx = idx[:cfg.TEST.DETECTIONS_PER_IM] scores = scores_all[idx] labels = labels_all[idx] return scores, idx, labels
def box_results_with_nms_and_limit( scores, boxes, num_classes=81, score_thresh=0.05, overlap_thresh=0.5, do_soft_nms=False, soft_nms_sigma=0.5, soft_nms_method='linear', do_bbox_vote=False, bbox_vote_thresh=0.8, bbox_vote_method='ID', max_detections_per_img=100, ### over all classes ### ): """Returns bounding-box detection results by thresholding on scores and applying non-maximum suppression (NMS). A number of #detections presist after this and are returned, sorted by class `boxes` has shape (#detections, 4 * #classes), where each row represents a list of predicted bounding boxes for each of the object classes in the dataset (including the background class). The detections in each row originate from the same object proposal. `scores` has shape (#detection, #classes), where each row represents a list of object detection confidence scores for each of the object classes in the dataset (including the background class). `scores[i, j]`` corresponds to the box at `boxes[i, j * 4:(j + 1) * 4]`. """ cls_boxes = [[] for _ in range(num_classes)] # Apply threshold on detection probabilities and apply NMS # Skip j = 0, because it's the background class for j in range(1, num_classes): inds = np.where(scores[:, j] > score_thresh)[0] scores_j = scores[inds, j] boxes_j = boxes[inds, j * 4:(j + 1) * 4] dets_j = np.hstack((boxes_j, scores_j[:, np.newaxis])).astype(np.float32, copy=False) if do_soft_nms: nms_dets, _ = box_utils.soft_nms(dets_j, sigma=soft_nms_sigma, overlap_thresh=overlap_thresh, score_thresh=0.0001, method=soft_nms_method) else: keep = box_utils.nms(dets_j, overlap_thresh) nms_dets = dets_j[keep, :] # Refine the post-NMS boxes using bounding-box voting if do_bbox_vote: nms_dets = box_utils.box_voting(nms_dets, dets_j, bbox_vote_thresh, scoring_method=bbox_vote_method) cls_boxes[j] = nms_dets # Limit to max_per_image detections **over all classes** if max_detections_per_img > 0: image_scores = np.hstack( [cls_boxes[j][:, -1] for j in range(1, num_classes)]) if len(image_scores) > max_detections_per_img: image_thresh = np.sort(image_scores)[-max_detections_per_img] for j in range(1, num_classes): keep = np.where(cls_boxes[j][:, -1] >= image_thresh)[0] cls_boxes[j] = cls_boxes[j][keep, :] im_results = np.vstack([cls_boxes[j] for j in range(1, num_classes)]) boxes = im_results[:, :-1] scores = im_results[:, -1] return scores, boxes, cls_boxes
def box_results_with_nms_and_limit(scores, boxes, prev_cls_boxes=None): # NOTE: support single-batch """Returns bounding-box detection results by thresholding on scores and applying non-maximum suppression (NMS). `boxes` has shape (#detections, 4 * #classes), where each row represents a list of predicted bounding boxes for each of the object classes in the dataset (including the background class). The detections in each row originate from the same object proposal. `scores` has shape (#detection, #classes), where each row represents a list of object detection confidence scores for each of the object classes in the dataset (including the background class). `scores[i, j]`` corresponds to the box at `boxes[i, j * 4:(j + 1) * 4]`. """ num_classes = cfg.MODEL.NUM_CLASSES cls_boxes = [[] for _ in range(num_classes)] # Apply threshold on detection probabilities and apply NMS # Skip j = 0, because it's the background class for j in range(1, num_classes): inds = np.where(scores[:, j] >= cfg.TEST.SCORE_THRESH)[0] scores_j = scores[inds, j] boxes_j = boxes[inds, j * 4:(j + 1) * 4] dets_j = np.hstack((boxes_j, scores_j[:, np.newaxis])).astype(np.float32, copy=False) if cfg.TEST.SOFT_NMS.ENABLED: nms_dets, _ = box_utils.soft_nms( dets_j, sigma=cfg.TEST.SOFT_NMS.SIGMA, overlap_thresh=cfg.TEST.NMS, score_thresh=0.0001, method=cfg.TEST.SOFT_NMS.METHOD ) else: keep = box_utils.nms(dets_j, cfg.TEST.NMS) nms_dets = dets_j[keep, :] # Refine the post-NMS boxes using bounding-box voting if cfg.TEST.BBOX_VOTE.ENABLED: nms_dets = box_utils.box_voting( nms_dets, dets_j, cfg.TEST.BBOX_VOTE.VOTE_TH, scoring_method=cfg.TEST.BBOX_VOTE.SCORING_METHOD ) cls_boxes[j] = nms_dets # Limit to max_per_image detections **over all classes** if cfg.TEST.DETECTIONS_PER_IM > 0: image_scores = np.hstack( [cls_boxes[j][:, -1] for j in range(1, num_classes)] ) if len(image_scores) > cfg.TEST.DETECTIONS_PER_IM: image_thresh = np.sort(image_scores)[-cfg.TEST.DETECTIONS_PER_IM] for j in range(1, num_classes): keep = np.where(cls_boxes[j][:, -1] >= image_thresh)[0] cls_boxes[j] = cls_boxes[j][keep, :] # select two best for each class if score is low.. ''' if cfg.TEST.NUM_DET_PER_CLASS_PRE==1: for j in range(1, num_classes): keep = np.argsort(-cls_boxes[j][:, -1])[:2] cls_boxes[j] = cls_boxes[j][keep, :] # if one has a very strong cls score, we only keep two boxes for weak cls score. if len(cls_boxes[j])>0 and cls_boxes[j][0,-1]>0.5: cls_boxes[j] = cls_boxes[j][:1, :]''' # nms between classes. if cfg.TEST.NMS_CROSS_CLASS > 0.: ''' # code to keep some of the dets for which class there is only one det. all_cls_boxes = [] reserved_cls = [] for j in range(1, num_classes): tmp_cls_boxes = np.copy(cls_boxes[j]) # if only one det for cls j, we keep it. if tmp_cls_boxes.shape[0] == 1: tmp_cls_boxes[:,-1] = 1.0 all_cls_boxes.append(tmp_cls_boxes) all_dets_for_nms = np.vstack(all_cls_boxes) ''' all_dets = np.vstack([cls_boxes[j] for j in range(1, num_classes)]) class_ids = np.vstack([np.ones(shape=(len(cls_boxes[j]), 1))*j for j in range(1, num_classes)]) keep = box_utils.nms(all_dets, cfg.TEST.NMS_CROSS_CLASS) all_dets = all_dets[keep, :] class_ids = class_ids[keep, :] for j in range(1, num_classes): idx_j = np.where(class_ids==j)[0] cls_boxes[j] = all_dets[idx_j, :] # select one best for each class. if cfg.TEST.NUM_DET_PER_CLASS_PRE>0: for j in range(1, num_classes): keep = np.argsort(-cls_boxes[j][:, -1])[:cfg.TEST.NUM_DET_PER_CLASS_PRE] cls_boxes[j] = cls_boxes[j][keep, :] # nms by previous box. if cfg.TEST.NMS_SMALL_BOX_IOU>0: for j in range(1, num_classes): if prev_cls_boxes is not None: assert len(prev_cls_boxes[j])<2, 'number of prev boxes should <2.' if len(prev_cls_boxes[j])==1: if prev_cls_boxes[j][0][-1]<cfg.TEST.NMS_SMALL_BOX_SCORE_THRESHOLD: #if not confident about previous box, no nms. continue prev_cls_box = prev_cls_boxes[j][0][:-1] index_to_remove = [] for id_box in range(len(cls_boxes[j])-1,-1,-1): box = cls_boxes[j][id_box][:-1] iou = bb_intersection_over_union(prev_cls_box, box) if iou<cfg.TEST.NMS_SMALL_BOX_IOU: index_to_remove.append(id_box) cls_boxes[j] = np.delete(cls_boxes[j], index_to_remove, 0) im_results = np.vstack([cls_boxes[j] for j in range(1, num_classes)]) boxes = im_results[:, :-1] scores = im_results[:, -1] return scores, boxes, cls_boxes
def evaluate_mAP_combine(json_datasets, roidbs, all_boxes_list, output_dir, cleanup=False): """ LJY all_boxes: num_cls x num_images x [num_boxes x 5] """ json_dataset = json_datasets[0] mAP_folder = '/home/liujingyu/code/mAP' roidb, roidb_part = roidbs[0], roidbs[1] all_boxes, all_boxes_part = all_boxes_list[0], all_boxes_list[1] small_classes = ['结节', '肺实变', '膈面异常', '骨折'] for i, (entry, entry_part) in enumerate(zip( roidb, roidb_part)): # for each pair of images # print(i, entry['eva_id'], entry['file_name']) assert entry['file_name'] == entry_part['file_name'] file_name = entry['file_name'][:-4] + '.txt' fgt = open(osp.join(mAP_folder, 'ground-truth', file_name), 'w') fpred = open(osp.join(mAP_folder, 'predicted', file_name), 'w') for cls_ind, cls in enumerate(json_dataset.classes): # for each cls if cls == '__background__': continue if cls_ind >= len(all_boxes): break gt_classes = roidb[i]['gt_classes'] ind = np.where(gt_classes == cls_ind) gt_boxes = roidb[i]['boxes'][ind] dets = all_boxes[cls_ind][i] # N x 5, [x1, y1, x2, y2, score] dets_part = all_boxes_part[cls_ind][i] # N x 5 # offset the dets_part based on offset dets_part[:, 0] += entry_part['offset_x'] dets_part[:, 2] += entry_part['offset_x'] dets_part[:, 1] += entry_part['offset_y'] dets_part[:, 3] += entry_part['offset_y'] # select # if cls in small_classes: # dets = dets_part # merge dets = np.vstack((dets, dets_part)) # NMS on dets and dets_part if cfg.TEST.SOFT_NMS.ENABLED: nms_dets, _ = box_utils.soft_nms( dets, sigma=cfg.TEST.SOFT_NMS.SIGMA, overlap_thresh=cfg.TEST.NMS, score_thresh=0.0001, method=cfg.TEST.SOFT_NMS.METHOD) else: keep = box_utils.nms(dets, cfg.TEST.NMS) nms_dets = dets[keep, :] # Refine the post-NMS boxes using bounding-box voting if cfg.TEST.BBOX_VOTE.ENABLED: nms_dets = box_utils.box_voting( nms_dets, dets, cfg.TEST.BBOX_VOTE.VOTE_TH, scoring_method=cfg.TEST.BBOX_VOTE.SCORING_METHOD) dets = nms_dets # write gt boxes, format: tvmonitor 2 10 173 238 for k in range(gt_boxes.shape[0]): s = '{} {:f} {:f} {:f} {:f}'.format(cls, gt_boxes[k, 0], gt_boxes[k, 1], gt_boxes[k, 2], gt_boxes[k, 3]) fgt.write(s) fgt.write('\n') if cls == '肿块' or cls == '结节' or cls == '钙化' or cls == '乳头影': s = '{} {:f} {:f} {:f} {:f}'.format( '肿块结节钙化', gt_boxes[k, 0], gt_boxes[k, 1], gt_boxes[k, 2], gt_boxes[k, 3]) fgt.write(s) fgt.write('\n') if cls == '纤维化表现' or cls == '肺实变' or cls == '肺纹理增多' or cls == '肿块' or cls == '弥漫性结节': s = '{} {:f} {:f} {:f} {:f}'.format( '高密度影', gt_boxes[k, 0], gt_boxes[k, 1], gt_boxes[k, 2], gt_boxes[k, 3]) fgt.write(s) fgt.write('\n') if cls == '气胸' or cls == '气肿': s = '{} {:f} {:f} {:f} {:f}'.format( '低密度影', gt_boxes[k, 0], gt_boxes[k, 1], gt_boxes[k, 2], gt_boxes[k, 3]) fgt.write(s) fgt.write('\n') # write pred boxes, format: tvmonitor 0.471781 0 13 174 244 for k in range(dets.shape[0]): s = '{} {:f} {:f} {:f} {:f} {:f}'.format( cls, dets[k, -1], dets[k, 0], dets[k, 1], dets[k, 2], dets[k, 3]) fpred.write(s) fpred.write('\n') if cls == '肿块' or cls == '结节' or cls == '钙化' or cls == '乳头影': s = '{} {:f} {:f} {:f} {:f} {:f}'.format( '肿块结节钙化', dets[k, -1], dets[k, 0], dets[k, 1], dets[k, 2], dets[k, 3]) fpred.write(s) fpred.write('\n') if cls == '纤维化表现' or cls == '肺实变' or cls == '肺纹理增多' or cls == '肿块' or cls == '弥漫性结节': s = '{} {:f} {:f} {:f} {:f} {:f}'.format( '高密度影', dets[k, -1], dets[k, 0], dets[k, 1], dets[k, 2], dets[k, 3]) fpred.write(s) fpred.write('\n') if cls == '气胸' or cls == '气肿': s = '{} {:f} {:f} {:f} {:f} {:f}'.format( '低密度影', dets[k, -1], dets[k, 0], dets[k, 1], dets[k, 2], dets[k, 3]) fpred.write(s) fpred.write('\n') if gt_boxes.shape[0] > 0: # then we draw gt boxes and pred boxes im = cv2.imread(entry['image']) more_text = str(entry['eva_id']) + ' ' + entry['doc_name'] im = vis_boxes_ljy(im, gt_boxes, dets[:, :-1], more_text) out_path = os.path.join( '/data5/liujingyu/mask_rcnn_Outputs/vis', cls, entry['file_name']) cv2.imwrite(out_path, im) pdb.set_trace()
print(n) c += 1 c = 0 for k in bboxes_dict.keys(): bboxes = np.array(bboxes_dict[k]) bboxes[:, 2] += bboxes[:, 0] bboxes[:, 3] += bboxes[:, 1] scores = np.array(scores_dict[k]) scores = np.reshape(scores, (scores.shape[0], 1)) dets_j = np.hstack((bboxes, scores)).astype(np.float32, copy=False) nms_dets, _ = box_utils.soft_nms( dets_j, sigma=0.3, overlap_thresh=0.5, score_thresh=0.0001, method='gaussian' ) # keep = box_utils.nms(dets_j, 0.5) # nms_dets = dets_j[keep, :] for j in range(nms_dets.shape[0]): ensemble_results.append({'image_id': k, 'category_id': 1, 'bbox': nms_dets[j][0:4].tolist(), 'score': nms_dets[j][4].tolist()}) print(k) f2 = open('../results/ensemble_results.json', 'w') json.dump(ensemble_results, f2) print('finished') # det_file = os.path.join(output_dir, det_name)
def box_results_with_nms_and_limit(scores, boxes, num_classes=81, score_thresh=0.05, overlap_thresh=0.5, do_soft_nms=False, soft_nms_sigma=0.5, soft_nms_method='linear', do_bbox_vote=False, bbox_vote_thresh=0.8, bbox_vote_method='ID', max_detections_per_img=100, ### over all classes ### ): """Returns bounding-box detection results by thresholding on scores and applying non-maximum suppression (NMS). A number of #detections presist after this and are returned, sorted by class `boxes` has shape (#detections, 4 * #classes), where each row represents a list of predicted bounding boxes for each of the object classes in the dataset (including the background class). The detections in each row originate from the same object proposal. `scores` has shape (#detection, #classes), where each row represents a list of object detection confidence scores for each of the object classes in the dataset (including the background class). `scores[i, j]`` corresponds to the box at `boxes[i, j * 4:(j + 1) * 4]`. """ cls_boxes = [[] for _ in range(num_classes)] # Apply threshold on detection probabilities and apply NMS # Skip j = 0, because it's the background class for j in range(1, num_classes): inds = np.where(scores[:, j] > score_thresh)[0] scores_j = scores[inds, j] boxes_j = boxes[inds, j * 4:(j + 1) * 4] dets_j = np.hstack((boxes_j, scores_j[:, np.newaxis])).astype( np.float32, copy=False ) if do_soft_nms: nms_dets, _ = box_utils.soft_nms( dets_j, sigma=soft_nms_sigma, overlap_thresh=overlap_thresh, score_thresh=0.0001, method=soft_nms_method ) else: keep = box_utils.nms(dets_j, overlap_thresh) nms_dets = dets_j[keep, :] # Refine the post-NMS boxes using bounding-box voting if do_bbox_vote: nms_dets = box_utils.box_voting( nms_dets, dets_j, bbox_vote_thresh, scoring_method=bbox_vote_method ) cls_boxes[j] = nms_dets # Limit to max_per_image detections **over all classes** if max_detections_per_img > 0: image_scores = np.hstack( [cls_boxes[j][:, -1] for j in range(1, num_classes)] ) if len(image_scores) > max_detections_per_img: image_thresh = np.sort(image_scores)[-max_detections_per_img] for j in range(1, num_classes): keep = np.where(cls_boxes[j][:, -1] >= image_thresh)[0] cls_boxes[j] = cls_boxes[j][keep, :] im_results = np.vstack([cls_boxes[j] for j in range(1, num_classes)]) boxes = im_results[:, :-1] scores = im_results[:, -1] return scores, boxes, cls_boxes