示例#1
0
def test_warp_perspective():
    inp_shape = (1, 1, 4, 4)
    x = tensor(np.arange(16, dtype=np.float32).reshape(inp_shape))
    M_shape = (1, 3, 3)
    # M defines a translation: dst(1, 1, h, w) = rst(1, 1, h+1, w+1)
    M = tensor(
        np.array([[1.0, 0.0, 1.0], [0.0, 1.0, 1.0], [0.0, 0.0, 1.0]],
                 dtype=np.float32).reshape(M_shape))
    outp = F.warp_perspective(x, M, (2, 2))
    np.testing.assert_equal(
        outp.numpy(), np.array([[[[5.0, 6.0], [9.0, 10.0]]]],
                               dtype=np.float32))
    def _get_transformed_image(self, image, mat3x3):
        """apply perspective transform to the image
        note: there is NO need to guarantee the bottom right element equals 1

        Args:
            image (Tensor): input images (shape: n * 3 * 112 * 112)
            mat3x3 (Tensor): perspective matrix (shape: n * 3 * 3)

        Returns:
            transformed_image (Tensor): perspectively transformed image
        """
        s = self.input_size
        transformed_image = F.warp_perspective(image, mat3x3, [s, s])
        return transformed_image
示例#3
0
 def forward(self, data, quad):
     """
     data: (1, 3, 48, 160)
     quad: (1, 4, 2)
     """
     N = quad.shape[0]
     dst = F.repeat(self.bb_out, N, axis=0).reshape(-1, 4, 2)
     I = F.broadcast_to(self.I, quad.shape)
     A = F.broadcast_to(self.A, (N, 8, 8))
     A[:, 0:4, 0:2] = quad
     A[:, 4:8, 5:6] = I[:, :, 0:1]
     A[:, 0:4, 6:8] = -quad * dst[:, :, 0:1]
     A[:, 4:8, 3:5] = quad
     A[:, 0:4, 2:3] = I[:, :, 0:1]
     A[:, 4:8, 6:8] = -quad * dst[:, :, 1:2]
     B = dst.transpose(0, 2, 1).reshape(-1, 8, 1)
     M = F.concat([F.matmul(F.matinv(A), B)[:, :, 0], I[:, 0:1, 0]],
                  axis=1).reshape(-1, 3, 3)
     new_data = F.warp_perspective(data, M, (48, 160))  # (N, 3, 48, 160)
     return {"data": new_data}
示例#4
0
 def forward(self, data, idx, roi):
     N, H, W, C = data.shape
     xmax = roi[:, 1, 0]
     xmin = roi[:, 0, 0]
     ymax = roi[:, 1, 1]
     ymin = roi[:, 0, 1]
     scale = F.maximum((xmax - xmin) / W, (ymax - ymin) / H)
     I = F.broadcast_to(self.I, (N, ))
     M = F.broadcast_to(self.M, (N, 3, 3))
     M[:, 0, 0] = scale
     M[:, 0, 2] = xmin
     M[:, 1, 1] = scale
     M[:, 1, 2] = ymin
     M[:, 2, 2] = I
     resized = (F.warp_perspective(data,
                                   M, (H, W),
                                   mat_idx=idx,
                                   border_mode="CONSTANT",
                                   format="NHWC").transpose(
                                       0, 3, 1, 2).astype(np.float32))
     return resized
示例#5
0
 def f(x, M):
     out = F.warp_perspective(x, M, (2, 2))
     np.testing.assert_equal(out.shape.numpy(), np.array([1, 1, 2, 2]))
     return out