π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
Fix #1520 (#1521)
Jian Shi314d6b80886a
diff --git a/kornia/augmentation/container/augment.py b/kornia/augmentation/container/augment.pyindex ef5f9c80e..bf0160e70 100644--- a/kornia/augmentation/container/augment.py+++ b/kornia/augmentation/container/augment.py@@ -181,11 +181,17 @@ class AugmentationSequential(ImageSequential):"""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]+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)}.")++ args = self._arguments_preproc(*args, data_keys=_data_keys)+if params is None:if self._params is None:raise ValueError(@@ -194,10 +200,15 @@ class AugmentationSequential(ImageSequential):)params = self._params- outputs = []- for input, dcate in zip(args, data_keys):- if dcate == DataKey.INPUT and isinstance(input, (tuple, list)):- input, _ = input # ignore the transformation matrix whilst inverse+ outputs: List[torch.Tensor] = [None] * len(data_keys) # type: ignore+ for idx, (arg, dcate) in enumerate(zip(args, data_keys)):+ if dcate == DataKey.INPUT and isinstance(arg, (tuple, list)):+ input, _ = arg # ignore the transformation matrix whilst inverse+ # Using tensors straight-away+ if isinstance(arg, (Boxes,)):+ input = arg.data # all boxes are in (B, N, 4, 2) format now.+ else:+ input = argfor (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@@ -220,7 +231,11 @@ class AugmentationSequential(ImageSequential):raise ValueError(f"Unsupported Sequential {module}.")else:raise NotImplementedError(f"data_key {dcate} is not implemented for {module}.")- outputs.append(input)+ if isinstance(arg, (Boxes,)):+ arg._data = input+ outputs[idx] = arg.to_tensor()+ else:+ outputs[idx] = inputif len(outputs) == 1 and isinstance(outputs, (tuple, list)):return outputs[0]@@ -282,10 +297,12 @@ class AugmentationSequential(ImageSequential):Tuple[List[AugmentationSequentialInput], Optional[torch.Tensor]],]:"""Compute multiple tensors simultaneously according to ``self.data_keys``."""+ _data_keys: List[DataKey]if data_keys is None:_data_keys = self.data_keyselse:- _data_keys = list(DataKey.get(inp) for inp in data_keys)+ _data_keys = [DataKey.get(inp) for inp in data_keys]+ self.data_keys = _data_keysself._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.pyindex ebe62af50..10064fe12 100644--- a/kornia/augmentation/container/utils.py+++ b/kornia/augmentation/container/utils.py@@ -1,3 +1,4 @@+import warningsfrom abc import ABCMeta, abstractmethodfrom functools import partialfrom typing import Callable, Dict, Iterator, List, Optional, Tuple, Type, Union, cast@@ -6,8 +7,7 @@ import torchimport torch.nn as nnimport kornia # lazy loading for circular dependencies-from kornia.augmentation._2d.geometric.base import GeometricAugmentationBase2D-from kornia.augmentation._2d.mix.base import MixAugmentationBase+from kornia.augmentation import GeometricAugmentationBase2D, MixAugmentationBase, RandomCropfrom kornia.augmentation.base import TensorWithTransformMat, _AugmentationBasefrom kornia.augmentation.container.base import ParamItemfrom kornia.constants import DataKey@@ -269,38 +269,62 @@ class BBoxApplyInverse(ApplyInverseImpl):This is for transform boxes in the format (B, N, 4, 2)."""- apply_func = partial(transform_bbox, mode="xyxy", restore_coordinates=True)+ @classmethod+ def _get_padding_size(cls, module: nn.Module, param: Optional[ParamItem]) -> Optional[torch.Tensor]:+ if isinstance(module, RandomCrop):+ _param = cast(Dict[str, torch.Tensor], param.data) # type: ignore+ return _param.get("padding_size")+ return None+ @classmethod+ def pad(cls, input: torch.Tensor, padding_size: torch.Tensor) -> torch.Tensor:+ """+ Args:+ input: (B, N, 4, 2)+ padding_size: (B, 4)+ """+ assert len(input.shape) in (3, 4,), input.shape+ assert len(padding_size.shape) == 2, padding_size.shape-class BBoxXYXYApplyInverse(ApplyInverseImpl):- """Apply and inverse transformations for bounding box tensors.+ _input = input.clone()- This is for transform boxes in the format [xmin, ymin, xmax, ymax].- """+ if input.dim() == 3:+ # B,4,2 to B,1,4,2+ _input = _input[:, None]- apply_func = partial(transform_bbox, mode="xyxy", restore_coordinates=True)+ _input[..., 0] += padding_size[..., None, :1] # left padding+ _input[..., 1] += padding_size[..., None, 2:3] # top padding- @classmethod- def _get_padding_size(cls, module, param):- if isinstance(module, GeometricAugmentationBase2D): # TODO: Or only RandomCrop?- _param = cast(Dict[str, torch.Tensor], param.data) # type: ignore- return _param.get("padding_size")- else:- return None+ if input.dim() == 3:+ _input = _input[:, 0] # squeeze back- @classmethod- def pad(cls, input, padding_size):- for i in range(len(padding_size)):- input[i, :, 0::2] += padding_size[i][0] # left padding- input[i, :, 1::2] += padding_size[i][2] # top padding- return input+ return _input@classmethod- def unpad(cls, input, padding_size):- for i in range(len(padding_size)):- input[i, :, 0::2] -= padding_size[i][0] # left padding- input[i, :, 1::2] -= padding_size[i][2] # top padding- return input+ def unpad(cls, input: torch.Tensor, padding_size: torch.Tensor) -> torch.Tensor:+ """+ Args:+ input: (B, N, 4, 2)+ padding_size: (B, 4)+ """+ assert len(input.shape) in (3, 4,), input.shape+ assert len(padding_size.shape) == 2, padding_size.shape++ _input = input.clone()++ if input.dim() == 3:+ # B,4,2 to B,1,4,2+ _input = _input[:, None]++ _input[..., 0] -= padding_size[..., None, :1] # left padding+ _input[..., 1] -= padding_size[..., None, 2:3] # top padding++ if input.dim() == 3:+ _input = _input[:, 0] # squeeze back++ return _input++ apply_func = partial(transform_bbox, mode="xyxy", restore_coordinates=True)@classmethoddef apply_trans(@@ -309,7 +333,7 @@ class BBoxXYXYApplyInverse(ApplyInverseImpl):"""Apply a transformation with respect to the parameters.Args:- input: the input tensor.+ input: the input tensor, (B, N, 4, 2) or (B, 4, 2).label: the optional label tensor.module: any torch Module but only kornia augmentation modules will countto apply transformations.@@ -337,13 +361,50 @@ class BBoxXYXYApplyInverse(ApplyInverseImpl):to apply transformations.param: the corresponding parameters to the module."""- inverse = super().inverse(input, module, param)+ _input = input.clone()++ _input = super().inverse(_input, module, param)padding_size = cls._get_padding_size(module, param)if padding_size is not None:- inverse = cls.unpad(inverse, padding_size)+ _input = cls.unpad(_input, padding_size)- return inverse+ return _input+++class BBoxXYXYApplyInverse(BBoxApplyInverse):+ """Apply and inverse transformations for bounding box tensors.++ This is for transform boxes in the format [xmin, ymin, xmax, ymax].+ """++ apply_func = partial(transform_bbox, mode="xyxy", restore_coordinates=True)++ @classmethod+ def pad(cls, input, padding_size):+ for i in range(len(padding_size)):+ input[i, :, 0::2] += padding_size[i][0] # left padding+ input[i, :, 1::2] += padding_size[i][2] # top padding+ return input++ @classmethod+ def unpad(cls, input, padding_size):+ for i in range(len(padding_size)):+ input[i, :, 0::2] -= padding_size[i][0] # left padding+ input[i, :, 1::2] -= padding_size[i][2] # top padding+ return input++ @classmethod+ def apply_trans(+ cls, input: torch.Tensor, label: Optional[torch.Tensor], module: nn.Module, param: ParamItem+ ) -> Tuple[torch.Tensor, Optional[torch.Tensor]]:+ warnings.warn("BBoxXYXYApplyInverse is no longer maintained. Please use BBoxApplyInverse instead.")+ return super().apply_trans(input, label=label, module=module, param=param)++ @classmethod+ def inverse(cls, input: torch.Tensor, module: nn.Module, param: Optional[ParamItem] = None) -> torch.Tensor:+ warnings.warn("BBoxXYXYApplyInverse is no longer maintained. Please use BBoxApplyInverse instead.")+ return super().inverse(input, module=module, param=param)class BBoxXYWHApplyInverse(BBoxXYXYApplyInverse):@@ -371,12 +432,56 @@ class BBoxXYWHApplyInverse(BBoxXYXYApplyInverse):return input-class KeypointsApplyInverse(BBoxXYWHApplyInverse):- """Apply and inverse transformations for keypoints tensors."""+class KeypointsApplyInverse(BBoxApplyInverse):+ """Apply and inverse transformations for keypoints tensors.++ This is for transform keypoints in the format (B, N, 2).+ """# Hot fix for the typing mismatchingapply_func = partial(transform_points)+ @classmethod+ def pad(cls, input: torch.Tensor, padding_size: torch.Tensor) -> torch.Tensor:++ assert len(input.shape) in (2, 3,), input.shape+ assert len(padding_size.shape) == 2, padding_size.shape++ _input = input.clone()++ if input.dim() == 2:+ # B,2 to B,1,2+ _input = _input[:, None]++ _input[..., 0] += padding_size[..., :1] # left padding+ _input[..., 1] += padding_size[..., 2:3] # top padding++ if input.dim() == 2:+ _input = _input[:, 0] # squeeze back++ return _input++ @classmethod+ def unpad(cls, input: torch.Tensor, padding_size: torch.Tensor) -> torch.Tensor:++ assert len(input.shape) in (2, 3,), input.shape+ assert len(padding_size.shape) == 2, padding_size.shape++ _input = input.clone()++ if input.dim() == 2:+ # B,2 to B,1,2+ _input = _input[:, None]++ # unpad only xy, not wh+ _input[..., 0] -= padding_size[..., :1] # left padding+ _input[..., 1] -= padding_size[..., 2:3] # top padding++ if input.dim() == 2:+ _input = _input[:, 0] # squeeze back++ return _input+class ApplyInverse:"""Apply and inverse transformations for any tensors (e.g. mask, box, points)."""@@ -385,7 +490,7 @@ class ApplyInverse:def _get_func_by_key(cls, dcate: Union[str, int, DataKey]) -> Type[ApplyInverseInterface]:if DataKey.get(dcate) == DataKey.INPUT:return InputApplyInverse- if DataKey.get(dcate) in [DataKey.MASK]:+ if DataKey.get(dcate) == DataKey.MASK:return MaskApplyInverseif DataKey.get(dcate) in [DataKey.BBOX, DataKey.BBOX_XYXY, DataKey.BBOX_XYWH]:# We are converting to (B, 4, 2) internally for all formats.diff --git a/kornia/geometry/boxes.py b/kornia/geometry/boxes.pyindex 7d2a77ad4..bbc005ec3 100644--- a/kornia/geometry/boxes.py+++ b/kornia/geometry/boxes.py@@ -207,6 +207,8 @@ class Boxes:if not (3 <= boxes.ndim <= 4 and boxes.shape[-2:] == (4, 2)):raise ValueError(f"Boxes shape must be (N, 4, 2) or (B, N, 4, 2). Got {boxes.shape}.")+ self._is_batched = False if boxes.ndim == 3 else True+self._data = boxesself._mode = mode@@ -432,10 +434,6 @@ class Boxes:"""Inplace version of :func:`Boxes.transform_boxes`"""return self.transform_boxes(M, inplace=True)- @property- def _is_batched(self) -> bool:- return self._data.ndim == 4-@propertydef data(self) -> torch.Tensor:return self._data@@ -479,7 +477,10 @@ class Boxes3D:``width = xmax - xmin + 1``, ``height = ymax - ymin + 1`` and ``depth = zmax - zmin + 1``. Examples of`hexahedrons <https://en.wikipedia.org/wiki/Hexahedron>`_ are cubes and rhombohedrons."""- def __init__(self, boxes: torch.Tensor, raise_if_not_floating_point: bool = True):+ def __init__(+ self, boxes: torch.Tensor, raise_if_not_floating_point: bool = True,+ mode: str = "xyzxyz_plus"+ ) -> None:if not isinstance(boxes, torch.Tensor):raise TypeError(f"Input boxes is not a Tensor. Got: {type(boxes)}.")@@ -497,7 +498,10 @@ class Boxes3D:if not (3 <= boxes.ndim <= 4 and boxes.shape[-2:] == (8, 3)):raise ValueError(f"3D bbox shape must be (N, 8, 3) or (B, N, 8, 3). Got {boxes.shape}.")+ self._is_batched = False if boxes.ndim == 3 else True+self._data = boxes+ self._mode = modedef get_boxes_shape(self) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:r"""Compute boxes heights and widths.@@ -594,7 +598,7 @@ class Boxes3D:hexahedrons = hexahedrons if batched else hexahedrons.squeeze(0)# Due to some torch.jit.script bug (at least <= 1.9), you need to pass all arguments to __init__ when# constructing the class from inside of a method.- return cls(hexahedrons, raise_if_not_floating_point=False)+ return cls(hexahedrons, raise_if_not_floating_point=False, mode=mode)def to_tensor(self, mode: str = "xyzxyz") -> torch.Tensor:r"""Cast :class:`Boxes3D` to a tensor. ``mode`` controls which 3D boxes format should be use to represent boxes@@ -776,20 +780,20 @@ class Boxes3D:self._data = transformed_boxesreturn self- return Boxes3D(transformed_boxes, False)+ return Boxes3D(transformed_boxes, False, "xyzxyz_plus")def transform_boxes_(self, M: torch.Tensor) -> "Boxes3D":"""Inplace version of :func:`Boxes3D.transform_boxes`"""return self.transform_boxes(M, inplace=True)- @property- def _is_batched(self) -> bool:- return self._data.ndim == 4-@propertydef data(self) -> torch.Tensor:return self._data+ @property+ def mode(self) -> str:+ return self._mode+@propertydef device(self) -> torch.device:"""Returns boxes device."""diff --git a/test/augmentation/test_container.py b/test/augmentation/test_container.pyindex 1c93d8140..76f125b4f 100644--- a/test/augmentation/test_container.py+++ b/test/augmentation/test_container.py@@ -377,7 +377,7 @@ class TestAugmentationSequential:return_transform=return_transform,)out = aug(inp, mask, bbox, keypoints)- if return_transform and isinstance(out, (tuple, list)):+ if return_transform and isinstance(out[0], (tuple, list)):assert out[0][0].shape == inp.shapeelse:assert out[0].shape == inp.shape@@ -387,7 +387,10 @@ class TestAugmentationSequential:reproducibility_test((inp, mask, bbox, keypoints), aug)out_inv = aug.inverse(*out)- assert out_inv[0].shape == inp.shape+ if return_transform and isinstance(out_inv[0], (tuple, list)):+ assert out_inv[0][0].shape == inp.shape+ else:+ assert out_inv[0].shape == inp.shapeassert out_inv[1].shape == mask.shapeassert out_inv[2].shape == bbox.shapeassert out_inv[3].shape == keypoints.shape