kornia: warp_perspective produced wrong results with integer input

Describe the bug

got a runtime error when I test warp_perspective

Reproduction steps

run



def test_warp_perspective()        :
    import kornia as K
    import torch

    input = torch.zeros(1,3,1000,1000).cuda()

    print(input.shape)

    h, w = 64, 128  # destination size
    points_src = torch.tensor([[
    [100, 100], [200, 100], [200, 200], [100, 200],
    ]])

    # points_src = torch.tensor([[
    # [125., 150.], [562., 40.], [562., 282.], [54., 328.],
    # ]])

    points_dst = torch.tensor([[
        [0., 0.], [w - 1., 0.], [w - 1., h - 1.], [0., h - 1.],
    ]])
    print(points_src, points_dst)
    # compute perspective transform
    M: torch.tensor = K.get_perspective_transform(points_src, points_dst)

    print(input.shape, M)
    # warp the original image by the found transform
    img_warp: torch.tensor = K.warp_perspective(input, M.cuda(), dsize=(h, w))
    print(img_warp.shape)

if __name__ == "__main__":
    test_warp_perspective()


File “test2.py”, line 33, in <module> test_warp_perspective() File “test2.py”, line 29, in test_warp_perspective img_warp: torch.tensor = K.warp_perspective(input, M.cuda(), dsize=(h, w)) File “/usr/local/lib/python3.6/dist-packages/kornia/geometry/transform/imgwarp.py”, line 96, in warp_perspective dst_norm_trans_src_norm: torch.Tensor = normalize_homography(M, (H, W), (h_out, w_out)) # Bx3x3 File “/usr/local/lib/python3.6/dist-packages/kornia/geometry/transform/homography_warper.py”, line 378, in normalize_homography src_pix_trans_src_norm = _torch_inverse_cast(src_norm_trans_src_pix) File “/usr/local/lib/python3.6/dist-packages/kornia/utils/helpers.py”, line 50, in _torch_inverse_cast return torch.inverse(input.to(dtype)).to(input.dtype) RuntimeError: inverse_cuda: For batch 0: U(1,1) is zero, singular U.

Expected behavior

no RuntimeError

Environment

>>> torch.__version__
'1.9.1+cu102'
>>> kornia.__version__
'0.5.11'


### Additional context

_No response_

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 17 (13 by maintainers)

Most upvoted comments

@anirudhb11 looks like you found it - Please, send a PR to fix.

#1413

get_perspective_transform got wrong result when points_src is type of int

torch.int64 tensor([[[   1,    0, -127],
         [   0,    0,  -63],
         [   0,    0,    1]]])

when points_src is type of float: the result seems normal

         torch.float32 tensor([[[ 1.2700e+00,  0.0000e+00, -1.2700e+02],
         [-8.3447e-09,  6.3000e-01, -6.3000e+01],
         [ 5.0144e-10, -1.0169e-10,  1.0000e+00]]])

6.3000e-01 vs 0

my mistake. points_src should be float

points_src = torch.tensor([[
    [100., 100.], [200., 100.], [200., 200.], [100., 200.],
    ]])