Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Resize compile (#2774)

Edgar Riba4b31df47eff7
diff --git a/kornia/geometry/bbox.py b/kornia/geometry/bbox.py
index 7c7c5681f..8c49c9199 100644
--- a/kornia/geometry/bbox.py
+++ b/kornia/geometry/bbox.py
@@ -32,22 +32,24 @@ def validate_bbox(boxes: torch.Tensor) -> bool:
bottom-left. The coordinates must be in the x, y order.
"""
if not (len(boxes.shape) in [3, 4] and boxes.shape[-2:] == torch.Size([4, 2])):
- raise AssertionError(f"Box shape must be (B, 4, 2) or (B, N, 4, 2). Got {boxes.shape}.")
+ return False
if len(boxes.shape) == 4:
boxes = boxes.view(-1, 4, 2)
- if not torch.allclose((boxes[:, 1, 0] - boxes[:, 0, 0] + 1), (boxes[:, 2, 0] - boxes[:, 3, 0] + 1), atol=1e-4):
- raise ValueError(
- f"Boxes must have be rectangular, while get widths {boxes[:, 1, 0] - boxes[:, 0, 0] + 1!s}"
- f" and {boxes[:, 2, 0] - boxes[:, 3, 0] + 1!s}"
- )
+ x_tl, y_tl = boxes[..., 0, 0], boxes[..., 0, 1]
+ x_tr, y_tr = boxes[..., 1, 0], boxes[..., 1, 1]
+ x_br, y_br = boxes[..., 2, 0], boxes[..., 2, 1]
+ x_bl, y_bl = boxes[..., 3, 0], boxes[..., 3, 1]
- if not torch.allclose((boxes[:, 2, 1] - boxes[:, 0, 1] + 1), (boxes[:, 3, 1] - boxes[:, 1, 1] + 1), atol=1e-4):
- raise ValueError(
- f"Boxes must be rectangular, while getting heights {boxes[:, 2, 1] - boxes[:, 0, 1] + 1!s}"
- f" and {boxes[:, 3, 1] - boxes[:, 1, 1] + 1!s}"
- )
+ width_t, width_b = x_tr - x_tl + 1, x_br - x_bl + 1
+ height_t, height_b = y_tr - y_tl + 1, y_br - y_bl + 1
+
+ if not torch.allclose(width_t, width_b, atol=1e-4):
+ return False
+
+ if not torch.allclose(height_t, height_b, atol=1e-4):
+ return False
return True
@@ -555,4 +557,5 @@ def nms(boxes: torch.Tensor, scores: torch.Tensor, iou_threshold: float) -> torc
if len(keep) > 0:
return stack(keep)
+
return torch.tensor(keep)
diff --git a/kornia/geometry/transform/crop2d.py b/kornia/geometry/transform/crop2d.py
index 057981d5b..bac930b23 100644
--- a/kornia/geometry/transform/crop2d.py
+++ b/kornia/geometry/transform/crop2d.py
@@ -3,6 +3,7 @@ from typing import Optional, Tuple
import torch
from kornia.core import Tensor, as_tensor, pad, tensor
+from kornia.core.check import KORNIA_CHECK_SHAPE
from kornia.geometry.bbox import infer_bbox_shape, validate_bbox
from .affwarp import resize
@@ -310,6 +311,9 @@ def crop_by_indices(
shape_compensation: if the cropped slice sizes are not exactly align `size`, the image can either be padded
or resized.
"""
+ KORNIA_CHECK_SHAPE(input_tensor, ["B", "C", "H", "W"])
+ KORNIA_CHECK_SHAPE(src_box, ["B", "4", "2"])
+
B, C, _, _ = input_tensor.shape
src = as_tensor(src_box, device=input_tensor.device, dtype=torch.long)
x1 = src[:, 0, 0]
diff --git a/tests/geometry/transform/test_affine.py b/tests/geometry/transform/test_affine.py
index e15607e82..a9fa52bd1 100644
--- a/tests/geometry/transform/test_affine.py
+++ b/tests/geometry/transform/test_affine.py
@@ -174,8 +174,18 @@ class TestResize(BaseTester):
def test_gradcheck(self, device):
# test parameters
new_size = 4
- input = torch.rand(1, 2, 3, 4, device=device, dtype=torch.float64)
- self.gradcheck(kornia.geometry.transform.Resize(new_size, align_corners=False), (input,))
+ inp = torch.rand(1, 2, 3, 4, device=device, dtype=torch.float64)
+ self.gradcheck(kornia.geometry.transform.Resize(new_size, align_corners=False), (inp,))
+
+ @pytest.mark.parametrize("anti_alias", [True, False])
+ def test_dynamo(self, device, dtype, anti_alias, torch_optimizer):
+ new_size = (5, 6)
+ inp = torch.rand(1, 2, 3, 4, device=device, dtype=dtype)
+ op = torch_optimizer(kornia.geometry.transform.resize)
+ out = op(inp, new_size, align_corners=False, antialias=anti_alias)
+ assert out.shape == (1, 2, 5, 6)
+ expected = op(inp, new_size, align_corners=False, antialias=anti_alias)
+ self.assert_close(out, expected)
class TestRescale(BaseTester):
diff --git a/tests/geometry/transform/test_crop2d.py b/tests/geometry/transform/test_crop2d.py
index 72e90a17c..f368c6f69 100644
--- a/tests/geometry/transform/test_crop2d.py
+++ b/tests/geometry/transform/test_crop2d.py
@@ -258,3 +258,25 @@ class TestCropByTransform(BaseTester):
(inp, transform, (2, 2)),
requires_grad=(True, False, False),
)
+
+
+class TestCropByIndices(BaseTester):
+ def test_crop_by_indices_no_resizing(self, device, dtype):
+ inp = torch.tensor([[[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7, 8, 9]]]], device=device, dtype=dtype) # 1x3x3
+
+ # provide the indices to crop as 4 points
+ indices = torch.tensor([[[0, 0], [1, 0], [1, 1], [0, 1]]], device=device, dtype=torch.int64)
+ expected = torch.tensor([[[[1.0, 2.0], [4.0, 5.0]]]], device=device, dtype=dtype)
+
+ self.assert_close(kornia.geometry.transform.crop_by_indices(inp, indices), expected)
+
+ def test_dynamo(self, device, dtype, torch_optimizer):
+ # Define script
+ op = kornia.geometry.transform.crop_by_indices
+ op_script = torch_optimizer(op)
+ # Define input
+ img = torch.ones(1, 2, 5, 4, device=device, dtype=dtype)
+
+ actual = op_script(img, torch.tensor([[[0, 0], [1, 0], [1, 1], [0, 1]]]))
+ expected = op(img, torch.tensor([[[0, 0], [1, 0], [1, 1], [0, 1]]]))
+ self.assert_close(actual, expected, rtol=1e-4, atol=1e-4)