Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Pad small images to fit an extraction window (#5806)

Anton Soloviev6384bb2f617f
diff --git a/changelog.d/5806.breaking.md b/changelog.d/5806.breaking.md
new file mode 100644
index 000000000..cd8c50519
--- /dev/null
+++ b/changelog.d/5806.breaking.md
@@ -0,0 +1 @@
+`compute_padding` now pads an axis that is shorter than the window up to one full window. It used to return only the stride-alignment padding, which can leave the padded image smaller than the window: for a 4x6 image with an 8x8 window and stride 2 it returned `(0, 0, 0, 0)`, and `extract_tensor_patches(..., allow_auto_padding=True)` and `combine_tensor_patches(..., allow_auto_unpadding=True)` raised a `RuntimeError`. They now extract one window along such an axis and reconstruct the image. Results are unchanged wherever the old padding already fitted a window.
diff --git a/kornia/contrib/extract_patches.py b/kornia/contrib/extract_patches.py
index fb748936b..101647374 100644
--- a/kornia/contrib/extract_patches.py
+++ b/kornia/contrib/extract_patches.py
@@ -91,8 +91,11 @@ def compute_padding(
# that fewer patches are affected by the padding.
# For now, just use the default padding
vertical_padding = stride[0] - remainder_vertical if remainder_vertical != 0 else 0
+ # Stride alignment alone can leave the padded image smaller than one window.
+ vertical_padding = max(vertical_padding, window_size[0] - original_size[0])
horizontal_padding = stride[1] - remainder_horizontal if remainder_horizontal != 0 else 0
+ horizontal_padding = max(horizontal_padding, window_size[1] - original_size[1])
if vertical_padding % 2 == 0:
top_padding = bottom_padding = vertical_padding // 2
@@ -328,7 +331,12 @@ def _check_patch_fit(original_size: Tuple[int, int], window_size: Tuple[int, int
# the rest of the image is divided based on the stride, not the window
# size
# we can fit a full number of patches in, based on the stride, only if nothing remains
- return remainder_horizontal == 0 and remainder_vertical == 0
+ return (
+ original_size[0] >= window_size[0]
+ and original_size[1] >= window_size[1]
+ and remainder_horizontal == 0
+ and remainder_vertical == 0
+ )
def combine_tensor_patches(
diff --git a/tests/contrib/test_combine_tensor_patch.py b/tests/contrib/test_combine_tensor_patch.py
index 4f824338f..a06a13203 100644
--- a/tests/contrib/test_combine_tensor_patch.py
+++ b/tests/contrib/test_combine_tensor_patch.py
@@ -126,6 +126,51 @@ class TestCombineTensorPatches(BaseTester):
assert recomb.shape == img.shape
self.assert_close(img, recomb)
+ @pytest.mark.parametrize(
+ "shape,window,stride,padding",
+ [
+ ((4, 6), (8, 8), (2, 2), (2, 2, 1, 1)),
+ ((4, 6), (9, 9), (2, 2), (2, 3, 1, 2)),
+ ((12, 6), (8, 8), (2, 2), (0, 0, 1, 1)),
+ ((6, 12), (8, 8), (2, 2), (1, 1, 0, 0)),
+ ((4, 15), (9, 8), (4, 3), (2, 3, 1, 1)),
+ ],
+ )
+ def test_image_smaller_than_window(self, device, dtype, shape, window, stride, padding):
+ image = torch.rand(2, 3, *shape, device=device, dtype=dtype)
+ assert kornia.contrib.compute_padding(shape, window, stride) == padding
+ patches = kornia.contrib.extract_tensor_patches(image, window, stride, allow_auto_padding=True)
+ top, bottom, left, right = padding
+ padded = torch.nn.functional.pad(image, (left, right, top, bottom))
+ expected = torch.stack(
+ [
+ padded[..., y : y + window[0], x : x + window[1]]
+ for y in range(0, padded.shape[-2] - window[0] + 1, stride[0])
+ for x in range(0, padded.shape[-1] - window[1] + 1, stride[1])
+ ],
+ dim=1,
+ )
+ self.assert_close(patches, expected)
+ restored = kornia.contrib.combine_tensor_patches(patches, shape, window, stride, allow_auto_unpadding=True)
+ self.assert_close(restored, image)
+
+ def test_small_image_gradcheck(self, device):
+ image = torch.rand(1, 1, 2, 3, device=device, dtype=torch.float64, requires_grad=True)
+
+ def roundtrip(x):
+ patches = kornia.contrib.extract_tensor_patches(x, (4, 5), 2, allow_auto_padding=True)
+ return kornia.contrib.combine_tensor_patches(patches, (2, 3), (4, 5), 2, allow_auto_unpadding=True)
+
+ self.gradcheck(roundtrip, (image,))
+
+ def test_small_image_dynamo(self, device, dtype, torch_optimizer):
+ image = torch.rand(2, 3, 4, 6, device=device, dtype=dtype)
+ op = torch.nn.Sequential(
+ kornia.contrib.ExtractTensorPatches((8, 8), stride=2, allow_auto_padding=True),
+ kornia.contrib.CombineTensorPatches((4, 6), (8, 8), stride=2, allow_auto_unpadding=True),
+ )
+ self.assert_close(torch_optimizer(op)(image), op(image))
+
def test_gradcheck(self, device):
patches = kornia.contrib.extract_tensor_patches(
torch.arange(16.0, device=device, dtype=torch.float64).view(1, 1, 4, 4), window_size=(2, 2), stride=(2, 2)
diff --git a/tests/contrib/test_extract_tensor_patch.py b/tests/contrib/test_extract_tensor_patch.py
index 174875f52..dc5411309 100644
--- a/tests/contrib/test_extract_tensor_patch.py
+++ b/tests/contrib/test_extract_tensor_patch.py
@@ -153,7 +153,7 @@ class TestExtractTensorPatches(BaseTester):
# 5 patches vertical, 6 2/3 = 7 horizontal = 35 patches
assert patches.shape == (1, 35, 1, *window_size)
- @pytest.mark.parametrize("img_shape, fits", [((4, 4), True), ((5, 4), False), ((4, 5), False)])
+ @pytest.mark.parametrize("img_shape, fits", [((4, 4), True), ((5, 4), False), ((4, 5), False), ((2, 2), True)])
def test_warns_when_the_window_does_not_fit(self, device, dtype, img_shape, fits):
# With window 2 and stride 2, a remainder along either axis alone leaves pixels uncovered.
img = torch.zeros(1, 1, *img_shape, device=device, dtype=dtype)