Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(geometry): honor shape_compensation in crop_by_indices uniform-box path (#4756)

yi11109d74aa47da1
diff --git a/changelog.d/4756.breaking.md b/changelog.d/4756.breaking.md
new file mode 100644
index 000000000..f0a9521a7
--- /dev/null
+++ b/changelog.d/4756.breaking.md
@@ -0,0 +1 @@
+`crop_by_indices` now honors `shape_compensation` when every box in the batch is identical. With a `shape_compensation` other than `"resize"` and a slice that does not match `size`, it used to resize the slice to `size` whenever all boxes in the batch coincided (always for a batch of one) and zero-pad or trim it otherwise; it now zero-pads or trims it in both cases, so a row's output no longer depends on the other rows in the batch. `RandomMosaic(cropping_mode="slice")` crops this way (`shape_compensation="F.pad"`): for a batch of one, or any batch whose crop boxes coincide, a crop that does not match `output_size` (for example because `output_size` differs from the input size) used to be resized to `output_size`, out of register with the transformed boxes; it is now zero-padded or trimmed at the bottom and right, as batches with differing boxes already were, so fixed-seed outputs change.
diff --git a/kornia/geometry/transform/crop2d.py b/kornia/geometry/transform/crop2d.py
index d7401f3cb..d723db636 100644
--- a/kornia/geometry/transform/crop2d.py
+++ b/kornia/geometry/transform/crop2d.py
@@ -425,12 +425,10 @@ def crop_by_indices(
- unlike the other crop operators in this module: ``interpolation=`` (not
``mode=``), ``align_corners=None`` by default (not ``True``), and an
``antialias=False`` option
- - ``shape_compensation`` (``'resize'`` by default) only takes effect when
- ``src_box`` is not literally identical across the batch (same position and
- size for every item); when ``src_box`` **is** identical across the batch,
- ``shape_compensation`` is ignored β€” the result is an exact integer slice
- when the slice shape already matches ``size`` (or when ``size=None``), and
- is resized to ``size`` otherwise
+ - ``shape_compensation`` (``'resize'`` by default) applies whenever the cropped
+ slice does not match ``size``, whether or not ``src_box`` is identical across
+ the batch β€” each row's output depends only on its own box. Graph export is the
+ exception: it always resamples (see the note below)
Args:
input_tensor: the 2D image torch.Tensor with shape (B, C, H, W).
@@ -495,9 +493,16 @@ def crop_by_indices(
):
out = input_tensor[..., y1l[0] : y2l[0], x1l[0] : x2l[0]]
if size is not None and out.shape[-2:] != size:
- return resize(
- out, size, interpolation=interpolation, align_corners=align_corners, side="short", antialias=antialias
- )
+ if shape_compensation == "resize":
+ return resize(
+ out,
+ size,
+ interpolation=interpolation,
+ align_corners=align_corners,
+ side="short",
+ antialias=antialias,
+ )
+ return F.pad(out, [0, size[1] - out.shape[-1], 0, size[0] - out.shape[-2]])
if size is None:
h, w = infer_bbox_shape(src)
diff --git a/tests/geometry/transform/test_crop2d.py b/tests/geometry/transform/test_crop2d.py
index bcc952ec7..ab4372085 100644
--- a/tests/geometry/transform/test_crop2d.py
+++ b/tests/geometry/transform/test_crop2d.py
@@ -521,14 +521,13 @@ class TestCropByIndices(BaseTester):
kornia.geometry.transform.crop_by_indices(img, src_box, size=None)
def test_convention_shape_compensation_pad_vs_resize(self, device, dtype):
- # shape_compensation is pinned per the crop_by_indices Convention block: when
- # src_box is identical across the batch it is ignored (exact integer slice if the
- # slice already matches `size`, resized otherwise); it only takes effect for a
- # non-uniform batch, where 'pad' trims via F.pad's negative padding (keeps the
- # top-left corner, no interpolation) while 'resize' downsamples via interpolation.
+ # shape_compensation is pinned per the crop_by_indices Convention block: it applies
+ # whenever the cropped slice does not match `size`, whether or not src_box is identical
+ # across the batch. 'pad' trims via F.pad's negative padding (keeps the top-left corner,
+ # no interpolation) while 'resize' resamples via interpolation.
inp = torch.arange(0.0, 32.0, device=device, dtype=dtype).view(1, 1, 4, 8).repeat(2, 1, 1, 1)
- # --- identical src_box across the batch: shape_compensation is ignored ---
+ # --- identical src_box across the batch: shape_compensation still applies (#4749) ---
box_3x3 = torch.tensor([[[0, 0], [2, 0], [2, 2], [0, 2]]], device=device, dtype=torch.int64).expand(2, -1, -1)
box_2x2 = torch.tensor([[[0, 0], [1, 0], [1, 1], [0, 1]]], device=device, dtype=torch.int64).expand(2, -1, -1)
size = (2, 2)
@@ -542,14 +541,29 @@ class TestCropByIndices(BaseTester):
self.assert_close(out_resize_match, expected_slice, atol=0.0, rtol=0.0)
self.assert_close(out_pad_match, expected_slice, atol=0.0, rtol=0.0)
- # slice (3x3) differs from `size` (2x2): resized under both settings.
+ # slice (3x3) differs from `size` (2x2): 'resize' resamples, 'pad' trims β€” the same
+ # result the same box produces in the non-uniform batch below.
out_resize_diff = kornia.geometry.transform.crop_by_indices(
inp, box_3x3, size=size, shape_compensation="resize"
)
out_pad_diff = kornia.geometry.transform.crop_by_indices(inp, box_3x3, size=size, shape_compensation="pad")
- self.assert_close(out_resize_diff, out_pad_diff, atol=0.0, rtol=0.0)
+ expected_resize_uniform = torch.tensor([[2.25, 3.75], [14.25, 15.75]], device=device, dtype=dtype)
+ expected_pad_uniform = torch.tensor([[0.0, 1.0], [8.0, 9.0]], device=device, dtype=dtype)
+ self.assert_close(out_resize_diff[:, 0], expected_resize_uniform.expand(2, -1, -1), rtol=1e-2, atol=1e-2)
+ self.assert_close(out_pad_diff[:, 0], expected_pad_uniform.expand(2, -1, -1), rtol=1e-2, atol=1e-2)
assert out_resize_diff.shape[-2:] == size
+ # identical 2x2 box padded UP to (3, 3): zero ring, not a 2x2->3x3 resample (#4749).
+ out_pad_grow = kornia.geometry.transform.crop_by_indices(inp, box_2x2, size=(3, 3), shape_compensation="pad")
+ expected_grow = torch.nn.functional.pad(inp[..., 0:2, 0:2], [0, 1, 0, 1])
+ self.assert_close(out_pad_grow, expected_grow, atol=0.0, rtol=0.0)
+
+ # identical 3x3 box to (4, 2): grows the height and trims the width, so the two axes'
+ # pad amounts differ and a swapped F.pad argument order changes the result.
+ out_pad_mixed = kornia.geometry.transform.crop_by_indices(inp, box_3x3, size=(4, 2), shape_compensation="pad")
+ expected_mixed = torch.nn.functional.pad(inp[..., 0:3, 0:3], [0, -1, 0, 1])
+ self.assert_close(out_pad_mixed, expected_mixed, atol=0.0, rtol=0.0)
+
# --- non-uniform batch (box 0 != box 1): shape_compensation genuinely takes effect ---
src_box = torch.tensor(
[