Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(augmentation): track integer-size Resize shapes under torch.export (#6027)

Parman Mohammadalizadeh704e09ee38a5
diff --git a/changelog.d/6027.fixed.md b/changelog.d/6027.fixed.md
new file mode 100644
index 000000000..bcfc20b60
--- /dev/null
+++ b/changelog.d/6027.fixed.md
@@ -0,0 +1 @@
+`torch.export` of an `AugmentationSequential` holding a `Resize` with an integer `size`, a `LongestMaxSize` or a `SmallestMaxSize` no longer fails in the container's shape tracking. The integer side length is now converted to the aspect-preserving output size, as the eager path already does, so a later shape-dependent transform such as a resample-mode crop sees the shape it actually receives.
diff --git a/kornia/augmentation/container/image.py b/kornia/augmentation/container/image.py
index d2150103a..9b76b814b 100644
--- a/kornia/augmentation/container/image.py
+++ b/kornia/augmentation/container/image.py
@@ -547,6 +547,8 @@ def _get_new_batch_shape(param: ParamItem, batch_shape: torch.Size, module: Opti
# Intermediate probabilities still require data-dependent control flow in forward.
if size is None or (isinstance(module, (K.CenterCrop3D, K.RandomCrop3D)) and module.p_batch == 0.0):
return batch_shape
+ if isinstance(module, K.Resize) and isinstance(size, int):
+ size = _side_to_image_size(size, batch_shape[-1] / batch_shape[-2], module.flags["side"])
new_batch_shape = list(batch_shape)
new_batch_shape[-data["output_size"].shape[-1] :] = [operator.index(dim) for dim in size]
return torch.Size(new_batch_shape)
diff --git a/tests/augmentation/test_torch_export.py b/tests/augmentation/test_torch_export.py
index 157da5119..c2d934ff7 100644
--- a/tests/augmentation/test_torch_export.py
+++ b/tests/augmentation/test_torch_export.py
@@ -147,6 +147,48 @@ def test_torch_export_container(name: str, factory: Callable[[], torch.nn.Module
torch.testing.assert_close(out, eager, atol=1e-5, rtol=1e-5)
+def _resize_int() -> torch.nn.Module:
+ return K.Resize(16)
+
+
+def _longest_max_size() -> torch.nn.Module:
+ return K.LongestMaxSize(16)
+
+
+def _smallest_max_size() -> torch.nn.Module:
+ return K.SmallestMaxSize(16)
+
+
+INT_SIZE_RESIZES: list[Tuple[str, Callable[[], torch.nn.Module]]] = [
+ ("Resize", _resize_int),
+ ("LongestMaxSize", _longest_max_size),
+ ("SmallestMaxSize", _smallest_max_size),
+]
+
+
[email protected](not hasattr(torch, "export"), reason="torch.export requires torch>=2.1")
[email protected](not dynamo_is_available(), reason=DYNAMO_UNAVAILABLE_REASON)
[email protected](not _HAS_TORCH_EXPORT_TRACKING, reason=_TORCH_EXPORT_TRACKING_REASON)
[email protected]("name,factory", INT_SIZE_RESIZES, ids=[n for n, _ in INT_SIZE_RESIZES])
[email protected]("then_crop", [False, True], ids=["alone", "then_resample_crop"])
+def test_torch_export_container_int_size_resize_6024(
+ name: str, factory: Callable[[], torch.nn.Module], then_crop: bool
+) -> None:
+ """An integer-size resize tracks its aspect-preserving output shape under ``torch.export`` (#6024)."""
+ torch.manual_seed(0)
+ x = torch.randn(2, 3, 24, 40)
+ augs = [factory()]
+ if then_crop:
+ augs.append(K.CenterCrop(8, cropping_mode="resample"))
+ seq = K.AugmentationSequential(*augs, data_keys=["input"])
+ eager = seq(x)
+ exported = torch.export.export(seq, (x,))
+ out = exported.module()(x)
+ assert out.shape == eager.shape, f"{name}: shape {out.shape} vs {eager.shape}"
+ torch.testing.assert_close(out, eager, atol=1e-5, rtol=1e-5)
+
+
def _mask_export_case(case: str) -> Tuple[torch.nn.Module, tuple, Callable[[object], torch.Tensor], torch.Tensor]:
"""Build a float64 image/mask pipeline for ``case``: the module, its arguments, a mask getter, the expected mask."""
image = torch.zeros(1, 1, 1, 2, dtype=torch.float64)