Esempio n. 1
0
def test_constructor_and_accessors(device):
    dtype = o3c.Dtype.Float32

    # Constructor.
    pcd = o3d.tgeometry.PointCloud(dtype, device)
    assert "points" in pcd.point
    assert "colors" not in pcd.point
    assert isinstance(pcd.point, o3d.tgeometry.TensorListMap)
    assert isinstance(pcd.point["points"], o3c.TensorList)

    # Assignment.
    pcd.point["points"] = o3c.TensorList(o3c.SizeVector([3]), dtype, device)
    pcd.point["colors"] = o3c.TensorList(o3c.SizeVector([3]), dtype, device)
    assert len(pcd.point["points"]) == 0
    assert len(pcd.point["colors"]) == 0

    one_point = o3c.Tensor.ones((3, ), dtype, device)
    one_color = o3c.Tensor.ones((3, ), dtype, device)
    pcd.point["points"].push_back(one_point)
    pcd.point["colors"].push_back(one_color)
    assert len(pcd.point["points"]) == 1
    assert len(pcd.point["colors"]) == 1

    # Edit and access values.
    pcd.point["points"][0] = o3c.Tensor([1, 2, 3], dtype, device)
    assert pcd.point["points"][0].allclose(o3c.Tensor([1, 2, 3], dtype,
                                                      device))

    # Pushback.
    pcd.synchronized_push_back({"points": one_point, "colors": one_color})
    assert len(pcd.point["points"]) == 2
    assert len(pcd.point["colors"]) == 2
Esempio n. 2
0
def test_svd(device, dtype):
    a = o3c.Tensor([[2, 4], [1, 3], [0, 0], [0, 0]],
                   dtype=dtype,
                   device=device)
    if dtype in [o3c.int32, o3c.int64]:
        with pytest.raises(RuntimeError) as excinfo:
            o3c.svd(a)
            assert 'Only tensors with Float32 or Float64 are supported' in str(
                excinfo.value)
        return

    u, s, vt = o3c.svd(a)
    assert u.shape == o3c.SizeVector([4, 4])
    assert s.shape == o3c.SizeVector([2])
    assert vt.shape == o3c.SizeVector([2, 2])

    # u and vt are orthogonal matrices
    uut = u @ u.T()
    eye_uut = o3c.Tensor.eye(4, dtype=dtype)
    np.testing.assert_allclose(uut.cpu().numpy(),
                               eye_uut.cpu().numpy(),
                               atol=1e-6)

    vvt = vt.T() @ vt
    eye_vvt = o3c.Tensor.eye(2, dtype=dtype)
    np.testing.assert_allclose(vvt.cpu().numpy(),
                               eye_vvt.cpu().numpy(),
                               atol=1e-6)

    usvt = u[:, :2] @ o3c.Tensor.diag(s) @ vt
    # The accuracy of svd over Float32 is very low...
    np.testing.assert_allclose(a.cpu().numpy(), usvt.cpu().numpy(), atol=1e-5)

    u = u.cpu().numpy()
    s = s.cpu().numpy()
    vt = vt.cpu().numpy()
    u_numpy, s_numpy, vt_numpy = np.linalg.svd(a.cpu().numpy())

    # u, vt can be different by several signs
    np.testing.assert_allclose(np.abs(u), np.abs(u_numpy), 1e-6)
    np.testing.assert_allclose(np.abs(vt), np.abs(vt_numpy), 1e-6)

    np.testing.assert_allclose(s, s_numpy, 1e-6)

    for shapes in [(0, 0), (0, 2)]:
        with pytest.raises(RuntimeError) as excinfo:
            a = o3c.Tensor.zeros(shapes, dtype=dtype, device=device)
            a.svd()
            assert 'dimensions with zero' in str(excinfo.value)
    for shapes in [(), [1], (1, 2, 4)]:
        with pytest.raises(RuntimeError) as excinfo:
            a = o3c.Tensor.zeros(shapes, dtype=dtype, device=device)
            a.svd()
            assert 'must be 2D' in str(excinfo.value)

    with pytest.raises(RuntimeError) as excinfo:
        a = o3c.Tensor(shapes, dtype=dtype, device=device)
        a.svd()
        assert 'must be 2D' in str(excinfo.value)
