def test_creation(device): # Shape takes tuple, list or o3d.SizeVector t = o3d.Tensor.empty((2, 3), o3d.Dtype.Float32, device=device) assert t.shape == o3d.SizeVector([2, 3]) t = o3d.Tensor.empty([2, 3], o3d.Dtype.Float32, device=device) assert t.shape == o3d.SizeVector([2, 3]) t = o3d.Tensor.empty(o3d.SizeVector([2, 3]), o3d.Dtype.Float32, device=device) assert t.shape == o3d.SizeVector([2, 3]) # Test zeros and ones t = o3d.Tensor.zeros((2, 3), o3d.Dtype.Float32, device=device) np.testing.assert_equal(t.cpu().numpy(), np.zeros((2, 3), dtype=np.float32)) t = o3d.Tensor.ones((2, 3), o3d.Dtype.Float32, device=device) np.testing.assert_equal(t.cpu().numpy(), np.ones((2, 3), dtype=np.float32)) # Automatic casting of dtype. t = o3d.Tensor.full((2, ), False, o3d.Dtype.Float32, device=device) np.testing.assert_equal(t.cpu().numpy(), np.full((2, ), False, dtype=np.float32)) t = o3d.Tensor.full((2, ), 3.5, o3d.Dtype.UInt8, device=device) np.testing.assert_equal(t.cpu().numpy(), np.full((2, ), 3.5, dtype=np.uint8))
def test_to(): a = o3d.Tensor(np.array([0.1, 1.2, 2.3, 3.4, 4.5, 5.6]).astype(np.float32)) b = a.to(o3d.Dtype.Int32) np.testing.assert_equal(b.numpy(), np.array([0, 1, 2, 3, 4, 5])) assert b.shape == o3d.SizeVector([6]) assert b.strides == o3d.SizeVector([1]) assert b.dtype == o3d.Dtype.Int32 assert b.device == a.device
def _reduction_dim_to_size_vector(self, dim): if dim is None: return o3d.SizeVector(list(range(self.ndim))) elif isinstance(dim, int): return o3d.SizeVector([dim]) elif isinstance(dim, list) or isinstance(dim, tuple): return o3d.SizeVector(dim) else: raise TypeError( f"dim must be int, list or tuple, but was {type(dim)}.")
def ones(shape, dtype, device=o3d.Device("CPU:0")): """ Create a tensor with fill with ones. Args: shape (list, tuple, o3d.SizeVector): Shape of the tensor. dtype (o3d.Dtype): Data type of the tensor. device (o3d.Device): Device where the tensor is created. """ if not isinstance(shape, o3d.SizeVector): shape = o3d.SizeVector(shape) return super(Tensor, Tensor).ones(shape, dtype, device)
def full(shape, fill_value, dtype, device=o3d.Device("CPU:0")): """ Create a tensor with fill with the specified value. Args: shape (list, tuple, o3d.SizeVector): Shape of the tensor. fill_value (scalar): The value to be filled. dtype (o3d.Dtype): Data type of the tensor. device (o3d.Device): Device where the tensor is created. """ if not isinstance(shape, o3d.SizeVector): shape = o3d.SizeVector(shape) return super(Tensor, Tensor).full(shape, fill_value, dtype, device)
def __init__(self, shape, dtype=None, device=None, size=None): if isinstance(shape, list) or isinstance(shape, tuple): shape = o3d.SizeVector(shape) elif isinstance(shape, o3d.SizeVector): pass else: raise ValueError('shape must be a list, tuple, or o3d.SizeVector') if dtype is None: dtype = o3d.Dtype.Float32 if device is None: device = o3d.Device("CPU:0") if size is None: size = 0 super(TensorList, self).__init__(shape, dtype, device, size)
def test_size_vector(): # List sv = o3d.SizeVector([-1, 2, 3]) assert "{}".format(sv) == "{-1, 2, 3}" # Tuple sv = o3d.SizeVector((-1, 2, 3)) assert "{}".format(sv) == "{-1, 2, 3}" # Numpy 1D array sv = o3d.SizeVector(np.array([-1, 2, 3])) assert "{}".format(sv) == "{-1, 2, 3}" # Empty sv = o3d.SizeVector() assert "{}".format(sv) == "{}" sv = o3d.SizeVector([]) assert "{}".format(sv) == "{}" sv = o3d.SizeVector(()) assert "{}".format(sv) == "{}" sv = o3d.SizeVector(np.array([])) assert "{}".format(sv) == "{}" # Automatic int casting (not rounding to nearest) sv = o3d.SizeVector((1.9, 2, 3)) assert "{}".format(sv) == "{1, 2, 3}" # Automatic casting negative sv = o3d.SizeVector((-1.5, 2, 3)) assert "{}".format(sv) == "{-1, 2, 3}" # 2D list exception with pytest.raises(ValueError): sv = o3d.SizeVector([[1, 2], [3, 4]]) # 2D Numpy array exception with pytest.raises(ValueError): sv = o3d.SizeVector(np.array([[1, 2], [3, 4]])) # Garbage input with pytest.raises(ValueError): sv = o3d.SizeVector(["foo", "bar"])