def anchor_target_layer(self, gt_boxes_h_batch, gt_boxes_r_batch, anchor_batch, gpu_id=0):

        all_labels, all_target_delta, all_anchor_states, all_target_boxes, all_target_encode_label = [], [], [], [], []
        for i in range(self.cfgs.BATCH_SIZE):
            anchors = np.array(anchor_batch[i], np.float32)
            gt_boxes_h = gt_boxes_h_batch[i, :, :]
            gt_boxes_r = gt_boxes_r_batch[i, :, :]
            anchor_states = np.zeros((anchors.shape[0],))
            labels = np.zeros((anchors.shape[0], self.cfgs.CLASS_NUM))
            if gt_boxes_r.shape[0]:
                # [N, M]

                if self.cfgs.METHOD == 'H':
                    overlaps = bbox_overlaps(np.ascontiguousarray(anchors, dtype=np.float),
                                             np.ascontiguousarray(gt_boxes_h, dtype=np.float))
                else:
                    overlaps = rbbx_overlaps(np.ascontiguousarray(anchors, dtype=np.float32),
                                             np.ascontiguousarray(gt_boxes_r[:, :-1], dtype=np.float32), gpu_id)

                argmax_overlaps_inds = np.argmax(overlaps, axis=1)
                max_overlaps = overlaps[np.arange(overlaps.shape[0]), argmax_overlaps_inds]

                # compute box regression targets
                target_boxes = gt_boxes_r[argmax_overlaps_inds]

                positive_indices = max_overlaps >= self.cfgs.IOU_POSITIVE_THRESHOLD
                ignore_indices = (max_overlaps > self.cfgs.IOU_NEGATIVE_THRESHOLD) & ~positive_indices

                anchor_states[ignore_indices] = -1
                anchor_states[positive_indices] = 1

                # compute target class labels
                labels[positive_indices, target_boxes[positive_indices, -1].astype(int) - 1] = 1
            else:
                # no annotations? then everything is background
                target_boxes = np.zeros((anchors.shape[0], gt_boxes_r.shape[1]))

            if self.cfgs.METHOD == 'H':
                x_c = (anchors[:, 2] + anchors[:, 0]) / 2
                y_c = (anchors[:, 3] + anchors[:, 1]) / 2
                h = anchors[:, 2] - anchors[:, 0] + 1
                w = anchors[:, 3] - anchors[:, 1] + 1
                theta = -90 * np.ones_like(x_c)
                anchors = np.vstack([x_c, y_c, w, h, theta]).transpose()

            if self.cfgs.ANGLE_RANGE == 180:
                anchors = coordinate_present_convert(anchors, mode=-1)
                target_boxes = coordinate_present_convert(target_boxes, mode=-1)
            target_delta = bbox_transform.rbbox_transform(ex_rois=anchors, gt_rois=target_boxes)

            all_labels.append(labels)
            all_target_delta.append(target_delta)
            all_anchor_states.append(anchor_states)
            all_target_boxes.append(target_boxes)

        return np.array(all_labels, np.float32), np.array(all_target_delta, np.float32), \
               np.array(all_anchor_states, np.float32), np.array(all_target_boxes, np.float32), \
    def anchor_target_layer(self, gt_boxes_h, gt_boxes_r, anchors, gpu_id=0):

        anchor_states = np.zeros((anchors.shape[0], ))
        labels = np.zeros((anchors.shape[0], self.cfgs.CLASS_NUM))
        if gt_boxes_r.shape[0]:
            # [N, M]

            if self.cfgs.METHOD == 'H':
                overlaps = bbox_overlaps(
                    np.ascontiguousarray(anchors, dtype=np.float),
                    np.ascontiguousarray(gt_boxes_h, dtype=np.float))
            else:
                overlaps = rbbx_overlaps(
                    np.ascontiguousarray(anchors, dtype=np.float32),
                    np.ascontiguousarray(gt_boxes_r[:, :-1], dtype=np.float32),
                    gpu_id)

            argmax_overlaps_inds = np.argmax(overlaps, axis=1)
            max_overlaps = overlaps[np.arange(overlaps.shape[0]),
                                    argmax_overlaps_inds]

            # compute box regression targets
            target_boxes = gt_boxes_r[argmax_overlaps_inds]

            positive_indices = max_overlaps >= self.cfgs.IOU_POSITIVE_THRESHOLD
            ignore_indices = (max_overlaps > self.cfgs.IOU_NEGATIVE_THRESHOLD
                              ) & ~positive_indices

            anchor_states[ignore_indices] = -1
            anchor_states[positive_indices] = 1

            # compute target class labels
            labels[positive_indices,
                   target_boxes[positive_indices, -1].astype(int) - 1] = 1
        else:
            # no annotations? then everything is background
            target_boxes = np.zeros((anchors.shape[0], gt_boxes_r.shape[1]))

        # if self.cfgs.METHOD == 'H':
        #     w = anchors[:, 2] - anchors[:, 0] + 1
        #     h = anchors[:, 3] - anchors[:, 1] + 1
        #     x1 = anchors[:, 0]
        #     y1 = anchors[:, 1]
        #     x2 = anchors[:, 2]
        #     y2 = anchors[:, 1]
        #     x3 = anchors[:, 2]
        #     y3 = anchors[:, 3]
        #     x4 = anchors[:, 0]
        #     y4 = anchors[:, 3]
        #     anchors = np.stack([x1, y1, x2, y2, x3, y3, x4, y4, w, h]).transpose()
        #
        # target_delta = bbox_transform.qbbox_transform(ex_rois=anchors, gt_rois=target_boxes)

        return np.array(labels, np.float32), np.array(
            anchor_states, np.float32), np.array(target_boxes, np.float32)
    def anchor_target_layer(self, gt_boxes_h, gt_boxes_r, anchors, gpu_id=0):

        anchor_states = np.zeros((anchors.shape[0], ))
        labels = np.zeros((anchors.shape[0], self.cfgs.CLASS_NUM))
        if gt_boxes_r.shape[0]:
            # [N, M]

            if self.cfgs.METHOD == 'H':
                overlaps = bbox_overlaps(
                    np.ascontiguousarray(anchors, dtype=np.float),
                    np.ascontiguousarray(gt_boxes_h, dtype=np.float))
            else:
                overlaps = rbbx_overlaps(
                    np.ascontiguousarray(anchors, dtype=np.float32),
                    np.ascontiguousarray(gt_boxes_r[:, :-1], dtype=np.float32),
                    gpu_id)

            argmax_overlaps_inds = np.argmax(overlaps, axis=1)
            max_overlaps = overlaps[np.arange(overlaps.shape[0]),
                                    argmax_overlaps_inds]

            # compute box regression targets
            target_boxes = gt_boxes_r[argmax_overlaps_inds]

            positive_indices = max_overlaps >= self.cfgs.IOU_POSITIVE_THRESHOLD
            ignore_indices = (max_overlaps > self.cfgs.IOU_NEGATIVE_THRESHOLD
                              ) & ~positive_indices

            anchor_states[ignore_indices] = -1
            anchor_states[positive_indices] = 1

            # compute target class labels
            labels[positive_indices,
                   target_boxes[positive_indices, -1].astype(int) - 1] = 1
        else:
            # no annotations? then everything is background
            target_boxes = np.zeros((anchors.shape[0], gt_boxes_r.shape[1]))

        if self.cfgs.METHOD == 'H':
            x_c = (anchors[:, 2] + anchors[:, 0]) / 2
            y_c = (anchors[:, 3] + anchors[:, 1]) / 2
            h = anchors[:, 2] - anchors[:, 0] + 1
            w = anchors[:, 3] - anchors[:, 1] + 1
            theta = -90 * np.ones_like(x_c)
            anchors = np.vstack([x_c, y_c, w, h, theta]).transpose()

        target_delta = bbox_transform.rbbox_transform(ex_rois=anchors,
                                                      gt_rois=target_boxes)

        return np.array(labels, np.float32), np.array(target_delta, np.float32), \
               np.array(anchor_states, np.float32), np.array(target_boxes, np.float32)
