def test_bbox_corner2center(self):
        a1 = torch.FloatTensor([0, 0, 100, 100])
        b1 = torch.FloatTensor([50, 50, 100, 100])
        a2 = torch.FloatTensor([[0, 0, 100, 100]])
        b2 = torch.FloatTensor([[50, 50, 100, 100]])
        a3 = torch.FloatTensor([[50, 50, 100, 100]])
        b3 = torch.FloatTensor([[75, 75, 50, 50]])

        self.assertTrue(bbox_corner2center(a1).equal(b1))
        self.assertTrue(bbox_corner2center(a2).equal(b2))
        self.assertTrue(bbox_corner2center(a3).equal(b3))
Exemplo n.º 2
0
    def _refine_proposal(self, proposal, bbox_reg):
        """Refine proposal bbox with the result of bbox regression.
        
        Args:
            proposal(Tensor): (x1, y1, x2, y2), bbox proposal from RPN.
            bbox_reg(Tensor): (dx, dy, dw, dh), bbox regression value.

        Returns:
            bbox_refined(Tensor): (x1, y1, x2, y2)
        """

        x, y, w, h = bbox_corner2center(proposal).chunk(4)
        dx, dy, dw, dh = bbox_reg.chunk(4)
        px, py = w * dx + x, h * dy + y
        pw, ph = w * torch.exp(dw), h * torch.exp(dh)
        bbox_refined = bbox_center2corner(torch.cat([px, py, pw, ph]))

        px1, py1, px2, py2 = bbox_refined.chunk(4)
        px1 = torch.clamp(px1, max=self.img_width, min=0)
        px2 = torch.clamp(px2, max=self.img_width, min=0)
        py1 = torch.clamp(py1, max=self.img_height, min=0)
        py2 = torch.clamp(py2, max=self.img_height, min=0)

        bbox_refined = torch.cat([px1, py1, px2, py2])

        return bbox_refined
Exemplo n.º 3
0
    def _get_bbox_targets(proposals, gt_bboxes):
        """ Calculate bounding box targets, input coord format is (left, top, right, bottom),
            see R-CNN paper for the formula.

        Args:
            proposals(Tensor): [n, 4]
            gt_bboxes(Tensor): [n, 4]

        Returns:
            bbox_targets(Tensor): [n, 4]
        """
        proposals = bbox_corner2center(proposals)
        gt_bboxes = bbox_corner2center(gt_bboxes)
        xy = (gt_bboxes[:, :2] - proposals[:, :2]) / proposals[:, 2:]
        wh = torch.log(gt_bboxes[:, 2:] / proposals[:, 2:])
        x, y = xy.chunk(2, dim=1)
        w, h = wh.chunk(2, dim=1)
        bbox_targets = torch.cat([x, y, w, h], dim=1)

        return bbox_targets