29 #include <tbb/parallel_for.h> 39 template <
class TFeat,
46 bool INDIVIDUAL_EXTENT,
47 bool ISOTROPIC_EXTENT,
48 bool POINT_IMPORTANCE>
50 const std::vector<int>& filter_dims,
53 const TReal* out_positions,
55 const TReal* inp_positions,
56 const TFeat* inp_features,
57 const TFeat* inp_importance,
58 size_t neighbors_index_size,
59 const TIndex* neighbors_index,
60 const TFeat* neighbors_importance,
61 const int64_t* neighbors_row_splits,
65 const bool NEIGHBORS_IMPORTANCE = neighbors_importance !=
nullptr;
66 const int VECSIZE = 32;
67 typedef Eigen::Array<TReal, VECSIZE, 1> Vec_t;
69 InterpolationVec_t interpolation;
71 const int in_channels = filter_dims[filter_dims.size() - 2];
72 const int out_channels = filter_dims[filter_dims.size() - 1];
74 int spatial_filter_size = 1;
75 for (
int i = 0; i < 3; ++i) spatial_filter_size *= filter_dims[i];
76 Eigen::Array<int, 3, 1> filter_size_xyz(filter_dims[2], filter_dims[1],
79 memset(out_features, 0,
sizeof(TOut) * num_out * out_channels);
82 tbb::blocked_range<size_t>(0, num_out, 32),
83 [&](
const tbb::blocked_range<size_t>& r) {
84 int range_length = r.end() - r.begin();
86 Eigen::Matrix<TOut, Eigen::Dynamic, 1> normalizers(range_length,
88 normalizers.setZero();
90 Eigen::Matrix<TFeat, Eigen::Dynamic, Eigen::Dynamic> B(
91 in_channels * spatial_filter_size, range_length);
94 typedef Eigen::Array<TFeat, VECSIZE, Eigen::Dynamic> Matrix;
95 Matrix infeat(VECSIZE, in_channels);
97 Eigen::Array<TReal, 3, 1> offsets_(offsets[0], offsets[1],
100 Eigen::Array<TReal, VECSIZE, 3> inv_extents;
101 if (INDIVIDUAL_EXTENT ==
false) {
102 if (ISOTROPIC_EXTENT) {
103 inv_extents = 1 / extents[0];
105 inv_extents.col(0) = 1 / extents[0];
106 inv_extents.col(1) = 1 / extents[1];
107 inv_extents.col(2) = 1 / extents[2];
111 for (
size_t out_idx = r.begin(); out_idx != r.end();
113 const int out_col = out_idx - r.begin();
114 const size_t neighbor_start = neighbors_row_splits[out_idx];
115 const size_t neighbor_end =
116 neighbors_row_splits[out_idx + 1];
118 if (INDIVIDUAL_EXTENT) {
119 if (ISOTROPIC_EXTENT) {
120 inv_extents = 1 / extents[out_idx];
122 inv_extents.col(0) = 1 / extents[3 * out_idx + 0];
123 inv_extents.col(1) = 1 / extents[3 * out_idx + 1];
124 inv_extents.col(2) = 1 / extents[3 * out_idx + 2];
128 typename InterpolationVec_t::Weight_t interp_weights;
129 typename InterpolationVec_t::Idx_t interp_indices;
131 int vec_valid_count = 0;
139 for (
size_t n = neighbor_start; n < neighbor_end; ++n) {
140 const size_t inp_idx = neighbors_index[n];
141 const int i = vec_valid_count;
142 x(i) = inp_positions[inp_idx * 3 + 0] -
143 out_positions[out_idx * 3 + 0];
144 y(i) = inp_positions[inp_idx * 3 + 1] -
145 out_positions[out_idx * 3 + 1];
146 z(i) = inp_positions[inp_idx * 3 + 2] -
147 out_positions[out_idx * 3 + 2];
149 const TFeat n_importance =
150 (NEIGHBORS_IMPORTANCE ? neighbors_importance[n]
152 normalizers(out_col) += TOut(n_importance);
154 for (
int ic = 0; ic < in_channels; ++ic)
156 inp_features[inp_idx * in_channels + ic];
158 TFeat importance(1.0);
159 if (POINT_IMPORTANCE)
160 importance = inp_importance[inp_idx];
161 if (NEIGHBORS_IMPORTANCE) importance *= n_importance;
163 if (POINT_IMPORTANCE || NEIGHBORS_IMPORTANCE) {
164 for (
int ic = 0; ic < in_channels; ++ic)
165 infeat(i, ic) *= importance;
169 if (vec_valid_count == VECSIZE) {
170 ComputeFilterCoordinates<ALIGN_CORNERS, MAPPING>(
171 x, y, z, filter_size_xyz, inv_extents,
173 interpolation.Interpolate(
174 interp_weights, interp_indices, x, y, z,
175 filter_size_xyz, in_channels);
176 for (
int k = 0; k < VECSIZE; ++k)
177 for (
int j = 0; j < InterpolationVec_t::Size();
179 for (
int ic = 0; ic < in_channels; ++ic)
180 B(interp_indices(j, k) + ic, out_col) +=
181 TFeat(interp_weights(j, k)) *
187 if (vec_valid_count) {
188 ComputeFilterCoordinates<ALIGN_CORNERS, MAPPING>(
189 x, y, z, filter_size_xyz, inv_extents,
191 interpolation.Interpolate(interp_weights,
192 interp_indices, x, y, z,
193 filter_size_xyz, in_channels);
194 for (
int k = 0; k < vec_valid_count; ++k)
195 for (
int j = 0; j < InterpolationVec_t::Size();
197 for (
int ic = 0; ic < in_channels; ++ic)
198 B(interp_indices(j, k) + ic, out_col) +=
199 TFeat(interp_weights(j, k)) *
206 Eigen::Map<
const Eigen::Matrix<TFeat, Eigen::Dynamic,
208 A(filter, out_channels,
209 spatial_filter_size * in_channels);
210 Eigen::Map<Eigen::Matrix<TOut, Eigen::Dynamic, Eigen::Dynamic>>
211 C(out_features + (r.begin() * out_channels),
212 out_channels, range_length);
214 C = (A * B).
template cast<TOut>();
216 for (
int i = 0; i < range_length; ++i) {
217 if (normalizers(i) != TOut(0))
218 C.col(i) /= normalizers(i);
298 template <
class TFeat,
class TOut,
class TReal,
class TIndex>
300 const std::vector<int>& filter_dims,
303 const TReal* out_positions,
305 const TReal* inp_positions,
306 const TFeat* inp_features,
307 const TFeat* inp_importance,
308 size_t neighbors_index_size,
309 const TIndex* neighbors_index,
310 const TFeat* neighbors_importance,
311 const int64_t* neighbors_row_splits,
312 const TReal* extents,
313 const TReal* offsets,
317 bool individual_extent,
318 bool isotropic_extent,
321 bool has_importance = inp_importance;
323 #define FN_PARAMETERS \ 324 out_features, filter_dims, filter, num_out, out_positions, num_inp, \ 325 inp_positions, inp_features, inp_importance, neighbors_index_size, \ 326 neighbors_index, neighbors_importance, neighbors_row_splits, \ 327 extents, offsets, normalize 329 #define CALL_TEMPLATE(INTERPOLATION, MAPPING, ALIGN_CORNERS, \ 330 INDIVIDUAL_EXTENT, ISOTROPIC_EXTENT, HAS_IMPORTANCE) \ 331 if (INTERPOLATION == interpolation && MAPPING == coordinate_mapping && \ 332 ALIGN_CORNERS == align_corners && \ 333 INDIVIDUAL_EXTENT == individual_extent && \ 334 ISOTROPIC_EXTENT == isotropic_extent && \ 335 HAS_IMPORTANCE == has_importance) \ 336 _CConvComputeFeaturesCPU<TFeat, TOut, TReal, TIndex, INTERPOLATION, \ 337 MAPPING, ALIGN_CORNERS, INDIVIDUAL_EXTENT, \ 338 ISOTROPIC_EXTENT, HAS_IMPORTANCE>( \ 341 #define CALL_TEMPLATE2(INTERPOLATION, MAPPING) \ 342 CALL_TEMPLATE(INTERPOLATION, MAPPING, true, true, true, true) \ 343 CALL_TEMPLATE(INTERPOLATION, MAPPING, true, true, true, false) \ 344 CALL_TEMPLATE(INTERPOLATION, MAPPING, true, true, false, true) \ 345 CALL_TEMPLATE(INTERPOLATION, MAPPING, true, true, false, false) \ 346 CALL_TEMPLATE(INTERPOLATION, MAPPING, true, false, true, true) \ 347 CALL_TEMPLATE(INTERPOLATION, MAPPING, true, false, true, false) \ 348 CALL_TEMPLATE(INTERPOLATION, MAPPING, true, false, false, true) \ 349 CALL_TEMPLATE(INTERPOLATION, MAPPING, true, false, false, false) \ 350 CALL_TEMPLATE(INTERPOLATION, MAPPING, false, true, true, true) \ 351 CALL_TEMPLATE(INTERPOLATION, MAPPING, false, true, true, false) \ 352 CALL_TEMPLATE(INTERPOLATION, MAPPING, false, true, false, true) \ 353 CALL_TEMPLATE(INTERPOLATION, MAPPING, false, true, false, false) \ 354 CALL_TEMPLATE(INTERPOLATION, MAPPING, false, false, true, true) \ 355 CALL_TEMPLATE(INTERPOLATION, MAPPING, false, false, true, false) \ 356 CALL_TEMPLATE(INTERPOLATION, MAPPING, false, false, false, true) \ 357 CALL_TEMPLATE(INTERPOLATION, MAPPING, false, false, false, false) 359 #define CALL_TEMPLATE3(INTERPOLATION) \ 360 CALL_TEMPLATE2(INTERPOLATION, CoordinateMapping::BALL_TO_CUBE_RADIAL) \ 361 CALL_TEMPLATE2(INTERPOLATION, \ 362 CoordinateMapping::BALL_TO_CUBE_VOLUME_PRESERVING) \ 363 CALL_TEMPLATE2(INTERPOLATION, CoordinateMapping::IDENTITY) 365 #define CALL_TEMPLATE4 \ 366 CALL_TEMPLATE3(InterpolationMode::LINEAR) \ 367 CALL_TEMPLATE3(InterpolationMode::LINEAR_BORDER) \ 368 CALL_TEMPLATE3(InterpolationMode::NEAREST_NEIGHBOR) 373 #undef CALL_TEMPLATE2 374 #undef CALL_TEMPLATE3 375 #undef CALL_TEMPLATE4 InterpolationMode
Definition: ContinuousConvTypes.h:37
void CConvComputeFeaturesCPU(TOut *out_features, const std::vector< int > &filter_dims, const TFeat *filter, size_t num_out, const TReal *out_positions, size_t num_inp, const TReal *inp_positions, const TFeat *inp_features, const TFeat *inp_importance, 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: ContinuousConv.h:299
Class for computing interpolation weights.
Definition: CoordinateTransformation.h:204
void _CConvComputeFeaturesCPU(TOut *out_features, const std::vector< int > &filter_dims, const TFeat *filter, size_t num_out, const TReal *out_positions, size_t num_inp, const TReal *inp_positions, const TFeat *inp_features, const TFeat *inp_importance, 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, bool normalize)
Definition: ContinuousConv.h:49
CoordinateMapping
Definition: ContinuousConvTypes.h:45
Definition: PinholeCameraIntrinsic.cpp:35