Esempio n. 1
0
    def proposal_layer(self, rpn_class, rpn_bbox):
        # handling proposals
        scores = rpn_class[:, :, 1]
        # Box deltas [batch, num_rois, 4]
        deltas_mul = Variable(
            torch.from_numpy(
                np.reshape(self.config.RPN_BBOX_STD_DEV,
                           [1, 1, 4]).astype(np.float32))).cuda()
        deltas = rpn_bbox * deltas_mul

        pre_nms_limit = min(6000, self.anchors.shape[0])

        scores, ix = torch.topk(scores,
                                pre_nms_limit,
                                dim=-1,
                                largest=True,
                                sorted=True)

        ix = torch.unsqueeze(ix, 2)
        ix = torch.cat([ix, ix, ix, ix], dim=2)
        deltas = torch.gather(deltas, 1, ix)

        _anchors = []
        for i in range(self.config.IMAGES_PER_GPU):
            anchors = Variable(
                torch.from_numpy(self.anchors.astype(np.float32))).cuda()
            _anchors.append(anchors)
        anchors = torch.stack(_anchors, 0)

        pre_nms_anchors = torch.gather(anchors, 1, ix)
        refined_anchors = apply_box_deltas_graph(pre_nms_anchors, deltas)

        # Clip to image boundaries. [batch, N, (y1, x1, y2, x2)]
        height, width = self.config.IMAGE_SHAPE[:2]
        window = np.array([0, 0, height, width]).astype(np.float32)
        window = Variable(torch.from_numpy(window)).cuda()

        refined_anchors_clipped = clip_boxes_graph(refined_anchors, window)

        refined_proposals = []
        for i in range(self.config.IMAGES_PER_GPU):
            indices = nms(
                torch.cat([
                    refined_anchors_clipped.data[i],
                    scores.data[i].unsqueeze(1)
                ], 1), self.config.RPN_NMS_THRESHOLD)
            indices = indices[:self.proposal_count]
            indices = torch.stack([indices, indices, indices, indices], dim=1)
            indices = Variable(indices).cuda()
            proposals = torch.gather(refined_anchors_clipped[i], 0, indices)
            padding = self.proposal_count - proposals.size()[0]
            proposals = torch.cat(
                [proposals,
                 Variable(torch.zeros([padding, 4])).cuda()], 0)
            refined_proposals.append(proposals)

        rpn_rois = torch.stack(refined_proposals, 0)

        return rpn_rois
Esempio n. 2
0
    def proposal_layer(self, rpn_class, rpn_bbox):
        # handling proposals
        scores = rpn_class[:, :, 1]
        # Box deltas [batch, num_rois, 4]
        deltas_mul = Variable(torch.from_numpy(np.reshape(
            self.config.RPN_BBOX_STD_DEV, [1, 1, 4]).astype(np.float32))).cuda()
        deltas = rpn_bbox * deltas_mul

        pre_nms_limit = min(6000, self.anchors.shape[0])

        scores, ix = torch.topk(scores, pre_nms_limit, dim=-1,
                                largest=True, sorted=True)


        ix = torch.unsqueeze(ix, 2)
        ix = torch.cat([ix, ix, ix, ix], dim=2)
        deltas = torch.gather(deltas, 1, ix)

        _anchors = []
        for i in range(self.config.IMAGES_PER_GPU):
            anchors = Variable(torch.from_numpy(
                self.anchors.astype(np.float32))).cuda()
            _anchors.append(anchors)
        anchors = torch.stack(_anchors, 0) 
    
        pre_nms_anchors = torch.gather(anchors, 1, ix)
        refined_anchors = apply_box_deltas_graph(pre_nms_anchors, deltas)

        # Clip to image boundaries. [batch, N, (y1, x1, y2, x2)]
        height, width = self.config.IMAGE_SHAPE[:2]
        window = np.array([0, 0, height, width]).astype(np.float32)
        window = Variable(torch.from_numpy(window)).cuda()

        refined_anchors_clipped = clip_boxes_graph(refined_anchors, window)

        refined_proposals = []
        for i in range(self.config.IMAGES_PER_GPU):
            indices = nms(
                torch.cat([refined_anchors_clipped.data[i], scores.data[i]], 1), 0.7)
            indices = indices[:self.proposal_count]
            indices = torch.stack([indices, indices, indices, indices], dim=1)
            indices = Variable(indices).cuda()
            proposals = torch.gather(refined_anchors_clipped[i], 0, indices)
            padding = self.proposal_count - proposals.size()[0]
            proposals = torch.cat(
                [proposals, Variable(torch.zeros([padding, 4])).cuda()], 0)
            refined_proposals.append(proposals)

        rpn_rois = torch.stack(refined_proposals, 0)

        return rpn_rois
