Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(geometry): reject a VideoBoxes assignment with another frame count (#6389)

Saksham Sharmaf78a989b2497
diff --git a/changelog.d/6389.fixed.md b/changelog.d/6389.fixed.md
new file mode 100644
index 000000000..3555cb9ce
--- /dev/null
+++ b/changelog.d/6389.fixed.md
@@ -0,0 +1 @@
+Assigning a `kornia.geometry.boxes.VideoBoxes` value whose `temporal_channel_size` differs from the target's, `video_boxes[key] = other`, now raises `ValueError` instead of regrouping the value's frames into videos of the target's length: two 2-frame videos assigned to one 4-frame video used to be stitched into it, one 4-frame video assigned to two 2-frame videos was split across them, and a frame total that did not divide by the target's length raised a `RuntimeError` from `view`. A plain `Boxes` value is still read as frames of the target's length.
diff --git a/kornia/geometry/boxes.py b/kornia/geometry/boxes.py
index 0671073c4..b47670d73 100644
--- a/kornia/geometry/boxes.py
+++ b/kornia/geometry/boxes.py
@@ -1155,8 +1155,16 @@ class VideoBoxes(Boxes):
return out
def __setitem__(self, key: slice | int | torch.Tensor, value: Boxes) -> VideoBoxes:
- r"""Write whole videos, selected by ``key`` on the video-batch axis as in :meth:`__getitem__`."""
+ r"""Write whole videos, selected by ``key`` on the video-batch axis as in :meth:`__getitem__`.
+
+ A :class:`VideoBoxes` value must have the same ``temporal_channel_size``: otherwise its frames would be
+ regrouped into videos of another length. A plain :class:`Boxes` value is read as frames of this length.
+ """
size = self.temporal_channel_size
+ if isinstance(value, VideoBoxes) and value.temporal_channel_size != size:
+ raise ValueError(
+ f"Cannot assign videos of {value.temporal_channel_size} frames to videos of {size} frames."
+ )
# Explicit video counts: a clip with no boxes has 0 elements, so a -1 in the view would be ambiguous.
n, m = self._data.shape[0] // size, value.data.shape[0] // size
self._data.view(n, size, *self._data.shape[1:])[key] = value.data.view(m, size, *value.data.shape[1:])
diff --git a/tests/geometry/test_boxes.py b/tests/geometry/test_boxes.py
index 93030f76c..8af87ff6f 100644
--- a/tests/geometry/test_boxes.py
+++ b/tests/geometry/test_boxes.py
@@ -2520,6 +2520,27 @@ class TestVideoBoxes(BaseTester):
expected[videos] = boxes[videos]
self.assert_close(target.to_tensor(), expected, atol=0.0, rtol=0.0)
+ @pytest.mark.parametrize(("target_time", "value_time", "key"), [(4, 2, 0), (2, 4, "mask"), (2, 3, 0)])
+ def test_index_assignment_rejects_another_frame_count_6388(self, target_time, value_time, key, device, dtype):
+ # A value whose videos have another number of frames used to be regrouped into videos of the target's length:
+ # two 2-frame videos were stitched into one 4-frame video, one 4-frame video was split across two, and a
+ # frame total that does not divide by the target's length raised a RuntimeError from view.
+ target = VideoBoxes.from_tensor(torch.zeros(2, target_time, 1, 4, 2, device=device, dtype=dtype))
+ num_videos = 2 if value_time == 2 else 1 # one 3-frame video does not divide into 2-frame videos
+ value = VideoBoxes.from_tensor(torch.ones(num_videos, value_time, 1, 4, 2, device=device, dtype=dtype))
+ if key == "mask":
+ key = torch.tensor([True, True], device=device)
+ original = target.to_tensor().clone()
+ with pytest.raises(ValueError, match=f"videos of {value_time} frames to videos of {target_time} frames"):
+ target[key] = value
+ self.assert_close(target.to_tensor(), original, atol=0.0, rtol=0.0)
+
+ # A plain Boxes value is read as frames of the target's length, as before.
+ target[0] = Boxes(torch.ones(target_time, 1, 4, 2, device=device, dtype=dtype))
+ expected = original.clone()
+ expected[0] = 1
+ self.assert_close(target.to_tensor(), expected, atol=0.0, rtol=0.0)
+
def test_index_assignment_without_boxes_5871(self, device, dtype):
# A clip with no boxes stores (B * T, 0, 4, 2): the write still lands in place.
target = VideoBoxes.from_tensor(torch.zeros(3, 2, 0, 4, 2, device=device, dtype=dtype))