Esempio n. 3
0
def test_to(device):
    a = o3c.Tensor(np.array([0.1, 1.2, 2.3, 3.4, 4.5, 5.6]).astype(np.float32),
                   device=device)
    b = a.to(o3c.int32)
    np.testing.assert_equal(b.cpu().numpy(), np.array([0, 1, 2, 3, 4, 5]))
    assert b.shape == o3c.SizeVector([6])
    assert b.strides == o3c.SizeVector([1])
    assert b.dtype == o3c.int32
    assert b.device == a.device
Esempio n. 4
0
def test_tensorlist_create(device):
    dtype = o3c.Dtype.Float32

    # Construct uninitialized tensorlist.
    tl = o3c.TensorList(o3c.SizeVector([3, 4]), dtype, device)
    assert tl.size == 0
    assert tl.element_shape == o3c.SizeVector([3, 4])

    tl = o3c.TensorList(o3c.SizeVector([3, 4]), dtype, device=device)
    assert tl.size == 0
    assert tl.element_shape == o3c.SizeVector([3, 4])

    tl = o3c.TensorList(o3c.SizeVector([3, 4]), dtype=dtype, device=device)
    assert tl.size == 0
    assert tl.element_shape == o3c.SizeVector([3, 4])

    tl = o3c.TensorList(element_shape=o3c.SizeVector([3, 4]),
                        dtype=dtype,
                        device=device)
    assert tl.size == 0
    assert tl.element_shape == o3c.SizeVector([3, 4])

    # Construct from a list of tensors.
    t0 = o3c.Tensor.ones((2, 3), dtype, device) * 0
    t1 = o3c.Tensor.ones((2, 3), dtype, device) * 1
    t2 = o3c.Tensor.ones((2, 3), dtype, device) * 2
    tl = o3c.TensorList([t0, t1, t2])
    assert tl[0].allclose(t0)
    assert tl[1].allclose(t1)
    assert tl[2].allclose(t2)
    assert tl[-1].allclose(t2)
    assert not tl[0].issame(t0)
    assert not tl[1].issame(t1)
    assert not tl[2].issame(t2)
    assert not tl[-1].issame(t2)

    # Create from a internal tensor.
    t = o3c.Tensor.ones((4, 2, 3), dtype, device)
    tl = o3c.TensorList.from_tensor(o3c.Tensor.ones((4, 2, 3), dtype, device))
    assert tl.element_shape == o3c.SizeVector([2, 3])
    assert tl.size == 4
    assert tl.dtype == dtype
    assert tl.device == device
    assert tl.is_resizable
    assert not tl.as_tensor().issame(t)

    # Create from a internal tensor, in-place.
    t = o3c.Tensor.ones((4, 2, 3), dtype, device)
    tl = o3c.TensorList.from_tensor(t, inplace=True)
    assert tl.element_shape == o3c.SizeVector([2, 3])
    assert tl.size == 4
    assert tl.dtype == dtype
    assert tl.device == device
    assert not tl.is_resizable
    assert tl.as_tensor().issame(t)
