def setUp(self):
        self.feat_stride = 16
        self.n_channels, self.width, self.height = 16, 14, 14
        self.x = np.arange(1 * self.n_channels * self.height * self.width,
                           dtype=np.float32)
        self.x = self.x.reshape(1, self.n_channels, self.height, self.width)
        self.im_info = np.array([[224, 224, 0.85]])
        self.anchor_target_layer = AnchorTargetLayer(self.feat_stride,
                                                     2**np.arange(1, 6))
        self.height, self.width = self.x.shape[2:]
        self.shifts = self.anchor_target_layer.generate_shifts(
            self.width, self.height)
        self.all_anchors, self.total_anchors = \
            self.anchor_target_layer.generate_proposals(self.shifts)
        self.inds_inside, self.anchors = self.anchor_target_layer.keep_inside(
            self.all_anchors, self.im_info)

        self.gt_boxes = np.array([[10, 10, 60, 200, 0], [50, 100, 210, 210, 1],
                                  [160, 40, 200, 70, 2]])
        gt_canvas = np.zeros((224, 224))
        for gt in self.gt_boxes:
            cv.rectangle(gt_canvas, (gt[0], gt[1]), (gt[2], gt[3]), 255)
        cv.imwrite('tests/gt_boxes.png', gt_canvas)

        self.argmax_overlaps, self.max_overlaps, self.gt_max_overlaps, \
            self.gt_argmax_overlaps = self.anchor_target_layer.calc_overlaps(
                self.anchors, self.gt_boxes, self.inds_inside)

        self.argmax_overlaps, self.labels = \
            self.anchor_target_layer.create_labels(
                self.inds_inside, self.anchors, self.gt_boxes)
    def test_generate_anchors(self):
        anchor_target_layer = AnchorTargetLayer()
        ret = np.array([[-83., -39., 100., 56.], [-175., -87., 192., 104.],
                        [-359., -183., 376., 200.], [-55., -55., 72., 72.],
                        [-119., -119., 136., 136.], [-247., -247., 264., 264.],
                        [-35., -79., 52., 96.], [-79., -167., 96., 184.],
                        [-167., -343., 184., 360.]]) - 1
        self.assertEqual(anchor_target_layer.anchors.shape, ret.shape)
        np.testing.assert_array_equal(anchor_target_layer.anchors, ret)

        ret = self.anchor_target_layer.anchors
        min_x = ret[:, 0].min()
        min_y = ret[:, 1].min()
        max_x = ret[:, 2].max()
        max_y = ret[:, 3].max()
        canvas = np.zeros(
            (int(abs(min_y) + max_y) + 1, int(abs(min_x) + max_x) + 1),
            dtype=np.uint8)
        ret[:, 0] -= min_x
        ret[:, 2] -= min_x
        ret[:, 1] -= min_y
        ret[:, 3] -= min_y
        for anchor in ret:
            anchor = list(six.moves.map(int, anchor))
            cv.rectangle(canvas, (anchor[0], anchor[1]),
                         (anchor[2], anchor[3]), 255)
        cv.imwrite('tests/anchors.png', canvas)
Beispiel #3
0
 def __init__(
         self, in_ch=512, out_ch=512, n_anchors=9, feat_stride=16,
         anchor_scales=[8, 16, 32], num_classes=21, rpn_sigma=3.0):
     super(RPN, self).__init__(
         rpn_conv_3x3=L.Convolution2D(in_ch, out_ch, 3, 1, 1),
         rpn_cls_score=L.Convolution2D(out_ch, 2 * n_anchors, 1, 1, 0),
         rpn_bbox_pred=L.Convolution2D(out_ch, 4 * n_anchors, 1, 1, 0)
     )
     self.anchor_target_layer = AnchorTargetLayer(feat_stride)
     self.proposal_layer = ProposalLayer(feat_stride, anchor_scales)
     self.rpn_sigma = rpn_sigma