def im_detect(im, im_file):
    blobs, im_scales = get_blobs(im)
    assert len(im_scales) == 1, "Only single-image batch implemented"

    im_blob = blobs['data']
    blobs['im_info'] = np.array([im_blob.shape[1], im_blob.shape[2], im_scales[0]], dtype=np.float32)

    input0 = blobs['data'].tolist()
    input1 = blobs['im_info'].tolist()
    parms = {"signature_name": "tf_faster_rcnn_cls", 
             "inputs":{
                      'input0': input0,
                      'input1' : input1
                     }
            }
    
    from urllib import parse
    import requests
    import json
    headers = {'Content-Type': 'application/json'}
    url = 'http://localhost:8501/v1/models/saved_model:predict'
    
    u = requests.post(url , headers=headers, data=json.dumps(parms)).json()
    u = u['outputs']
    _nosess = np.array(u['output0'])
    scores = np.array(u['output1'])
    bbox_pred = np.array(u['output2'])
    rois = np.array(u['output3'])
    boxes = rois[:, 1:5] / im_scales[0]
    scores = np.reshape(scores, [scores.shape[0], -1])
    bbox_pred = np.reshape(bbox_pred, [bbox_pred.shape[0], -1])
    
    if True:
        box_deltas = bbox_pred
        pred_boxes = bbox_transform_inv(boxes, box_deltas)
        pred_boxes = clip_boxes(pred_boxes, im.shape)
    else:
        pred_boxes = np.tile(boxes, (1, scores.shape[1]))
    
    ###### boxes to pred_boxes
    boxes = pred_boxes

    CLASSES = ('__background__',
               'aeroplane', 'bicycle', 'bird', 'boat',
               'bottle', 'bus', 'car', 'cat', 'chair',
               'cow', 'diningtable', 'dog', 'horse',
               'motorbike', 'person', 'pottedplant',
               'sheep', 'sofa', 'train', 'tvmonitor')

    CONF_THRESH = 0.8
    NMS_THRESH = 0.3
    
    from lib.nms_wrapper import nms
    result_list = []
    for cls_ind, cls in enumerate(CLASSES[1:]):
        cls_ind += 1 # because we skipped background
        cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)]
        cls_scores = scores[:, cls_ind]
        dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32)
        keep = nms(dets, NMS_THRESH)
        dets = dets[keep, :]
        result = vis_detections(im, cls, dets, thresh=CONF_THRESH)
        if result is not None:
            #print(result)
            result_list.append(result)
        
    pwd = os.path.join('/data/wxh/www/result/', im_file)
    cv2.imwrite(pwd, im)
    return result_list
Esempio n. 4
0
def _proposal_layer(rpn_cls_prob,
                    rpn_bbox_pred,
                    img_info,
                    config_key,
                    feature_stride=[
                        16,
                    ],
                    anchor_scales=[8, 16, 32]):
    rpn_cls_prob = np.transpose(rpn_cls_prob, [0, 3, 1, 2])
    rpn_bbox_pred = np.transpose(rpn_bbox_pred, [0, 3, 1, 2])

    img_info = img_info[0]

    config_key = config_key.decode('utf-8')
    config = CONFIG[config_key]

    anchors = GenerateAnchor(scales=np.array(anchor_scales))
    anchors_size = anchors.shape[0]

    scores = rpn_cls_prob[:, anchors_size:, :, :]
    bboxes = rpn_bbox_pred

    height, width = scores.shape[-2:]

    shift_x = np.arange(0, width) * feature_stride
    shift_y = np.arange(0, height) * feature_stride
    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())).transpose()

    A_size = anchors_size
    K_size = shifts.shape[0]
    all_anchors = (anchors.reshape((1, A_size, 4)) + shifts.reshape(
        (1, K_size, 4)).transpose((1, 0, 2)))
    all_anchors = all_anchors.reshape((K_size * A_size, 4))

    bboxes = bboxes.transpose((0, 2, 3, 1)).reshape((-1, 4))
    scores = scores.transpose((0, 2, 3, 1)).reshape((-1, 1))

    proposal_bboxes = BBoxTransformInverse(all_anchors, bboxes)
    proposal_bboxes = ClipBoxes(proposal_bboxes, img_info[:2])

    keep_indices = FilterBoxes(proposal_bboxes, img_info[2])
    proposal_bboxes = proposal_bboxes[keep_indices, :]
    scores = scores[keep_indices]

    sorted_indices = scores.ravel().argsort()[::-1]
    if config.RPN_PRE_NMS_TOP_N > 0:
        sorted_indices = sorted_indices[:config.RPN_PRE_NMS_TOP_N]
    proposal_bboxes = proposal_bboxes[sorted_indices, :]
    scores = scores[sorted_indices]

    keep_indices = nms(np.hstack((proposal_bboxes, scores)),
                       config.RPN_NMS_THRESHOLD)
    if config.RPN_POST_NMS_TOP_N > 0:
        keep_indices = keep_indices[:config.RPN_POST_NMS_TOP_N]
    proposal_bboxes = proposal_bboxes[keep_indices, :]
    scores = scores[keep_indices]

    batch_indices = np.zeros((proposal_bboxes.shape[0], 1), dtype=np.float32)
    blob = np.hstack(
        (batch_indices, proposal_bboxes.astype(np.float32, copy=False)))

    return blob