def generateBBoxfromCAM(cams, reshape_size=(224, 224), percentage_heat=0.4, size_restriction=0.1, box_expansion=0.1): predicted_bboxes = [] predicted_scores = [] # heatmap = resize(heatmap, tuple(reshape_size), order=1, preserve_range=True) bb_thres = np.max(cams) * percentage_heat binary_heat = cams binary_heat = np.where(binary_heat > bb_thres, 255, 0) min_size = reshape_size[0] * reshape_size[1] * size_restriction labeled, nr_objects = ndimage.label(binary_heat) [objects, counts] = np.unique(labeled, return_counts=True) biggest_components = np.argsort(counts[1:])[::-1] selected_components = [1 if counts[i + 1] >= min_size else 0 for i in biggest_components] biggest_components = biggest_components[:min([np.sum(selected_components), 9999])] # cams = cams / 255.0 # Get bboxes for selected, comp in zip(selected_components, biggest_components): if selected: max_heat = np.where(labeled == comp + 1, 255, 0) # get the biggest box = list(bbox(max_heat)) # expand box before final detection x_exp = box[2] * box_expansion y_exp = box[3] * box_expansion box[0] = int(max([0, box[0] - x_exp / 2])) box[1] = int(max([0, box[1] - y_exp / 2])) # change width and height by xmax and ymax box[2] += box[0] box[3] += box[1] box[2] = int(min([reshape_size[1] - 1, box[2] + x_exp])) box[3] = int(min([reshape_size[0] - 1, box[3] + y_exp])) predicted_bboxes.append(box) # Get score for current bbox score = np.mean(cams[box[1]:box[3], box[0]:box[2]]) # use mean CAM value of the bbox as a score predicted_scores.append(score) # Now apply NMS on all the obtained bboxes nms_threshold = 0.3 # logging.info('bboxes before NMS: '+str(len(predicted_scores))) if len(predicted_scores) > 0: dets = np.hstack((np.array(predicted_bboxes), np.array(predicted_scores)[:, np.newaxis])).astype(np.float32) keep = cpu_nms(dets, nms_threshold) dets = dets[keep, :] predicted_bboxes = [] predicted_scores = [] for idet in range(dets.shape[0]): predicted_bboxes.append(dets[idet, :4]) predicted_scores.append(dets[idet, -1]) # logging.info('bboxes after NMS: '+str(len(predicted_scores))) return [predicted_bboxes, predicted_scores]
def nms(dets, thresh, force_cpu=False): """Dispatch to either CPU or GPU NMS implementations.""" if dets.shape[0] == 0: return [] else: return cpu_nms(dets, thresh)
def nms(dets, thresh): """Dispatch to either CPU or GPU NMS implementations.""" if dets.shape[0] == 0: return [] if cfg.USE_GPU_NMS: return gpu_nms(dets, thresh, device_id=cfg.GPU_ID) else: return cpu_nms(dets, thresh)
def nms(dets, thresh, force_cpu=False): """Dispatch to either CPU or GPU NMS implementations. #得到的是[?*21,x,y,w,h,scores] 0.3 """ if dets.shape[0] == 0: return [] if cfg.USE_GPU_NMS and not force_cpu: return gpu_nms(dets, thresh, device_id=0) else: return cpu_nms(dets, thresh)
def nms(dets, thresh, force_cpu=False): """Dispatch to either CPU or GPU NMS implementations.""" if dets.shape[0] == 0: print("dets shape is 0...!!!!!!!!!!!!!!!!!!!!!!!") return [] # ---numpy version--- # original: return gpu_nms(dets, thresh, device_id=cfg.GPU_ID) # ---pytorch version--- if not force_cpu: return nms_gpu(dets, thresh) else: dets = dets.numpy() keep=cpu_nms(dets, thresh) return torch.from_numpy(np.array(keep)).float().to("cuda:1") #converting to float tensor tensor
def nms(self, dets: np.ndarray, nms_thresh: float = 0.4, box_conf_thresh: float = 0.5) -> np.ndarray: keep = dets[:, 4] > box_conf_thresh dets = dets[keep, :] nms_dets = [] for i in range(YoloDecorate.NUM_CLASS): cls = dets[:, 5].astype(np.int32) == i dets_cls = dets[cls, :] if dets_cls.size == 0: continue keep = cpu_nms(dets_cls, nms_thresh) # fast speed nms # keep = py_cpu_nms(dets_cls, nms_thresh) # slow nms_dets_cls = dets_cls[keep, :] nms_dets.append(nms_dets_cls) if len(nms_dets) > 0: nms_dets = np.concatenate(nms_dets) else: nms_dets = np.array([[]], dtype=np.float32) return nms_dets
def _nms(dets): return cpu_nms(dets, thresh)
def _nms(dets): return cpu_nms(dets, thresh)
def proposal_layer_py(rpn_bbox_cls_prob, rpn_bbox_pred, im_dims, mode, feat_strides, anchor_scales): """ # clip predicted boxes to image # remove predicted boxes with either height or width < threshold # sort all ( proposal , score) pairs by score from highest to lowest # take top pre_nums_ no N proposal before non-maximal suppresion # appy NMS with threshold 0.7 to remaining proposals # take after_nms_topN proposals after NMS # return the top proposlas ( -> ROIs, top, scores top) """ anchors = generate_anchors.generate_anchors(base_size=16, ratios=[0.5, 1, 2], scales=anchor_scales) num_anchors = anchors.shape[0] rpn_bbox_cls_prob = np.transpose(rpn_bbox_cls_prob, [0, 3, 1, 2]) # [1, 9*2, height, width ] rpn_bbox_pred = np.transpose(rpn_bbox_pred, [0, 3, 1, 2]) # [1, 9*4, height, width ] if mode == 'train': pre_nms_topN = 12000 post_nms_topN = 2000 nms_thresh = 0.7 min_size = 16 else: pre_nms_topN = 6000 post_nms_topN = 300 nms_thresh = 0.7 min_size = 16 # the first set of num_anchors channels are bg probabilities, the second set are the fg probablilities. scores = rpn_bbox_cls_prob[:, : num_anchors, :, :] # score for fg probablilities, [1, 9, height, width] bbox_deltas = rpn_bbox_pred # [1, 9*4, height, width] # step1 : generate proposal from bbox deltas and shifted anchors height, width = scores.shape[-2:] shift_x = np.arange(0, width) * feat_strides shift_y = np.arange(0, height) * feat_strides shift_x, shift_y = np.meshgrid(shift_x, shift_y) shifts = np.vstack( (shift_x.ravel(), shift_y.ravel(), shift_x.ravel(), shift_y.ravel())) shifts = shifts.transpose() # A = num_anchors # number of anchor per shift = 9 K = shifts.shape[0] # number of shift aaa = anchors.reshape((1, A, 4)) bbb = shifts.reshape(1, K, 4).transpose((1, 0, 2)) anchors = aaa + bbb #anchors = anchors.reshape((1, A, 4 )) + shifts.reshape( 1, K, 4).transpose((1, 0, 2)) anchors = anchors.reshape((K * A), 4) # [ K*A, 4] # transpose and reshape predicted bbox transformations to get the same order as anchors # bbox_deltas is [1, 4*A, H, W ] bbox_deltas = bbox_deltas.transpose((0, 2, 3, 1)).reshape( (-1, 4)) # [ A*K, 4] scores = scores.transpose((0, 2, 3, 1)).reshape((-1, 1)) # [ A*K, 1] # convert anchor into proposals via bbox transformations proposals = bbox_transform.bbox_transform_inv(anchors, bbox_deltas) # [K*A, 4] # step2 : clip predicted boxes accodring to image size proposals = bbox_transform.clip_boxes(proposals, im_dims) # step3: remove predicted boxes with either height or width < threshold keep = filter_boxes(proposals, min_size) proposals = proposals[keep, :] scores = scores[keep] # step4: sort all (proposal, score) pairs by score from highest to lowest order = scores.ravel().argsort()[::-1] if pre_nms_topN > 0: order = order[:pre_nms_topN] # step5: take top pre_nms_topN proposals = proposals[order, :] scores = scores[order] # step6: apply nms ( e.g. threshold = 0.7 ) keep = cpu_nms.cpu_nms(np.hstack((proposals, scores)), nms_thresh) if post_nms_topN > 0: keep = keep[:post_nms_topN] # step7: take after_nms_topN proposals = proposals[keep, :] scores = scores[keep] print "proposals.shape after nms", proposals.shape print "scores.shape", scores.shape # step8: return the top proposal batch_inds = np.zeros((proposals.shape[0], 1), dtype=np.float32) # [ len(keep), 1] blob = np.hstack( (batch_inds, proposals.astype(np.float32, copy=False))) # proposal structure: [0,x1,y1,x2,y2] print "blob.shape", blob.shape return blob