Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(augmentation): handle video tensor annotation shapes (#5881)

Karnajeet Gosavi416db047aa3d
diff --git a/changelog.d/5881.breaking.md b/changelog.d/5881.breaking.md
new file mode 100644
index 000000000..1fec1a03d
--- /dev/null
+++ b/changelog.d/5881.breaking.md
@@ -0,0 +1 @@
+`VideoSequential.inverse_keypoints` called with a raw `(B, T, N, 2)` tensor used to return it shaped `(T, B, N, 2)`, with the values still in `(B, T)` order; it now returns `(B, T, N, 2)`, as `transform_keypoints` does. The raw-tensor branches of `transform_keypoints`, `inverse_keypoints`, `transform_boxes` and `inverse_boxes` used to raise for zero keypoints or boxes, an empty video batch and a non-contiguous tensor; they now return the input's shape.
diff --git a/kornia/augmentation/container/video.py b/kornia/augmentation/container/video.py
index ffd8fb283..e13f70404 100644
--- a/kornia/augmentation/container/video.py
+++ b/kornia/augmentation/container/video.py
@@ -399,9 +399,11 @@ class VideoSequential(ImageSequential):
"""
if isinstance(input, torch.Tensor):
batchsize, frame_num = input.size(0), input.size(1)
- input = Boxes.from_tensor(input.view(-1, input.size(2), input.size(3), input.size(4)), mode="vertices_plus")
+ input = Boxes.from_tensor(
+ input.reshape(batchsize * frame_num, input.size(2), input.size(3), input.size(4)), mode="vertices_plus"
+ )
input = super().transform_boxes(input, params, extra_args=extra_args)
- input = input.data.view(batchsize, frame_num, -1, 4, 2)
+ input = input.data.reshape(batchsize, frame_num, input.data.size(1), 4, 2)
else:
input = super().transform_boxes(input, params, extra_args=extra_args)
return input
@@ -419,9 +421,11 @@ class VideoSequential(ImageSequential):
"""
if isinstance(input, torch.Tensor):
batchsize, frame_num = input.size(0), input.size(1)
- input = Boxes.from_tensor(input.view(-1, input.size(2), input.size(3), input.size(4)), mode="vertices_plus")
+ input = Boxes.from_tensor(
+ input.reshape(batchsize * frame_num, input.size(2), input.size(3), input.size(4)), mode="vertices_plus"
+ )
input = super().inverse_boxes(input, params, extra_args=extra_args)
- input = input.data.view(batchsize, frame_num, -1, 4, 2)
+ input = input.data.reshape(batchsize, frame_num, input.data.size(1), 4, 2)
else:
input = super().inverse_boxes(input, params, extra_args=extra_args)
return input
@@ -442,9 +446,9 @@ class VideoSequential(ImageSequential):
"""
if isinstance(input, torch.Tensor):
batchsize, frame_num = input.size(0), input.size(1)
- input = Keypoints(input.view(-1, input.size(2), input.size(3)))
+ input = Keypoints(input.reshape(batchsize * frame_num, input.size(2), input.size(3)))
input = super().transform_keypoints(input, params, extra_args=extra_args)
- input = input.data.view(batchsize, frame_num, -1, 2)
+ input = input.data.reshape(batchsize, frame_num, input.data.size(1), 2)
else:
input = super().transform_keypoints(input, params, extra_args=extra_args)
return input
@@ -464,10 +468,10 @@ class VideoSequential(ImageSequential):
extra_args: Optional dictionary of extra arguments with specific options for different input types.
"""
if isinstance(input, torch.Tensor):
- frame_num, batchsize = input.size(0), input.size(1)
- input = Keypoints(input.view(-1, input.size(2), input.size(3)))
+ batchsize, frame_num = input.size(0), input.size(1)
+ input = Keypoints(input.reshape(batchsize * frame_num, input.size(2), input.size(3)))
input = super().inverse_keypoints(input, params, extra_args=extra_args)
- input = input.data.view(batchsize, frame_num, -1, 2)
+ input = input.data.reshape(batchsize, frame_num, input.data.size(1), 2)
else:
input = super().inverse_keypoints(input, params, extra_args=extra_args)
return input
diff --git a/tests/augmentation/container/test_video_sequential.py b/tests/augmentation/container/test_video_sequential.py
index 23fac7459..b460cc8ec 100644
--- a/tests/augmentation/container/test_video_sequential.py
+++ b/tests/augmentation/container/test_video_sequential.py
@@ -213,3 +213,51 @@ class TestVideoSequential:
assert same_batch == same_on_batch
reproducibility_test(x, aug)
+
+ def test_tensor_annotation_methods_preserve_video_shape_and_accept_empty_or_noncontiguous(self, device, dtype):
+ video = torch.rand(2, 3, 1, 6, 6, device=device, dtype=dtype)
+ aug = K.VideoSequential(K.RandomHorizontalFlip(p=1.0), data_format="BTCHW")
+ aug(video)
+ params = aug._params
+
+ # Integer coordinates keep the flip and its inverse exact in float16 and bfloat16.
+ keypoints = torch.randint(0, 6, (2, 3, 4, 2), device=device).to(dtype)
+ transformed = aug.transform_keypoints(keypoints, params)
+ assert transformed.shape == keypoints.shape
+ assert_close(aug.inverse_keypoints(transformed, params), keypoints)
+
+ noncontiguous_keypoints = keypoints.transpose(0, 1).contiguous().transpose(0, 1)
+ assert not noncontiguous_keypoints.is_contiguous()
+ assert_close(aug.transform_keypoints(noncontiguous_keypoints, params), transformed)
+ assert_close(aug.inverse_keypoints(noncontiguous_keypoints, params), aug.inverse_keypoints(keypoints, params))
+
+ empty_keypoints = keypoints[:, :, :0]
+ assert aug.transform_keypoints(empty_keypoints, params).shape == empty_keypoints.shape
+ assert aug.inverse_keypoints(empty_keypoints, params).shape == empty_keypoints.shape
+
+ boxes = torch.randint(0, 6, (2, 3, 2, 4, 2), device=device).to(dtype)
+ transformed_boxes = aug.transform_boxes(boxes, params)
+ assert transformed_boxes.shape == boxes.shape
+ assert_close(aug.inverse_boxes(transformed_boxes, params), boxes)
+
+ noncontiguous_boxes = boxes.transpose(0, 1).contiguous().transpose(0, 1)
+ assert not noncontiguous_boxes.is_contiguous()
+ assert_close(aug.transform_boxes(noncontiguous_boxes, params), transformed_boxes)
+ assert_close(aug.inverse_boxes(noncontiguous_boxes, params), aug.inverse_boxes(boxes, params))
+
+ empty_boxes = boxes[:, :, :0]
+ assert aug.transform_boxes(empty_boxes, params).shape == empty_boxes.shape
+ assert aug.inverse_boxes(empty_boxes, params).shape == empty_boxes.shape
+
+ def test_tensor_annotation_methods_accept_an_empty_video_batch(self, device, dtype):
+ aug = K.VideoSequential(K.RandomHorizontalFlip(p=1.0), data_format="BTCHW")
+ aug(torch.rand(0, 3, 1, 6, 6, device=device, dtype=dtype))
+ params = aug._params
+
+ keypoints = torch.rand(0, 3, 4, 2, device=device, dtype=dtype)
+ assert aug.transform_keypoints(keypoints, params).shape == keypoints.shape
+ assert aug.inverse_keypoints(keypoints, params).shape == keypoints.shape
+
+ boxes = torch.rand(0, 3, 2, 4, 2, device=device, dtype=dtype)
+ assert aug.transform_boxes(boxes, params).shape == boxes.shape
+ assert aug.inverse_boxes(boxes, params).shape == boxes.shape