コード例 #1
0
    def forward(self, bottom, top):
        # Text Quad
        # Proposal ROIS(0, x1, y1, x2, y2, x3, y3, x4, y4)
        all_rois = bottom[0].data
        # GT boxes (x1, y1, x2, y2, x3, y3, x4, y4, label)
        gt_boxes = bottom[1].data
        # Include ground-truth boxes in the set of candidate rois
        zeros = np.zeros((gt_boxes.shape[0], 1), dtype=gt_boxes.dtype)
        all_rois = np.vstack((all_rois, np.hstack((zeros, gt_boxes[:, :-1]))))

        # Sanity check: single batch only
        assert np.all(
            all_rois[:, 0] == 0), 'Only single item batches are supported'

        num_images = 1
        rois_per_image = cfg.TRAIN.BATCH_SIZE / num_images
        fg_rois_per_image = np.round(cfg.TRAIN.FG_FRACTION * rois_per_image)

        # Sample rois with classification labels and bounding box regression targets
        labels, quads, bbox_targets, bbox_inside_weights = _sample_rois(
            all_rois, gt_boxes, fg_rois_per_image, rois_per_image,
            self._num_classes, self._num_weights)

        # dual rois pooling module
        if cfg.DUAL_ROI:
            rois = quad_2_obb(np.array(quads[:, 1:9], dtype=np.float32))
            rois = dual_roi(rois)
        else:
            rois = quad_2_obb(quads[:, 1:9])

        batch_inds = np.zeros((rois.shape[0], 1), dtype=np.float32)
        rois = np.hstack((batch_inds, rois.astype(np.float32, copy=False)))

        if DEBUG:
            print '#num fg: {}'.format((labels == 1).sum())
            print '%num bg: {}'.format((labels == 0).sum())

        # sampled rois
        top[0].reshape(*rois.shape)
        top[0].data[...] = rois

        # classification labels
        top[1].reshape(*labels.shape)
        top[1].data[...] = labels

        # bbox_targets
        top[2].reshape(*bbox_targets.shape)
        top[2].data[...] = bbox_targets

        # bbox_inside_weights
        top[3].reshape(*bbox_inside_weights.shape)
        top[3].data[...] = bbox_inside_weights

        # bbox_outside_weights
        top[4].reshape(*bbox_inside_weights.shape)
        top[4].data[...] = np.array(bbox_inside_weights > 0).astype(np.float32)
コード例 #2
0
ファイル: proposal_layer.py プロジェクト: Wovchena/crpn
    def forward(self, bottom, top):
        # params
        cfg_key = self.phase  # either 'TRAIN' or 'TEST'
        if cfg_key == 0:
            cfg_ = cfg.TRAIN
        else:
            cfg_ = cfg.TEST

        # corner params
        pt_thres = cfg_.PT_THRESH
        pt_max_num = cfg.PT_MAX_NUM
        pt_nms_range = cfg.PT_NMS_RANGE
        pt_nms_thres = cfg.PT_NMS_THRESH
        # proposal params
        ld_interval = cfg.LD_INTERVAL
        ld_um_thres = cfg.LD_UM_THRESH
        # rpn params
        # min_size = cfg_.RPN_MIN_SIZE
        nms_thresh = cfg_.RPN_NMS_THRESH
        pre_nms_topN = cfg_.RPN_PRE_NMS_TOP_N
        post_nms_topN = cfg_.RPN_POST_NMS_TOP_N

        im_info = bottom[0].data[0, :]
        score_tl = bottom[1].data[0, :].transpose((1, 2, 0))
        score_tr = bottom[2].data[0, :].transpose((1, 2, 0))
        score_br = bottom[3].data[0, :].transpose((1, 2, 0))
        score_bl = bottom[4].data[0, :].transpose((1, 2, 0))
        scores = np.concatenate([
            score_tl[:, :, :, np.newaxis], score_tr[:, :, :, np.newaxis],
            score_br[:, :, :, np.newaxis], score_bl[:, :, :, np.newaxis]
        ],
                                axis=3)

        map_info = scores.shape[:2]
        # 1. sample corner candidates from prob maps
        tl, tr, br, bl = _corner_sampling(scores, pt_thres, pt_max_num,
                                          pt_nms_range, pt_nms_thres)
        # 2. assemble corner candidates into proposals
        proposals = _proposal_sampling(tl, tr, br, bl, map_info, ld_interval,
                                       ld_um_thres)
        # 3. filter
        proposals = filter_quads(proposals)
        scores = proposals[:, 8]
        proposals = proposals[:, :8]
        # 3. rescale quads into raw image space
        proposals = proposals * self._feat_stride
        # 4. quadrilateral non-max surpression
        order = scores.ravel().argsort()[::-1]
        if pre_nms_topN > 0:
            order = order[:pre_nms_topN]
        proposals = proposals[order, :]
        scores = scores[order]
        keep = nms(
            np.hstack((proposals, scores[:, np.newaxis])).astype(np.float32,
                                                                 copy=False),
            nms_thresh)
        proposals = proposals[keep, :]
        scores = scores[keep]
        if post_nms_topN > 0:
            proposals = proposals[:post_nms_topN, :]
            scores = scores[:post_nms_topN]
        if proposals.shape[0] == 0:
            # add whole image to avoid error
            print('NO PROPOSALS!')
            proposals = np.array(
                [[0, 0, im_info[1], 0, im_info[1], im_info[0], 0, im_info[0]]])
            scores = np.array([0.0])

        # output
        # top[0]: quads(x1, y1, x2, y2, x3, y3, x4, y4)
        # top[1]: rois(xmin, ymin, xmax, ymax, theta)
        # top[2]: scores
        batch_inds = np.zeros((proposals.shape[0], 1), dtype=np.float32)
        blob = np.hstack((batch_inds, proposals.astype(np.float32,
                                                       copy=False)))
        top[0].reshape(*blob.shape)
        top[0].data[...] = blob
        if len(top) > 1:
            if cfg.DUAL_ROI:
                rois = quad_2_obb(np.array(proposals, dtype=np.float32))
                rois = dual_roi(rois)
            else:
                rois = quad_2_obb(np.array(proposals, dtype=np.float32))
            batch_inds = np.zeros((rois.shape[0], 1), dtype=np.float32)
            blob = np.hstack((batch_inds, rois.astype(np.float32, copy=False)))
            top[1].reshape(*blob.shape)
            top[1].data[...] = blob
        if len(top) > 2:
            scores = np.vstack((scores, scores)).transpose()
            top[2].reshape(*scores.shape)
            top[2].data[...] = scores