π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(augmentation): normalize a resample override in AugmentationSequential extra_args (#4859)
RitiGroverf8cea3d5315d
diff --git a/changelog.d/4859.fixed.md b/changelog.d/4859.fixed.mdnew file mode 100644index 000000000..9b00f6cff--- /dev/null+++ b/changelog.d/4859.fixed.md@@ -0,0 +1,2 @@+`AugmentationSequential` now normalizes a string or int `resample` in `extra_args` as the augmentation+constructors do. It used to raise `AttributeError` wherever the mask was resampled. (#4859)diff --git a/kornia/augmentation/container/augment.py b/kornia/augmentation/container/augment.pyindex 4998519d8..0f5d8713c 100644--- a/kornia/augmentation/container/augment.py+++ b/kornia/augmentation/container/augment.py@@ -98,9 +98,8 @@ class AugmentationSequential(TransformMatrixMinIn, ImageSequential)::class:`~kornia.augmentation.RandomResizedCrop` in the default ``cropping_mode='slice'``ignores it for nearest resampling.:class:`~kornia.augmentation.RandomElasticTransform` has its own mask path and honours- both entries. Unlike the constructors, the override is not normalized: a string ``resample``- raises ``AttributeError`` wherever the mask is resampled, so pass a- ``kornia.constants.Resample`` member (`#4815 <https://github.com/kornia/kornia/issues/4815>`_).+ both entries. As in the constructors, ``resample`` may be a string, an int or a+ ``kornia.constants.Resample`` member.Convention:- each child keeps the contract of its own base and class; mix and 3D children do not inherit every@@ -351,7 +350,12 @@ class AugmentationSequential(TransformMatrixMinIn, ImageSequential):if isinstance(arg, AugmentationBase3D):self.contains_3d_augmentation = Trueself._transform_matrix = None- self.extra_args = extra_args or {DataKey.MASK: {"resample": Resample.NEAREST, "align_corners": None}}+ extra_args = extra_args or {DataKey.MASK: {"resample": Resample.NEAREST, "align_corners": None}}+ # Normalize a ``resample`` override as the constructors do, so a string or int works like a member.+ self.extra_args = {+ key: {**value, "resample": Resample.get(value["resample"])} if "resample" in value else value+ for key, value in extra_args.items()+ }def clear_state(self) -> None:"""Reset cached params and transformation-matrix state."""diff --git a/tests/augmentation/container/test_augmentation_sequential.py b/tests/augmentation/container/test_augmentation_sequential.pyindex b8206f5d9..512553ea2 100644--- a/tests/augmentation/container/test_augmentation_sequential.py+++ b/tests/augmentation/container/test_augmentation_sequential.py@@ -1289,19 +1289,25 @@ class TestConventionAugmentationSequential(BaseTester):assert shape(True, False) == (3, 6, 8)@pytest.mark.parametrize("factory", [lambda: K.RandomAffine(30.0, p=1.0), lambda: K.RandomElasticTransform(p=1.0)])- def test_wart_extra_args_mask_resample_must_be_a_resample_member_4815(self, factory, device, dtype):- # #4815: flips when the container normalizes a string override as the constructors do, or rejects it with- # a kornia error.+ def test_extra_args_mask_resample_accepts_a_string_or_int_4815(self, factory, device, dtype):+ # #4815: the container normalizes a `resample` override as the constructors do.image = torch.rand(1, 1, 6, 8, device=device, dtype=dtype)- for resample, raises in ((Resample.NEAREST, False), ("nearest", True)):+ masks = []+ for resample in (Resample.NEAREST, "nearest", "NEAREST", 0):seq = K.AugmentationSequential(factory(), data_keys=["input", "mask"], extra_args={DataKey.MASK: {"resample": resample}})- if raises:- with pytest.raises(AttributeError):- seq(image, image.clone())- else:- assert seq(image, image.clone())[1].shape == image.shape+ assert seq.extra_args[DataKey.MASK]["resample"] is Resample.NEAREST+ torch.manual_seed(0)+ masks.append(seq(image, image.clone())[1])+ for mask in masks[1:]:+ assert torch.equal(mask, masks[0])++ def test_extra_args_rejects_an_unknown_resample_4815(self):+ with pytest.raises(KeyError):+ K.AugmentationSequential(+ K.RandomAffine(30.0, p=1.0), data_keys=["input", "mask"], extra_args={DataKey.MASK: {"resample": "foo"}}+ )def test_extra_args_mask_override_reaches_the_sampler_4419(self, device, dtype):# #4419: both halves of `extra_args[DataKey.MASK]` reach the sampler; `RandomElasticTransform`, which has