File: test_cconv_python.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 (266 lines) | stat: -rw-r--r-- 12,601 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# ----------------------------------------------------------------------------
# -                        Open3D: www.open3d.org                            -
# ----------------------------------------------------------------------------
# Copyright (c) 2018-2024 www.open3d.org
# SPDX-License-Identifier: MIT
# ----------------------------------------------------------------------------
"""Tests the reference python implementation of the continuous conv"""

import open3d as o3d
import numpy as np
import pytest
import mltest
from check_gradients import check_gradients
from cconv_python import *

# skip all tests if the ml ops were not built
pytestmark = mltest.default_marks


# yapf: disable
@pytest.mark.parametrize("filter_size, out_channels, in_channels, with_inp_importance, with_normalization",[
                             ([3,5,1],            2,           7,                True,              False),
                             ([3,3,3],            1,           1,               False,              False),
                             ([5,5,5],            5,           3,               False,               True),
                        ])
# yapf: enable
@pytest.mark.parametrize('dtype', [np.float32, np.float64])
def test_compare_to_conv3d(dtype, filter_size, out_channels, in_channels,
                           with_inp_importance, with_normalization):
    """Compares to the 3D convolution in tensorflow"""
    tf = pytest.importorskip('tensorflow')
    ml3d = pytest.importorskip('open3d.ml.tf')
    np.random.seed(0)

    conv_attrs = {
        'align_corners': False,
        'coordinate_mapping': IDENTITY,
        'normalize': with_normalization,
        'interpolation': NEAREST_NEIGHBOR,
    }

    filters = np.random.random(size=(*filter_size, in_channels,
                                     out_channels)).astype(dtype)

    max_grid_extent = 10
    inp_positions = np.unique(np.random.randint(0, max_grid_extent,
                                                (256, 3)).astype(dtype),
                              axis=0)
    inp_positions_int = inp_positions.astype(np.int32)
    if (with_inp_importance):
        inp_importance = np.random.rand(
            inp_positions.shape[0]).astype(dtype) - 0.5
    else:
        inp_importance = np.empty((0,))
    out_positions = np.unique(np.random.randint(
        np.max(filter_size) // 2, max_grid_extent - np.max(filter_size) // 2,
        (5, 3)).astype(dtype),
                              axis=0)
    out_positions_int = out_positions.astype(np.int32)

    voxel_size = np.array([1, 1, 1], dtype=dtype)
    voxel_offset = np.array([0, 0, 0], dtype=dtype)
    extent = voxel_size[np.newaxis, :] * np.array(filter_size[::-1])
    offset = np.array([0.0, 0.0, 0.0], dtype=dtype)

    inp_features = np.random.uniform(size=inp_positions.shape[0:1] +
                                     (in_channels,)).astype(dtype)
    fixed_radius_search = ml3d.layers.FixedRadiusSearch(metric='Linf')
    neighbors_index, neighbors_row_splits, _ = fixed_radius_search(
        inp_positions / extent,
        out_positions / extent,
        voxel_size[0] / 2 + 0.01,
    )
    neighbors_index = neighbors_index.numpy()
    neighbors_row_splits = neighbors_row_splits.numpy()

    neighbors_importance = np.empty((0,))

    y = cconv(filters, out_positions, extent, offset, inp_positions,
              inp_features, inp_importance, neighbors_index,
              neighbors_importance, neighbors_row_splits, **conv_attrs)

    # Compare the output to a standard 3d conv
    # store features in a volume to use standard 3d convs
    inp_volume = np.zeros(
        (1, max_grid_extent, max_grid_extent, max_grid_extent, in_channels))

    if with_inp_importance:
        inp_features *= inp_importance[:, np.newaxis]
    inp_volume[0, inp_positions_int[:, 2], inp_positions_int[:, 1],
               inp_positions_int[:, 0], :] = inp_features

    y_conv3d = tf.nn.conv3d(
        inp_volume,
        filters,
        strides=[1] * 5,
        padding='SAME',
    ).numpy()

    # extract result at output positions
    y_conv3d = np.ascontiguousarray(y_conv3d[0, out_positions_int[:, 2],
                                             out_positions_int[:, 1],
                                             out_positions_int[:, 0], :])

    if with_normalization:
        for i, v in enumerate(y_conv3d):
            num_neighbors = neighbors_row_splits[i +
                                                 1] - neighbors_row_splits[i]
            v /= dtype(num_neighbors)

    np.testing.assert_allclose(y, y_conv3d, rtol=1e-5, atol=1e-8)


