π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
Fix integer-coordinate sampling in SOLD2 endpoint refinement (#6367)
A Aswanth Raj2f402b804869
diff --git a/changelog.d/6367.fixed.md b/changelog.d/6367.fixed.mdnew file mode 100644index 000000000..3f1624ad2--- /dev/null+++ b/changelog.d/6367.fixed.md@@ -0,0 +1 @@+SOLD2 line-endpoint refinement samples integer pixel coordinates correctly instead of assigning them zero heatmap scores, keeping horizontal and vertical segments on their supporting pixels.diff --git a/kornia/feature/sold2/sold2_detector.py b/kornia/feature/sold2/sold2_detector.pyindex 945b3e6a4..aee2e6e69 100644--- a/kornia/feature/sold2/sold2_detector.py+++ b/kornia/feature/sold2/sold2_detector.py@@ -507,19 +507,27 @@ class LineSegmentDetectionModule:return line_mapdef detect_bilinear(self, heatmap: torch.Tensor, cand_h: torch.Tensor, cand_w: torch.Tensor) -> torch.Tensor:- """Detect by bilinear sampling."""- # Get the floor and ceiling locations+ """Detect by bilinear sampling.++ The reference SOLD2 implementation takes ``ceil`` as the second neighbour, so at an integer coordinate, where+ ``floor`` and ``ceil`` coincide, both weights of that axis are 0 and the sample scores 0. kornia takes+ ``floor + 1`` (clipped at the image border) instead: an integer sample keeps its pixel value, and the junction+ refinement no longer moves horizontal and vertical segments off the pixels that support them.+ """+ # Use the next pixel even at integer coordinates, where floor and ceil coincide.cand_h_floor = torch.floor(cand_h).to(torch.long)- cand_h_ceil = torch.ceil(cand_h).to(torch.long)+ cand_h_ceil = (cand_h_floor + 1).clamp(max=heatmap.shape[0] - 1)cand_w_floor = torch.floor(cand_w).to(torch.long)- cand_w_ceil = torch.ceil(cand_w).to(torch.long)+ cand_w_ceil = (cand_w_floor + 1).clamp(max=heatmap.shape[1] - 1)+ h_weight = cand_h - cand_h_floor+ w_weight = cand_w - cand_w_floor# Perform the bilinear samplingreturn (- heatmap[cand_h_floor, cand_w_floor] * (cand_h_ceil - cand_h) * (cand_w_ceil - cand_w)- + heatmap[cand_h_floor, cand_w_ceil] * (cand_h_ceil - cand_h) * (cand_w - cand_w_floor)- + heatmap[cand_h_ceil, cand_w_floor] * (cand_h - cand_h_floor) * (cand_w_ceil - cand_w)- + heatmap[cand_h_ceil, cand_w_ceil] * (cand_h - cand_h_floor) * (cand_w - cand_w_floor)+ heatmap[cand_h_floor, cand_w_floor] * (1 - h_weight) * (1 - w_weight)+ + heatmap[cand_h_floor, cand_w_ceil] * (1 - h_weight) * w_weight+ + heatmap[cand_h_ceil, cand_w_floor] * h_weight * (1 - w_weight)+ + heatmap[cand_h_ceil, cand_w_ceil] * h_weight * w_weight)def detect_local_max(diff --git a/tests/feature/test_sold2.py b/tests/feature/test_sold2.pyindex 1f75208ed..b72a7b1ef 100644--- a/tests/feature/test_sold2.py+++ b/tests/feature/test_sold2.py@@ -61,6 +61,40 @@ class TestSOLD2_detector(BaseTester):class TestJunctionsAndSegments(BaseTester):+ def test_bilinear_integer_coordinates(self, device, dtype):+ detector = LineSegmentDetectionModule()+ heatmap = torch.arange(20, device=device, dtype=dtype).reshape(4, 5)+ rows = torch.tensor([[0.0, 1.0, 1.5, 2.0, 3.0, 3.0]], device=device, dtype=dtype)+ cols = torch.tensor([[0.0, 2.5, 2.0, 3.0, 2.5, 4.0]], device=device, dtype=dtype)+ expected = torch.tensor([[0.0, 7.5, 9.5, 13.0, 17.5, 19.0]], device=device, dtype=dtype)+ self.assert_close(detector.detect_bilinear(heatmap, rows, cols), expected, atol=0, rtol=0)++ @pytest.mark.parametrize("transpose", [False, True], ids=["horizontal", "vertical"])+ def test_refinement_keeps_integer_line(self, device, dtype, transpose):+ heatmap = torch.zeros(32, 96, device=device, dtype=dtype)+ heatmap[16, 16:80] = 1+ junctions = torch.tensor([[16.0, 16.0], [16.0, 79.0]], device=device, dtype=dtype)+ if transpose:+ heatmap = heatmap.t()+ junctions = junctions.flip(-1)+ config = LineDetectorCfg(use_heatmap_refinement=False, use_candidate_suppression=False)+ line_map, refined, _ = LineSegmentDetectionModule(config).detect(junctions, heatmap)+ segments = line_map_to_segments(refined, line_map)+ assert segments.shape == (1, 2, 2)+ axis = 1 if transpose else 0+ self.assert_close(segments[..., axis], torch.full_like(segments[..., axis], 16.0), atol=0, rtol=0)+ # Candidates may shorten a constant-strength line by one pixel without changing its score.+ self.assert_close(segments, junctions[None].to(segments.dtype), atol=1.0, rtol=0)++ def test_bilinear_integer_coordinate_gradient(self, device, dtype):+ detector = LineSegmentDetectionModule()+ heatmap = torch.arange(20, device=device, dtype=dtype).reshape(4, 5)+ rows = torch.tensor([[1.0, 1.5]], device=device, dtype=dtype, requires_grad=True)+ cols = torch.tensor([[2.0, 2.5]], device=device, dtype=dtype, requires_grad=True)+ row_grad, col_grad = torch.autograd.grad(detector.detect_bilinear(heatmap, rows, cols).sum(), (rows, cols))+ self.assert_close(row_grad, torch.full_like(rows, 5.0), atol=0, rtol=0)+ self.assert_close(col_grad, torch.ones_like(cols), atol=0, rtol=0)+@staticmethoddef _junction_map(points, device, dtype, shape=(96, 128)):prob = torch.zeros(shape, device=device, dtype=dtype)