Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

[Fix] gpu tests for crop3d and flip (#727)

Edgar Riba755dce732119
diff --git a/docs/source/geometry.transform.rst b/docs/source/geometry.transform.rst
index 824090ab4..113ef8066 100644
--- a/docs/source/geometry.transform.rst
+++ b/docs/source/geometry.transform.rst
@@ -6,9 +6,11 @@ kornia.geometry.transform
The functions in this section perform various geometrical transformations of 2D images.
.. autofunction:: warp_perspective
+.. autofunction:: warp_perspective3d
.. autofunction:: warp_affine
-.. autofunction:: warp_projective
+.. autofunction:: warp_affine3d
.. autofunction:: get_perspective_transform
+.. autofunction:: get_perspective_transform3d
.. autofunction:: get_projective_transform
.. autofunction:: get_rotation_matrix2d
.. autofunction:: get_shear_matrix2d
@@ -51,4 +53,4 @@ The functions in this section perform various geometrical transformations of 2D
.. autoclass:: Rot180
.. autoclass:: Resize
.. autoclass:: Rescale
-.. autoclass:: Affine
\ No newline at end of file
+.. autoclass:: Affine
diff --git a/kornia/geometry/transform/crop/crop3d.py b/kornia/geometry/transform/crop/crop3d.py
index 384d8ea52..16ab552e0 100644
--- a/kornia/geometry/transform/crop/crop3d.py
+++ b/kornia/geometry/transform/crop/crop3d.py
@@ -1,9 +1,9 @@
-from typing import Tuple, Union
+from typing import Tuple
import torch
from kornia.geometry.transform.projwarp import (
- get_perspective_transform3d, get_projective_transform, warp_affine3d
+ get_perspective_transform3d, warp_affine3d
)
__all__ = [
@@ -350,12 +350,12 @@ def infer_box_shape3d(boxes: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor,
"""
validate_bboxes3d(boxes)
- left = torch.index_select(boxes, 1, torch.tensor([1, 2, 5, 6]))[:, :, 0]
- right = torch.index_select(boxes, 1, torch.tensor([0, 3, 4, 7]))[:, :, 0]
+ left = torch.index_select(boxes, 1, torch.tensor([1, 2, 5, 6], device=boxes.device, dtype=torch.long))[:, :, 0]
+ right = torch.index_select(boxes, 1, torch.tensor([0, 3, 4, 7], device=boxes.device, dtype=torch.long))[:, :, 0]
widths = (left - right + 1)[:, 0]
- bot = torch.index_select(boxes, 1, torch.tensor([2, 3, 6, 7]))[:, :, 1]
- upper = torch.index_select(boxes, 1, torch.tensor([0, 1, 4, 5]))[:, :, 1]
+ bot = torch.index_select(boxes, 1, torch.tensor([2, 3, 6, 7], device=boxes.device, dtype=torch.long))[:, :, 1]
+ upper = torch.index_select(boxes, 1, torch.tensor([0, 1, 4, 5], device=boxes.device, dtype=torch.long))[:, :, 1]
heights = (bot - upper + 1)[:, 0]
depths = (boxes[:, 4:, 2] - boxes[:, :4, 2] + 1)[:, 0]
@@ -376,14 +376,14 @@ def validate_bboxes3d(boxes: torch.Tensor) -> None:
assert len(boxes.shape) == 3 and boxes.shape[1:] == torch.Size([8, 3]), \
f"Box shape must be (B, 8, 3). Got {boxes.shape}."
- left = torch.index_select(boxes, 1, torch.tensor([1, 2, 5, 6]))[:, :, 0]
- right = torch.index_select(boxes, 1, torch.tensor([0, 3, 4, 7]))[:, :, 0]
+ left = torch.index_select(boxes, 1, torch.tensor([1, 2, 5, 6], device=boxes.device, dtype=torch.long))[:, :, 0]
+ right = torch.index_select(boxes, 1, torch.tensor([0, 3, 4, 7], device=boxes.device, dtype=torch.long))[:, :, 0]
widths = (left - right + 1)
assert torch.allclose(widths.permute(1, 0), widths[:, 0]), \
f"Boxes must have be cube, while get different widths {widths}."
- bot = torch.index_select(boxes, 1, torch.tensor([2, 3, 6, 7]))[:, :, 1]
- upper = torch.index_select(boxes, 1, torch.tensor([0, 1, 4, 5]))[:, :, 1]
+ bot = torch.index_select(boxes, 1, torch.tensor([2, 3, 6, 7], device=boxes.device, dtype=torch.long))[:, :, 1]
+ upper = torch.index_select(boxes, 1, torch.tensor([0, 1, 4, 5], device=boxes.device, dtype=torch.long))[:, :, 1]
heights = (bot - upper + 1)
assert torch.allclose(heights.permute(1, 0), heights[:, 0]), \
f"Boxes must have be cube, while get different heights {heights}."
@@ -448,9 +448,15 @@ def bbox_to_mask3d(boxes: torch.Tensor, size: Tuple[int, int, int]) -> torch.Ten
mask_out = []
# TODO: Looking for a vectorized way
for m, box in zip(mask, boxes):
- m = m.index_fill(0, torch.arange(box[0, 2].item(), box[4, 2].item() + 1, dtype=torch.long), torch.tensor(1))
- m = m.index_fill(1, torch.arange(box[1, 1].item(), box[2, 1].item() + 1, dtype=torch.long), torch.tensor(1))
- m = m.index_fill(2, torch.arange(box[0, 0].item(), box[1, 0].item() + 1, dtype=torch.long), torch.tensor(1))
+ m = m.index_fill(0, torch.arange(
+ box[0, 2].item(), box[4, 2].item() + 1, device=box.device, dtype=torch.long
+ ), torch.tensor(1, device=box.device, dtype=box.dtype))
+ m = m.index_fill(1, torch.arange(
+ box[1, 1].item(), box[2, 1].item() + 1, device=box.device, dtype=torch.long
+ ), torch.tensor(1, device=box.device, dtype=box.dtype))
+ m = m.index_fill(2, torch.arange(
+ box[0, 0].item(), box[1, 0].item() + 1, device=box.device, dtype=torch.long
+ ), torch.tensor(1, device=box.device, dtype=box.dtype))
m = m.unsqueeze(dim=0)
m_out = torch.ones_like(m)
m_out = m_out * (m == 1).all(dim=2, keepdim=True).all(dim=1, keepdim=True)
diff --git a/kornia/geometry/transform/flips.py b/kornia/geometry/transform/flips.py
index d3e295f96..fd15b3b4b 100644
--- a/kornia/geometry/transform/flips.py
+++ b/kornia/geometry/transform/flips.py
@@ -124,7 +124,7 @@ def hflip(input: torch.Tensor) -> torch.Tensor:
"""
w = input.shape[-1]
- return input[..., torch.arange(w - 1, -1, -1)]
+ return input[..., torch.arange(w - 1, -1, -1, device=input.device)]
def vflip(input: torch.Tensor) -> torch.Tensor:
@@ -140,4 +140,4 @@ def vflip(input: torch.Tensor) -> torch.Tensor:
"""
h = input.shape[-2]
- return input[..., torch.arange(h - 1, -1, -1), :]
+ return input[..., torch.arange(h - 1, -1, -1, device=input.device), :]
diff --git a/kornia/geometry/transform/projwarp.py b/kornia/geometry/transform/projwarp.py
index 0ca2810c5..8cf3bca7d 100644
--- a/kornia/geometry/transform/projwarp.py
+++ b/kornia/geometry/transform/projwarp.py
@@ -220,6 +220,10 @@ def get_perspective_transform3d(src: torch.Tensor, dst: torch.Tensor) -> torch.T
if not (src.shape[0] == dst.shape[0]):
raise ValueError("Inputs must have same batch size dimension. Expect {} but got {}"
.format(src.shape, dst.shape))
+ assert src.device == dst.device and src.dtype == dst.dtype, (
+ f"Expect `src` and `dst` to be in the same device (Got {src.dtype}, {dst.dtype}) "
+ f"with the same dtype (Got {src.dtype}, {dst.dtype})."
+ )
# we build matrix A by using only 4 point correspondence. The linear
# system is solved with the least square method, so here
diff --git a/setup.py b/setup.py
index f234e5237..d29a4847b 100644
--- a/setup.py
+++ b/setup.py
@@ -25,6 +25,7 @@ version = '0.4.1' # this a tag for the current development version
# Once a pytorch version (in the future) breaks a kornia version, we could just
# add a maximal version.
kornia_pt_dependencies = {
+ '0.4.1': '>=1.6.0',
'0.4.0': '>=1.6.0,<1.7.0',
'0.3.2': '>=1.5.0,<1.6.0',
'0.3.1': '>=1.5.0',
diff --git a/test/augmentation/test_augmentation_3d.py b/test/augmentation/test_augmentation_3d.py
index bd548961d..efbdede7e 100644
--- a/test/augmentation/test_augmentation_3d.py
+++ b/test/augmentation/test_augmentation_3d.py
@@ -509,7 +509,6 @@ class TestRandomDepthicalFlip3D:
assert gradcheck(RandomDepthicalFlip3D(p=1.), (input, ), raise_exception=True)
[email protected](reason="need to recompute values")
class TestRandomRotation3D:
torch.manual_seed(0) # for random reproductibility
@@ -543,38 +542,38 @@ class TestRandomRotation3D:
[0., 0., 1., 2.]]]) # 3 x 4 x 4
input = input.to(device)
- expected = torch.tensor([[[[[9.9412e-01, 0.0000e+00, 8.5407e-03, 1.9535e+00],
- [1.7328e-05, 3.8945e-03, 1.5617e-02, 1.0553e-04],
- [0.0000e+00, 9.8295e-01, 1.9789e+00, 4.9531e-02],
- [0.0000e+00, 0.0000e+00, 9.7034e-01, 1.9548e+00]],
- [[9.6646e-01, 0.0000e+00, 4.3866e-02, 1.9559e+00],
- [1.1586e-02, 0.0000e+00, 1.0260e-04, 0.0000e+00],
- [4.4472e-03, 9.9659e-01, 1.9833e+00, 3.4181e-05],
- [0.0000e+00, 7.8456e-03, 9.9959e-01, 1.9956e+00]],
- [[9.3772e-01, 0.0000e+00, 7.8179e-02, 1.8983e+00],
- [2.2707e-02, 0.0000e+00, 9.6477e-04, 2.2624e-02],
- [2.1575e-02, 9.9975e-01, 1.9243e+00, 0.0000e+00],
- [3.1300e-04, 3.2790e-02, 1.0249e+00, 1.9474e+00]]]]])
+ expected = torch.tensor([[[[[0.0000, 0.0000, 0.6810, 0.5250],
+ [0.5052, 0.0000, 0.0000, 0.0613],
+ [0.1159, 0.1072, 0.5324, 0.0870],
+ [0.0000, 0.0000, 0.1927, 0.0000]],
+ [[0.0000, 0.1683, 0.6963, 0.1131],
+ [0.0566, 0.0000, 0.5215, 0.2796],
+ [0.0694, 0.6039, 1.4519, 1.1240],
+ [0.0000, 0.1325, 0.1542, 0.2510]],
+ [[0.0000, 0.2054, 0.0000, 0.0000],
+ [0.0026, 0.6088, 0.7358, 0.2319],
+ [0.1262, 1.0830, 1.3687, 1.4940],
+ [0.0000, 0.0416, 0.2012, 0.3124]]]]])
expected = expected.to(device)
- expected_transform = torch.tensor([[[0.7168, 0.5830, 0.3825, -1.1651],
- [-0.5853, 0.8012, -0.1242, 1.0699],
- [-0.3789, -0.1349, 0.9155, 0.7079],
+ expected_transform = torch.tensor([[[0.6523, 0.3666, -0.6635, 0.6352],
+ [-0.6185, 0.7634, -0.1862, 1.4689],
+ [0.4382, 0.5318, 0.7247, -1.1797],
[0.0000, 0.0000, 0.0000, 1.0000]]])
expected_transform = expected_transform.to(device)
- expected_2 = torch.tensor([[[[[0.5337, 0.0176, 0.0066, 0.2952],
- [0.1152, 0.1210, 0.7456, 1.4092],
- [0.0000, 0.0000, 0.3873, 0.5115],
- [0.0000, 0.0000, 0.0000, 0.0000]],
- [[0.1647, 1.5763, 0.5870, 0.0000],
- [0.0000, 0.2845, 0.0000, 0.0000],
- [0.0000, 0.5602, 0.6448, 0.2305],
- [0.3809, 1.3558, 1.5374, 1.5583]],
- [[0.0000, 0.0000, 0.0000, 0.0000],
- [0.4242, 0.0000, 0.0000, 0.0000],
- [1.4400, 0.1584, 0.0000, 0.0000],
- [0.0946, 0.0000, 0.0000, 0.0000]]]]])
+ expected_2 = torch.tensor([[[[[0.0000, 0.4771, 0.0243, 0.0000],
+ [0.0000, 0.1652, 0.0000, 0.6771],
+ [0.1668, 1.1430, 0.7131, 0.2692],
+ [0.0285, 0.7100, 0.6012, 0.0000]],
+ [[0.0000, 0.3068, 0.0000, 0.0000],
+ [0.0000, 0.3175, 0.0000, 0.6602],
+ [0.1330, 1.1962, 0.9750, 0.0000],
+ [0.0648, 0.9818, 0.9785, 0.0000]],
+ [[0.0000, 0.1136, 0.0000, 0.0000],
+ [0.0518, 0.4617, 0.0000, 0.4928],
+ [0.0407, 1.0954, 1.1413, 0.0000],
+ [0.0587, 0.8768, 1.1815, 0.0000]]]]])
expected_2 = expected_2.to(device)
out, mat = f(input)
@@ -602,41 +601,41 @@ class TestRandomRotation3D:
[0., 0., 1., 2.]]]]) # 1 x 1 x 4 x 4
input = input.to(device)
- expected = torch.tensor([[[[[9.9412e-01, 0.0000e+00, 8.5407e-03, 1.9535e+00],
- [1.7328e-05, 3.8945e-03, 1.5617e-02, 1.0553e-04],
- [0.0000e+00, 9.8295e-01, 1.9789e+00, 4.9531e-02],
- [0.0000e+00, 0.0000e+00, 9.7034e-01, 1.9548e+00]],
- [[9.6646e-01, 0.0000e+00, 4.3866e-02, 1.9559e+00],
- [1.1586e-02, 0.0000e+00, 1.0260e-04, 0.0000e+00],
- [4.4472e-03, 9.9659e-01, 1.9833e+00, 3.4181e-05],
- [0.0000e+00, 7.8456e-03, 9.9959e-01, 1.9956e+00]],
- [[9.3772e-01, 0.0000e+00, 7.8179e-02, 1.8983e+00],
- [2.2707e-02, 0.0000e+00, 9.6477e-04, 2.2624e-02],
- [2.1575e-02, 9.9975e-01, 1.9243e+00, 0.0000e+00],
- [3.1300e-04, 3.2790e-02, 1.0249e+00, 1.9474e+00]]]],
-
- [[[[5.9268e-01, 4.6201e-01, 0.0000e+00, 1.3414e-01],
- [5.0854e-02, 0.0000e+00, 1.2416e-02, 1.1548e+00],
- [6.1057e-01, 9.5876e-01, 1.9510e-01, 0.0000e+00],
- [7.4132e-01, 8.6556e-01, 1.9031e-01, 0.0000e+00]],
- [[0.0000e+00, 1.0143e-01, 2.2231e-01, 0.0000e+00],
- [0.0000e+00, 1.8495e-01, 5.0593e-01, 4.9479e-01],
- [4.9456e-02, 5.0849e-01, 1.5325e+00, 7.8474e-01],
- [0.0000e+00, 4.7385e-01, 1.3469e+00, 1.1854e+00]],
- [[0.0000e+00, 0.0000e+00, 0.0000e+00, 9.7841e-02],
- [0.0000e+00, 0.0000e+00, 1.2882e-01, 8.0175e-01],
- [0.0000e+00, 0.0000e+00, 0.0000e+00, 7.2928e-01],
- [0.0000e+00, 0.0000e+00, 0.0000e+00, 2.1100e-01]]]]])
+ expected = torch.tensor([[[[[7.5651e-01, 6.4340e-02, 0.0000e+00, 0.0000e+00],
+ [8.9954e-02, 2.0099e-01, 8.2089e-01, 4.4695e-01],
+ [0.0000e+00, 4.8303e-01, 8.0751e-01, 1.1574e+00],
+ [0.0000e+00, 6.5891e-02, 1.7392e-01, 2.9013e-01]],
+ [[4.0104e-01, 0.0000e+00, 1.9018e-02, 7.2668e-04],
+ [3.5247e-01, 0.0000e+00, 6.5445e-01, 3.7179e-01],
+ [5.4804e-02, 7.0015e-01, 1.7578e+00, 1.2048e+00],
+ [3.7536e-02, 3.8235e-01, 9.0737e-01, 1.1033e+00]],
+ [[1.2648e-02, 0.0000e+00, 9.4951e-01, 4.6696e-01],
+ [1.2791e-01, 0.0000e+00, 8.2977e-02, 0.0000e+00],
+ [1.3047e-02, 2.8671e-01, 8.3294e-01, 1.7991e-01],
+ [0.0000e+00, 5.8076e-02, 6.7866e-01, 1.5130e+00]]]],
+
+ [[[[1.0000e+00, 0.0000e+00, 0.0000e+00, 2.0000e+00],
+ [0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00],
+ [0.0000e+00, 1.0000e+00, 2.0000e+00, 0.0000e+00],
+ [0.0000e+00, 0.0000e+00, 1.0000e+00, 2.0000e+00]],
+ [[1.0000e+00, 0.0000e+00, 0.0000e+00, 2.0000e+00],
+ [0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00],
+ [0.0000e+00, 1.0000e+00, 2.0000e+00, 0.0000e+00],
+ [0.0000e+00, 0.0000e+00, 1.0000e+00, 2.0000e+00]],
+ [[1.0000e+00, 0.0000e+00, 0.0000e+00, 2.0000e+00],
+ [0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00],
+ [0.0000e+00, 1.0000e+00, 2.0000e+00, 0.0000e+00],
+ [0.0000e+00, 0.0000e+00, 1.0000e+00, 2.0000e+00]]]]])
expected = expected.to(device)
- expected_transform = torch.tensor([[[0.7559, 0.2793, -0.5921, 0.7132],
- [-0.2756, 0.9561, 0.0991, 0.1928],
- [0.5938, 0.0883, 0.7998, -0.4259],
+ expected_transform = torch.tensor([[[0.8017, 0.4358, -0.4090, 0.0527],
+ [-0.0877, 0.7627, 0.6408, -0.1533],
+ [0.5912, -0.4779, 0.6497, 0.1803],
[0.0000, 0.0000, 0.0000, 1.0000]],
- [[0.8194, -0.3079, -0.4836, 1.3678],
- [0.0754, 0.8941, -0.4415, 0.7456],
- [0.5683, 0.3253, 0.7558, -0.6899],
+ [[1.0000, 0.0000, 0.0000, 0.0000],
+ [0.0000, 1.0000, 0.0000, 0.0000],
+ [0.0000, 0.0000, 1.0000, 0.0000],
[0.0000, 0.0000, 0.0000, 1.0000]]])
expected_transform = expected_transform.to(device)
@@ -679,32 +678,32 @@ class TestRandomRotation3D:
[0., 0., 1., 2.]]]) # 3 x 4 x 4
input = input.to(device)
- expected = torch.tensor([[[[[5.8413e-01, 3.1238e-01, 2.7060e-02, 4.1555e-01],
- [4.3787e-02, 8.6122e-02, 8.3644e-02, 6.6170e-01],
- [3.7727e-01, 8.5232e-01, 5.1960e-01, 9.1474e-02],
- [2.7198e-01, 6.9109e-01, 5.7987e-01, 1.7646e-01]],
+ expected = torch.tensor([[[[[4.5604e-02, 4.6441e-03, 7.1205e-01, 7.3379e-01],
+ [2.7580e-01, 3.3129e-02, 1.1986e-01, 5.7227e-01],
+ [1.4329e-01, 4.0604e-02, 4.4003e-03, 1.3030e-01],
+ [8.0267e-05, 9.0396e-03, 2.8991e-02, 4.0690e-03]],
- [[8.8109e-02, 1.6755e-01, 1.0494e-01, 3.5867e-02],
- [6.1397e-02, 2.6093e-01, 4.2951e-01, 3.5508e-01],
- [7.9497e-02, 5.5609e-01, 1.3745e+00, 6.2975e-01],
- [8.9196e-03, 3.6598e-01, 1.1553e+00, 1.2267e+00]],
+ [[9.8822e-03, 4.4220e-02, 1.2963e-01, 3.9873e-02],
+ [4.3757e-02, 3.4982e-01, 5.1378e-01, 8.6131e-02],
+ [6.6809e-02, 5.5708e-01, 1.0904e+00, 4.6732e-01],
+ [2.9877e-02, 1.9682e-01, 3.5764e-01, 1.0877e-01]],
- [[6.1910e-03, 1.4123e-02, 5.0222e-02, 6.4776e-03],
- [2.9561e-05, 1.1410e-01, 5.5537e-01, 2.2792e-01],
- [0.0000e+00, 5.5169e-02, 5.4580e-01, 4.4281e-01],
- [0.0000e+00, 2.9420e-03, 1.7743e-01, 3.0618e-01]]]]])
+ [[2.3905e-02, 2.1605e-01, 2.5145e-02, 3.3507e-04],
+ [1.1453e-01, 1.1965e+00, 1.2008e+00, 3.6272e-01],
+ [1.3368e-01, 5.4211e-01, 1.2059e+00, 1.0104e+00],
+ [8.6762e-03, 6.7149e-02, 2.0946e-01, 2.7900e-01]]]]])
expected = expected.to(device)
- expected_transform = torch.tensor([[[0.4690, 0.4978, 0.7295, -1.3100],
- [-0.2616, 0.8673, -0.4236, 1.0961],
- [-0.8435, 0.0078, 0.5370, 1.5263],
+ expected_transform = torch.tensor([[[0.8369, 0.0343, -0.5463, 0.7395],
+ [-0.5104, 0.4091, -0.7563, 2.4083],
+ [0.1976, 0.9118, 0.3599, -1.0240],
[0.0000, 0.0000, 0.0000, 1.0000]]])
expected_transform = expected_transform.to(device)
- expected_transform_2 = torch.tensor([[[0.2207, 0.0051, 0.9753, -0.6914],
- [0.4092, 0.9072, -0.0974, -0.1240],
- [-0.8853, 0.4206, 0.1981, 1.4573],
+ expected_transform_2 = torch.tensor([[[0.9869, -0.1351, 0.0879, 0.1343],
+ [0.1598, 0.7501, -0.6417, 0.7769],
+ [0.0208, 0.6474, 0.7619, -0.7641],
[0.0000, 0.0000, 0.0000, 1.0000]]])
expected_transform_2 = expected_transform_2.to(device)
@@ -764,7 +763,7 @@ class TestRandomRotation3D:
input = torch.rand((3, 3, 3)).to(device) # 3 x 3 x 3
input = utils.tensor_to_gradcheck_var(input) # to var
- assert gradcheck(RandomRotation3D(degrees=(15.0, 15.0)), (input, ), raise_exception=True)
+ assert gradcheck(RandomRotation3D(degrees=(15.0, 15.0), p=1.), (input, ), raise_exception=True)
class TestRandomCrop3D:
diff --git a/test/geometry/transform/crop/test_crop3d.py b/test/geometry/transform/crop/test_crop3d.py
index 6ade68d72..3d67b5daf 100644
--- a/test/geometry/transform/crop/test_crop3d.py
+++ b/test/geometry/transform/crop/test_crop3d.py
@@ -11,7 +11,7 @@ from torch.autograd import gradcheck
class TestBoundingBoxInferring3D:
- def test_bounding_boxes_dim_inferring(self, device):
+ def test_bounding_boxes_dim_inferring(self, device, dtype):
boxes = torch.tensor([
[[0, 1, 2],
[10, 1, 2],
@@ -28,12 +28,13 @@ class TestBoundingBoxInferring3D:
[3, 4, 65],
[43, 4, 65],
[43, 54, 65],
- [3, 54, 65]]]) # 2x8x3
+ [3, 54, 65]
+ ]], device=device, dtype=dtype) # 2x8x3
d, h, w = kornia.geometry.transform.crop.infer_box_shape3d(boxes)
- assert (d == torch.tensor([31, 61])).all()
- assert (h == torch.tensor([21, 51])).all()
- assert (w == torch.tensor([11, 41])).all()
+ assert_allclose(d, torch.tensor([31., 61.], device=device, dtype=dtype))
+ assert_allclose(h, torch.tensor([21., 51.], device=device, dtype=dtype))
+ assert_allclose(w, torch.tensor([11., 41.], device=device, dtype=dtype))
def test_gradcheck(self, device, dtype):
boxes = torch.tensor([[
@@ -222,27 +223,6 @@ class TestCenterCrop3D:
expected = kornia.center_crop3d(img, (4, 3, 2))
assert_allclose(actual, expected)
- def test_jit_trace(self, device, dtype):
- # Define script
- op = kornia.center_crop3d
- op_script = torch.jit.script(op)
- # 1. Trace op
- img = torch.ones(4, 3, 5, 6, 7, device=device, dtype=dtype)
-
- op_trace = torch.jit.trace(
- op_script,
- (img, (torch.tensor(4), torch.tensor(3), torch.tensor(2))))
-
- # 2. Generate new input
- img = torch.ones(4, 3, 5, 6, 7, device=device, dtype=dtype)
-
- # 3. Evaluate
- crop_height, crop_width = 2, 3
- actual = op_trace(
- img, (torch.tensor(4), torch.tensor(3), torch.tensor(2)))
- expected = op(img, (4, 3, 2))
- assert_allclose(actual, expected)
-
class TestCropByBoxes3D:
def test_crop_by_boxes_no_resizing(self, device, dtype):
diff --git a/test/geometry/transform/test_projwarp.py b/test/geometry/transform/test_projwarp.py
index f3221adc8..c7f42495b 100644
--- a/test/geometry/transform/test_projwarp.py
+++ b/test/geometry/transform/test_projwarp.py
@@ -9,12 +9,7 @@ import kornia.testing as utils # test utils
import kornia.geometry.transform.projwarp as proj
-def dtype():
- return torch.float32
-
-
-class TestWarpProjective:
+class TestWarpAffine3d:
def test_smoke(self, device, dtype):
input = torch.rand(1, 3, 3, 4, 5, device=device, dtype=dtype)
P = torch.rand(1, 3, 4, device=device, dtype=dtype)
@@ -81,7 +76,7 @@ class TestWarpProjective:
angles = torch.tensor([[90., 0., 0.]], device=device, dtype=dtype)
- scales: torch.Tensor = torch.ones_like(angles)
+ scales: torch.Tensor = torch.ones_like(angles, device=device, dtype=dtype)
P = proj.get_projective_transform(center, angles, scales)
output = proj.warp_affine3d(input, P, (3, 3, 3))
assert_allclose(output, expected)
@@ -121,7 +116,7 @@ class TestWarpProjective:
angles = torch.tensor([[0., 90., 0.]], device=device, dtype=dtype)
- scales: torch.Tensor = torch.ones_like(angles)
+ scales: torch.Tensor = torch.ones_like(angles, device=device, dtype=dtype)
P = proj.get_projective_transform(center, angles, scales)
output = proj.warp_affine3d(input, P, (3, 3, 3))
assert_allclose(output, expected)
@@ -161,7 +156,7 @@ class TestWarpProjective:
angles = torch.tensor([[0., 0., 90.]], device=device, dtype=dtype)
- scales: torch.Tensor = torch.ones_like(angles)
+ scales: torch.Tensor = torch.ones_like(angles, device=device, dtype=dtype)
P = proj.get_projective_transform(center, angles, scales)
output = proj.warp_affine3d(input, P, (3, 3, 3))
assert_allclose(output, expected)
@@ -226,7 +221,7 @@ class TestWarpProjective:
angles = torch.tensor([[0., 90., 0.]], device=device, dtype=dtype)
- scales: torch.Tensor = torch.ones_like(angles)
+ scales: torch.Tensor = torch.ones_like(angles, device=device, dtype=dtype)
P = proj.get_projective_transform(center, angles, scales)
output = proj.warp_affine3d(input, P, (3, 3, 3))
assert_allclose(output, expected)
@@ -236,7 +231,7 @@ class TestGetRotationMatrix3d:
def test_smoke(self, device, dtype):
center = torch.rand(1, 3, device=device, dtype=dtype)
angle = torch.rand(1, 3, device=device, dtype=dtype)
- scales: torch.Tensor = torch.ones_like(angle)
+ scales: torch.Tensor = torch.ones_like(angle, device=device, dtype=dtype)
P = proj.get_projective_transform(center, angle, scales)
assert P.shape == (1, 3, 4)
@@ -245,112 +240,144 @@ class TestGetRotationMatrix3d:
B: int = batch_size
center = torch.rand(B, 3, device=device, dtype=dtype)
angle = torch.rand(B, 3, device=device, dtype=dtype)
- scales: torch.Tensor = torch.ones_like(angle)
+ scales: torch.Tensor = torch.ones_like(angle, device=device, dtype=dtype)
P = proj.get_projective_transform(center, angle, scales)
assert P.shape == (B, 3, 4)
def test_identity(self, device, dtype):
center = torch.zeros(1, 3, device=device, dtype=dtype)
angle = torch.zeros(1, 3, device=device, dtype=dtype)
- scales: torch.Tensor = torch.ones_like(angle)
+ scales: torch.Tensor = torch.ones_like(angle, device=device, dtype=dtype)
P = proj.get_projective_transform(center, angle, scales)
P_expected = torch.tensor([
[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
], device=device, dtype=dtype)
- assert_allclose(P, P_expected)
+ assert_allclose(P, P_expected, atol=1e-4, rtol=1e-4)
def test_rot90x(self, device, dtype):
center = torch.zeros(1, 3, device=device, dtype=dtype)
angle = torch.tensor([[90., 0., 0.]], device=device, dtype=dtype)
- scales: torch.Tensor = torch.ones_like(angle)
+ scales: torch.Tensor = torch.ones_like(angle, device=device, dtype=dtype)
P = proj.get_projective_transform(center, angle, scales)
P_expected = torch.tensor([
[1., 0., 0., 0.],
[0., 0., -1., 0.],
[0., 1., 0., 0.],
], device=device, dtype=dtype)
- assert_allclose(P, P_expected)
+ assert_allclose(P, P_expected, atol=1e-4, rtol=1e-4)
def test_rot90y(self, device, dtype):
center = torch.zeros(1, 3, device=device, dtype=dtype)
angle = torch.tensor([[0., 90., 0.]], device=device, dtype=dtype)
- scales: torch.Tensor = torch.ones_like(angle)
+ scales: torch.Tensor = torch.ones_like(angle, device=device, dtype=dtype)
P = proj.get_projective_transform(center, angle, scales)
P_expected = torch.tensor([
[0., 0., 1., 0.],
[0., 1., 0., 0.],
[-1., 0., 0., 0.],
], device=device, dtype=dtype)
- assert_allclose(P, P_expected)
+ assert_allclose(P, P_expected, atol=1e-4, rtol=1e-4)
def test_rot90z(self, device, dtype):
center = torch.zeros(1, 3, device=device, dtype=dtype)
angle = torch.tensor([[0., 0., 90.]], device=device, dtype=dtype)
- scales: torch.Tensor = torch.ones_like(angle)
+ scales: torch.Tensor = torch.ones_like(angle, device=device, dtype=dtype)
P = proj.get_projective_transform(center, angle, scales)
P_expected = torch.tensor([
[0., -1., 0., 0.],
[1., 0., 0., 0.],
[0., 0., 1., 0.],
], device=device, dtype=dtype)
- assert_allclose(P, P_expected)
+ assert_allclose(P, P_expected, atol=1e-4, rtol=1e-4)
- def test_gradcheck(self, device):
+ def test_gradcheck(self, device, dtype):
# generate input data
center = torch.rand(1, 3, device=device, dtype=torch.float64, requires_grad=True)
angle = torch.rand(1, 3, device=device, dtype=torch.float64)
- scales: torch.Tensor = torch.ones_like(angle)
+ scales: torch.Tensor = torch.ones_like(angle, device=device, dtype=torch.float64)
assert gradcheck(proj.get_projective_transform, (center, angle, scales), raise_exception=True)
[email protected]("batch_size", [1, 2])
-def test_get_perspective_transform3d(batch_size, device, dtype):
- torch.manual_seed(0)
- src = kornia.bbox_generator3d(
- torch.randint_like(torch.ones(batch_size), 0, 50, device=device, dtype=dtype),
- torch.randint_like(torch.ones(batch_size), 0, 50, device=device, dtype=dtype),
- torch.randint_like(torch.ones(batch_size), 0, 50, device=device, dtype=dtype),
- torch.randint(0, 50, (1,), device=device, dtype=dtype).repeat(batch_size),
- torch.randint(0, 50, (1,), device=device, dtype=dtype).repeat(batch_size),
- torch.randint(0, 50, (1,), device=device, dtype=dtype).repeat(batch_size),
- )
- dst = kornia.bbox_generator3d(
- torch.randint_like(torch.ones(batch_size), 0, 50, device=device, dtype=dtype),
- torch.randint_like(torch.ones(batch_size), 0, 50, device=device, dtype=dtype),
- torch.randint_like(torch.ones(batch_size), 0, 50, device=device, dtype=dtype),
- torch.randint(0, 50, (1,), device=device, dtype=dtype).repeat(batch_size),
- torch.randint(0, 50, (1,), device=device, dtype=dtype).repeat(batch_size),
- torch.randint(0, 50, (1,), device=device, dtype=dtype).repeat(batch_size),
- )
- out = kornia.get_perspective_transform3d(src, dst)
- if batch_size == 1:
- expected = torch.tensor([[
- [3.3000, 0.0000, 0.0000, -118.2000],
- [0.0000, 0.0769, 0.0000, 0.0000],
- [0.0000, 0.0000, 0.5517, 28.7930],
- [0.0000, 0.0000, 0.0000, 1.0000]
- ]])
- if batch_size == 2:
- expected = torch.tensor([
- [[0.9630, 0.0000, 0.0000, -9.3702],
- [0.0000, 2.0000, 0.0000, -49.9999],
- [0.0000, 0.0000, 0.3830, 44.0213],
- [0.0000, 0.0000, 0.0000, 1.0000]],
- [[0.9630, 0.0000, 0.0000, -36.5555],
- [0.0000, 2.0000, 0.0000, -14.0000],
- [0.0000, 0.0000, 0.3830, 16.8940],
- [0.0000, 0.0000, 0.0000, 1.0000]],
- ])
-
- assert_allclose(out, expected, rtol=1e-4, atol=1e-4)
-
- # compute gradient check
- points_src = utils.tensor_to_gradcheck_var(src) # to var
- points_dst = utils.tensor_to_gradcheck_var(dst) # to var
- assert gradcheck(
- kornia.get_perspective_transform3d, (
- points_src,
- points_dst,
- ), raise_exception=True)
+class TestPerspectiveTransform3D:
+ @pytest.mark.skip("Not working")
+ @pytest.mark.parametrize("batch_size", [1, 2, 5])
+ def test_get_perspective_transform3d(self, batch_size, device, dtype):
+ # generate input data
+ d_max, h_max, w_max = 16, 64, 32 # height, width
+ d = torch.ceil(d_max * torch.rand(batch_size, device=device, dtype=dtype))
+ 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, 8, 3, device=device, dtype=dtype)
+ points_src = torch.rand_like(norm, device=device, dtype=dtype)
+ points_dst = points_src + norm
+
+ # compute transform from source to target
+ dst_homo_src = kornia.get_perspective_transform3d(points_src, points_dst)
+
+ # TODO: get_perspective_transform3d seems to be correct since it would result in the
+ # expected output for cropping volumes. Not sure what is going on here.
+ assert_allclose(
+ kornia.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.get_perspective_transform3d, (
+ points_src,
+ points_dst,
+ ),
+ raise_exception=True)
+
+ @pytest.mark.parametrize("batch_size", [1, 2])
+ def test_get_perspective_transform3d_2(self, batch_size, device, dtype):
+ torch.manual_seed(0)
+ src = kornia.bbox_generator3d(
+ torch.randint_like(torch.ones(batch_size), 0, 50, dtype=dtype),
+ torch.randint_like(torch.ones(batch_size), 0, 50, dtype=dtype),
+ torch.randint_like(torch.ones(batch_size), 0, 50, dtype=dtype),
+ torch.randint(0, 50, (1,), dtype=dtype).repeat(batch_size),
+ torch.randint(0, 50, (1,), dtype=dtype).repeat(batch_size),
+ torch.randint(0, 50, (1,), dtype=dtype).repeat(batch_size),
+ ).to(device=device, dtype=dtype)
+ dst = kornia.bbox_generator3d(
+ torch.randint_like(torch.ones(batch_size), 0, 50, dtype=dtype),
+ torch.randint_like(torch.ones(batch_size), 0, 50, dtype=dtype),
+ torch.randint_like(torch.ones(batch_size), 0, 50, dtype=dtype),
+ torch.randint(0, 50, (1,), dtype=dtype).repeat(batch_size),
+ torch.randint(0, 50, (1,), dtype=dtype).repeat(batch_size),
+ torch.randint(0, 50, (1,), dtype=dtype).repeat(batch_size),
+ ).to(device=device, dtype=dtype)
+ out = kornia.get_perspective_transform3d(src, dst)
+ if batch_size == 1:
+ expected = torch.tensor([[
+ [3.3000, 0.0000, 0.0000, -118.2000],
+ [0.0000, 0.0769, 0.0000, 0.0000],
+ [0.0000, 0.0000, 0.5517, 28.7930],
+ [0.0000, 0.0000, 0.0000, 1.0000]
+ ]], device=device, dtype=dtype)
+ if batch_size == 2:
+ expected = torch.tensor([
+ [[0.9630, 0.0000, 0.0000, -9.3702],
+ [0.0000, 2.0000, 0.0000, -49.9999],
+ [0.0000, 0.0000, 0.3830, 44.0213],
+ [0.0000, 0.0000, 0.0000, 1.0000]],
+ [[0.9630, 0.0000, 0.0000, -36.5555],
+ [0.0000, 2.0000, 0.0000, -14.0000],
+ [0.0000, 0.0000, 0.3830, 16.8940],
+ [0.0000, 0.0000, 0.0000, 1.0000]],
+ ], device=device, dtype=dtype)
+
+ assert_allclose(out, expected, rtol=1e-4, atol=1e-4)
+
+ # compute gradient check
+ points_src = utils.tensor_to_gradcheck_var(src) # to var
+ points_dst = utils.tensor_to_gradcheck_var(dst) # to var
+ assert gradcheck(
+ kornia.get_perspective_transform3d, (
+ points_src,
+ points_dst,
+ ), raise_exception=True)