Open3D (C++ API)  0.19.0
ContinuousConvTranspose.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.open3d.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <tbb/parallel_for.h>
11 
13 
14 namespace open3d {
15 namespace ml {
16 namespace impl {
17 
20 template <class TFeat,
21  class TOut,
22  class TReal,
23  class TIndex,
24  InterpolationMode INTERPOLATION,
25  CoordinateMapping MAPPING,
26  bool ALIGN_CORNERS,
27  bool INDIVIDUAL_EXTENT,
28  bool ISOTROPIC_EXTENT,
29  bool NORMALIZE>
31  TOut* out_features,
32  const std::vector<int>& filter_dims,
33  const TFeat* filter,
34  size_t num_out,
35  const TReal* out_positions,
36  const TFeat* out_importance,
37  size_t num_inp,
38  const TReal* inp_positions,
39  const TFeat* inp_features,
40  const TFeat* inp_neighbors_importance_sum,
41  const int64_t* inp_neighbors_row_splits,
42  size_t neighbors_index_size,
43  const TIndex* neighbors_index,
44  const TFeat* neighbors_importance,
45  const int64_t* neighbors_row_splits,
46  const TReal* extents,
47  const TReal* offsets) {
48  const bool NEIGHBORS_IMPORTANCE = inp_neighbors_importance_sum;
49  const int VECSIZE = 32;
50  typedef Eigen::Array<TReal, VECSIZE, 1> Vec_t;
51  typedef InterpolationVec<TReal, VECSIZE, INTERPOLATION> InterpolationVec_t;
52  InterpolationVec_t interpolation;
53 
54  const int in_channels = filter_dims[filter_dims.size() - 2];
55  const int out_channels = filter_dims[filter_dims.size() - 1];
56 
57  int spatial_filter_size = 1;
58  for (int i = 0; i < 3; ++i) spatial_filter_size *= filter_dims[i];
59  Eigen::Array<int, 3, 1> filter_size_xyz(filter_dims[2], filter_dims[1],
60  filter_dims[0]);
61 
62  memset(out_features, 0, sizeof(TOut) * num_out * out_channels);
63 
64  typedef Eigen::Array<TFeat, VECSIZE, Eigen::Dynamic> Matrix;
65  typedef Eigen::Array<TReal, VECSIZE, 3> Matrix3C;
66 
67  tbb::parallel_for(
68  tbb::blocked_range<size_t>(0, num_out, 32),
69  [&](const tbb::blocked_range<size_t>& r) {
70  int range_length = r.end() - r.begin();
71 
72  Eigen::Matrix<TFeat, Eigen::Dynamic, Eigen::Dynamic> B(
73  in_channels * spatial_filter_size, range_length);
74  B.setZero();
75 
76  Matrix infeat(VECSIZE, in_channels);
77 
78  Eigen::Array<TReal, 3, 1> offsets_(offsets[0], offsets[1],
79  offsets[2]);
80 
81  Matrix3C inv_extents;
82  if (INDIVIDUAL_EXTENT == false) {
83  if (ISOTROPIC_EXTENT) {
84  inv_extents = 1 / extents[0];
85  } else {
86  inv_extents.col(0) = 1 / extents[0];
87  inv_extents.col(1) = 1 / extents[1];
88  inv_extents.col(2) = 1 / extents[2];
89  }
90  }
91 
92  for (size_t out_idx = r.begin(); out_idx != r.end();
93  ++out_idx) {
94  const int out_col = out_idx - r.begin();
95  const size_t neighbor_start = neighbors_row_splits[out_idx];
96  const size_t neighbor_end =
97  (out_idx + 1 < num_out
98  ? neighbors_row_splits[out_idx + 1]
99  : neighbors_index_size);
100 
101  typename InterpolationVec_t::Weight_t interp_weights;
102  typename InterpolationVec_t::Idx_t interp_indices;
103 
104  int vec_valid_count = 0;
105  Vec_t x, y, z;
106 
107  // set to zero to avoid problems with vectors with less than
108  // VECSIZE valid entries
109  x.setZero();
110  y.setZero();
111  z.setZero();
112  for (size_t n = neighbor_start; n < neighbor_end; ++n) {
113  const size_t inp_idx = neighbors_index[n];
114 
115  const int i = vec_valid_count;
116  x(i) = out_positions[out_idx * 3 + 0] -
117  inp_positions[inp_idx * 3 + 0];
118  y(i) = out_positions[out_idx * 3 + 1] -
119  inp_positions[inp_idx * 3 + 1];
120  z(i) = out_positions[out_idx * 3 + 2] -
121  inp_positions[inp_idx * 3 + 2];
122 
123  if (INDIVIDUAL_EXTENT) {
124  if (ISOTROPIC_EXTENT) {
125  inv_extents.row(i) = 1 / extents[inp_idx];
126  } else {
127  inv_extents(i, 0) =
128  1 / extents[3 * inp_idx + 0];
129  inv_extents(i, 1) =
130  1 / extents[3 * inp_idx + 1];
131  inv_extents(i, 2) =
132  1 / extents[3 * inp_idx + 2];
133  }
134  }
135 
136  TFeat n_importance = NEIGHBORS_IMPORTANCE
137  ? neighbors_importance[n]
138  : TFeat(1);
139  for (int ic = 0; ic < in_channels; ++ic)
140  infeat(i, ic) =
141  inp_features[inp_idx * in_channels + ic] *
142  n_importance;
143 
144  if (NORMALIZE) {
145  TFeat normalizer(1);
146  if (NEIGHBORS_IMPORTANCE) {
147  if (inp_neighbors_importance_sum[inp_idx] !=
148  TFeat(0))
149  normalizer /= inp_neighbors_importance_sum
150  [inp_idx];
151  } else {
152  size_t num_inp_neighbors;
153  const size_t inp_neighbor_start =
154  inp_neighbors_row_splits[inp_idx];
155  const size_t inp_neighbor_end =
156  inp_neighbors_row_splits[inp_idx + 1];
157  num_inp_neighbors =
158  inp_neighbor_end - inp_neighbor_start;
159  if (num_inp_neighbors > 0)
160  normalizer /= TFeat(num_inp_neighbors);
161  }
162  for (int ic = 0; ic < in_channels; ++ic)
163  infeat(i, ic) *= normalizer;
164  }
165 
166  ++vec_valid_count;
167  if (vec_valid_count == VECSIZE ||
168  n + 1 == neighbor_end) {
169  ComputeFilterCoordinates<ALIGN_CORNERS, MAPPING>(
170  x, y, z, filter_size_xyz, inv_extents,
171  offsets_);
172  interpolation.Interpolate(
173  interp_weights, interp_indices, x, y, z,
174  filter_size_xyz, in_channels);
175  for (int k = 0; k < vec_valid_count; ++k) {
176  for (int j = 0; j < InterpolationVec_t::Size();
177  ++j) {
178  for (int ic = 0; ic < in_channels; ++ic)
179  B(interp_indices(j, k) + ic, out_col) +=
180  TFeat(interp_weights(j, k)) *
181  infeat(k, ic);
182  }
183  }
184  vec_valid_count = 0;
185  }
186  }
187 
188  } // out_idx
189 
190  Eigen::Map<const Eigen::Matrix<TFeat, Eigen::Dynamic,
191  Eigen::Dynamic>>
192  A(filter, out_channels,
193  spatial_filter_size * in_channels);
194  Eigen::Map<Eigen::Matrix<TOut, Eigen::Dynamic, Eigen::Dynamic>>
195  C(out_features + (r.begin() * out_channels),
196  out_channels, range_length);
197 
198  C = (A * B).template cast<TOut>();
199  if (out_importance) {
200  for (int i = 0; i < range_length; ++i)
201  C.col(i) *= TOut(out_importance[r.begin() + i]);
202  }
203  });
204 }
205 
287 template <class TFeat, class TOut, class TReal, class TIndex>
288 void CConvTransposeComputeFeaturesCPU(TOut* out_features,
289  const std::vector<int>& filter_dims,
290  const TFeat* filter,
291  size_t num_out,
292  const TReal* out_positions,
293  const TFeat* out_importance,
294  size_t num_inp,
295  const TReal* inp_positions,
296  const TFeat* inp_features,
297  const TFeat* inp_neighbors_importance_sum,
298  const int64_t* inp_neighbors_row_splits,
299  size_t neighbors_index_size,
300  const TIndex* neighbors_index,
301  const TFeat* neighbors_importance,
302  const int64_t* neighbors_row_splits,
303  const TReal* extents,
304  const TReal* offsets,
305  InterpolationMode interpolation,
306  CoordinateMapping coordinate_mapping,
307  bool align_corners,
308  bool individual_extent,
309  bool isotropic_extent,
310  bool normalize) {
311 #define FN_PARAMETERS \
312  out_features, filter_dims, filter, num_out, out_positions, out_importance, \
313  num_inp, inp_positions, inp_features, \
314  inp_neighbors_importance_sum, inp_neighbors_row_splits, \
315  neighbors_index_size, neighbors_index, neighbors_importance, \
316  neighbors_row_splits, extents, offsets
317 
318 #define CALL_TEMPLATE(INTERPOLATION, MAPPING, ALIGN_CORNERS, \
319  INDIVIDUAL_EXTENT, ISOTROPIC_EXTENT, NORMALIZE) \
320  if (INTERPOLATION == interpolation && MAPPING == coordinate_mapping && \
321  ALIGN_CORNERS == align_corners && \
322  INDIVIDUAL_EXTENT == individual_extent && \
323  ISOTROPIC_EXTENT == isotropic_extent && NORMALIZE == normalize) \
324  _CConvTransposeComputeFeaturesCPU<TFeat, TOut, TReal, TIndex, \
325  INTERPOLATION, MAPPING, \
326  ALIGN_CORNERS, INDIVIDUAL_EXTENT, \
327  ISOTROPIC_EXTENT, NORMALIZE>( \
328  FN_PARAMETERS);
329 
330 #define CALL_TEMPLATE2(INTERPOLATION, MAPPING) \
331  CALL_TEMPLATE(INTERPOLATION, MAPPING, true, true, true, true) \
332  CALL_TEMPLATE(INTERPOLATION, MAPPING, true, true, true, false) \
333  CALL_TEMPLATE(INTERPOLATION, MAPPING, true, true, false, true) \
334  CALL_TEMPLATE(INTERPOLATION, MAPPING, true, true, false, false) \
335  CALL_TEMPLATE(INTERPOLATION, MAPPING, true, false, true, true) \
336  CALL_TEMPLATE(INTERPOLATION, MAPPING, true, false, true, false) \
337  CALL_TEMPLATE(INTERPOLATION, MAPPING, true, false, false, true) \
338  CALL_TEMPLATE(INTERPOLATION, MAPPING, true, false, false, false) \
339  CALL_TEMPLATE(INTERPOLATION, MAPPING, false, true, true, true) \
340  CALL_TEMPLATE(INTERPOLATION, MAPPING, false, true, true, false) \
341  CALL_TEMPLATE(INTERPOLATION, MAPPING, false, true, false, true) \
342  CALL_TEMPLATE(INTERPOLATION, MAPPING, false, true, false, false) \
343  CALL_TEMPLATE(INTERPOLATION, MAPPING, false, false, true, true) \
344  CALL_TEMPLATE(INTERPOLATION, MAPPING, false, false, true, false) \
345  CALL_TEMPLATE(INTERPOLATION, MAPPING, false, false, false, true) \
346  CALL_TEMPLATE(INTERPOLATION, MAPPING, false, false, false, false)
347 
348 #define CALL_TEMPLATE3(INTERPOLATION) \
349  CALL_TEMPLATE2(INTERPOLATION, CoordinateMapping::BALL_TO_CUBE_RADIAL) \
350  CALL_TEMPLATE2(INTERPOLATION, \
351  CoordinateMapping::BALL_TO_CUBE_VOLUME_PRESERVING) \
352  CALL_TEMPLATE2(INTERPOLATION, CoordinateMapping::IDENTITY)
353 
354 #define CALL_TEMPLATE4 \
355  CALL_TEMPLATE3(InterpolationMode::LINEAR) \
356  CALL_TEMPLATE3(InterpolationMode::LINEAR_BORDER) \
357  CALL_TEMPLATE3(InterpolationMode::NEAREST_NEIGHBOR)
358 
360 
361 #undef CALL_TEMPLATE
362 #undef CALL_TEMPLATE2
363 #undef CALL_TEMPLATE3
364 #undef CALL_TEMPLATE4
365 
366 #undef FN_PARAMETERS
367 }
368 
369 } // namespace impl
370 } // namespace ml
371 } // namespace open3d
#define CALL_TEMPLATE4
#define VECSIZE
Eigen::Matrix3d B
Definition: PointCloudPlanarPatchDetection.cpp:519
InterpolationMode
Definition: ContinuousConvTypes.h:18
void _CConvTransposeComputeFeaturesCPU(TOut *out_features, const std::vector< int > &filter_dims, const TFeat *filter, size_t num_out, const TReal *out_positions, const TFeat *out_importance, size_t num_inp, const TReal *inp_positions, const TFeat *inp_features, const TFeat *inp_neighbors_importance_sum, const int64_t *inp_neighbors_row_splits, size_t neighbors_index_size, const TIndex *neighbors_index, const TFeat *neighbors_importance, const int64_t *neighbors_row_splits, const TReal *extents, const TReal *offsets)
Definition: ContinuousConvTranspose.h:30
void CConvTransposeComputeFeaturesCPU(TOut *out_features, const std::vector< int > &filter_dims, const TFeat *filter, size_t num_out, const TReal *out_positions, const TFeat *out_importance, size_t num_inp, const TReal *inp_positions, const TFeat *inp_features, const TFeat *inp_neighbors_importance_sum, const int64_t *inp_neighbors_row_splits, size_t neighbors_index_size, const TIndex *neighbors_index, const TFeat *neighbors_importance, const int64_t *neighbors_row_splits, const TReal *extents, const TReal *offsets, InterpolationMode interpolation, CoordinateMapping coordinate_mapping, bool align_corners, bool individual_extent, bool isotropic_extent, bool normalize)
Definition: ContinuousConvTranspose.h:288
CoordinateMapping
Definition: ContinuousConvTypes.h:26
Definition: PinholeCameraIntrinsic.cpp:16
Class for computing interpolation weights.
Definition: CoordinateTransformation.h:185