File size: 20,514 Bytes
36c95ba |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 |
import pytest
import torch
from torch.autograd import gradcheck
import kornia
import kornia.testing as utils # test utils
from kornia.testing import assert_close
from kornia.utils.helpers import _torch_inverse_cast
class TestHomographyWarper:
num_tests = 10
threshold = 0.1
def test_identity(self, device, dtype):
# create input data
height, width = 2, 5
patch_src = torch.rand(1, 1, height, width, device=device, dtype=dtype)
dst_homo_src = utils.create_eye_batch(batch_size=1, eye_size=3, device=device, dtype=dtype)
# instantiate warper
warper = kornia.geometry.transform.HomographyWarper(height, width, align_corners=True)
# warp from source to destination
patch_dst = warper(patch_src, dst_homo_src)
assert_close(patch_src, patch_dst)
@pytest.mark.parametrize("batch_size", [1, 3])
def test_normalize_homography_identity(self, batch_size, device, dtype):
# create input data
height, width = 2, 5
dst_homo_src = utils.create_eye_batch(batch_size=batch_size, eye_size=3, device=device, dtype=dtype)
res = torch.tensor([[[0.5, 0.0, -1.0], [0.0, 2.0, -1.0], [0.0, 0.0, 1.0]]], device=device, dtype=dtype)
assert (
kornia.geometry.transform.normal_transform_pixel(height, width, device=device, dtype=dtype) == res
).all()
norm_homo = kornia.geometry.transform.normalize_homography(dst_homo_src, (height, width), (height, width))
assert (norm_homo == dst_homo_src).all()
# change output scale
norm_homo = kornia.geometry.transform.normalize_homography(
dst_homo_src, (height, width), (height * 2, width // 2)
)
res = torch.tensor(
[[[4.0, 0.0, 3.0], [0.0, 1 / 3, -2 / 3], [0.0, 0.0, 1.0]]], device=device, dtype=dtype
).repeat(batch_size, 1, 1)
assert_close(norm_homo, res, atol=1e-4, rtol=1e-4)
@pytest.mark.parametrize("batch_size", [1, 3])
def test_denormalize_homography_identity(self, batch_size, device, dtype):
# create input data
height, width = 2, 5
dst_homo_src = utils.create_eye_batch(batch_size=batch_size, eye_size=3, device=device, dtype=dtype)
res = torch.tensor([[[0.5, 0.0, -1.0], [0.0, 2.0, -1.0], [0.0, 0.0, 1.0]]], device=device, dtype=dtype)
assert (
kornia.geometry.transform.normal_transform_pixel(height, width, device=device, dtype=dtype) == res
).all()
denorm_homo = kornia.geometry.transform.denormalize_homography(dst_homo_src, (height, width), (height, width))
assert (denorm_homo == dst_homo_src).all()
# change output scale
denorm_homo = kornia.geometry.transform.denormalize_homography(
dst_homo_src, (height, width), (height * 2, width // 2)
)
res = torch.tensor([[[0.25, 0.0, 0.0], [0.0, 3.0, 0], [0.0, 0.0, 1.0]]], device=device, dtype=dtype).repeat(
batch_size, 1, 1
)
assert_close(denorm_homo, res, atol=1e-4, rtol=1e-4)
@pytest.mark.parametrize("batch_size", [1, 3])
def test_normalize_homography_general(self, batch_size, device, dtype):
# create input data
height, width = 2, 5
dst_homo_src = torch.eye(3, device=device, dtype=dtype)
dst_homo_src[..., 0, 0] = 0.5
dst_homo_src[..., 1, 1] = 2.0
dst_homo_src[..., 0, 2] = 1.0
dst_homo_src[..., 1, 2] = 2.0
dst_homo_src = dst_homo_src.expand(batch_size, -1, -1)
norm_homo = kornia.geometry.transform.normalize_homography(dst_homo_src, (height, width), (height, width))
res = torch.tensor([[[0.5, 0.0, 0.0], [0.0, 2.0, 5.0], [0.0, 0.0, 1.0]]], device=device, dtype=dtype)
assert (norm_homo == res).all()
@pytest.mark.parametrize("batch_size", [1, 3])
def test_denormalize_homography_general(self, batch_size, device, dtype):
# create input data
height, width = 2, 5
dst_homo_src = torch.eye(3, device=device, dtype=dtype)
dst_homo_src[..., 0, 0] = 0.5
dst_homo_src[..., 1, 1] = 2.0
dst_homo_src[..., 0, 2] = 1.0
dst_homo_src[..., 1, 2] = 2.0
dst_homo_src = dst_homo_src.expand(batch_size, -1, -1)
denorm_homo = kornia.geometry.transform.denormalize_homography(dst_homo_src, (height, width), (height, width))
res = torch.tensor([[[0.5, 0.0, 3.0], [0.0, 2.0, 0.5], [0.0, 0.0, 1.0]]], device=device, dtype=dtype)
assert (denorm_homo == res).all()
@pytest.mark.parametrize("batch_size", [1, 3])
def test_consistency(self, batch_size, device, dtype):
# create input data
height, width = 2, 5
dst_homo_src = torch.eye(3, device=device, dtype=dtype)
dst_homo_src[..., 0, 0] = 0.5
dst_homo_src[..., 1, 1] = 2.0
dst_homo_src[..., 0, 2] = 1.0
dst_homo_src[..., 1, 2] = 2.0
dst_homo_src = dst_homo_src.expand(batch_size, -1, -1)
denorm_homo = kornia.geometry.transform.denormalize_homography(dst_homo_src, (height, width), (height, width))
norm_denorm_homo = kornia.geometry.transform.normalize_homography(denorm_homo, (height, width), (height, width))
assert (dst_homo_src == norm_denorm_homo).all()
norm_homo = kornia.geometry.transform.normalize_homography(dst_homo_src, (height, width), (height, width))
denorm_norm_homo = kornia.geometry.transform.denormalize_homography(norm_homo, (height, width), (height, width))
assert (dst_homo_src == denorm_norm_homo).all()
@pytest.mark.parametrize("offset", [1, 3, 7])
@pytest.mark.parametrize("shape", [(4, 5), (2, 6), (4, 3), (5, 7)])
def test_warp_grid_translation(self, shape, offset, device, dtype):
# create input data
height, width = shape
dst_homo_src = utils.create_eye_batch(batch_size=1, eye_size=3, device=device, dtype=dtype)
dst_homo_src[..., 0, 2] = offset # apply offset in x
grid = kornia.utils.create_meshgrid(height, width, normalized_coordinates=False)
flow = kornia.geometry.transform.warp_grid(grid, dst_homo_src)
# the grid the src plus the offset should be equal to the flow
# on the x-axis, y-axis remains the same.
assert_close(grid[..., 0].to(device=device, dtype=dtype) + offset, flow[..., 0])
assert_close(grid[..., 1].to(device=device, dtype=dtype), flow[..., 1])
@pytest.mark.parametrize("batch_shape", [(1, 1, 4, 5), (2, 2, 4, 6), (3, 1, 5, 7)])
def test_identity_resize(self, batch_shape, device, dtype):
# create input data
batch_size, channels, height, width = batch_shape
patch_src = torch.rand(batch_size, channels, height, width, device=device, dtype=dtype)
dst_homo_src = utils.create_eye_batch(batch_size, eye_size=3, device=device, dtype=dtype)
# instantiate warper warp from source to destination
warper = kornia.geometry.transform.HomographyWarper(height // 2, width // 2, align_corners=True)
patch_dst = warper(patch_src, dst_homo_src)
# check the corners
assert_close(patch_src[..., 0, 0], patch_dst[..., 0, 0], atol=1e-4, rtol=1e-4)
assert_close(patch_src[..., 0, -1], patch_dst[..., 0, -1], atol=1e-4, rtol=1e-4)
assert_close(patch_src[..., -1, 0], patch_dst[..., -1, 0], atol=1e-4, rtol=1e-4)
assert_close(patch_src[..., -1, -1], patch_dst[..., -1, -1], atol=1e-4, rtol=1e-4)
@pytest.mark.parametrize("shape", [(4, 5), (2, 6), (4, 3), (5, 7)])
def test_translation(self, shape, device, dtype):
# create input data
offset = 2.0 # in pixel
height, width = shape
patch_src = torch.rand(1, 1, height, width, device=device, dtype=dtype)
dst_homo_src = utils.create_eye_batch(batch_size=1, eye_size=3, device=device, dtype=dtype)
dst_homo_src[..., 0, 2] = offset / (width - 1) # apply offset in x
# instantiate warper and from source to destination
warper = kornia.geometry.transform.HomographyWarper(height, width, align_corners=True)
patch_dst = warper(patch_src, dst_homo_src)
assert_close(patch_src[..., 1:], patch_dst[..., :-1], atol=1e-4, rtol=1e-4)
@pytest.mark.parametrize("batch_shape", [(1, 1, 3, 5), (2, 2, 4, 3), (3, 1, 2, 3)])
def test_rotation(self, batch_shape, device, dtype):
# create input data
batch_size, channels, height, width = batch_shape
patch_src = torch.rand(batch_size, channels, height, width, device=device, dtype=dtype)
# rotation of 90deg
dst_homo_src = torch.eye(3, device=device, dtype=dtype)
dst_homo_src[..., 0, 0] = 0.0
dst_homo_src[..., 0, 1] = 1.0
dst_homo_src[..., 1, 0] = -1.0
dst_homo_src[..., 1, 1] = 0.0
dst_homo_src = dst_homo_src.expand(batch_size, -1, -1)
# instantiate warper and warp from source to destination
warper = kornia.geometry.transform.HomographyWarper(height, width, align_corners=True)
patch_dst = warper(patch_src, dst_homo_src)
# check the corners
assert_close(patch_src[..., 0, 0], patch_dst[..., 0, -1], atol=1e-4, rtol=1e-4)
assert_close(patch_src[..., 0, -1], patch_dst[..., -1, -1], atol=1e-4, rtol=1e-4)
assert_close(patch_src[..., -1, 0], patch_dst[..., 0, 0], atol=1e-4, rtol=1e-4)
assert_close(patch_src[..., -1, -1], patch_dst[..., -1, 0], atol=1e-4, rtol=1e-4)
@pytest.mark.parametrize("batch_size", [1, 2, 3])
def test_homography_warper(self, batch_size, device, dtype):
# generate input data
height, width = 128, 64
eye_size = 3 # identity 3x3
patch_src = torch.ones(batch_size, 1, height, width, device=device, dtype=dtype)
# create base homography
dst_homo_src = utils.create_eye_batch(batch_size, eye_size, device=device, dtype=dtype)
# instantiate warper
warper = kornia.geometry.transform.HomographyWarper(height, width, align_corners=True)
for _ in range(self.num_tests):
# generate homography noise
homo_delta = torch.rand_like(dst_homo_src) * 0.3
dst_homo_src_i = dst_homo_src + homo_delta
# transform the points from dst to ref
patch_dst = warper(patch_src, dst_homo_src_i)
patch_dst_to_src = warper(patch_dst, _torch_inverse_cast(dst_homo_src_i))
# same transform precomputing the grid
warper.precompute_warp_grid(_torch_inverse_cast(dst_homo_src_i))
patch_dst_to_src_precomputed = warper(patch_dst)
assert (patch_dst_to_src_precomputed == patch_dst_to_src).all()
# projected should be equal as initial
error = utils.compute_patch_error(patch_src, patch_dst_to_src, height, width)
assert error.item() < self.threshold
# check functional api
patch_dst_to_src_functional = kornia.geometry.transform.homography_warp(
patch_dst, _torch_inverse_cast(dst_homo_src_i), (height, width), align_corners=True
)
assert_close(patch_dst_to_src, patch_dst_to_src_functional, atol=1e-4, rtol=1e-4)
@pytest.mark.parametrize("batch_shape", [(1, 1, 7, 5), (2, 3, 8, 5), (1, 1, 7, 16)])
def test_gradcheck(self, batch_shape, device, dtype):
# generate input data
eye_size = 3 # identity 3x3
# create checkerboard
patch_src = torch.rand(batch_shape, device=device, dtype=dtype)
patch_src = utils.tensor_to_gradcheck_var(patch_src) # to var
# create base homography
batch_size, _, height, width = patch_src.shape
dst_homo_src = utils.create_eye_batch(batch_size, eye_size, device=device, dtype=dtype)
dst_homo_src = utils.tensor_to_gradcheck_var(dst_homo_src, requires_grad=False) # to var
# instantiate warper
warper = kornia.geometry.transform.HomographyWarper(height, width, align_corners=True)
# evaluate function gradient
assert gradcheck(warper, (patch_src, dst_homo_src), raise_exception=True)
@pytest.mark.parametrize("batch_size", [1, 2, 3])
@pytest.mark.parametrize("align_corners", [True, False])
@pytest.mark.parametrize("normalized_coordinates", [True, False])
def test_jit_warp_homography(self, batch_size, align_corners, normalized_coordinates, device, dtype):
# generate input data
height, width = 128, 64
eye_size = 3 # identity 3x3
patch_src = torch.rand(batch_size, 1, height, width, device=device, dtype=dtype)
# create base homography
dst_homo_src = utils.create_eye_batch(batch_size, eye_size, device=device, dtype=dtype)
for _ in range(self.num_tests):
# generate homography noise
homo_delta = torch.rand_like(dst_homo_src) * 0.3
dst_homo_src_i = dst_homo_src + homo_delta
# transform the points with and without jit
patch_dst = kornia.geometry.transform.homography_warp(
patch_src,
dst_homo_src_i,
(height, width),
align_corners=align_corners,
normalized_coordinates=normalized_coordinates,
)
patch_dst_jit = torch.jit.script(kornia.geometry.transform.homography_warp)(
patch_src,
dst_homo_src_i,
(height, width),
align_corners=align_corners,
normalized_coordinates=normalized_coordinates,
)
assert_close(patch_dst, patch_dst_jit, atol=1e-4, rtol=1e-4)
class TestHomographyNormalTransform:
expected_2d_0 = torch.tensor([[[0.5, 0.0, -1.0], [0.0, 2.0, -1.0], [0.0, 0.0, 1.0]]])
expected_2d_1 = torch.tensor([[[0.5, 0.0, -1.0], [0.0, 2e14, -1.0], [0.0, 0.0, 1.0]]])
expected_3d_0 = expected = torch.tensor(
[[[0.4, 0.0, 0.0, -1.0], [0.0, 2.0, 0.0, -1.0], [0.0, 0.0, 0.6667, -1.0], [0.0, 0.0, 0.0, 1.0]]]
)
expected_3d_1 = torch.tensor(
[[[0.4, 0.0, 0.0, -1.0], [0.0, 2e14, 0.0, -1.0], [0.0, 0.0, 0.6667, -1.0], [0.0, 0.0, 0.0, 1.0]]]
)
@pytest.mark.parametrize("height,width,expected", [(2, 5, expected_2d_0), (1, 5, expected_2d_1)])
def test_transform2d(self, height, width, expected, device, dtype):
output = kornia.geometry.transform.normal_transform_pixel(height, width, device=device, dtype=dtype)
assert_close(output, expected.to(device=device, dtype=dtype), atol=1e-4, rtol=1e-4)
@pytest.mark.parametrize("height", [1, 2, 5])
@pytest.mark.parametrize("width", [1, 2, 5])
def test_divide_by_zero2d(self, height, width, device, dtype):
output = kornia.geometry.transform.normal_transform_pixel(height, width, device=device, dtype=dtype)
assert torch.isinf(output).sum().item() == 0
def test_transform2d_apply(self, device, dtype):
height, width = 2, 5
input = torch.tensor([[0.0, 0.0], [width - 1, height - 1]], device=device, dtype=dtype)
expected = torch.tensor([[-1.0, -1.0], [1.0, 1.0]], device=device, dtype=dtype)
transform = kornia.geometry.transform.normal_transform_pixel(height, width, device=device, dtype=dtype)
output = kornia.geometry.linalg.transform_points(transform, input)
assert_close(output, expected.to(device=device, dtype=dtype), atol=1e-4, rtol=1e-4)
@pytest.mark.parametrize("height,width,depth,expected", [(2, 6, 4, expected_3d_0), (1, 6, 4, expected_3d_1)])
def test_transform3d(self, height, width, depth, expected, device, dtype):
output = kornia.geometry.transform.normal_transform_pixel3d(depth, height, width, device=device, dtype=dtype)
assert_close(output, expected.to(device=device, dtype=dtype), atol=1e-4, rtol=1e-4)
@pytest.mark.parametrize("height", [1, 2, 5])
@pytest.mark.parametrize("width", [1, 2, 5])
@pytest.mark.parametrize("depth", [1, 2, 5])
def test_divide_by_zero3d(self, height, width, depth, device, dtype):
output = kornia.geometry.transform.normal_transform_pixel3d(depth, height, width, device=device, dtype=dtype)
assert torch.isinf(output).sum().item() == 0
def test_transform3d_apply(self, device, dtype):
depth, height, width = 3, 2, 5
input = torch.tensor([[0.0, 0.0, 0.0], [width - 1, height - 1, depth - 1]], device=device, dtype=dtype)
expected = torch.tensor([[-1.0, -1.0, -1.0], [1.0, 1.0, 1.0]], device=device, dtype=dtype)
transform = kornia.geometry.transform.normal_transform_pixel3d(depth, height, width, device=device, dtype=dtype)
output = kornia.geometry.linalg.transform_points(transform, input)
assert_close(output, expected.to(device=device, dtype=dtype), atol=1e-4, rtol=1e-4)
class TestHomographyWarper3D:
num_tests = 10
threshold = 0.1
@pytest.mark.parametrize("batch_size", [1, 3])
def test_normalize_homography_identity(self, batch_size, device, dtype):
# create input data
input_shape = (4, 8, 5)
dst_homo_src = utils.create_eye_batch(batch_size=batch_size, eye_size=4).to(device=device, dtype=dtype)
res = torch.tensor(
[[[0.5000, 0.0, 0.0, -1.0], [0.0, 0.2857, 0.0, -1.0], [0.0, 0.0, 0.6667, -1.0], [0.0, 0.0, 0.0, 1.0]]],
device=device,
dtype=dtype,
)
norm = kornia.geometry.transform.normal_transform_pixel3d(input_shape[0], input_shape[1], input_shape[2]).to(
device=device, dtype=dtype
)
assert_close(norm, res, rtol=1e-4, atol=1e-4)
norm_homo = kornia.geometry.transform.normalize_homography3d(dst_homo_src, input_shape, input_shape).to(
device=device, dtype=dtype
)
assert_close(norm_homo, dst_homo_src, rtol=1e-4, atol=1e-4)
norm_homo = kornia.geometry.transform.normalize_homography3d(dst_homo_src, input_shape, input_shape).to(
device=device, dtype=dtype
)
assert_close(norm_homo, dst_homo_src, rtol=1e-4, atol=1e-4)
# change output scale
norm_homo = kornia.geometry.transform.normalize_homography3d(
dst_homo_src, input_shape, (input_shape[0] // 2, input_shape[1] * 2, input_shape[2] // 2)
).to(device=device, dtype=dtype)
res = torch.tensor(
[[[4.0, 0.0, 0.0, 3.0], [0.0, 0.4667, 0.0, -0.5333], [0.0, 0.0, 3.0, 2.0], [0.0, 0.0, 0.0, 1.0]]],
device=device,
dtype=dtype,
).repeat(batch_size, 1, 1)
assert_close(norm_homo, res, rtol=1e-4, atol=1e-4)
@pytest.mark.parametrize("batch_size", [1, 3])
def test_normalize_homography_general(self, batch_size, device, dtype):
# create input data
dst_homo_src = torch.eye(4, device=device, dtype=dtype)
dst_homo_src[..., 0, 0] = 0.5
dst_homo_src[..., 1, 1] = 0.5
dst_homo_src[..., 2, 2] = 2.0
dst_homo_src[..., 0, 3] = 1.0
dst_homo_src[..., 1, 3] = 2.0
dst_homo_src[..., 2, 3] = 3.0
dst_homo_src = dst_homo_src.expand(batch_size, -1, -1)
norm_homo = kornia.geometry.transform.normalize_homography3d(dst_homo_src, (2, 2, 5), (2, 2, 5))
res = torch.tensor(
[[[0.5, 0.0, 0.0, 0.0], [0.0, 0.5, 0.0, 3.5], [0.0, 0.0, 2.0, 7.0], [0.0, 0.0, 0.0, 1.0]]],
device=device,
dtype=dtype,
)
assert (norm_homo == res).all()
@pytest.mark.parametrize("offset", [1, 3, 7])
@pytest.mark.parametrize("shape", [(4, 5, 6), (2, 4, 6), (4, 3, 9), (5, 7, 8)])
def test_warp_grid_translation(self, shape, offset, device, dtype):
# create input data
depth, height, width = shape
dst_homo_src = utils.create_eye_batch(batch_size=1, eye_size=4, device=device, dtype=dtype)
dst_homo_src[..., 0, 3] = offset # apply offset in x
grid = kornia.utils.create_meshgrid3d(depth, height, width, normalized_coordinates=False)
flow = kornia.geometry.transform.warp_grid3d(grid, dst_homo_src)
# the grid the src plus the offset should be equal to the flow
# on the x-axis, y-axis remains the same.
assert_close(grid[..., 0].to(device=device, dtype=dtype) + offset, flow[..., 0], atol=1e-4, rtol=1e-4)
assert_close(grid[..., 1].to(device=device, dtype=dtype), flow[..., 1], atol=1e-4, rtol=1e-4)
assert_close(grid[..., 2].to(device=device, dtype=dtype), flow[..., 2], atol=1e-4, rtol=1e-4)
|