Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Pass along data_keys or extra_args in *ApplyInverse with containers (#2046)

Miquel Martíbb433c1004b4
diff --git a/kornia/augmentation/container/augment.py b/kornia/augmentation/container/augment.py
index 27c36af05..ae53f9b09 100644
--- a/kornia/augmentation/container/augment.py
+++ b/kornia/augmentation/container/augment.py
@@ -156,7 +156,7 @@ class AugmentationSequential(ImageSequential):
random_apply_weights=random_apply_weights,
)
- self.data_keys = [DataKey.get(inp) for inp in data_keys]
+ self.data_keys: List[DataKey] = [DataKey.get(inp) for inp in data_keys]
if not all(in_type in DataKey for in_type in self.data_keys):
raise AssertionError(f"`data_keys` must be in {DataKey}. Got {data_keys}.")
@@ -200,15 +200,11 @@ class AugmentationSequential(ImageSequential):
``self.data_keys`` by default.
"""
if data_keys is None:
- data_keys = cast(List[Union[str, int, DataKey]], self.data_keys)
-
- _data_keys: List[DataKey] = [DataKey.get(inp) for inp in data_keys]
+ _data_keys = self.data_keys
+ else:
+ _data_keys = [DataKey.get(inp) for inp in data_keys]
- if len(args) != len(data_keys):
- raise AssertionError(
- "The number of inputs must align with the number of data_keys, "
- f"Got {len(args)} and {len(data_keys)}."
- )
+ self._validate_args_datakeys(*args, data_keys=_data_keys)
args = self._arguments_preproc(*args, data_keys=_data_keys)
@@ -220,11 +216,11 @@ class AugmentationSequential(ImageSequential):
)
params = self._params
- outputs: List[Tensor] = [None] * len(data_keys) # type: ignore
- for idx, (arg, dcate) in enumerate(zip(args, data_keys)):
+ outputs: List[Tensor] = [None] * len(_data_keys) # type: ignore
+ for idx, (arg, dcate) in enumerate(zip(args, _data_keys)):
- if DataKey.INPUT in self.extra_args:
- extra_args = self.extra_args[DataKey.INPUT]
+ if dcate in self.extra_args:
+ extra_args = self.extra_args[dcate]
else:
extra_args = {}
@@ -324,12 +320,11 @@ class AugmentationSequential(ImageSequential):
data_keys: Optional[List[Union[str, int, DataKey]]] = None,
) -> Union[Tensor, Tuple[Tensor, Optional[Tensor]], List[Tensor], Tuple[List[Tensor], Optional[Tensor]]]:
"""Compute multiple tensors simultaneously according to ``self.data_keys``."""
- _data_keys: List[DataKey]
if data_keys is None:
_data_keys = self.data_keys
else:
_data_keys = [DataKey.get(inp) for inp in data_keys]
- self.data_keys = _data_keys
+
self._validate_args_datakeys(*args, data_keys=_data_keys)
args = self._arguments_preproc(*args, data_keys=_data_keys)
diff --git a/kornia/augmentation/container/utils.py b/kornia/augmentation/container/utils.py
index d5a9eb74d..23edd42f7 100644
--- a/kornia/augmentation/container/utils.py
+++ b/kornia/augmentation/container/utils.py
@@ -213,7 +213,12 @@ class InputApplyInverse(ApplyInverseImpl):
temp = module.apply_inverse_func
module.apply_inverse_func = InputApplyInverse
if isinstance(module, kornia.augmentation.AugmentationSequential):
- input = cast(Tensor, module.inverse(input, params=None if param is None else cast(List, param.data)))
+ input = cast(
+ Tensor,
+ module.inverse(
+ input, params=None if param is None else cast(List, param.data), data_keys=[cls.data_key]
+ ),
+ )
else:
input = module.inverse(
input, params=None if param is None else cast(List, param.data), extra_args=extra_args
@@ -275,7 +280,14 @@ class MaskApplyInverse(ApplyInverseImpl):
temp = module.apply_inverse_func
module.apply_inverse_func = MaskApplyInverse
geo_param: List[ParamItem] = _get_geometric_only_param(module, _param)
- input = cls.make_input_only_sequential(module)(input, label=None, params=geo_param)
+ if isinstance(module, kornia.augmentation.AugmentationSequential):
+ input = cls.make_input_only_sequential(module)(
+ input, label=None, params=geo_param, data_keys=[cls.data_key]
+ )
+ else:
+ input = cls.make_input_only_sequential(module)(
+ input, label=None, params=geo_param, extra_args=extra_args
+ )
module.apply_inverse_func = temp
else:
pass # No need to update anything
@@ -299,7 +311,17 @@ class MaskApplyInverse(ApplyInverseImpl):
elif isinstance(module, kornia.augmentation.ImageSequential):
temp = module.apply_inverse_func
module.apply_inverse_func = MaskApplyInverse
- input = module.inverse(input, params=None if param is None else cast(List, param.data))
+ if isinstance(module, kornia.augmentation.AugmentationSequential):
+ input = cast(
+ Tensor,
+ module.inverse(
+ input, params=None if param is None else cast(List, param.data), data_keys=[cls.data_key]
+ ),
+ )
+ else:
+ input = module.inverse(
+ input, params=None if param is None else cast(List, param.data), extra_args=extra_args
+ )
module.apply_inverse_func = temp
return input
diff --git a/test/augmentation/test_container.py b/test/augmentation/test_container.py
index 0a7e3c957..7948b892c 100644
--- a/test/augmentation/test_container.py
+++ b/test/augmentation/test_container.py
@@ -440,12 +440,14 @@ class TestAugmentationSequential:
assert out[1].shape == mask.shape
assert out[2].shape == bbox.shape
assert out[3].shape == keypoints.shape
+ assert set(out[1].unique().tolist()).issubset(set(mask.unique().tolist()))
out_inv = aug.inverse(*out)
assert out_inv[0].shape == inp.shape
assert out_inv[1].shape == mask.shape
assert out_inv[2].shape == bbox.shape
assert out_inv[3].shape == keypoints.shape
+ assert set(out_inv[1].unique().tolist()).issubset(set(mask.unique().tolist()))
if random_apply is False:
reproducibility_test((inp, mask, bbox, keypoints), aug)