π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(image): restore TorchScript for draw_rectangle (#5822)
Dmytro Mishkinef36c57e6829
diff --git a/kornia/image/draw.py b/kornia/image/draw.pyindex 07f45388c..cf181b338 100644--- a/kornia/image/draw.py+++ b/kornia/image/draw.py@@ -62,7 +62,11 @@ def _rectangle_bands((y1, y2, x1, torch.minimum(x1 + line_width - 1, x2)), # left(y1, y2, torch.maximum(x2 - line_width + 1, x1), x2), # right]- r0, r1, c0, c1 = (torch.stack(bound, -1) for bound in zip(*bands))+ # one stack per bound: torch.jit.script cannot compile zip(*bands)+ r0 = torch.stack([band[0] for band in bands], -1)+ r1 = torch.stack([band[1] for band in bands], -1)+ c0 = torch.stack([band[2] for band in bands], -1)+ c1 = torch.stack([band[3] for band in bands], -1)# a start clamped to [0, size] and a stop to [0, size] give an empty slice whenever the band misses the imagereturn torch.stack([r0.clamp(0, h), r1.clamp(-1, h - 1) + 1, c0.clamp(0, w), c1.clamp(-1, w - 1) + 1], -1)@@ -305,11 +309,11 @@ def draw_rectangle(color = color.expand(batch, num_rectangle, c)# one host read for the bands of every rectangle- bands = _rectangle_bands(x1, y1, x2, y2, h, w, 1, fill).tolist()+ bands: List[List[List[List[int]]]] = _rectangle_bands(x1, y1, x2, y2, h, w, 1, fill).tolist()for b in range(batch):for n in range(num_rectangle):- for r0, r1, c0, c1 in bands[b][n]:- image[b, :, r0:r1, c0:c1] = color[b, n, :, None, None]+ for band in bands[b][n]:+ image[b, :, band[0] : band[1], band[2] : band[3]] = color[b, n, :, None, None]return imagediff --git a/testing/half_precision_eager_rng.py b/testing/half_precision_eager_rng.pyindex 48b992368..8d3a38adc 100644--- a/testing/half_precision_eager_rng.py+++ b/testing/half_precision_eager_rng.py@@ -199,7 +199,7 @@ AUDITED_EAGER_RNG_CALLS = tuple(),*_audited("tests/image/test_draw.py",- 423,+ 432,"torch.rand",4,("tests/image/test_draw.py::TestDrawLine::test_point_size",),diff --git a/tests/image/test_draw.py b/tests/image/test_draw.pyindex ba1a52762..87819c05a 100644--- a/tests/image/test_draw.py+++ b/tests/image/test_draw.py@@ -199,6 +199,15 @@ class TestDrawPoint(BaseTester):assert img[0].nonzero().tolist() == [[0, 3], [3, 1]] # (row, col): x 1.4 -> 1, y 2.6 -> 3assert img[:, 3, 1].tolist() == [1.0, 0.5, 0.25]+ def test_jit_script_matches_eager(self, device, dtype):+ img = torch.zeros(3, 4, 5, device=device, dtype=dtype)+ points = torch.tensor([[1, 2], [3, 0]], device=device)+ color = torch.tensor([1.0, 0.5, 0.25], device=device, dtype=dtype)+ scripted = torch.jit.script(draw_point2d)+ expected = draw_point2d(img.clone(), points, color)+ assert expected.abs().sum() > 0+ self.assert_close(scripted(img.clone(), points, color), expected, rtol=0, atol=0)+class TestDrawLine(BaseTester):def test_draw_line_vertical(self, dtype, device):@@ -783,6 +792,19 @@ class TestDrawRectangle(BaseTester):out = draw_rectangle(black_image, points, color=torch.tensor([1.0]))assert out.sum() == batch * 3 * (2 * height + 2 * width - 4)+ @pytest.mark.parametrize("fill", [False, True])+ def test_jit_script_matches_eager(self, device, dtype, fill):+ # draw_rectangle compiles under torch.jit.script and matches eager; H != W, the second box has reversed corners+ image = torch.zeros(2, 3, 7, 10, device=device, dtype=dtype)+ rectangle = torch.tensor(+ [[[1.0, 1.0, 6.0, 3.0]], [[8.0, 5.0, 2.0, 0.0]]], device=device, dtype=dtype+ ) # reversed corners+ color = torch.tensor([[[1.0, 0.5, 0.25]], [[0.25, 0.5, 1.0]]], device=device, dtype=dtype)+ scripted = torch.jit.script(draw_rectangle)+ expected = draw_rectangle(image.clone(), rectangle, color, fill=fill)+ assert expected.abs().sum() > 0+ self.assert_close(scripted(image.clone(), rectangle, color, fill=fill), expected, rtol=0, atol=0)+@pytest.mark.device_agnostic@pytest.mark.skipif(not dynamo_is_available(), reason="torch.export needs Dynamo on this torch/python pair")@pytest.mark.skipif(