Esempio n. 5
0
def test_matmul(device, dtype):
    # Shape takes tuple, list or o3c.SizeVector
    a = o3c.Tensor([[1, 2.5, 3], [4, 5, 6.2]], dtype=dtype, device=device)
    b = o3c.Tensor([[7.5, 8, 9, 10], [11, 12, 13, 14], [15, 16, 17.8, 18]],
                   dtype=dtype,
                   device=device)
    c = o3c.matmul(a, b)
    assert c.shape == o3c.SizeVector([2, 4])

    c_numpy = a.cpu().numpy() @ b.cpu().numpy()
    np.testing.assert_allclose(c.cpu().numpy(), c_numpy, 1e-6)

    # Non-contiguous test
    a = a[:, 1:]
    b = b[[0, 2], :]
    c = a.matmul(b)
    assert c.shape == o3c.SizeVector([2, 4])

    c_numpy = a.cpu().numpy() @ b.cpu().numpy()
    np.testing.assert_allclose(c.cpu().numpy(), c_numpy, 1e-6)

    # Incompatible shape test
    with pytest.raises(RuntimeError) as excinfo:
        a = o3c.Tensor.zeros((3, 4, 5), dtype=dtype)
        b = o3c.Tensor.zeros((4, 5), dtype=dtype)
        c = a @ b
        assert 'Tensor A must be 2D' in str(excinfo.value)

    with pytest.raises(RuntimeError) as excinfo:
        a = o3c.Tensor.zeros((3, 4), dtype=dtype)
        b = o3c.Tensor.zeros((4, 5, 6), dtype=dtype)
        c = a @ b
        assert 'Tensor B must be 1D (vector) or 2D (matrix)' in str(
            excinfo.value)

    with pytest.raises(RuntimeError) as excinfo:
        a = o3c.Tensor.zeros((3, 4), dtype=dtype)
        b = o3c.Tensor.zeros((3, 7), dtype=dtype)
        c = a @ b
        assert 'mismatch with' in str(excinfo.value)

    for shapes in [((0, 0), (0, 0)), ((2, 0), (0, 3)), ((0, 2), (2, 0)),
                   ((2, 0), (0, 0))]:
        with pytest.raises(RuntimeError) as excinfo:
            a_shape, b_shape = shapes
            a = o3c.Tensor.zeros(a_shape, dtype=dtype, device=device)
            b = o3c.Tensor.zeros(b_shape, dtype=dtype, device=device)
            c = a @ b
            assert 'dimensions with zero' in str(excinfo.value)
Esempio n. 6
0
def test_creation(dtype, device):
    # Shape takes tuple, list or o3c.SizeVector
    t = o3c.Tensor.empty((2, 3), dtype, device=device)
    assert t.shape == o3c.SizeVector([2, 3])
    t = o3c.Tensor.empty([2, 3], dtype, device=device)
    assert t.shape == o3c.SizeVector([2, 3])
    t = o3c.Tensor.empty(o3c.SizeVector([2, 3]), dtype, device=device)
    assert t.shape == o3c.SizeVector([2, 3])

    # Test zeros and ones
    t = o3c.Tensor.zeros((2, 3), dtype, device=device)
    np.testing.assert_equal(t.cpu().numpy(), np.zeros((2, 3), dtype=np.float32))
    t = o3c.Tensor.ones((2, 3), dtype, device=device)
    np.testing.assert_equal(t.cpu().numpy(), np.ones((2, 3), dtype=np.float32))

    # Automatic casting of dtype.
    t = o3c.Tensor.full((2,), False, o3c.float32, device=device)
    np.testing.assert_equal(t.cpu().numpy(),
                            np.full((2,), False, dtype=np.float32))
    t = o3c.Tensor.full((2,), 3.5, o3c.uint8, device=device)
    np.testing.assert_equal(t.cpu().numpy(), np.full((2,), 3.5, dtype=np.uint8))
Esempio n. 7
0
def test_tensorlistmap(device):
    dtype = o3c.Dtype.Float32

    # Constructor.
    tlm = o3d.tgeometry.TensorListMap("points")

    # Get primary key().
    assert tlm.get_primary_key() == "points"

    # Map member access, assignment and "contains" check. This should be the
    # preferrred way to construct a TensorListMap with values in python.
    points = o3c.TensorList(o3c.SizeVector([3]), dtype, device)
    colors = o3c.TensorList(o3c.SizeVector([3]), dtype, device)
    tlm = o3d.tgeometry.TensorListMap("points")
    assert "points" not in tlm
    tlm["points"] = points
    assert "points" in tlm
    assert "colors" not in tlm
    tlm["colors"] = colors
    assert "colors" in tlm

    # Constructor with tl values.
    tlm = o3d.tgeometry.TensorListMap("points", {
        "points": points,
        "colors": colors
    })

    # Syncronized pushback.
    one_point = o3c.Tensor.ones((3,), dtype, device)
    one_color = o3c.Tensor.ones((3,), dtype, device)
    tlm.synchronized_push_back({"points": one_point, "colors": one_color})
    tlm["points"].push_back(one_point)
    assert not tlm.is_size_synchronized()
    with pytest.raises(RuntimeError):
        tlm.assert_size_synchronized()
    with pytest.raises(RuntimeError):
        tlm.synchronized_push_back({"points": one_point, "colors": one_color})
