File: test_subsampling.py

package info (click to toggle)
open3d 0.19.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 83,496 kB
  • sloc: cpp: 206,543; python: 27,254; ansic: 8,356; javascript: 1,883; sh: 1,527; makefile: 259; xml: 69
file content (75 lines) | stat: -rw-r--r-- 3,047 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# ----------------------------------------------------------------------------
# -                        Open3D: www.open3d.org                            -
# ----------------------------------------------------------------------------
# Copyright (c) 2018-2024 www.open3d.org
# SPDX-License-Identifier: MIT
# ----------------------------------------------------------------------------

import numpy as np
import open3d as o3d
import pytest
import importlib


@pytest.mark.skipif(not o3d._build_config['BUILD_TENSORFLOW_OPS'],
                    reason='tf ops not built')
def test_tf_subsampling():
    ops = importlib.import_module('open3d.ml.tf.ops')

    points = np.array(range(21)).reshape(-1, 3).astype(np.float32)

    sub_points = ops.grid_subsampling(points, 10).cpu().numpy()
    sub_points_ref = np.array(
        [[13.5, 14.5, 15.5], [18, 19, 20], [9, 10, 11], [3, 4, 5]],
        dtype=np.float32)

    sub_points = sub_points[sub_points[:, 0].argsort()]
    sub_points_ref = sub_points_ref[sub_points_ref[:, 0].argsort()]
    np.testing.assert_equal(sub_points, sub_points_ref)

    sub_points = ops.grid_subsampling(points, 12).cpu().numpy()
    sub_points_ref = np.array([[15, 16, 17], [4.5, 5.5, 6.5]], dtype=np.float32)
    sub_points = sub_points[sub_points[:, 0].argsort()]
    sub_points_ref = sub_points_ref[sub_points_ref[:, 0].argsort()]

    np.testing.assert_equal(sub_points, sub_points_ref)

    points = np.array([[0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1], [1, 1, 1],
                       [5, 0, 0], [5, 1, 0]],
                      dtype=np.float32)
    sub_points_ref = np.array([[5, 0.5, 0], [0.4, 0.4, 0.4]], dtype=np.float32)
    sub_points = ops.grid_subsampling(points, 1.1).cpu().numpy()
    sub_points = sub_points[sub_points[:, 0].argsort()]
    sub_points_ref = sub_points_ref[sub_points_ref[:, 0].argsort()]

    np.testing.assert_equal(sub_points, sub_points_ref)

    with pytest.raises(ValueError):
        ops.grid_subsampling(None, 1)


@pytest.mark.skipif(not o3d._build_config['BUILD_TENSORFLOW_OPS'],
                    reason='tf ops not built')
def test_tf_batch_subsampling():
    ops = importlib.import_module('open3d.ml.tf.ops')

    points = np.array([[0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1], [1, 1, 1],
                       [5, 0, 0], [5, 1, 0]],
                      dtype=np.float32)
    batches = np.array([3, 2, 2], dtype=np.int32)
    sub_points_ref = np.array(
        [[0.3333333, 0.3333333, 0], [0.5, 0.5, 1], [5, 0.5, 0]],
        dtype=np.float32)
    sub_batch_ref = np.array([1, 1, 1], dtype=np.int32)

    (sub_points, sub_batch) = ops.batch_grid_subsampling(points, batches, 1.1)
    sub_points, sub_batch = sub_points.cpu().numpy(), sub_batch.cpu().numpy()

    np.testing.assert_almost_equal(sub_points, sub_points_ref)
    np.testing.assert_almost_equal(sub_batch, sub_batch_ref)

    with pytest.raises(ValueError):
        ops.batch_grid_subsampling(None, batches, 1)

    with pytest.raises(ValueError):
        ops.batch_grid_subsampling(points, None, 1)