File size: 22,605 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 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 |
import pytest
import torch
from torch.autograd import gradcheck
import kornia
import kornia.testing as utils # test utils
from kornia.testing import assert_close
@pytest.mark.parametrize("batch_size", [1, 2, 5])
def test_get_perspective_transform(batch_size, device, dtype):
# generate input data
h_max, w_max = 64, 32 # height, width
h = torch.ceil(h_max * torch.rand(batch_size, device=device, dtype=dtype))
w = torch.ceil(w_max * torch.rand(batch_size, device=device, dtype=dtype))
norm = torch.rand(batch_size, 4, 2, device=device, dtype=dtype)
points_src = torch.zeros_like(norm, device=device, dtype=dtype)
points_src[:, 1, 0] = h
points_src[:, 2, 1] = w
points_src[:, 3, 0] = h
points_src[:, 3, 1] = w
points_dst = points_src + norm
# compute transform from source to target
dst_homo_src = kornia.geometry.get_perspective_transform(points_src, points_dst)
assert_close(kornia.geometry.transform_points(dst_homo_src, points_src), points_dst, rtol=1e-4, atol=1e-4)
# compute gradient check
points_src = utils.tensor_to_gradcheck_var(points_src) # to var
points_dst = utils.tensor_to_gradcheck_var(points_dst) # to var
assert gradcheck(kornia.geometry.get_perspective_transform, (points_src, points_dst), raise_exception=True)
@pytest.mark.parametrize("batch_size", [1, 2, 5])
def test_rotation_matrix2d(batch_size, device, dtype):
# generate input data
center_base = torch.zeros(batch_size, 2, device=device, dtype=dtype)
angle_base = torch.ones(batch_size, device=device, dtype=dtype)
scale_base = torch.ones(batch_size, 2, device=device, dtype=dtype)
# 90 deg rotation
center = center_base
angle = 90.0 * angle_base
scale = scale_base
M = kornia.geometry.get_rotation_matrix2d(center, angle, scale)
for i in range(batch_size):
assert_close(M[i, 0, 0].item(), 0.0, rtol=1e-4, atol=1e-4)
assert_close(M[i, 0, 1].item(), 1.0, rtol=1e-4, atol=1e-4)
assert_close(M[i, 1, 0].item(), -1.0, rtol=1e-4, atol=1e-4)
assert_close(M[i, 1, 1].item(), 0.0, rtol=1e-4, atol=1e-4)
# 90 deg rotation + 2x scale
center = center_base
angle = 90.0 * angle_base
scale = 2.0 * scale_base
M = kornia.geometry.get_rotation_matrix2d(center, angle, scale)
for i in range(batch_size):
assert_close(M[i, 0, 0].item(), 0.0, rtol=1e-4, atol=1e-4)
assert_close(M[i, 0, 1].item(), 2.0, rtol=1e-4, atol=1e-4)
assert_close(M[i, 1, 0].item(), -2.0, rtol=1e-4, atol=1e-4)
assert_close(M[i, 1, 1].item(), 0.0, rtol=1e-4, atol=1e-4)
# 45 deg rotation
center = center_base
angle = 45.0 * angle_base
scale = scale_base
M = kornia.geometry.get_rotation_matrix2d(center, angle, scale)
for i in range(batch_size):
assert_close(M[i, 0, 0].item(), 0.7071)
assert_close(M[i, 0, 1].item(), 0.7071)
assert_close(M[i, 1, 0].item(), -0.7071)
assert_close(M[i, 1, 1].item(), 0.7071)
# evaluate function gradient
center = utils.tensor_to_gradcheck_var(center) # to var
angle = utils.tensor_to_gradcheck_var(angle) # to var
scale = utils.tensor_to_gradcheck_var(scale) # to var
assert gradcheck(kornia.geometry.get_rotation_matrix2d, (center, angle, scale), raise_exception=True)
class TestWarpAffine:
def test_smoke(self, device, dtype):
batch_size, channels, height, width = 1, 2, 3, 4
aff_ab = torch.eye(2, 3, device=device, dtype=dtype)[None] # 1x2x3
img_b = torch.rand(batch_size, channels, height, width, device=device, dtype=dtype)
img_a = kornia.geometry.warp_affine(img_b, aff_ab, (height, width))
assert_close(img_b, img_a)
@pytest.mark.parametrize("batch_shape", ([1, 3, 2, 5], [2, 4, 3, 4], [3, 5, 6, 2]))
@pytest.mark.parametrize("out_shape", ([2, 5], [3, 4], [6, 2]))
def test_cardinality(self, device, dtype, batch_shape, out_shape):
batch_size, channels, height, width = batch_shape
h_out, w_out = out_shape
aff_ab = torch.eye(2, 3, device=device, dtype=dtype).repeat(batch_size, 1, 1) # Bx2x3
img_b = torch.rand(batch_size, channels, height, width, device=device, dtype=dtype)
img_a = kornia.geometry.warp_affine(img_b, aff_ab, (h_out, w_out))
assert img_a.shape == (batch_size, channels, h_out, w_out)
def test_exception(self, device, dtype):
img = torch.rand(1, 2, 3, 4, device=device, dtype=dtype)
aff = torch.eye(2, 3, device=device, dtype=dtype)[None]
size = (4, 5)
with pytest.raises(TypeError):
assert kornia.geometry.warp_affine(0.0, aff, size)
with pytest.raises(TypeError):
assert kornia.geometry.warp_affine(img, 0.0, size)
with pytest.raises(ValueError):
img = torch.rand(2, 3, 4, device=device, dtype=dtype)
assert kornia.geometry.warp_affine(img, aff, size)
with pytest.raises(ValueError):
aff = torch.eye(2, 2, device=device, dtype=dtype)[None]
assert kornia.geometry.warp_affine(img, aff, size)
def test_translation(self, device, dtype):
offset = 1.0
h, w = 3, 4
aff_ab = torch.eye(2, 3, device=device, dtype=dtype)[None]
aff_ab[..., -1] += offset
img_b = torch.arange(float(h * w), device=device, dtype=dtype).view(1, 1, h, w)
expected = torch.zeros_like(img_b)
expected[..., 1:, 1:] = img_b[..., :2, :3]
# Same as opencv: cv2.warpAffine(kornia.tensor_to_image(img_b), aff_ab[0].numpy(), (w, h))
img_a = kornia.geometry.warp_affine(img_b, aff_ab, (h, w))
assert_close(img_a, expected)
def test_rotation_inverse(self, device, dtype):
h, w = 4, 4
img_b = torch.rand(1, 1, h, w, device=device, dtype=dtype)
# create rotation matrix of 90deg (anti-clockwise)
center = torch.tensor([[w - 1, h - 1]], device=device, dtype=dtype) / 2
scale = torch.ones((1, 2), device=device, dtype=dtype)
angle = 90.0 * torch.ones(1, device=device, dtype=dtype)
aff_ab = kornia.geometry.get_rotation_matrix2d(center, angle, scale)
# Same as opencv: cv2.getRotationMatrix2D(((w-1)/2,(h-1)/2), 90., 1.)
# warp the tensor
# Same as opencv: cv2.warpAffine(kornia.tensor_to_image(img_b), aff_ab[0].numpy(), (w, h))
img_a = kornia.geometry.warp_affine(img_b, aff_ab, (h, w))
# invert the transform
aff_ba = kornia.geometry.conversions.convert_affinematrix_to_homography(aff_ab).inverse()[..., :2, :]
img_b_hat = kornia.geometry.warp_affine(img_a, aff_ba, (h, w))
assert_close(img_b_hat, img_b, atol=1e-3, rtol=1e-3)
def test_jit(self, device, dtype):
aff_ab = torch.eye(2, 3, device=device, dtype=dtype)[None]
img = torch.rand(1, 2, 3, 4, device=device, dtype=dtype)
args = (img, aff_ab, (4, 5))
op = kornia.geometry.warp_affine
op_jit = torch.jit.script(op)
assert_close(op(*args), op_jit(*args))
def test_gradcheck(self, device, dtype):
batch_size, channels, height, width = 1, 2, 3, 4
aff_ab = torch.eye(2, 3, device=device, dtype=dtype)[None] + 1e-6 # 1x2x3
img_b = torch.rand(batch_size, channels, height, width, device=device, dtype=dtype)
aff_ab = utils.tensor_to_gradcheck_var(aff_ab) # to var
img_b = utils.tensor_to_gradcheck_var(img_b) # to var
assert gradcheck(kornia.geometry.warp_affine, (img_b, aff_ab, (height, width)), raise_exception=True)
class TestWarpPerspective:
def test_smoke(self, device, dtype):
batch_size, channels, height, width = 1, 2, 3, 4
img_b = torch.rand(batch_size, channels, height, width, device=device, dtype=dtype)
H_ab = kornia.eye_like(3, img_b)
img_a = kornia.geometry.warp_perspective(img_b, H_ab, (height, width))
assert_close(img_b, img_a)
@pytest.mark.parametrize("batch_shape", ([1, 3, 2, 5], [2, 4, 3, 4], [3, 5, 6, 2]))
@pytest.mark.parametrize("out_shape", ([2, 5], [3, 4], [6, 2]))
def test_cardinality(self, device, dtype, batch_shape, out_shape):
batch_size, channels, height, width = batch_shape
h_out, w_out = out_shape
img_b = torch.rand(batch_size, channels, height, width, device=device, dtype=dtype)
H_ab = kornia.eye_like(3, img_b)
img_a = kornia.geometry.warp_perspective(img_b, H_ab, (h_out, w_out))
assert img_a.shape == (batch_size, channels, h_out, w_out)
def test_exception(self, device, dtype):
img = torch.rand(1, 2, 3, 4, device=device, dtype=dtype)
homo = torch.eye(3, device=device, dtype=dtype)[None]
size = (4, 5)
with pytest.raises(TypeError):
assert kornia.geometry.warp_perspective(0.0, homo, size)
with pytest.raises(TypeError):
assert kornia.geometry.warp_perspective(img, 0.0, size)
with pytest.raises(ValueError):
img = torch.rand(2, 3, 4, device=device, dtype=dtype)
assert kornia.geometry.warp_perspective(img, homo, size)
with pytest.raises(ValueError):
homo = torch.eye(2, 2, device=device, dtype=dtype)[None]
assert kornia.geometry.warp_perspective(img, homo, size)
def test_translation(self, device, dtype):
offset = 1.0
h, w = 3, 4
img_b = torch.arange(float(h * w), device=device, dtype=dtype).view(1, 1, h, w)
homo_ab = kornia.eye_like(3, img_b)
homo_ab[..., :2, -1] += offset
expected = torch.zeros_like(img_b)
expected[..., 1:, 1:] = img_b[..., :2, :3]
# Same as opencv: cv2.warpPerspective(kornia.tensor_to_image(img_b), homo_ab[0].numpy(), (w, h))
img_a = kornia.geometry.warp_perspective(img_b, homo_ab, (h, w))
assert_close(img_a, expected, atol=1e-4, rtol=1e-4)
def test_rotation_inverse(self, device, dtype):
h, w = 4, 4
img_b = torch.rand(1, 1, h, w, device=device, dtype=dtype)
# create rotation matrix of 90deg (anti-clockwise)
center = torch.tensor([[w - 1, h - 1]], device=device, dtype=dtype) / 2
scale = torch.ones((1, 2), device=device, dtype=dtype)
angle = 90.0 * torch.ones(1, device=device, dtype=dtype)
aff_ab = kornia.geometry.get_rotation_matrix2d(center, angle, scale)
# Same as opencv: cv2.getRotationMatrix2D(((w-1)/2,(h-1)/2), 90., 1.)
H_ab = kornia.geometry.convert_affinematrix_to_homography(aff_ab) # Bx3x3
# warp the tensor
# Same as opencv: cv2.warpPerspecive(kornia.tensor_to_image(img_b), H_ab[0].numpy(), (w, h))
img_a = kornia.geometry.warp_perspective(img_b, H_ab, (h, w))
# invert the transform
H_ba = torch.inverse(H_ab)
img_b_hat = kornia.geometry.warp_perspective(img_a, H_ba, (h, w))
assert_close(img_b_hat, img_b, rtol=1e-4, atol=1e-4)
@pytest.mark.parametrize("batch_size", [1, 5])
@pytest.mark.parametrize("channels", [1, 5])
def test_crop(self, batch_size, channels, device, dtype):
# generate input data
src_h, src_w = 3, 3
dst_h, dst_w = 3, 3
# [x, y] origin
# top-left, top-right, bottom-right, bottom-left
points_src = torch.tensor(
[[[0, 0], [0, src_w - 1], [src_h - 1, src_w - 1], [src_h - 1, 0]]], device=device, dtype=dtype
)
# [x, y] destination
# top-left, top-right, bottom-right, bottom-left
points_dst = torch.tensor(
[[[0, 0], [0, dst_w - 1], [dst_h - 1, dst_w - 1], [dst_h - 1, 0]]], device=device, dtype=dtype
)
# compute transformation between points
dst_trans_src = kornia.geometry.get_perspective_transform(points_src, points_dst).expand(batch_size, -1, -1)
# warp tensor
patch = torch.tensor(
[[[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]]], device=device, dtype=dtype
).expand(batch_size, channels, -1, -1)
expected = patch[..., :3, :3]
# warp and assert
patch_warped = kornia.geometry.warp_perspective(patch, dst_trans_src, (dst_h, dst_w))
assert_close(patch_warped, expected)
@pytest.mark.parametrize("batch_size", [1, 5])
def test_crop_src_dst_type_mismatch(self, device, dtype, batch_size):
# generate input data
src_h, src_w = 3, 3
dst_h, dst_w = 3, 3
# [x, y] origin
# top-left, top-right, bottom-right, bottom-left
points_src = torch.tensor(
[[[0, 0], [0, src_w - 1], [src_h - 1, src_w - 1], [src_h - 1, 0]]], device=device, dtype=torch.int64
)
# [x, y] destination
# top-left, top-right, bottom-right, bottom-left
points_dst = torch.tensor(
[[[0, 0], [0, dst_w - 1], [dst_h - 1, dst_w - 1], [dst_h - 1, 0]]], device=device, dtype=dtype
)
# compute transformation between points
with pytest.raises(TypeError):
kornia.geometry.get_perspective_transform(points_src, points_dst).expand(batch_size, -1, -1)
def test_crop_center_resize(self, device, dtype):
# generate input data
dst_h, dst_w = 4, 4
# [x, y] origin
# top-left, top-right, bottom-right, bottom-left
points_src = torch.tensor([[[1, 1], [1, 2], [2, 2], [2, 1]]], device=device, dtype=dtype)
# [x, y] destination
# top-left, top-right, bottom-right, bottom-left
points_dst = torch.tensor(
[[[0, 0], [0, dst_w - 1], [dst_h - 1, dst_w - 1], [dst_h - 1, 0]]], device=device, dtype=dtype
)
# compute transformation between points
dst_trans_src = kornia.geometry.get_perspective_transform(points_src, points_dst)
# warp tensor
patch = torch.tensor(
[[[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]]], device=device, dtype=dtype
)
expected = torch.tensor(
[
[
[
[6.0000, 6.3333, 6.6667, 7.0000],
[7.3333, 7.6667, 8.0000, 8.3333],
[8.6667, 9.0000, 9.3333, 9.6667],
[10.0000, 10.3333, 10.6667, 11.0000],
]
]
],
device=device,
dtype=dtype,
)
# warp and assert
patch_warped = kornia.geometry.warp_perspective(patch, dst_trans_src, (dst_h, dst_w))
assert_close(patch_warped, expected)
def test_jit(self, device, dtype):
img = torch.rand(1, 2, 3, 4, device=device, dtype=dtype)
H_ab = kornia.eye_like(3, img)
args = (img, H_ab, (4, 5))
op = kornia.geometry.warp_perspective
op_jit = torch.jit.script(op)
assert_close(op(*args), op_jit(*args))
def test_gradcheck(self, device, dtype):
batch_size, channels, height, width = 1, 2, 3, 4
img_b = torch.rand(batch_size, channels, height, width, device=device, dtype=dtype)
H_ab = kornia.eye_like(3, img_b)
img_b = utils.tensor_to_gradcheck_var(img_b) # to var
# TODO(dmytro/edgar): firgure out why gradient don't propagate for the tranaform
H_ab = utils.tensor_to_gradcheck_var(H_ab, requires_grad=False) # to var
assert gradcheck(kornia.geometry.warp_perspective, (img_b, H_ab, (height, width)), raise_exception=True)
class TestRemap:
def test_smoke(self, device, dtype):
height, width = 3, 4
input = torch.ones(1, 1, height, width, device=device, dtype=dtype)
grid = kornia.utils.create_meshgrid(height, width, normalized_coordinates=False, device=device).to(dtype)
input_warped = kornia.geometry.remap(input, grid[..., 0], grid[..., 1], align_corners=True)
assert_close(input, input_warped, rtol=1e-4, atol=1e-4)
def test_shift(self, device, dtype):
height, width = 3, 4
inp = torch.tensor(
[[[[1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0]]]], device=device, dtype=dtype
)
expected = torch.tensor(
[[[[1.0, 1.0, 1.0, 0.0], [1.0, 1.0, 1.0, 0.0], [0.0, 0.0, 0.0, 0.0]]]], device=device, dtype=dtype
)
grid = kornia.utils.create_meshgrid(height, width, normalized_coordinates=False, device=device).to(dtype)
grid += 1.0 # apply shift in both x/y direction
input_warped = kornia.geometry.remap(inp, grid[..., 0], grid[..., 1], align_corners=True)
assert_close(input_warped, expected, rtol=1e-4, atol=1e-4)
def test_shift_batch(self, device, dtype):
height, width = 3, 4
inp = torch.tensor(
[[[[1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0]]]], device=device, dtype=dtype
).repeat(2, 1, 1, 1)
expected = torch.tensor(
[
[[[1.0, 1.0, 1.0, 0.0], [1.0, 1.0, 1.0, 0.0], [1.0, 1.0, 1.0, 0.0]]],
[[[1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0], [0.0, 0.0, 0.0, 0.0]]],
],
device=device,
dtype=dtype,
)
# generate a batch of grids
grid = kornia.utils.create_meshgrid(height, width, normalized_coordinates=False, device=device).to(dtype)
grid = grid.repeat(2, 1, 1, 1)
grid[0, ..., 0] += 1.0 # apply shift in the x direction
grid[1, ..., 1] += 1.0 # apply shift in the y direction
input_warped = kornia.geometry.remap(inp, grid[..., 0], grid[..., 1], align_corners=True)
assert_close(input_warped, expected, rtol=1e-4, atol=1e-4)
def test_shift_batch_broadcast(self, device, dtype):
height, width = 3, 4
inp = torch.tensor(
[[[[1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0]]]], device=device, dtype=dtype
).repeat(2, 1, 1, 1)
expected = torch.tensor(
[[[[1.0, 1.0, 1.0, 0.0], [1.0, 1.0, 1.0, 0.0], [0.0, 0.0, 0.0, 0.0]]]], device=device, dtype=dtype
).repeat(2, 1, 1, 1)
grid = kornia.utils.create_meshgrid(height, width, normalized_coordinates=False, device=device).to(dtype)
grid += 1.0 # apply shift in both x/y direction
input_warped = kornia.geometry.remap(inp, grid[..., 0], grid[..., 1], align_corners=True)
assert_close(input_warped, expected, rtol=1e-4, atol=1e-4)
def test_normalized_coordinates(self, device, dtype):
height, width = 3, 4
normalized_coordinates = True
inp = torch.tensor(
[[[[1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0]]]], device=device, dtype=dtype
).repeat(2, 1, 1, 1)
expected = torch.tensor(
[[[[1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0]]]], device=device, dtype=dtype
).repeat(2, 1, 1, 1)
grid = kornia.utils.create_meshgrid(
height, width, normalized_coordinates=normalized_coordinates, device=device
).to(dtype)
# Normalized input coordinates
input_warped = kornia.geometry.remap(
inp, grid[..., 0], grid[..., 1], align_corners=True, normalized_coordinates=normalized_coordinates
)
assert_close(input_warped, expected, rtol=1e-4, atol=1e-4)
def test_gradcheck(self, device, dtype):
batch_size, channels, height, width = 1, 2, 3, 4
img = torch.rand(batch_size, channels, height, width, device=device, dtype=dtype)
img = utils.tensor_to_gradcheck_var(img) # to var
grid = kornia.utils.create_meshgrid(height, width, normalized_coordinates=False, device=device).to(dtype)
grid = utils.tensor_to_gradcheck_var(grid, requires_grad=False) # to var
assert gradcheck(
kornia.geometry.remap, (img, grid[..., 0], grid[..., 1], 'bilinear', 'zeros', True), raise_exception=True
)
def test_jit(self, device, dtype):
batch_size, channels, height, width = 1, 1, 3, 4
img = torch.ones(batch_size, channels, height, width, device=device, dtype=dtype)
grid = kornia.utils.create_meshgrid(height, width, normalized_coordinates=False, device=device).to(dtype)
grid += 1.0 # apply some shift
op = kornia.geometry.remap
op_script = torch.jit.script(op)
inputs = (img, grid[..., 0], grid[..., 1], 'bilinear', 'zeros', True)
actual = op_script(*inputs)
expected = op(*inputs)
assert_close(actual, expected, rtol=1e-4, atol=1e-4)
class TestInvertAffineTransform:
def test_smoke(self, device, dtype):
matrix = torch.eye(2, 3, device=device, dtype=dtype)[None]
matrix_inv = kornia.geometry.invert_affine_transform(matrix)
assert_close(matrix, matrix_inv, rtol=1e-4, atol=1e-4)
def test_rot90(self, device, dtype):
angle = torch.tensor([90.0], device=device, dtype=dtype)
scale = torch.tensor([[1.0, 1.0]], device=device, dtype=dtype)
center = torch.tensor([[0.0, 0.0]], device=device, dtype=dtype)
expected = torch.tensor([[[0.0, -1.0, 0.0], [1.0, 0.0, 0.0]]], device=device, dtype=dtype)
matrix = kornia.geometry.get_rotation_matrix2d(center, angle, scale)
matrix_inv = kornia.geometry.invert_affine_transform(matrix)
assert_close(matrix_inv, expected, rtol=1e-4, atol=1e-4)
def test_rot90_batch(self, device, dtype):
angle = torch.tensor([90.0], device=device, dtype=dtype)
scale = torch.tensor([[1.0, 1.0]], device=device, dtype=dtype)
center = torch.tensor([[0.0, 0.0]], device=device, dtype=dtype)
expected = torch.tensor([[[0.0, -1.0, 0.0], [1.0, 0.0, 0.0]]], device=device, dtype=dtype).repeat(2, 1, 1)
matrix = kornia.geometry.get_rotation_matrix2d(center, angle, scale).repeat(2, 1, 1)
matrix_inv = kornia.geometry.invert_affine_transform(matrix)
assert_close(matrix_inv, expected, rtol=1e-4, atol=1e-4)
def test_gradcheck(self, device, dtype):
matrix = torch.eye(2, 3, device=device, dtype=dtype)[None]
matrix = utils.tensor_to_gradcheck_var(matrix) # to var
assert gradcheck(kornia.geometry.invert_affine_transform, (matrix,), raise_exception=True)
def test_jit(self, device, dtype):
op = kornia.geometry.invert_affine_transform
op_script = torch.jit.script(op)
matrix = torch.eye(2, 3, device=device, dtype=dtype)[None]
actual = op_script(matrix)
expected = op(matrix)
assert_close(actual, expected, rtol=1e-4, atol=1e-4)
|