Esempio n. 8
0
def test_size_vector():
    # List
    sv = o3c.SizeVector([-1, 2, 3])
    assert "{}".format(sv) == "SizeVector[-1, 2, 3]"

    # Tuple
    sv = o3c.SizeVector((-1, 2, 3))
    assert "{}".format(sv) == "SizeVector[-1, 2, 3]"

    # Numpy 1D array
    sv = o3c.SizeVector(np.array([-1, 2, 3]))
    assert "{}".format(sv) == "SizeVector[-1, 2, 3]"

    # Empty
    sv = o3c.SizeVector()
    assert "{}".format(sv) == "SizeVector[]"
    sv = o3c.SizeVector([])
    assert "{}".format(sv) == "SizeVector[]"
    sv = o3c.SizeVector(())
    assert "{}".format(sv) == "SizeVector[]"
    sv = o3c.SizeVector(np.array([]))
    assert "{}".format(sv) == "SizeVector[]"

    # Not integer: thorws exception
    with pytest.raises(Exception):
        sv = o3c.SizeVector([1.9, 2, 3])

    with pytest.raises(Exception):
        sv = o3c.SizeVector([-1.5, 2, 3])

    # 2D list exception
    with pytest.raises(Exception):
        sv = o3c.SizeVector([[1, 2], [3, 4]])

    # 2D Numpy array exception
    with pytest.raises(Exception):
        sv = o3c.SizeVector(np.array([[1, 2], [3, 4]]))

    # Garbage input
    with pytest.raises(Exception):
        sv = o3c.SizeVector(["foo", "bar"])
Esempio n. 9
0
def test_tensorlist_operation(device):
    dtype = o3c.Dtype.Float32
    t0 = o3c.Tensor.ones((2, 3), dtype, device) * 0
    t1 = o3c.Tensor.ones((2, 3), dtype, device) * 1
    t2 = o3c.Tensor.ones((2, 3), dtype, device) * 2

    # push_back
    tl = o3c.TensorList(o3c.SizeVector([2, 3]), dtype, device)
    assert tl.size == 0
    tl.push_back(t0)
    assert tl.size == 1

    # resize
    tl.resize(10)
    assert tl.size == 10
    tl.resize(1)
    assert tl.size == 1

    # extend
    tl = o3c.TensorList(o3c.SizeVector([2, 3]), dtype, device)
    tl.push_back(t0)
    tl_other = o3c.TensorList(o3c.SizeVector([2, 3]), dtype, device)
    tl_other.push_back(t1)
    tl_other.push_back(t2)
    tl.extend(tl_other)
    assert tl.size == 3
    assert tl[0].allclose(t0)
    assert tl[1].allclose(t1)
    assert tl[2].allclose(t2)

    # +=
    tl = o3c.TensorList(o3c.SizeVector([2, 3]), dtype, device)
    tl.push_back(t0)
    tl_other = o3c.TensorList(o3c.SizeVector([2, 3]), dtype, device)
    tl_other.push_back(t1)
    tl_other.push_back(t2)
    tl += tl_other
    assert tl.size == 3
    assert tl[0].allclose(t0)
    assert tl[1].allclose(t1)
    assert tl[2].allclose(t2)

    # concat
    tl = o3c.TensorList(o3c.SizeVector([2, 3]), dtype, device)
    tl.push_back(t0)
    tl_other = o3c.TensorList(o3c.SizeVector([2, 3]), dtype, device)
    tl_other.push_back(t1)
    tl_combined = o3c.TensorList.concat(tl, tl_other)
    assert tl_combined.size == 2
    assert tl_combined[0].allclose(t0)
    assert tl_combined[1].allclose(t1)

    # +
    tl = o3c.TensorList(o3c.SizeVector([2, 3]), dtype, device)
    tl.push_back(t0)
    tl_other = o3c.TensorList(o3c.SizeVector([2, 3]), dtype, device)
    tl_other.push_back(t1)
    tl_combined = tl + tl_other
    assert tl_combined.size == 2
    assert tl_combined[0].allclose(t0)
    assert tl_combined[1].allclose(t1)