π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
Update _utils.py (#3547)
yugsavalia5eb50af63b08
diff --git a/kornia/geometry/liegroup/_utils.py b/kornia/geometry/liegroup/_utils.pydeleted file mode 100644index 95daff3b4..000000000--- a/kornia/geometry/liegroup/_utils.py+++ /dev/null@@ -1,98 +0,0 @@-# LICENSE HEADER MANAGED BY add-license-header-#-# Copyright 2018 Kornia Team-#-# Licensed under the Apache License, Version 2.0 (the "License");-# you may not use this file except in compliance with the License.-# You may obtain a copy of the License at-#-# http://www.apache.org/licenses/LICENSE-2.0-#-# Unless required by applicable law or agreed to in writing, software-# distributed under the License is distributed on an "AS IS" BASIS,-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-# See the License for the specific language governing permissions and-# limitations under the License.-#-import torch--#-# Copyright 2018 Kornia Team-#-# Licensed under the Apache License, Version 2.0 (the "License");-# you may not use this file except in compliance with the License.-# You may obtain a copy of the License at-#-# http://www.apache.org/licenses/LICENSE-2.0-#-# Unless required by applicable law or agreed to in writing, software-# distributed under the License is distributed on an "AS IS" BASIS,-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-# See the License for the specific language governing permissions and-# limitations under the License.-#---# TODO: Temporary shape check functions until KORNIA_CHECK_SHAPE is ready-def check_so2_z_shape(z: torch.Tensor) -> None:- z_shape = z.shape- len_z_shape = len(z_shape)- if (len_z_shape == 2 and z_shape[1] != 1) or (len_z_shape == 0 and not z.numel()) or (len_z_shape > 2):- raise ValueError(f"Invalid input size, we expect [B]. Got: {z.shape}")---def check_so2_t_shape(t: torch.Tensor) -> None:- t_shape = t.shape- len_t_shape = len(t_shape)- if ((len_t_shape == 2) and (t_shape[1] != 2)) or ((len_t_shape == 1) and (t_shape[0] != 2)) or (len_t_shape > 2):- raise ValueError(f"Invalid translation shape, we expect [B, 2], or [2] Got: {t_shape}")---def check_so2_theta_shape(theta: torch.Tensor) -> None:- theta_shape = theta.shape- len_theta_shape = len(theta_shape)- if (- (len_theta_shape == 2 and theta_shape[1] != 1)- or (len_theta_shape == 0 and not theta.numel())- or (len_theta_shape > 2)- ):- raise ValueError(f"Invalid input size, we expect [B]. Got: {theta_shape}")---def check_so2_matrix_shape(matrix: torch.Tensor) -> None:- matrix_shape = matrix.shape- len_matrix_shape = len(matrix_shape)- if (- (len_matrix_shape == 3 and (matrix_shape[1] != 2 or matrix_shape[2] != 2))- or (len_matrix_shape == 2 and (matrix_shape[0] != 2 or matrix_shape[1] != 2))- or (len_matrix_shape > 3 or len_matrix_shape < 2)- ):- raise ValueError(f"Invalid input size, we expect [B, 2, 2] or [2, 2]. Got: {matrix_shape}")---def check_so2_matrix(matrix: torch.Tensor) -> None:- for m in matrix.reshape(-1, 2, 2):- if m[0, 0] != m[1, 1] or m[0, 1] != -m[1, 0]:- raise ValueError("Invalid rotation matrix")---def check_se2_t_shape(t: torch.Tensor) -> None:- check_so2_t_shape(t)---def check_v_shape(v: torch.Tensor) -> None:- v_shape = v.shape- len_v_shape = len(v_shape)- if ((len_v_shape == 2) and (v_shape[1] != 3)) or ((len_v_shape == 1) and (v_shape[0] != 3)) or (len_v_shape > 3):- raise ValueError(f"Invalid input shape, we expect [B, 3], [3] Got: {v_shape}")---def check_se2_omega_shape(matrix: torch.Tensor) -> None:- matrix_shape = matrix.shape- len_matrix_shape = len(matrix_shape)- if (- (len_matrix_shape == 3 and (matrix_shape[1] != 3 or matrix_shape[2] != 3))- or (len_matrix_shape == 2 and (matrix_shape[0] != 3 or matrix_shape[1] != 3))- or (len_matrix_shape > 3 or len_matrix_shape < 2)- ):- raise ValueError(f"Invalid input size, we expect [B, 3, 3] or [3, 3]. Got: {matrix_shape}")diff --git a/kornia/geometry/liegroup/se2.py b/kornia/geometry/liegroup/se2.pyindex 264c937e9..5349d7138 100644--- a/kornia/geometry/liegroup/se2.py+++ b/kornia/geometry/liegroup/se2.py@@ -25,8 +25,7 @@ import torchimport torch.nn.functional as Ffrom torch import nn-from kornia.core.check import KORNIA_CHECK, KORNIA_CHECK_SAME_DEVICES, KORNIA_CHECK_TYPE-from kornia.geometry.liegroup._utils import check_se2_omega_shape, check_se2_t_shape, check_v_shape+from kornia.core.check import KORNIA_CHECK, KORNIA_CHECK_SAME_DEVICES, KORNIA_CHECK_SHAPE, KORNIA_CHECK_TYPEfrom kornia.geometry.liegroup.so2 import So2from kornia.geometry.vector import Vector2@@ -34,7 +33,11 @@ from kornia.geometry.vector import Vector2def _check_se2_r_t_shape(r: So2, t: torch.Tensor) -> None:z_shape = r.z.shapeif ((len(z_shape) == 1) and (len(t.shape) == 2)) or ((len(z_shape) == 0) and len(t.shape) == 1):- check_se2_t_shape(t)+ # check_se2_t_shape+ is_batch_shape = KORNIA_CHECK_SHAPE(t, ["B", "2"], raises=False)+ is_single_shape = KORNIA_CHECK_SHAPE(t, ["2"], raises=False)+ if not (is_batch_shape or is_single_shape):+ raise ValueError(f"Invalid translation shape, we expect [B, 2], or [2] Got: {t.shape}")else:raise ValueError(f"Invalid input, both the inputs should be either batched or unbatched. Got: {r.z.shape} and {t.shape}"@@ -82,8 +85,6 @@ class Se2(nn.Module):"""super().__init__()KORNIA_CHECK_TYPE(rotation, So2)- # TODO change to KORNIA_CHECK_SHAPE once there is multiple shape support- # KORNIA_CHECK_TYPE(translation, (Vector3, torch.Tensor))if not isinstance(translation, (Vector2, torch.Tensor)):raise TypeError(f"translation type is {type(translation)}")self._translation: Vector2 | nn.Parameter@@ -129,7 +130,6 @@ class Se2(nn.Module):KORNIA_CHECK_TYPE(right, Se2)return self._mul_se2(right)elif isinstance(right, (Vector2, torch.Tensor)):- # TODO change to KORNIA_CHECK_SHAPE once there is multiple shape support# _check_se2_r_t_shape(so2, risght)return so2 * right + telse:@@ -178,7 +178,11 @@ class Se2(nn.Module):tensor([[0.3818, 1.3012]], requires_grad=True)"""- check_v_shape(v)+ # check_v_shape+ is_batch = KORNIA_CHECK_SHAPE(v, ["B", "3"], raises=False)+ is_single = KORNIA_CHECK_SHAPE(v, ["3"], raises=False)+ if not (is_batch or is_single):+ raise ValueError(f"Invalid input shape, we expect [B, 3], [3] Got: {v.shape}")theta = v[..., 2]so2 = So2.exp(theta)z = torch.tensor(0.0, device=v.device, dtype=v.dtype)@@ -228,8 +232,11 @@ class Se2(nn.Module):[1.5707, 0.0000]])"""- # TODO change to KORNIA_CHECK_SHAPE once there is multiple shape support- check_v_shape(v)+ # check_v_shape+ is_batch = KORNIA_CHECK_SHAPE(v, ["B", "3"], raises=False)+ is_single = KORNIA_CHECK_SHAPE(v, ["3"], raises=False)+ if not (is_batch or is_single):+ raise ValueError(f"Invalid input shape, we expect [B, 3], [3] Got: {v.shape}")upsilon = torch.stack((v[..., 0], v[..., 1]), -1)theta = v[..., 2]col0 = torch.cat((So2.hat(theta), upsilon.unsqueeze(-2)), -2)@@ -252,8 +259,11 @@ class Se2(nn.Module):tensor([1., 1., 1.])"""- # TODO change to KORNIA_CHECK_SHAPE once there is multiple shape support- check_se2_omega_shape(omega)+ # check_se2_omega_shape+ is_batch = KORNIA_CHECK_SHAPE(omega, ["B", "3", "3"], raises=False)+ is_single = KORNIA_CHECK_SHAPE(omega, ["3", "3"], raises=False)+ if not (is_batch or is_single):+ raise ValueError(f"Invalid input size, we expect [B, 3, 3] or [3, 3]. Got: {omega.shape}")upsilon = omega[..., 2, :2]theta = So2.vee(omega[..., :2, :2])return torch.cat((upsilon, theta[..., None]), -1)diff --git a/kornia/geometry/liegroup/so2.py b/kornia/geometry/liegroup/so2.pyindex 3ca562928..731f12b30 100644--- a/kornia/geometry/liegroup/so2.py+++ b/kornia/geometry/liegroup/so2.py@@ -24,14 +24,7 @@ from typing import Optional, Union, overloadimport torchfrom torch import nn-from kornia.core.check import KORNIA_CHECK, KORNIA_CHECK_IS_TENSOR-from kornia.geometry.liegroup._utils import (- check_so2_matrix,- check_so2_matrix_shape,- check_so2_t_shape,- check_so2_theta_shape,- check_so2_z_shape,-)+from kornia.core.check import KORNIA_CHECK, KORNIA_CHECK_IS_TENSOR, KORNIA_CHECK_SHAPEfrom kornia.geometry.vector import Vector2@@ -71,8 +64,13 @@ class So2(nn.Module):"""super().__init__()KORNIA_CHECK_IS_TENSOR(z)- # TODO change to KORNIA_CHECK_SHAPE once there is multiple shape support- check_so2_z_shape(z)+ # check_so2_z_shape(z)+ is_scalar = len(z.shape) == 0+ is_flat = KORNIA_CHECK_SHAPE(z, ["B"], raises=False)+ is_column = KORNIA_CHECK_SHAPE(z, ["B", "1"], raises=False)++ if not (is_scalar or is_flat or is_column):+ raise ValueError(f"Invalid input size, we expect [], [B], or [B, 1]. Got: {z.shape}")self._z = nn.Parameter(z)def __repr__(self) -> str:@@ -101,9 +99,12 @@ class So2(nn.Module):if isinstance(right, So2):return So2(z * right.z)elif isinstance(right, (Vector2, torch.Tensor)):- # TODO change to KORNIA_CHECK_SHAPE once there is multiple shape supportif isinstance(right, torch.Tensor):- check_so2_t_shape(right)+ # check_so2_t_shape+ is_batch_shape = KORNIA_CHECK_SHAPE(right, ["B", "2"], raises=False)+ is_single_shape = KORNIA_CHECK_SHAPE(right, ["2"], raises=False)+ if not (is_batch_shape or is_single_shape):+ raise ValueError(f"Invalid translation shape, we expect [B, 2], or [2] Got: {right.shape}")x = right.data[..., 0]y = right.data[..., 1]real = z.real@@ -136,8 +137,12 @@ class So2(nn.Module):tensor([4.6329e-05+1.j], requires_grad=True)"""- # TODO change to KORNIA_CHECK_SHAPE once there is multiple shape support- check_so2_theta_shape(theta)+ # check_so2_theta_shape+ is_scalar = len(theta.shape) == 0+ is_flat = KORNIA_CHECK_SHAPE(theta, ["B"], raises=False)+ is_column = KORNIA_CHECK_SHAPE(theta, ["B", "1"], raises=False)+ if not (is_scalar or is_flat or is_column):+ raise ValueError(f"Invalid input size, we expect [], [B], or [B, 1]. Got: {theta.shape}")return So2(torch.complex(torch.cos(theta), torch.sin(theta)))def log(self) -> torch.Tensor:@@ -166,8 +171,12 @@ class So2(nn.Module):[1.5707, 0.0000]])"""- # TODO change to KORNIA_CHECK_SHAPE once there is multiple shape support- check_so2_theta_shape(theta)+ # check_so2_theta_shape+ is_scalar = len(theta.shape) == 0+ is_flat = KORNIA_CHECK_SHAPE(theta, ["B"], raises=False)+ is_column = KORNIA_CHECK_SHAPE(theta, ["B", "1"], raises=False)+ if not (is_scalar or is_flat or is_column):+ raise ValueError(f"Invalid input size, we expect [], [B], or [B, 1]. Got: {theta.shape}")z = torch.zeros_like(theta)row0 = torch.stack((z, theta), -1)row1 = torch.stack((theta, z), -1)@@ -187,8 +196,11 @@ class So2(nn.Module):tensor([1., 1., 1.])"""- # TODO change to KORNIA_CHECK_SHAPE once there is multiple shape support- check_so2_matrix_shape(omega)+ # check_so2_matrix_shape+ is_batch = KORNIA_CHECK_SHAPE(omega, ["B", "2", "2"], raises=False)+ is_single = KORNIA_CHECK_SHAPE(omega, ["2", "2"], raises=False)+ if not (is_batch or is_single):+ raise ValueError(f"Invalid input size, we expect [B, 2, 2] or [2, 2]. Got: {omega.shape}")return omega[..., 0, 1]def matrix(self) -> torch.Tensor:@@ -221,9 +233,19 @@ class So2(nn.Module):tensor(1.+0.j, requires_grad=True)"""- # TODO change to KORNIA_CHECK_SHAPE once there is multiple shape support- check_so2_matrix_shape(matrix)- check_so2_matrix(matrix)+ # check_so2_matrix_shape+ is_batch = KORNIA_CHECK_SHAPE(matrix, ["B", "2", "2"], raises=False)+ is_single = KORNIA_CHECK_SHAPE(matrix, ["2", "2"], raises=False)+ if not (is_batch or is_single):+ raise ValueError(f"Invalid input size, we expect [B, 2, 2] or [2, 2]. Got: {matrix.shape}")+ # check_so2_matrix+ KORNIA_CHECK_IS_TENSOR(matrix)+ if len(matrix.shape) < 2 or matrix.shape[-2:] != (2, 2):+ raise ValueError(f"Input size must be (*, 2, 2). Got {matrix.shape}")+ mask_diag = torch.allclose(matrix[..., 0, 0], matrix[..., 1, 1])+ mask_off_diag = torch.allclose(matrix[..., 0, 1], -matrix[..., 1, 0])+ if not (mask_diag and mask_off_diag):+ raise ValueError("Invalid SO2 rotation matrix: constraints m00==m11 and m01==-m10 not met.")z = torch.complex(matrix[..., 0, 0], matrix[..., 1, 0])return cls(z)