# yapf: disable
@pytest.mark.parametrize("filter_size, out_channels, in_channels, with_inp_importance, with_neighbors_importance, with_individual_extent, with_normalization, align_corners, coordinate_mapping, interpolation",[
                             ([3,5,1],            2,           7,                True,                     False,                  False,              False,          True,        IDENTITY, NEAREST_NEIGHBOR),
                             ([3,3,3],            1,           1,               False,                     False,                   True,              False,         False, BALL_TO_CUBE_RADIAL,       LINEAR),
                             ([5,5,5],            5,           3,               False,                      True,                  False,              True, False, BALL_TO_CUBE_VOLUME_PRESERVING, LINEAR_BORDER),
                             ([5,1,3],            3,           4,               False,                      True,                  False,              False,         False,           IDENTITY,        LINEAR),
                        ])
# yapf: enable
def test_cconv_gradient(filter_size, out_channels, in_channels,
                        with_inp_importance, with_neighbors_importance,
                        with_individual_extent, with_normalization,
                        align_corners, coordinate_mapping, interpolation):
    tf = pytest.importorskip('tensorflow')
    ml3d = pytest.importorskip('open3d.ml.tf')
    dtype = np.float64
    np.random.seed(0)

    conv_attrs = {
        'align_corners': align_corners,
        'coordinate_mapping': coordinate_mapping,
        'normalize': with_normalization,
        'interpolation': interpolation,
    }

    filters = np.random.random(size=(*filter_size, in_channels,
                                     out_channels)).astype(dtype)

    inp_positions = np.random.rand(128, 3).astype(dtype)
    if (with_inp_importance):
        inp_importance = np.random.rand(inp_positions.shape[0]).astype(dtype)
    else:
        inp_importance = np.empty((0,))

    out_positions = np.random.rand(5, 3).astype(dtype)

    if with_individual_extent:
        extent = 0.4 + 0.01 * (np.random.rand(out_positions.shape[0], 1) - 0.5)
    else:
        extent = np.array([[0.4]], dtype=dtype)
    offset = np.array([0.0, 0.0, 0.0], dtype=dtype)

    inp_features = np.random.uniform(size=inp_positions.shape[0:1] +
                                     (in_channels,)).astype(dtype)
    fixed_radius_search = ml3d.layers.FixedRadiusSearch(metric='Linf')
    neighbors_index, neighbors_row_splits, _ = fixed_radius_search(
        inp_positions, out_positions, extent[0, 0] / 2)
    neighbors_index = neighbors_index.numpy()
    neighbors_row_splits = neighbors_row_splits.numpy()

    if (with_neighbors_importance):
        neighbors_importance = np.random.rand(
            neighbors_index.shape[0]).astype(dtype) - 0.5
    else:
        neighbors_importance = np.empty((0,))

    inverted_neighbors_index, inverted_neighbors_row_splits, inverted_neighbors_importance = ml3d.ops.invert_neighbors_list(
        inp_positions.shape[0], neighbors_index, neighbors_row_splits,
        neighbors_importance)

    inverted_neighbors_index = inverted_neighbors_index.numpy()
    inverted_neighbors_row_splits = inverted_neighbors_row_splits.numpy()
    inverted_neighbors_importance = inverted_neighbors_importance.numpy()

    # print(neighbors_row_splits, inverted_neighbors_row_splits)
    # print(neighbors_index, inverted_neighbors_index)

    # define functions for the gradient checker
    def conv_infeats(inp_features):
        return cconv(filters, out_positions, extent, offset, inp_positions,
                     inp_features, inp_importance, neighbors_index,
                     neighbors_importance, neighbors_row_splits, **conv_attrs)

    def conv_filter(filter):
        return cconv(filter, out_positions, extent, offset, inp_positions,
                     inp_features, inp_importance, neighbors_index,
                     neighbors_importance, neighbors_row_splits, **conv_attrs)

    def conv_filter_backprop(out_features_gradient, filter):
        return cconv_backprop_filter(filter, out_positions, extent, offset,
                                     inp_positions, inp_features,
                                     inp_importance, neighbors_index,
                                     neighbors_importance, neighbors_row_splits,
                                     out_features_gradient, **conv_attrs)

    def conv_transpose_as_infeat_backprop(out_features_gradient, inp_features):
        return cconv_transpose(filters.transpose([0, 1, 2, 4, 3]),
                               inp_positions, inp_importance, extent, offset,
                               out_positions, out_features_gradient,
                               neighbors_index, neighbors_importance,
                               neighbors_row_splits, inverted_neighbors_index,
                               inverted_neighbors_importance,
                               inverted_neighbors_row_splits, **conv_attrs)

    def conv_transpose_filter(filter):
        return cconv_transpose(filter.transpose([0, 1, 2, 4, 3]), inp_positions,
                               inp_importance, extent, offset, out_positions,
                               y_arr, neighbors_index, neighbors_importance,
                               neighbors_row_splits, inverted_neighbors_index,
                               inverted_neighbors_importance,
                               inverted_neighbors_row_splits, **conv_attrs)

    def conv_transpose_infeats(inp_features):
        return cconv_transpose(filters.transpose([0, 1, 2, 4, 3]),
                               inp_positions, inp_importance, extent, offset,
                               out_positions, inp_features, neighbors_index,
                               neighbors_importance, neighbors_row_splits,
                               inverted_neighbors_index,
                               inverted_neighbors_importance,
                               inverted_neighbors_row_splits, **conv_attrs)

    def conv_transpose_filter_backprop(out_features_gradient, filter):
        ans = cconv_transpose_backprop_filter(
            filter.transpose([0, 1, 2, 4, 3]), inp_positions, inp_importance,
            extent, offset, out_positions, y_arr, neighbors_index,
            neighbors_importance, neighbors_row_splits,
            inverted_neighbors_index, inverted_neighbors_importance,
            inverted_neighbors_row_splits, out_features_gradient, **conv_attrs)
        return ans.transpose([0, 1, 2, 4, 3])

    def conv_transpose_infeat_backprop(out_features_gradient, inp_features):
        ans = cconv(filters, out_positions, extent, offset, inp_positions,
                    out_features_gradient, inp_importance, neighbors_index,
                    neighbors_importance, neighbors_row_splits, **conv_attrs)
        return ans

    y_arr = conv_infeats(inp_features)

    dbg = {}
    filter_gradient_OK = check_gradients(filters,
                                         conv_filter,
                                         conv_filter_backprop,
                                         debug_outputs=dbg)
    assert filter_gradient_OK

    feature_gradient_OK = check_gradients(inp_features,
                                          conv_infeats,
                                          conv_transpose_as_infeat_backprop,
                                          debug_outputs=dbg)
    assert feature_gradient_OK

    transpose_filter_gradient_OK = check_gradients(
        filters,
        conv_transpose_filter,
        conv_transpose_filter_backprop,
        debug_outputs=dbg)
    assert transpose_filter_gradient_OK

    transpose_feature_gradient_OK = check_gradients(
        y_arr,
        conv_transpose_infeats,
        conv_transpose_infeat_backprop,
        debug_outputs=dbg)
    assert transpose_feature_gradient_OK