π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(augmentation): repair regressions from ONNX-exportability refactor (#3779)
Edgar Riba5b106d6885cd
diff --git a/kornia/augmentation/_2d/base.py b/kornia/augmentation/_2d/base.pyindex 9f395dce5..15193fae1 100644--- a/kornia/augmentation/_2d/base.py+++ b/kornia/augmentation/_2d/base.py@@ -101,7 +101,7 @@ class RigidAffineAugmentationBase2D(AugmentationBase2D):) -> torch.Tensor:"""Generate transformation matrices with the given input and param settings."""batch_prob = params["batch_prob"]- to_apply = batch_prob > 0.5+ to_apply = torch.atleast_1d(batch_prob > 0.5)in_tensor = self.transform_tensor(input)diff --git a/kornia/augmentation/_2d/geometric/base.py b/kornia/augmentation/_2d/geometric/base.pyindex 7837dfc5e..a607c7343 100644--- a/kornia/augmentation/_2d/geometric/base.py+++ b/kornia/augmentation/_2d/geometric/base.py@@ -217,7 +217,7 @@ class GeometricAugmentationBase2D(RigidAffineAugmentationBase2D):in_tensor = self.transform_tensor(input)output = in_tensor.clone()batch_prob = params["batch_prob"]- to_apply = batch_prob > 0.5 # NOTE: in case of Relaxed Distributions.+ to_apply = torch.atleast_1d(batch_prob > 0.5)params, flags = self._process_kwargs_to_params_and_flags(self._params if params is None else params, flags, **kwargs@@ -309,7 +309,7 @@ class GeometricAugmentationBase2D(RigidAffineAugmentationBase2D):) -> Boxes:output = input.clone()batch_prob = params["batch_prob"]- to_apply = batch_prob > 0.5 # NOTE: in case of Relaxed Distributions.+ to_apply = torch.atleast_1d(batch_prob > 0.5)if transform is None:raise RuntimeError("`transform` has to be a torch.Tensor. Got None.")@@ -349,7 +349,7 @@ class GeometricAugmentationBase2D(RigidAffineAugmentationBase2D):"""output = input.clone()batch_prob = params["batch_prob"]- to_apply = batch_prob > 0.5 # NOTE: in case of Relaxed Distributions.+ to_apply = torch.atleast_1d(batch_prob > 0.5)if transform is None:raise RuntimeError("`transform` has to be a torch.Tensor. Got None.")diff --git a/kornia/augmentation/_2d/intensity/gaussian_illumination.py b/kornia/augmentation/_2d/intensity/gaussian_illumination.pyindex 84a28c907..663a7e85f 100644--- a/kornia/augmentation/_2d/intensity/gaussian_illumination.py+++ b/kornia/augmentation/_2d/intensity/gaussian_illumination.py@@ -170,7 +170,9 @@ class RandomGaussianIllumination(IntensityAugmentationBase2D):# We must explicitly move it to the input's device before adding.gradient = params["gradient"].to(input.device, input.dtype)- return input.add_(gradient).clamp_(0, 1)+ # Out-of-place add: in-place would mutate the caller's tensor and corrupt+ # the non-transformed branch, which shares the same input.+ return input.add(gradient).clamp_(0, 1)self._fn = _apply_transformdiff --git a/kornia/augmentation/_2d/intensity/planckian_jitter.py b/kornia/augmentation/_2d/intensity/planckian_jitter.pyindex d04c3f659..16c178d79 100644--- a/kornia/augmentation/_2d/intensity/planckian_jitter.py+++ b/kornia/augmentation/_2d/intensity/planckian_jitter.py@@ -148,14 +148,14 @@ class RandomPlanckianJitter(IntensityAugmentationBase2D):[ 1.4437, 0.2660]]],<BLANKLINE><BLANKLINE>- [[[ 0.2465, 1.0000],- [-0.2125, -0.1653]],+ [[[ 0.1268, 0.6658],+ [-0.1093, -0.0850]],<BLANKLINE>[[ 0.9318, 1.0000],[ 1.0000, 0.0537]],<BLANKLINE>- [[ 0.2426, -0.1621],- [-0.3302, -0.9093]]]])+ [[ 0.9134, -0.6101],+ [-1.2430, -3.4228]]]])"""diff --git a/kornia/augmentation/_2d/mix/base.py b/kornia/augmentation/_2d/mix/base.pyindex 7c629dd8a..8e40be7dd 100644--- a/kornia/augmentation/_2d/mix/base.py+++ b/kornia/augmentation/_2d/mix/base.py@@ -89,7 +89,7 @@ class MixAugmentationBaseV2(_BasicAugmentationBase):self, input: torch.Tensor, params: Dict[str, torch.Tensor], flags: Dict[str, Any]) -> torch.Tensor:batch_prob = params["batch_prob"]- to_apply = batch_prob > 0.5+ to_apply = torch.atleast_1d(batch_prob > 0.5)ori_shape = input.shapein_tensor = self.transform_tensor(input)@@ -124,7 +124,7 @@ class MixAugmentationBaseV2(_BasicAugmentationBase):self, input: torch.Tensor, params: Dict[str, torch.Tensor], flags: Dict[str, Any]) -> torch.Tensor:batch_prob = params["batch_prob"]- to_apply = batch_prob > 0.5 # NOTE: in case of Relaxed Distributions.+ to_apply = torch.atleast_1d(batch_prob > 0.5)output = inputif sum(to_apply) != len(to_apply):output = self.apply_non_transform_mask(input, params, flags)@@ -141,7 +141,7 @@ class MixAugmentationBaseV2(_BasicAugmentationBase):raise RuntimeError(f"Only BxNx4x2 torch.Tensor is supported. Got {input.shape}.")input = Boxes(input, False, mode="vertices_plus")batch_prob = params["batch_prob"]- to_apply = batch_prob > 0.5 # NOTE: in case of Relaxed Distributions.+ to_apply = torch.atleast_1d(batch_prob > 0.5)output = inputif sum(to_apply) != len(to_apply):output = self.apply_non_transform_boxes(input, params, flags)@@ -153,7 +153,7 @@ class MixAugmentationBaseV2(_BasicAugmentationBase):self, input: torch.Tensor, params: Dict[str, torch.Tensor], flags: Dict[str, Any]) -> torch.Tensor:batch_prob = params["batch_prob"]- to_apply = batch_prob > 0.5 # NOTE: in case of Relaxed Distributions.+ to_apply = torch.atleast_1d(batch_prob > 0.5)output = inputif sum(to_apply) != len(to_apply):output = self.apply_non_transform_keypoint(input, params, flags)@@ -165,7 +165,7 @@ class MixAugmentationBaseV2(_BasicAugmentationBase):self, input: torch.Tensor, params: Dict[str, torch.Tensor], flags: Dict[str, Any]) -> torch.Tensor:batch_prob = params["batch_prob"]- to_apply = batch_prob > 0.5 # NOTE: in case of Relaxed Distributions.+ to_apply = torch.atleast_1d(batch_prob > 0.5)output = inputif sum(to_apply) != len(to_apply):output = self.apply_non_transform_class(input, params, flags)diff --git a/kornia/augmentation/_2d/mix/jigsaw.py b/kornia/augmentation/_2d/mix/jigsaw.pyindex b56feb8cc..ba2f1dd8a 100644--- a/kornia/augmentation/_2d/mix/jigsaw.py+++ b/kornia/augmentation/_2d/mix/jigsaw.py@@ -72,14 +72,10 @@ class RandomJigsaw(MixAugmentationBaseV2):def apply_transform(self, input: torch.Tensor, params: Dict[str, torch.Tensor], maybe_flags: Optional[Dict[str, Any]] = None) -> torch.Tensor:- # different from the Base class routine. This function will not refer to any non-transformation images.- batch_prob = params["batch_prob"]- to_apply = batch_prob > 0.5 # NOTE: in case of Relaxed Distributions.- input = input[to_apply].clone()-+ # Operate on the full batch: params are full-batch sized and the base class+ # where-blends this result with the non-transformed branch per `batch_prob`.b, c, h, w = input.shape- # Params are generated for the full batch; slice to subset.- perm = params["permutation"][to_apply]+ perm = params["permutation"]# Note: with a 100x100 image and a grid size of 3x3, it could work if# we make h = piece_size_h * self.flags["grid"][0] with one pixel loss, then resize to 100 x 100.# Probably worth to check if we should tolerate such "errorness" or to raise it as an error.diff --git a/kornia/augmentation/_3d/base.py b/kornia/augmentation/_3d/base.pyindex b8d208b14..c251bd331 100644--- a/kornia/augmentation/_3d/base.py+++ b/kornia/augmentation/_3d/base.py@@ -93,7 +93,7 @@ class RigidAffineAugmentationBase3D(AugmentationBase3D):) -> torch.Tensor:"""Generate transformation matrices with the given input and param settings."""batch_prob = params["batch_prob"]- to_apply = batch_prob > 0.5+ to_apply = torch.atleast_1d(batch_prob > 0.5)in_tensor = self.transform_tensor(input)diff --git a/kornia/augmentation/base.py b/kornia/augmentation/base.pyindex c213baa3c..242659fcb 100644--- a/kornia/augmentation/base.py+++ b/kornia/augmentation/base.py@@ -313,7 +313,7 @@ class _AugmentationBase(_BasicAugmentationBase):)batch_prob = params["batch_prob"]- to_apply = batch_prob > 0.5+ to_apply = torch.atleast_1d(batch_prob > 0.5)ori_shape = input.shapein_tensor = self.transform_tensor(input)@@ -356,7 +356,7 @@ class _AugmentationBase(_BasicAugmentationBase):)batch_prob = params["batch_prob"]- to_apply = batch_prob > 0.5+ to_apply = torch.atleast_1d(batch_prob > 0.5)ori_shape = input.shapeshape = params["forward_input_shape"]@@ -398,7 +398,7 @@ class _AugmentationBase(_BasicAugmentationBase):)batch_prob = params["batch_prob"]- to_apply = batch_prob > 0.5+ to_apply = torch.atleast_1d(batch_prob > 0.5)output_transformed = self.apply_transform_box(input, params, flags, transform=transform)output_not_transformed = self.apply_non_transform_box(input, params, flags, transform=transform)@@ -438,7 +438,7 @@ class _AugmentationBase(_BasicAugmentationBase):)batch_prob = params["batch_prob"]- to_apply = batch_prob > 0.5+ to_apply = torch.atleast_1d(batch_prob > 0.5)output_transformed = self.apply_transform_keypoint(input, params, flags, transform=transform)output_not_transformed = self.apply_non_transform_keypoint(input, params, flags, transform=transform)@@ -473,7 +473,7 @@ class _AugmentationBase(_BasicAugmentationBase):)batch_prob = params["batch_prob"]- to_apply = batch_prob > 0.5+ to_apply = torch.atleast_1d(batch_prob > 0.5)output_transformed = self.apply_transform_class(input, params, flags, transform=transform)output_not_transformed = self.apply_non_transform_class(input, params, flags, transform=transform)diff --git a/kornia/filters/gaussian.py b/kornia/filters/gaussian.pyindex 56fc9d05b..9c8574f03 100644--- a/kornia/filters/gaussian.py+++ b/kornia/filters/gaussian.py@@ -98,9 +98,10 @@ def gaussian_blur2d(KORNIA_CHECK_IS_TENSOR(sigma)sigma = sigma.to(device=input.device, dtype=input.dtype)- # Validate sigma values are positiveKORNIA_CHECK_SHAPE(sigma, ["B", "2"])- KORNIA_CHECK(bool((sigma > 0).all()), f"sigma must be positive, got {sigma}")+ # `bool()` on a tensor is untraceable by dynamo; skip the data-dependent check under compile.+ if not torch.compiler.is_compiling():+ KORNIA_CHECK(bool((sigma > 0).all()), f"sigma must be positive, got {sigma}")if separable:ky, kx = _unpack_2d_ks(kernel_size)diff --git a/tests/models/test_rtdetr_onnx.py b/tests/models/test_rtdetr_onnx.pyindex 80d2456c6..fe1ada9b1 100644--- a/tests/models/test_rtdetr_onnx.py+++ b/tests/models/test_rtdetr_onnx.py@@ -22,6 +22,7 @@ import pytestfrom kornia.models.rt_detr.model import RTDETR, RTDETRConfig[email protected](120)def test_rtdetr_to_onnx(tmp_path):"""RT-DETR exports to ONNX with correct dual output names."""onnx = pytest.importorskip("onnx")diff --git a/tests/models/test_sam_onnx.py b/tests/models/test_sam_onnx.pyindex 61fe2622b..dc33b4133 100644--- a/tests/models/test_sam_onnx.py+++ b/tests/models/test_sam_onnx.py@@ -27,6 +27,7 @@ def test_sam_has_to_onnx():assert hasattr(Sam, "to_onnx")[email protected](120)def test_sam_encoder_to_onnx(tmp_path):"""Sam.to_onnx() exports the image encoder subgraph with correct output name."""onnx = pytest.importorskip("onnx")