def gen_proposals(boxes, bbox_deltas, bound, min_size=8): # proposals=bbox_transform_inv(boxes,bbox_deltas) # proposals[:,0]=np.maximum(0, proposals[:,0]) # proposals[:,1]=np.maximum(0, proposals[:,1]) # proposals[:,2]=np.minimum(bound[0], proposals[:,2]) # proposals[:,3]=np.minimum(bound[1], proposals[:,3]) # # return proposals proposals = bbox_transform_inv(boxes, bbox_deltas) proposals = np.hstack((proposals, boxes[:, -1].reshape(-1, 1))) proposals[:, 0] = np.maximum(0, proposals[:, 0]) proposals[:, 1] = np.maximum(0, proposals[:, 1]) proposals[:, 2] = np.minimum(bound[0], proposals[:, 2]) proposals[:, 3] = np.minimum(bound[1], proposals[:, 3]) ws = proposals[:, 2] - proposals[:, 0] hs = proposals[:, 3] - proposals[:, 1] keep = np.where(np.bitwise_and(ws >= min_size, hs >= min_size) == 1)[0] proposals_keep = proposals[keep] boxes_keep = boxes[keep] # print(proposals[:30]) assert len( keep) > 0, 'Anchors bbox_transform_inv have all rois of negative sides' return proposals_keep, boxes_keep
def gen_proposals(self, rpn_logits, rpn_bbox, anchors, batch_size): out_cls_softmax = F.softmax(rpn_logits, dim=1) out_cls_np = out_cls_softmax.cpu().data.numpy()[:, 1, :, :] out_cls_np = out_cls_np.reshape(batch_size, self.K, self.out_height, self.out_width).transpose(0, 2, 3, 1).reshape( -1, 1) ''' out_cls=rpn_logits.view(batch_size, 2*self.K, self.out_height, self.out_width).permute(0,2,3,1).contiguous().view(-1,2) out_cls_softmax=F.softmax(out_cls, dim=1) out_cls_np=out_cls_softmax.cpu().data.numpy()[:,1].reshape(-1,1) ''' out_bbox_np = rpn_bbox.cpu().data.numpy().transpose(0, 2, 3, 1).reshape(-1, 4) tic = time() if cfg.BBOX_NORMALIZE_TARGETS_PRECOMPUTED: out_bbox_np = out_bbox_np * cfg.RPN_BBOX_STD_DEV bbox_pred = bbox_transform_inv(anchors, out_bbox_np) self.clip_boxes(bbox_pred, self.bound) toc = time() if DEBUG: print('--bbox transform costs {}s'.format(toc - tic)) bbox_pred_with_cls = np.hstack((bbox_pred, out_cls_np)) proposals_batch = np.split(bbox_pred_with_cls, batch_size, axis=0) all_proposals = [] tic = time() for proposals in proposals_batch: ''' x1,y1,x2,y2,_=np.split(proposals,5,axis=1) ws,hs=x2-x1+1,y2-y1+1 inds=np.where(np.bitwise_and(ws>32,hs>32)==1)[0] proposals=proposals[inds] ''' if cfg.NMS: order = np.argsort(proposals[:, -1])[::-1] proposals_order = proposals[ order[:cfg[cfg.PHASE].RPN_PRE_NMS_TOP_N]] pick = nms_cuda(proposals_order, nms_thresh=cfg[cfg.PHASE].RPN_NMS_THRESH, xyxy=True) if len(pick) == 0: print('No pick in proposal nms') if cfg[cfg.PHASE].RPN_POST_NMS_TOP_N > 0 and len(pick) > cfg[ cfg.PHASE].RPN_POST_NMS_TOP_N: pick = pick[:cfg[cfg.PHASE].RPN_POST_NMS_TOP_N] proposals_nms = proposals_order[pick] else: proposals_nms = proposals all_proposals.append(proposals_nms) toc = time() if DEBUG: print('--NMS costs {}s'.format(toc - tic)) '''list of all proposals of each image sample''' return all_proposals
def gen_proposals(self, rpn_cls, rpn_bbox, anchors, batch_size): # out_cls_softmax=self.rpn_cls_softmax(rpn_cls) out_cls_softmax = F.softmax(rpn_cls, dim=1) out_cls_np = out_cls_softmax.cpu().data.numpy()[:, 1, :, :] out_cls_np = out_cls_np.reshape(batch_size, self.K, self.out_size[1], self.out_size[0]).transpose( 0, 2, 3, 1).reshape(-1, 1) out_bbox_np = rpn_bbox.cpu().data.numpy().transpose(0, 2, 3, 1).reshape(-1, 4) if cfg.BBOX_NORMALIZE_TARGETS_PRECOMPUTED: out_bbox_np = out_bbox_np * cfg.RPN_BBOX_STD_DEV if anchors.shape[0] != out_bbox_np.shape[0]: anchors = np.tile(anchors, (batch_size, 1)) bbox_pred = U.bbox_transform_inv(anchors, out_bbox_np) self.clip_boxes(bbox_pred, self.bound) bbox_pred_with_cls = np.hstack((bbox_pred, out_cls_np)) proposals_batch = np.split(bbox_pred_with_cls, batch_size, axis=0) all_proposals = [] for proposals in proposals_batch: if cfg.NMS: order = np.argsort(proposals[:, -1])[::-1] proposals_order = proposals[ order[:cfg[cfg.PHASE].RPN_PRE_NMS_TOP_N]] # pick=nms_cuda(proposals_order, nms_thresh=0.7, xyxy=True) pick = nms_cuda(proposals_order, nms_thresh=cfg[cfg.PHASE].RPN_NMS_THRESH, xyxy=True) if len(pick) == 0: print('No pick in proposal nms') if cfg[cfg.PHASE].RPN_POST_NMS_TOP_N > 0 and len(pick) > cfg[ cfg.PHASE].RPN_POST_NMS_TOP_N: pick = pick[:cfg[cfg.PHASE].RPN_POST_NMS_TOP_N] proposals_nms = proposals_order[pick] all_proposals.append(proposals_nms) else: all_proposals.append(proposals) '''list of all proposals of each box, not each frame''' return all_proposals
def im_detect(image, anchors, model): im_w = model.im_width im_h = model.im_height h, w = image.shape[:2] image = cv2.resize(image, (im_w, im_h), interpolation=cv2.INTER_LINEAR) xscale = 1.0 * im_w / w yscale = 1.0 * im_h / h roidb = {} roidb['image'] = image[np.newaxis, :, :, :].astype(np.float32) if cfg.IMAGE_NORMALIZE: roidb['image'] -= roidb['image'].min() roidb['image'] /= roidb['image'].max() roidb['image'] = (roidb['image'] - 0.5) / 0.5 else: roidb['image'] -= cfg.PIXEL_MEANS roidb['gt_boxes'] = None roidb['gt_classes'] = None roidb['bbox_overlaps'] = None roidb['anchors'] = anchors output_dict = model([roidb]) frcnn_probs = output_dict['frcnn_probs'].cpu().data.numpy() frcnn_bbox = output_dict['frcnn_bbox'].cpu().data.numpy() proposals = output_dict['proposals'] num_proposals_per_image = output_dict['num_proposals'] print(num_proposals_per_image) print(frcnn_probs.shape) cls_bboxes = [[]] # classes=np.argmax(frcnn_probs[:,1:], axis=1)+1 # max_probs=np.max(frcnn_probs[:,1:], axis=1) classes = np.argmax(frcnn_probs, axis=1) max_probs = np.max(frcnn_probs, axis=1) # print(classes) # bbox_pred=bbox_transform_inv(proposals, frcnn_bbox) for i in range(1, cfg.NUM_CLASSES): cls_inds = np.where(classes == i)[0] if cls_inds.size == 0: cls_bboxes.append([]) else: cls_proposals = proposals[cls_inds] cls_frcnn_bbox = frcnn_bbox[cls_inds, 4 * i:4 * i + 4] if cfg.BBOX_NORMALIZE_TARGETS_PRECOMPUTED: cls_frcnn_bbox = cls_frcnn_bbox * cfg.BBOX_STD_DEV cls_bbox_pred = bbox_transform_inv(cls_proposals, cls_frcnn_bbox) cls_probs = max_probs[cls_inds] # print(frcnn_probs[cls_inds]) cls_bbox_pred = np.hstack((cls_bbox_pred, cls_probs.reshape(-1, 1))) order = np.argsort(cls_probs)[::-1] bbox_order = cls_bbox_pred[order] pick = nms_cuda(bbox_order, nms_thresh=cfg.TEST.NMS_THRESH, xyxy=True) # pick=pick[:600] bboxes = bbox_order[pick].reshape(-1, bbox_order.shape[1]) print('Class {} has {} instances!'.format(CLASSES[i], bboxes.shape[0])) cls_bboxes.append(bboxes) # print(len(proposals)) return cls_bboxes, (xscale, yscale), proposals
def get_track_output(output_dict, configs): K = configs['K'] temp_boxes = configs['temp_boxes'] search_boxes = configs['search_boxes'] rpn_conv_size = configs['rpn_conv_size'] raw_anchors = configs['raw_anchors'] bound = configs['bound'] track_rpn_logits = output_dict['track_rpn_logits'] track_rpn_bbox = output_dict['track_rpn_bbox'] num_targets = track_rpn_logits.shape[0] track_rpn_cls = F.softmax(track_rpn_logits, dim=1).cpu().data.numpy() track_rpn_cls = track_rpn_cls[:, 1, :, :].reshape( num_targets, K, rpn_conv_size, rpn_conv_size).transpose(0, 2, 3, 1).reshape(num_targets, -1) track_rpn_bbox = track_rpn_bbox.cpu().data.numpy().transpose( 0, 2, 3, 1).reshape(num_targets, -1, 4) bboxes_list = [] anchors_list = [] for i in range(num_targets): temp_box = temp_boxes[i] temp_cx = 0.5 * (temp_box[0] + temp_box[2]) temp_cy = 0.5 * (temp_box[1] + temp_box[3]) rpn_cls = track_rpn_cls[i] rpn_bbox = track_rpn_bbox[i] # print(rpn_cls.size) # print(rpn_bbox.shape[0]) order = np.argsort(rpn_cls)[::-1] _anchors = G.gen_region_anchors(raw_anchors, search_boxes[i].reshape(1, -1), bound, K, size=(rpn_conv_size, rpn_conv_size))[0] top = 1 # print(rpn_cls[order[:top]]) fg_anchors = _anchors[order[:top]] fg_rpn_bbox = rpn_bbox[order[:top]] if cfg.BBOX_NORMALIZE_TARGETS_PRECOMPUTED: fg_rpn_bbox *= cfg.BBOX_STD_DEV bboxes = bbox_transform_inv(fg_anchors, fg_rpn_bbox) ''' best_id=select_proposals_cosine(temp_boxes[i], bboxes, rpn_cls[order[:top]], order[:top], rpn_conv_size, K) anchors_list.append(fg_anchors[best_id][np.newaxis,:]) bboxes_list.append(bboxes[best_id][np.newaxis,:]) ''' if np.max(rpn_cls[order[:top]]) < cfg.TRACK_SCORE_THRESH: bboxes_list.append([]) else: cx = 0.5 * (bboxes[:, 0] + bboxes[:, 2]) cy = 0.5 * (bboxes[:, 1] + bboxes[:, 3]) dist_cx = np.abs(cx - temp_cx) dist_cy = np.abs(cy - temp_cy) fg_inds = np.where( np.bitwise_and(dist_cx < cfg.TRACK_MAX_DIST, dist_cy < cfg.TRACK_MAX_DIST) == 1)[0] bboxes_list.append(bboxes[fg_inds]) # bboxes_list.append(bboxes) anchors_list.append(fg_anchors) ret = {} ret['bboxes_list'] = bboxes_list ret['anchors_list'] = anchors_list return ret
def get_detect_output(output_dict, batch_size=-1): frcnn_probs = output_dict['frcnn_probs'].cpu().data.numpy() frcnn_bbox = output_dict['frcnn_bbox'].cpu().data.numpy() proposals = output_dict['proposals'] num_proposals_per_image = output_dict['num_proposals'] # print(num_proposals_per_image) # print(frcnn_probs.shape) outputs = [] B = len(num_proposals_per_image) if batch_size > 0: B = batch_size start = 0 for b in range(B): ret = {} cls_bboxes = [[]] frcnn_probs_this_sample = frcnn_probs[start:start + num_proposals_per_image[b]] frcnn_bbox_this_sample = frcnn_bbox[start:start + num_proposals_per_image[b]] proposals_this_sample = proposals[start:start + num_proposals_per_image[b]] classes = np.argmax(frcnn_probs_this_sample, axis=1) max_probs = np.max(frcnn_probs_this_sample, axis=1) # print(classes) for i in range(1, cfg.NUM_CLASSES): cls_inds = np.where(classes == i)[0] if cls_inds.size == 0: cls_bboxes.append(np.array([])) else: cls_proposals = proposals_this_sample[cls_inds] cls_frcnn_bbox = frcnn_bbox_this_sample[cls_inds, 4 * i:4 * i + 4] if cfg.BBOX_NORMALIZE_TARGETS_PRECOMPUTED: cls_frcnn_bbox = cls_frcnn_bbox * cfg.BBOX_STD_DEV cls_bbox_pred = bbox_transform_inv(cls_proposals, cls_frcnn_bbox) cls_probs = max_probs[cls_inds] # print(frcnn_probs_this_sample[cls_inds]) # cls_bbox_pred = np.hstack( (cls_bbox_pred, cls_probs.reshape(-1, 1))) order = np.argsort(cls_probs)[::-1] bbox_order = cls_bbox_pred[order] # pick=nms_cuda(bbox_order, nms_thresh=0.7, xyxy=True) pick = nms_cuda(bbox_order, nms_thresh=cfg[cfg.PHASE].NMS_THRESH, xyxy=True) pick = pick[:600] bboxes = bbox_order[pick].reshape(-1, bbox_order.shape[1]) nms_scores = bboxes[:, -1] bboxes = bboxes[np.where( nms_scores > cfg.DET_SCORE_THRESH)[0], :] if bboxes.shape[0] > 0: print('Class {} has {} instances!'.format( CLASSES[i], bboxes.shape[0])) cls_bboxes.append(bboxes) ret['cls_bboxes'] = cls_bboxes ret['proposals'] = proposals_this_sample outputs.append(ret) start += num_proposals_per_image[b] return outputs