Ejemplo n.º 4
0
    def _sample_rois(self, all_rois, gt_boxes_h, gt_boxes_r, fg_rois_per_image,
                     rois_per_image, num_classes):
        """Generate a random sample of RoIs comprising foreground and background
        examples.
        all_rois shape is [-1, 4]
        gt_boxes shape is [-1, 5]. that is [x1, y1, x2, y2, label]
        """
        # overlaps: (rois x gt_boxes)
        overlaps = bbox_overlaps(
            np.ascontiguousarray(all_rois, dtype=np.float),
            np.ascontiguousarray(gt_boxes_h[:, :-1], dtype=np.float))
        gt_assignment = overlaps.argmax(axis=1)
        max_overlaps = overlaps.max(axis=1)
        labels = gt_boxes_h[gt_assignment, -1]

        # Select foreground RoIs as those with >= FG_THRESH overlap
        fg_inds = np.where(max_overlaps >= self.cfgs.FAST_RCNN_IOU_POSITIVE_THRESHOLD)[0]
        # Guard against the case when an image has fewer than fg_rois_per_image
        # Select background RoIs as those within [BG_THRESH_LO, BG_THRESH_HI)
        bg_inds = np.where((max_overlaps < self.cfgs.FAST_RCNN_IOU_POSITIVE_THRESHOLD) &
                           (max_overlaps >= self.cfgs.FAST_RCNN_IOU_NEGATIVE_THRESHOLD))[0]
        # print("first fileter, fg_size: {} || bg_size: {}".format(fg_inds.shape, bg_inds.shape))
        # Guard against the case when an image has fewer than fg_rois_per_image
        # foreground RoIs
        fg_rois_per_this_image = min(fg_rois_per_image, fg_inds.size)

        # Sample foreground regions without replacement
        if fg_inds.size > 0:
            fg_inds = npr.choice(fg_inds, size=int(fg_rois_per_this_image), replace=False)
        # Compute number of background RoIs to take from this image (guarding
        # against there being fewer than desired)
        bg_rois_per_this_image = rois_per_image - fg_rois_per_this_image
        bg_rois_per_this_image = min(bg_rois_per_this_image, bg_inds.size)
        # Sample background regions without replacement
        if bg_inds.size > 0:
            bg_inds = npr.choice(bg_inds, size=int(bg_rois_per_this_image), replace=False)

        # print("second fileter, fg_size: {} || bg_size: {}".format(fg_inds.shape, bg_inds.shape))
        # The indices that we're selecting (both fg and bg)
        keep_inds = np.append(fg_inds, bg_inds)

        # Select sampled values from various arrays:
        labels = labels[keep_inds]

        # Clamp labels for the background RoIs to 0
        labels[int(fg_rois_per_this_image):] = 0
        rois = all_rois[keep_inds]

        target_gt_h = gt_boxes_h[gt_assignment[keep_inds], :-1]
        bbox_target_data_h = self._compute_targets_h(
            rois, target_gt_h, labels)

        target_gt_r = gt_boxes_r[gt_assignment[keep_inds], :-1]
        bbox_target_data_r = self._compute_targets_r(
            rois, target_gt_r, labels)

        bbox_targets_h = \
            self._get_bbox_regression_labels(bbox_target_data_h, num_classes)

        bbox_targets_r = \
            self._get_bbox_regression_labels_r(bbox_target_data_r, num_classes)

        return labels, rois, bbox_targets_h, bbox_targets_r, target_gt_h, target_gt_r
    def anchor_target_layer(self,
                            gt_boxes,
                            img_shape,
                            all_anchors,
                            is_restrict_bg=False):
        """Same as the anchor target layer in original Fast/er RCNN """

        total_anchors = all_anchors.shape[0]
        img_h, img_w = img_shape[1], img_shape[2]
        gt_boxes = gt_boxes[:, :-1]  # remove class label

        # allow boxes to sit over the edge by a small amount
        _allowed_border = 0

        # only keep anchors inside the image
        if self.cfgs.IS_FILTER_OUTSIDE_BOXES:
            inds_inside = np.where(
                (all_anchors[:, 0] >= -_allowed_border)
                & (all_anchors[:, 1] >= -_allowed_border)
                & (all_anchors[:, 2] < img_w + _allowed_border) &  # width
                (all_anchors[:, 3] < img_h + _allowed_border)  # height
            )[0]
        else:
            inds_inside = range(all_anchors.shape[0])

        anchors = all_anchors[inds_inside, :]

        # label: 1 is positive, 0 is negative, -1 is dont care
        labels = np.empty((len(inds_inside), ), dtype=np.float32)
        labels.fill(-1)

        # overlaps between the anchors and the gt boxes
        overlaps = bbox_overlaps(
            np.ascontiguousarray(anchors, dtype=np.float),
            np.ascontiguousarray(gt_boxes, dtype=np.float))

        argmax_overlaps = overlaps.argmax(axis=1)
        max_overlaps = overlaps[np.arange(len(inds_inside)), argmax_overlaps]
        gt_argmax_overlaps = overlaps.argmax(axis=0)
        gt_max_overlaps = overlaps[gt_argmax_overlaps,
                                   np.arange(overlaps.shape[1])]
        gt_argmax_overlaps = np.where(overlaps == gt_max_overlaps)[0]

        if not self.cfgs.TRAIN_RPN_CLOOBER_POSITIVES:
            labels[max_overlaps < self.cfgs.RPN_IOU_NEGATIVE_THRESHOLD] = 0

        labels[gt_argmax_overlaps] = 1
        labels[max_overlaps >= self.cfgs.RPN_IOU_POSITIVE_THRESHOLD] = 1

        if self.cfgs.TRAIN_RPN_CLOOBER_POSITIVES:
            labels[max_overlaps < self.cfgs.RPN_IOU_NEGATIVE_THRESHOLD] = 0

        num_fg = int(self.cfgs.RPN_MINIBATCH_SIZE *
                     self.cfgs.RPN_POSITIVE_RATE)
        fg_inds = np.where(labels == 1)[0]
        if len(fg_inds) > num_fg:
            disable_inds = npr.choice(fg_inds,
                                      size=(len(fg_inds) - num_fg),
                                      replace=False)
            labels[disable_inds] = -1

        num_bg = self.cfgs.RPN_MINIBATCH_SIZE - np.sum(labels == 1)
        if is_restrict_bg:
            num_bg = max(num_bg, num_fg * 1.5)
        bg_inds = np.where(labels == 0)[0]
        if len(bg_inds) > num_bg:
            disable_inds = npr.choice(bg_inds,
                                      size=(len(bg_inds) - num_bg),
                                      replace=False)
            labels[disable_inds] = -1

        bbox_targets = self._compute_targets(anchors,
                                             gt_boxes[argmax_overlaps, :])

        # map up to original set of anchors
        labels = self._unmap(labels, total_anchors, inds_inside, fill=-1)
        bbox_targets = self._unmap(bbox_targets,
                                   total_anchors,
                                   inds_inside,
                                   fill=0)

        # labels = labels.reshape((1, height, width, A))
        rpn_labels = labels.reshape((-1, 1))

        # bbox_targets
        bbox_targets = bbox_targets.reshape((-1, 4))
        rpn_bbox_targets = bbox_targets

        return rpn_labels, rpn_bbox_targets