π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(contrib): accept detection tensors in visualization (#5945)
NIKHIL0b10b1eada49
diff --git a/changelog.d/5945.fixed.md b/changelog.d/5945.fixed.mdnew file mode 100644index 000000000..c7ebb21b7--- /dev/null+++ b/changelog.d/5945.fixed.md@@ -0,0 +1 @@+`ObjectDetector.visualize` and `ObjectDetector.save` accept supplied detection tensors, including tensors with no detections, without raising an ambiguous truth-value error.diff --git a/kornia/contrib/object_detection.py b/kornia/contrib/object_detection.pyindex b8c04f161..83aead493 100644--- a/kornia/contrib/object_detection.py+++ b/kornia/contrib/object_detection.py@@ -191,7 +191,7 @@ class ObjectDetector(ModelBase, ONNXExportMixin):Needs to be more fancy later."""- dets = detections or self.forward(images)+ dets = self.forward(images) if detections is None else detectionsoutput = []for image, detection in zip(images, dets):out_img = image[None].clone()diff --git a/tests/models/test_object_detector.py b/tests/models/test_object_detector.pyindex 854a5e0ee..a3c83ec7b 100644--- a/tests/models/test_object_detector.py+++ b/tests/models/test_object_detector.py@@ -17,6 +17,7 @@import warningsfrom pathlib import Path+from unittest.mock import Mockimport pytestimport torch@@ -29,6 +30,53 @@ from testing.base import BaseTesterclass TestObjectDetector(BaseTester):+ @pytest.mark.parametrize("num_detections", [0, 1, 2])+ @pytest.mark.parametrize("as_list", [False, True])+ @pytest.mark.parametrize("method", ["visualize", "save"])+ def test_supplied_detections(self, device, dtype, num_detections, as_list, method, monkeypatch, tmp_path):+ detector = kornia.contrib.object_detection.ObjectDetector(+ torch.nn.Identity(), torch.nn.Identity(), torch.nn.Identity()+ )+ forward = Mock(side_effect=AssertionError("Supplied detections must not run inference"))+ monkeypatch.setattr(detector, "forward", forward)+ images = torch.ones(2, 3, 8, 8, device=device, dtype=dtype)+ detections = torch.tensor([[[0, 0.9, 1, 1, 2, 2], [1, 0.8, 4, 4, 2, 2]]], device=device, dtype=dtype).expand(+ 2, -1, -1+ )[:, :num_detections]+ expected = images.clone()+ if num_detections >= 1:+ expected[:, :, 1, 1:4] = expected[:, :, 3, 1:4] = 0+ expected[:, :, 1:4, 1] = expected[:, :, 1:4, 3] = 0+ if num_detections == 2:+ expected[:, :, 4, 4:7] = expected[:, :, 6, 4:7] = 0+ expected[:, :, 4:7, 4] = expected[:, :, 4:7, 6] = 0++ detections = list(detections) if as_list else detections+ if method == "save":+ detector.save(images, detections, directory=str(tmp_path))+ files = sorted(tmp_path.glob("*.png"))+ assert len(files) == len(images)+ output = [+ kornia.io.load_image(str(path), kornia.io.ImageLoadType.UNCHANGED).to(images) / 255 for path in files+ ]+ else:+ output = detector.visualize(images, detections)++ self.assert_close(torch.stack(output), expected)+ self.assert_close(images, torch.ones_like(images))+ forward.assert_not_called()++ def test_visualize_without_detections(self, device, dtype, monkeypatch):+ detector = kornia.contrib.object_detection.ObjectDetector(+ torch.nn.Identity(), torch.nn.Identity(), torch.nn.Identity()+ )+ images = torch.ones(2, 3, 8, 8, device=device, dtype=dtype)+ forward = Mock(return_value=torch.empty(2, 0, 6, device=device, dtype=dtype))+ monkeypatch.setattr(detector, "forward", forward)++ self.assert_close(torch.stack(detector.visualize(images)), images)+ forward.assert_called_once_with(images)+def test_visualize_keeps_a_border_box_four_sided(self, device):detector = kornia.contrib.object_detection.ObjectDetector(torch.nn.Identity(), torch.nn.Identity(), torch.nn.Identity()