Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix device and dtype in draw poly tests (#1642)

Edgar Ribaac7c4f613901
diff --git a/docs/source/utils.rst b/docs/source/utils.rst
index 16c56bc83..95ac5d742 100644
--- a/docs/source/utils.rst
+++ b/docs/source/utils.rst
@@ -8,7 +8,7 @@ Draw
.. autofunction:: draw_line
.. autofunction:: draw_rectangle
-.. autofunction:: draw_convex_polygons
+.. autofunction:: draw_convex_polygon
Image
-----
diff --git a/kornia/utils/__init__.py b/kornia/utils/__init__.py
index 9eb7eddec..99651566e 100644
--- a/kornia/utils/__init__.py
+++ b/kornia/utils/__init__.py
@@ -1,4 +1,4 @@
-from .draw import draw_convex_polygons, draw_line, draw_rectangle
+from .draw import draw_convex_polygon, draw_line, draw_rectangle
from .grid import create_meshgrid, create_meshgrid3d
from .helpers import _extract_device_dtype, safe_inverse_with_mask, safe_solve_with_mask
from .image import ImageToTensor, image_list_to_tensor, image_to_tensor, tensor_to_image
@@ -17,7 +17,7 @@ __all__ = [
"image_list_to_tensor",
"save_pointcloud_ply",
"load_pointcloud_ply",
- "draw_convex_polygons",
+ "draw_convex_polygon",
"draw_rectangle",
"draw_line",
"_extract_device_dtype",
diff --git a/kornia/utils/draw.py b/kornia/utils/draw.py
index d45a25f97..c82c52ef8 100644
--- a/kornia/utils/draw.py
+++ b/kornia/utils/draw.py
@@ -268,7 +268,7 @@ def _batch_polygons(polygons: List[Tensor]) -> Tensor:
return batched_polygons
-def draw_convex_polygons(images: Tensor, polygons: Union[Tensor, List[Tensor]], colors: Tensor) -> Tensor:
+def draw_convex_polygon(images: Tensor, polygons: Union[Tensor, List[Tensor]], colors: Tensor) -> Tensor:
r"""Draws convex polygons on a batch of image tensors.
Args:
@@ -290,7 +290,7 @@ def draw_convex_polygons(images: Tensor, polygons: Union[Tensor, List[Tensor]],
>>> img = torch.rand(1, 3, 12, 16)
>>> poly = torch.tensor([[[4, 4], [12, 4], [12, 8], [4, 8]]])
>>> color = torch.tensor([[0.5, 0.5, 0.5]])
- >>> out = draw_convex_polygons(img, poly, color)
+ >>> out = draw_convex_polygon(img, poly, color)
"""
# TODO: implement optional linetypes for smooth edges
KORNIA_CHECK_SHAPE(images, ["B", "C", "H", "W"])
diff --git a/test/utils/test_draw.py b/test/utils/test_draw.py
index 7009aeb69..aeaffc997 100644
--- a/test/utils/test_draw.py
+++ b/test/utils/test_draw.py
@@ -4,7 +4,7 @@ import pytest
import torch
from kornia.testing import assert_close
-from kornia.utils import create_meshgrid, draw_convex_polygons, draw_rectangle
+from kornia.utils import create_meshgrid, draw_convex_polygon, draw_rectangle
from kornia.utils.draw import draw_line
@@ -251,7 +251,7 @@ class TestDrawRectangle:
points_list[b][n].append(int(torch.randint(points_list[b][n][-2] + 1, w, (1,))))
points_list[b][n].append(int(torch.randint(points_list[b][n][-2] + 1, h, (1,))))
- points = torch.tensor(points_list).to(device)
+ points = torch.tensor(points_list, device=device)
random_background = torch.rand(batch, 3, h, w, device=device)
random_w_rectangle = random_background.clone()
@@ -347,14 +347,14 @@ class TestFillConvexPolygon:
b, c, h, w = 1, 3, 500, 500
n = 5000
im = torch.zeros(b, c, h, w, device=device, dtype=dtype)
- t = torch.linspace(0, 1, steps=n)[None].expand(b, n)
+ t = torch.linspace(0, 1, steps=n, device=device, dtype=dtype)[None].expand(b, n)
color = torch.tensor([1, 1, 1], device=device, dtype=dtype)[None].expand(b, c)
x = (2 * math.pi * t).cos()
y = (2 * math.pi * t).sin()
ctr = 200
radius = 200
pts = ctr + radius * torch.stack((x, y), dim=-1)
- poly_im = draw_convex_polygons(im, pts, color)
+ poly_im = draw_convex_polygon(im, pts, color)
XY = create_meshgrid(h, w, normalized_coordinates=False, device=device, dtype=dtype)
inside = (((XY[..., 1] - ctr) ** 2 + (XY[..., 0] - ctr) ** 2).sqrt() <= radius)[:, None].expand(b, c, h, w)
circ_im = inside * color[..., None, None]
@@ -364,7 +364,7 @@ class TestFillConvexPolygon:
b, c, h, w = 1, 3, 500, 500
n = 5000
im = torch.zeros(b, c, h, w, device=device, dtype=dtype)
- t = torch.linspace(0, 1, steps=n)[None].expand(b, n)
+ t = torch.linspace(0, 1, steps=n, device=device, dtype=dtype)[None].expand(b, n)
color = torch.tensor([1, 1, 1], device=device, dtype=dtype)[None].expand(b, c)
lam = 2
x = lam * (2 * math.pi * t).cos()
@@ -372,7 +372,7 @@ class TestFillConvexPolygon:
ctr = 200
radius = 100
pts = ctr + radius * torch.stack((x, y), dim=-1)
- poly_im = draw_convex_polygons(im, pts, color)
+ poly_im = draw_convex_polygon(im, pts, color)
XY = create_meshgrid(h, w, normalized_coordinates=False, device=device, dtype=dtype)
inside = (((XY[..., 1] - ctr) ** 2 + ((XY[..., 0] - ctr) / lam) ** 2).sqrt() <= radius)[:, None].expand(
b, c, h, w
@@ -385,22 +385,21 @@ class TestFillConvexPolygon:
im = torch.zeros(b, c, h, w, device=device, dtype=dtype)
color = torch.tensor([1, 1, 1], device=device, dtype=dtype)[None].expand(b, c)
pts = torch.tensor([[[50, 50], [200, 50], [200, 250], [50, 250]]], device=device, dtype=dtype)
- poly_im = draw_convex_polygons(im.clone(), pts, color)
+ poly_im = draw_convex_polygon(im.clone(), pts, color)
rect = torch.cat((pts[..., 0, :], pts[..., 2, :]), dim=-1)[:, None]
rect_im = draw_rectangle(im.clone(), rect, color[:, None], fill=True)
- assert torch.allclose(rect_im, poly_im)
+ assert_close(rect_im, poly_im)
def test_batch(self, device, dtype):
im = torch.rand(2, 3, 12, 16, dtype=dtype, device=device)
pts = torch.tensor(
[[[4, 4], [12, 4], [12, 8], [4, 8]], [[0, 0], [4, 0], [4, 4], [0, 4]]], dtype=dtype, device=device
)
- torch.tensor([[[0, 0, 4, 4]], [[4, 4, 10, 10]]])
- color = torch.tensor([[0.5, 0.5, 0.5], [0.5, 0.5, 0.75]])
- poly_im = draw_convex_polygons(im.clone(), pts, color)
+ color = torch.tensor([[0.5, 0.5, 0.5], [0.5, 0.5, 0.75]], dtype=dtype, device=device)
+ poly_im = draw_convex_polygon(im.clone(), pts, color)
rect = torch.tensor([[[4, 4, 12, 8]], [[0, 0, 4, 4]]], dtype=dtype, device=device)
rect_im = draw_rectangle(im.clone(), rect, color[:, None], fill=True)
- assert torch.allclose(rect_im, poly_im)
+ assert_close(rect_im, poly_im)
def test_batch_variable_size(self, device, dtype):
im = torch.rand(2, 3, 12, 16, dtype=dtype, device=device)
@@ -408,11 +407,11 @@ class TestFillConvexPolygon:
torch.tensor([[4, 4], [12, 4], [12, 8], [4, 8]], dtype=dtype, device=device),
torch.tensor([[0, 0], [2, 0], [4, 0], [4, 4], [0, 4]], dtype=dtype, device=device),
]
- color = torch.tensor([[0.5, 0.5, 0.5], [0.5, 0.5, 0.75]])
- poly_im = draw_convex_polygons(im.clone(), pts, color)
+ color = torch.tensor([[0.5, 0.5, 0.5], [0.5, 0.5, 0.75]], dtype=dtype, device=device)
+ poly_im = draw_convex_polygon(im.clone(), pts, color)
rect = torch.tensor([[[4, 4, 12, 8]], [[0, 0, 4, 4]]], dtype=dtype, device=device)
rect_im = draw_rectangle(im.clone(), rect, color[:, None], fill=True)
- assert torch.allclose(rect_im, poly_im)
+ assert_close(rect_im, poly_im)
def test_batch_color_no_batch(self, device, dtype):
im = torch.rand(2, 3, 12, 16, dtype=dtype, device=device)
@@ -420,23 +419,23 @@ class TestFillConvexPolygon:
torch.tensor([[4, 4], [12, 4], [12, 8], [4, 8]], dtype=dtype, device=device),
torch.tensor([[0, 0], [2, 0], [4, 0], [4, 4], [0, 4]], dtype=dtype, device=device),
]
- color = torch.tensor([0.5, 0.5, 0.75])
- poly_im = draw_convex_polygons(im.clone(), pts, color)
+ color = torch.tensor([0.5, 0.5, 0.75], dtype=dtype, device=device)
+ poly_im = draw_convex_polygon(im.clone(), pts, color)
rect = torch.tensor([[[4, 4, 12, 8]], [[0, 0, 4, 4]]], dtype=dtype, device=device)
rect_im = draw_rectangle(im.clone(), rect, color, fill=True)
- assert torch.allclose(rect_im, poly_im)
+ assert_close(rect_im, poly_im)
def test_out_of_bounds_rectangle(self, device, dtype):
b, c, h, w = 1, 3, 500, 500
im = torch.zeros(b, c, h, w, device=device, dtype=dtype)
color = torch.tensor([1, 1, 1], device=device, dtype=dtype)[None].expand(b, c)
pts = 350 + torch.tensor([[[50, 50], [200, 50], [200, 250], [50, 250]]], device=device, dtype=dtype)
- poly_im = draw_convex_polygons(im.clone(), pts, color)
+ poly_im = draw_convex_polygon(im.clone(), pts, color)
rect = torch.cat((pts[..., 0, :], pts[..., 2, :]), dim=-1)[:, None]
rect_im = draw_rectangle(im.clone(), rect, color[:, None], fill=True)
- assert torch.allclose(rect_im, poly_im)
+ assert_close(rect_im, poly_im)
pts = -150 + torch.tensor([[[50, 50], [200, 50], [200, 250], [50, 250]]], device=device, dtype=dtype)
- poly_im = draw_convex_polygons(im.clone(), pts, color)
+ poly_im = draw_convex_polygon(im.clone(), pts, color)
rect = torch.cat((pts[..., 0, :], pts[..., 2, :]), dim=-1)[:, None]
rect_im = draw_rectangle(im.clone(), rect, color[:, None], fill=True)
- assert torch.allclose(rect_im, poly_im)
+ assert_close(rect_im, poly_im)