π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
TensorWrapper bug fix + add __radd__, __rmul__, __rsub__ (#2190)
Christie Jacobe25792e9c6e8
diff --git a/kornia/core/tensor_wrapper.py b/kornia/core/tensor_wrapper.pyindex dd327d43b..56fb71f3d 100644--- a/kornia/core/tensor_wrapper.py+++ b/kornia/core/tensor_wrapper.py@@ -26,7 +26,7 @@ def unwrap(v):if type(v) in {tuple, list}:return type(v)(unwrap(vi) for vi in v)- return v._data if not isinstance(v, Tensor) else v+ return v._data if isinstance(v, TensorWrapper) else vclass TensorWrapper:@@ -106,12 +106,21 @@ class TensorWrapper:def __add__(self, other):return self.__unary_op__(torch.add, other)+ def __radd__(self, other):+ return self.__unary_op__(torch.add, other)+def __mul__(self, other):return self.__unary_op__(torch.mul, other)+ def __rmul__(self, other):+ return self.__unary_op__(torch.mul, other)+def __sub__(self, other):return self.__unary_op__(torch.sub, other)+ def __rsub__(self, other):+ return self.__unary_op__(torch.sub, other)+def __truediv__(self, other):return self.__unary_op__(torch.true_divide, other)diff --git a/test/core/test_tensor_wrapper.py b/test/core/test_tensor_wrapper.pyindex 2e04f6f81..8587db773 100644--- a/test/core/test_tensor_wrapper.py+++ b/test/core/test_tensor_wrapper.py@@ -31,18 +31,27 @@ class TestTensorWrapper(BaseTester):self.assert_close(loaded_tensor.unwrap(), tensor.unwrap())def test_wrap_list(self, device, dtype):- data_list = [torch.rand(2, device=device, dtype=dtype), torch.rand(3, device=device, dtype=dtype)]+ data_list = [+ torch.rand(2, device=device, dtype=dtype),+ torch.rand(3, device=device, dtype=dtype),+ TensorWrapper(torch.rand(3, device=device, dtype=dtype)),+ 1,+ 0.5,+ ]tensor_list = wrap(data_list, TensorWrapper)assert isinstance(tensor_list, list)- assert len(tensor_list) == 2+ assert len(tensor_list) == 5tensor_list_data = unwrap(tensor_list)- assert len(tensor_list_data) == 2+ assert len(tensor_list_data) == 5self.assert_close(tensor_list_data[0], data_list[0])self.assert_close(tensor_list_data[1], data_list[1])+ self.assert_close(tensor_list_data[2], data_list[2])+ assert tensor_list_data[3] == data_list[3]+ assert tensor_list_data[4] == data_list[4]- for i in range(len(tensor_list_data)):+ for i in range(len(tensor_list_data[:3])):self.assert_close(tensor_list[i].unwrap(), data_list[i])def test_accessors(self, device, dtype):@@ -59,8 +68,13 @@ class TestTensorWrapper(BaseTester):x = TensorWrapper(data)self.assert_close(x.add(x), x + x)+ self.assert_close(x.add(1), 1 + x)+ self.assert_close(x.add(1), x + 1)self.assert_close(x.mul(x), x * x)+ self.assert_close(x.mul(1), 1 * x)+ self.assert_close(x.mul(1), x * 1)self.assert_close(x.sub(x), x - x)+ self.assert_close(x.sub(1), x - 1)self.assert_close(x.div(x), x / x)self.assert_close(x.true_divide(x), x / x)self.assert_close(x.floor_divide(x), x // x)