Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

few mypy fixes (#1724)

Edgar Riba511cdd1cb58b
diff --git a/kornia/augmentation/container/augment.py b/kornia/augmentation/container/augment.py
index 7f4fb1462..144b4004f 100644
--- a/kornia/augmentation/container/augment.py
+++ b/kornia/augmentation/container/augment.py
@@ -147,12 +147,7 @@ class AugmentationSequential(ImageSequential):
keepdim: Optional[bool] = None,
random_apply: Union[int, bool, Tuple[int, int]] = False,
random_apply_weights: Optional[List[float]] = None,
- extra_args: Dict[DataKey, Dict[str, Any]] = {
- DataKey.MASK: dict(
- resample=Resample.NEAREST,
- align_corners=True
- )
- }
+ extra_args: Dict[DataKey, Dict[str, Any]] = {DataKey.MASK: dict(resample=Resample.NEAREST, align_corners=True)},
) -> None:
super().__init__(
*args,
@@ -192,7 +187,7 @@ class AugmentationSequential(ImageSequential):
return eye_like(3, input)
@property
- def transform_matrix(self,) -> Optional[Tensor]:
+ def transform_matrix(self) -> Optional[Tensor]:
return self._transform_matrix
def inverse( # type: ignore
@@ -244,11 +239,15 @@ class AugmentationSequential(ImageSequential):
input = arg
for (name, module), param in zip_longest(list(self.get_forward_sequence(params))[::-1], params[::-1]):
if isinstance(module, (_AugmentationBase, ImageSequential)):
- param = params[name] if name in params else param
+ # TODO(jian): verify what's happening here. Mypy was complaining,
+ param = params[name] if name in params else param # type: ignore
else:
param = None
- if isinstance(module, IntensityAugmentationBase2D) and dcate in DataKey \
- and not isinstance(module, RandomErasing):
+ if (
+ isinstance(module, IntensityAugmentationBase2D)
+ and dcate in DataKey
+ and not isinstance(module, RandomErasing)
+ ):
pass # Do nothing
elif isinstance(module, ImageSequential) and module.is_intensity_only() and dcate in DataKey:
pass # Do nothing
@@ -259,12 +258,13 @@ class AugmentationSequential(ImageSequential):
input = input.view(batch_size, -1, *input.shape[1:])
elif isinstance(module, PatchSequential):
raise NotImplementedError("Geometric involved PatchSequential is not supported.")
- elif isinstance(module, (AugmentationSequential)) \
- and dcate in DataKey:
+ elif isinstance(module, (AugmentationSequential)) and dcate in DataKey:
# AugmentationSequential shall not take the extra_args arguments.
input = ApplyInverse.inverse_by_key(input, module, param, dcate)
- elif isinstance(module, (GeometricAugmentationBase2D, ImageSequential, RandomErasing)) \
- and dcate in DataKey:
+ elif (
+ isinstance(module, (GeometricAugmentationBase2D, ImageSequential, RandomErasing))
+ and dcate in DataKey
+ ):
input = ApplyInverse.inverse_by_key(input, module, param, dcate, extra_args=extra_args)
elif isinstance(module, (SequentialBase,)):
raise ValueError(f"Unsupported Sequential {module}.")
@@ -283,12 +283,7 @@ class AugmentationSequential(ImageSequential):
def __packup_output__( # type: ignore
self, output: List[Tensor], label: Optional[Tensor] = None
- ) -> Union[
- Tensor,
- Tuple[Tensor, Optional[Tensor]],
- List[Tensor],
- Tuple[List[Tensor], Optional[Tensor]],
- ]:
+ ) -> Union[Tensor, Tuple[Tensor, Optional[Tensor]], List[Tensor], Tuple[List[Tensor], Optional[Tensor]]]:
if len(output) == 1 and isinstance(output, (tuple, list)) and self.return_label:
return output[0], label
if len(output) == 1 and isinstance(output, (tuple, list)):
@@ -329,12 +324,7 @@ class AugmentationSequential(ImageSequential):
label: Optional[Tensor] = None,
params: Optional[List[ParamItem]] = None,
data_keys: Optional[List[Union[str, int, DataKey]]] = None,
- ) -> Union[
- Tensor,
- Tuple[Tensor, Optional[Tensor]],
- List[Tensor],
- Tuple[List[Tensor], Optional[Tensor]],
- ]:
+ ) -> 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:
@@ -394,23 +384,26 @@ class AugmentationSequential(ImageSequential):
for param in params:
module = self.get_submodule(param.name)
- if isinstance(module, IntensityAugmentationBase2D) and dcate in DataKey \
- and not isinstance(module, RandomErasing):
+ if (
+ isinstance(module, IntensityAugmentationBase2D)
+ and dcate in DataKey
+ and not isinstance(module, RandomErasing)
+ ):
pass # Do nothing
elif isinstance(module, ImageSequential) and module.is_intensity_only() and dcate in DataKey:
pass # Do nothing
elif isinstance(module, VideoSequential) and dcate not in [DataKey.INPUT, DataKey.MASK]:
batch_size: int = input.size(0)
input = input.view(-1, *input.shape[2:])
- input, label = ApplyInverse.apply_by_key(
- input, label, module, param, dcate, extra_args=extra_args)
+ input, label = ApplyInverse.apply_by_key(input, label, module, param, dcate, extra_args=extra_args)
input = input.view(batch_size, -1, *input.shape[1:])
elif isinstance(module, PatchSequential):
raise NotImplementedError("Geometric involved PatchSequential is not supported.")
- elif isinstance(module, (GeometricAugmentationBase2D, ImageSequential, RandomErasing)) \
- and dcate in DataKey:
- input, label = ApplyInverse.apply_by_key(
- input, label, module, param, dcate, extra_args=extra_args)
+ elif (
+ isinstance(module, (GeometricAugmentationBase2D, ImageSequential, RandomErasing))
+ and dcate in DataKey
+ ):
+ input, label = ApplyInverse.apply_by_key(input, label, module, param, dcate, extra_args=extra_args)
elif isinstance(module, (SequentialBase,)):
raise ValueError(f"Unsupported Sequential {module}.")
else:
diff --git a/kornia/augmentation/container/image.py b/kornia/augmentation/container/image.py
index 727b53d7d..8ca21d959 100644
--- a/kornia/augmentation/container/image.py
+++ b/kornia/augmentation/container/image.py
@@ -208,7 +208,7 @@ class ImageSequential(SequentialBase):
label: Optional[Tensor],
module: Optional[nn.Module],
param: ParamItem,
- extra_args: Dict[str, Any]
+ extra_args: Dict[str, Any],
) -> Tuple[Tensor, Optional[Tensor]]:
if module is None:
module = self.get_submodule(param.name)
@@ -239,9 +239,7 @@ class ImageSequential(SequentialBase):
return True
return False
- def __packup_output__(
- self, output: Tensor, label: Optional[Tensor] = None
- ) -> Union[Tensor, Tuple[Tensor, Tensor]]:
+ def __packup_output__(self, output: Tensor, label: Optional[Tensor] = None) -> Union[Tensor, Tuple[Tensor, Tensor]]:
if self.return_label:
return output, label # type: ignore
# Implicitly indicating the label cannot be optional since there is a mix aug
@@ -252,8 +250,11 @@ class ImageSequential(SequentialBase):
return kornia.eye_like(3, input)
def get_transformation_matrix(
- self, input: Tensor, params: Optional[List[ParamItem]] = None, recompute: bool = False,
- extra_args: Dict[str, Any] = {}
+ self,
+ input: Tensor,
+ params: Optional[List[ParamItem]] = None,
+ recompute: bool = False,
+ extra_args: Dict[str, Any] = {},
) -> Optional[Tensor]:
"""Compute the transformation matrix according to the provided parameters.
@@ -269,10 +270,12 @@ class ImageSequential(SequentialBase):
# Define as 1 for broadcasting
res_mat: Optional[Tensor] = None
- for (_, module), param in zip(named_modules, params if params is not None else []):
+ params_list: List[ParamItem] = params if params is not None else []
+
+ for (_, module), param_item in zip(named_modules, params_list):
if isinstance(module, (_AugmentationBase,)) and not isinstance(module, (MixAugmentationBase,)):
- pdata = cast(Dict[str, Tensor], param.data)
- to_apply = pdata['batch_prob'] # type: ignore
+ param_data = cast(Dict[str, Tensor], param_item.data)
+ to_apply = param_data['batch_prob'] # type: ignore
ori_shape = input.shape
try:
input = module.transform_tensor(input)
@@ -283,8 +286,7 @@ class ImageSequential(SequentialBase):
if recompute:
mat: Tensor = self.identity_matrix(input)
flags = override_parameters(module.flags, extra_args, in_place=False)
- mat[to_apply] = module.compute_transformation(
- input[to_apply], pdata, flags) # type: ignore
+ mat[to_apply] = module.compute_transformation(input[to_apply], param_data, flags) # type: ignore
else:
mat = torch.as_tensor(module._transform_matrix, device=input.device, dtype=input.dtype)
res_mat = mat if res_mat is None else mat @ res_mat
@@ -296,8 +298,10 @@ class ImageSequential(SequentialBase):
if isinstance(module, (kornia.augmentation.AugmentationSequential,)) and not recompute:
mat = torch.as_tensor(module._transform_matrix, device=input.device, dtype=input.dtype)
else:
+ maybe_param_data = cast(Optional[List[ParamItem]], param_item.data)
_mat = module.get_transformation_matrix(
- input, param.data, recompute=recompute, extra_args=extra_args) # type: ignore
+ input, maybe_param_data, recompute=recompute, extra_args=extra_args
+ ) # type: ignore
mat = module.identity_matrix(input) if _mat is None else _mat
res_mat = mat if res_mat is None else mat @ res_mat
return res_mat
@@ -326,8 +330,7 @@ class ImageSequential(SequentialBase):
return True
def inverse(
- self, input: Tensor, params: Optional[List[ParamItem]] = None,
- extra_args: Dict[str, Any] = {}
+ self, input: Tensor, params: Optional[List[ParamItem]] = None, extra_args: Dict[str, Any] = {}
) -> Tensor:
"""Inverse transformation.
@@ -343,17 +346,17 @@ class ImageSequential(SequentialBase):
params = self._params
for (name, module), param in zip_longest(list(self.get_forward_sequence(params))[::-1], params[::-1]):
+ maybe_param: Optional[ParamItem] = None
if isinstance(module, (_AugmentationBase, ImageSequential)):
- param = params[name] if name in params else param
- else:
- param = None
+ maybe_param = params[name] if name in params else param # type: ignore
if isinstance(module, IntensityAugmentationBase2D):
pass # Do nothing
elif isinstance(module, ImageSequential) and module.is_intensity_only():
pass # Do nothing
- elif isinstance(module, ImageSequential):
- input = module.inverse(input, param.data, extra_args=extra_args)
+ elif isinstance(module, ImageSequential) and maybe_param is not None:
+ param_data = cast(List[ParamItem], cast(ParamItem, maybe_param).data)
+ input = module.inverse(input, param_data, extra_args=extra_args)
elif isinstance(module, (GeometricAugmentationBase2D,)):
input = self.apply_inverse_func.inverse(input, module, param, extra_args=extra_args)
else:
@@ -367,7 +370,7 @@ class ImageSequential(SequentialBase):
input: Tensor,
label: Optional[Tensor] = None,
params: Optional[List[ParamItem]] = None,
- extra_args: Dict[str, Any] = {}
+ extra_args: Dict[str, Any] = {},
) -> Union[Tensor, Tuple[Tensor, Tensor]]:
self.clear_state()
if params is None: