def list_devices(): devices = [o3d.Device("CPU:" + str(0))] if _torch_imported: if (o3d.cuda.device_count() != torch.cuda.device_count()): raise RuntimeError( f"o3d.cuda.device_count() != torch.cuda.device_count(), " f"{o3d.cuda.device_count()} != {torch.cuda.device_count()}") else: print( 'Warning: torch is not imported, cannot guarantee the correctness of device_count()' ) for i in range(o3d.cuda.device_count()): devices.append(o3d.Device("CUDA:" + str(i))) return devices
def test_tensorlist_operations(): a = o3d.TensorList([3, 4], o3d.Dtype.Float32, o3d.Device(), size=1) assert a.size() == 1 b = o3d.TensorList.from_tensor( o3d.Tensor(np.ones((2, 3, 4), dtype=np.float32))) assert b.size() == 2 c = o3d.TensorList.from_tensors( [o3d.Tensor(np.zeros((3, 4), dtype=np.float32))]) assert c.size() == 1 d = a + b assert d.size() == 3 e = o3d.TensorList.concat(c, d) assert e.size() == 4 e.push_back(o3d.Tensor(np.zeros((3, 4), dtype=np.float32))) assert e.size() == 5 e.extend(d) assert e.size() == 8 e += a assert e.size() == 9
def __init__(self, data, dtype=None, device=None): if isinstance(data, tuple) or isinstance(data, list): data = np.array(data) if not isinstance(data, np.ndarray): raise ValueError("data must be a list, tuple, or Numpy array.") if dtype is None: dtype = _numpy_dtype_to_dtype(data.dtype) if device is None: device = o3d.Device("CPU:0") super(Tensor, self).__init__(data, dtype, device)
def list_devices(): """ If Open3D is built with CUDA support: - If cuda device is available, returns [Device("CPU:0"), Device("CUDA:0")]. - If cuda device is not available, returns [Device("CPU:0")]. If Open3D is built without CUDA support: - returns [Device("CPU:0")]. """ devices = [o3d.Device("CPU:" + str(0))] if _torch_imported: if (o3d.cuda.device_count() != torch.cuda.device_count()): raise RuntimeError( "o3d.cuda.device_count() != torch.cuda.device_count(), " "{} != {}".format(o3d.cuda.device_count(), torch.cuda.device_count())) else: print("Warning: PyTorch is not imported") if o3d.cuda.device_count() > 0: devices.append(o3d.Device("CUDA:0")) return devices
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 test_device(): device = o3d.Device() assert device.get_type() == o3d.Device.DeviceType.CPU assert device.get_id() == 0 device = o3d.Device("CUDA", 1) assert device.get_type() == o3d.Device.DeviceType.CUDA assert device.get_id() == 1 device = o3d.Device("CUDA:2") assert device.get_type() == o3d.Device.DeviceType.CUDA assert device.get_id() == 2 assert o3d.Device("CUDA", 1) == o3d.Device("CUDA:1") assert o3d.Device("CUDA", 1) != o3d.Device("CUDA:0") assert o3d.Device("CUDA", 1).__str__() == "CUDA:1"
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_tensorlist_indexing(device): # 5 x (3, 4) dtype = o3d.Dtype.Float32 device = o3d.Device("CPU:0") np_t = np.ones((5, 3, 4), dtype=np.float32) t = o3d.Tensor(np_t, dtype, device) tl = o3d.TensorList.from_tensor(t, inplace=True) # set slices [1, 3] tl.tensor()[1:5:2] = o3d.Tensor(3 * np.ones((2, 3, 4), dtype=np.float32)) # set items [4] tl[-1] = o3d.Tensor(np.zeros((3, 4), dtype=np.float32)) # get items np.testing.assert_allclose(tl[0].cpu().numpy(), np.ones((3, 4), dtype=np.float32)) np.testing.assert_allclose(tl[1].cpu().numpy(), 3 * np.ones( (3, 4), dtype=np.float32)) np.testing.assert_allclose(tl[2].cpu().numpy(), np.ones((3, 4), dtype=np.float32)) np.testing.assert_allclose(tl[3].cpu().numpy(), 3 * np.ones( (3, 4), dtype=np.float32)) np.testing.assert_allclose(tl[4].cpu().numpy(), np.zeros((3, 4), dtype=np.float32)) # push_back tl.push_back(o3d.Tensor(-1 * np.ones((3, 4)), dtype, device)) assert tl.size() == 6 np.testing.assert_allclose(tl[5].cpu().numpy(), -1 * np.ones( (3, 4), dtype=np.float32)) tl += tl assert tl.size() == 12 for offset in [0, 6]: np.testing.assert_allclose(tl[0 + offset].cpu().numpy(), np.ones((3, 4), dtype=np.float32)) np.testing.assert_allclose(tl[1 + offset].cpu().numpy(), 3 * np.ones( (3, 4), dtype=np.float32)) np.testing.assert_allclose(tl[2 + offset].cpu().numpy(), np.ones((3, 4), dtype=np.float32)) np.testing.assert_allclose(tl[3 + offset].cpu().numpy(), 3 * np.ones( (3, 4), dtype=np.float32)) np.testing.assert_allclose(tl[4 + offset].cpu().numpy(), np.zeros((3, 4), dtype=np.float32))
def from_tensors(tensors, device=None): """ Returns a TensorList from a list of existing tensors. Args: tensors: The list of o3d.Tensor to construct from. The tensors' shapes should be compatible. device: The device where the tensorlist is targeted. """ if not isinstance(tensors, list) and not isinstance(tensors, tuple): raise ValueError('tensors must be a list or tuple') for tensor in tensors: if not isinstance(tensor, o3d.Tensor): raise ValueError( 'every element of the input list must be a valid tensor') if device is None: device = o3d.Device("CPU:0") return super(TensorList, TensorList).from_tensors(tensors, device)
def test_tensor_constructor(): dtype = o3d.Dtype.Int32 device = o3d.Device("CPU:0") # Numpy array np_t = np.array([[0, 1, 2], [3, 4, 5]]) o3_t = o3d.Tensor(np_t, dtype, device) np.testing.assert_equal(np_t, o3_t.numpy()) # 2D list li_t = [[0, 1, 2], [3, 4, 5]] no3_t = o3d.Tensor(li_t, dtype, device) np.testing.assert_equal(li_t, o3_t.numpy()) # 2D list, inconsistent length li_t = [[0, 1, 2], [3, 4]] with pytest.raises(ValueError): o3_t = o3d.Tensor(li_t, dtype, device) # Automatic casting np_t_double = np.array([[0., 1.5, 2.], [3., 4., 5.]]) np_t_int = np.array([[0, 1, 2], [3, 4, 5]]) o3_t = o3d.Tensor(np_t_double, dtype, device) np.testing.assert_equal(np_t_int, o3_t.numpy()) # Special strides np_t = np.random.randint(10, size=(10, 10))[1:10:2, 1:10:3].T o3_t = o3d.Tensor(np_t, dtype, device) np.testing.assert_equal(np_t, o3_t.numpy()) # Boolean np_t = np.array([True, False, True], dtype=np.bool) o3_t = o3d.Tensor([True, False, True], o3d.Dtype.Bool, device) np.testing.assert_equal(np_t, o3_t.numpy()) o3_t = o3d.Tensor(np_t, o3d.Dtype.Bool, device) np.testing.assert_equal(np_t, o3_t.numpy())