29 #include <tbb/parallel_for.h>
41 template <
class TFeat,
48 bool INDIVIDUAL_EXTENT,
49 bool ISOTROPIC_EXTENT,
52 const std::vector<int>& filter_dims,
54 const TReal* out_positions,
55 const TFeat* out_importance,
57 const TReal* inp_positions,
58 const TFeat* inp_features,
59 const TFeat* inp_neighbors_importance_sum,
60 const int64_t* inp_neighbors_row_splits,
61 size_t neighbors_index_size,
62 const TIndex* neighbors_index,
63 const TFeat* neighbors_importance,
64 const int64_t* neighbors_row_splits,
67 const TFeat* out_features_gradient) {
68 const bool NEIGHBOR_IMPORTANCE = neighbors_importance;
70 typedef Eigen::Array<TReal, VECSIZE, 1> Vec_t;
72 InterpolationVec_t interpolation;
74 const int in_channels = filter_dims[filter_dims.size() - 2];
75 const int out_channels = filter_dims[filter_dims.size() - 1];
77 int spatial_filter_size = 1;
78 for (
int i = 0; i < 3; ++i) spatial_filter_size *= filter_dims[i];
79 Eigen::Array<int, 3, 1> filter_size_xyz(filter_dims[2], filter_dims[1],
82 memset(filter_backprop, 0,
83 sizeof(TOut) * spatial_filter_size * in_channels * out_channels);
84 std::mutex filter_backprop_mutex;
87 tbb::blocked_range<size_t>(0, num_out, 32),
88 [&](
const tbb::blocked_range<size_t>& r) {
89 int range_length = r.end() - r.begin();
91 Eigen::Matrix<TFeat, Eigen::Dynamic, Eigen::Dynamic> B(
92 in_channels * spatial_filter_size, range_length);
94 Eigen::Matrix<TFeat, Eigen::Dynamic, Eigen::Dynamic> C(
95 out_channels, range_length);
97 typedef Eigen::Array<TFeat, VECSIZE, Eigen::Dynamic> Matrix;
98 Matrix infeat(
VECSIZE, in_channels);
100 Eigen::Array<TReal, 3, 1> offsets_(offsets[0], offsets[1],
103 Eigen::Array<TReal, VECSIZE, 3> inv_extents;
104 if (INDIVIDUAL_EXTENT ==
false) {
105 if (ISOTROPIC_EXTENT) {
106 inv_extents = 1 / extents[0];
108 inv_extents.col(0) = 1 / extents[0];
109 inv_extents.col(1) = 1 / extents[1];
110 inv_extents.col(2) = 1 / extents[2];
114 for (
size_t out_idx = r.begin(); out_idx != r.end();
116 const int out_col = out_idx - r.begin();
117 const size_t neighbor_start = neighbors_row_splits[out_idx];
118 const size_t neighbor_end =
119 neighbors_row_splits[out_idx + 1];
121 C.col(out_col) = Eigen::Map<
122 const Eigen::Array<TFeat, Eigen::Dynamic, 1>>(
123 out_features_gradient + out_idx * out_channels,
126 typename InterpolationVec_t::Weight_t interp_weights;
127 typename InterpolationVec_t::Idx_t interp_indices;
129 int vec_valid_count = 0;
137 for (
size_t n = neighbor_start; n < neighbor_end; ++n) {
138 const size_t inp_idx = neighbors_index[n];
140 const int i = vec_valid_count;
141 x(i) = out_positions[out_idx * 3 + 0] -
142 inp_positions[inp_idx * 3 + 0];
143 y(i) = out_positions[out_idx * 3 + 1] -
144 inp_positions[inp_idx * 3 + 1];
145 z(i) = out_positions[out_idx * 3 + 2] -
146 inp_positions[inp_idx * 3 + 2];
148 if (INDIVIDUAL_EXTENT) {
149 if (ISOTROPIC_EXTENT) {
150 inv_extents.row(i) = 1 / extents[inp_idx];
153 1 / extents[3 * inp_idx + 0];
155 1 / extents[3 * inp_idx + 1];
157 1 / extents[3 * inp_idx + 2];
161 TFeat n_importance = NEIGHBOR_IMPORTANCE
162 ? neighbors_importance[n]
164 for (
int ic = 0; ic < in_channels; ++ic)
166 inp_features[inp_idx * in_channels + ic] *
171 if (NEIGHBOR_IMPORTANCE) {
172 if (inp_neighbors_importance_sum[inp_idx] !=
174 normalizer /= inp_neighbors_importance_sum
177 size_t num_inp_neighbors;
178 const size_t inp_neighbor_start =
179 inp_neighbors_row_splits[inp_idx];
180 const size_t inp_neighbor_end =
181 inp_neighbors_row_splits[inp_idx + 1];
183 inp_neighbor_end - inp_neighbor_start;
184 if (num_inp_neighbors > 0)
185 normalizer /= TFeat(num_inp_neighbors);
187 for (
int ic = 0; ic < in_channels; ++ic)
188 infeat(i, ic) *= normalizer;
192 if (vec_valid_count ==
VECSIZE ||
193 n + 1 == neighbor_end) {
194 ComputeFilterCoordinates<ALIGN_CORNERS, MAPPING>(
195 x, y, z, filter_size_xyz, inv_extents,
197 interpolation.Interpolate(
198 interp_weights, interp_indices, x, y, z,
199 filter_size_xyz, in_channels);
200 for (
int k = 0; k < vec_valid_count; ++k) {
201 for (
int j = 0; j < InterpolationVec_t::Size();
203 for (
int ic = 0; ic < in_channels; ++ic)
204 B(interp_indices(j, k) + ic, out_col) +=
205 TFeat(interp_weights(j, k)) *
215 if (out_importance) {
216 for (
size_t out_idx = r.begin(); out_idx != r.end();
218 const int out_col = out_idx - r.begin();
219 C.col(out_col) *= out_importance[out_idx];
223 Eigen::Matrix<TOut, Eigen::Dynamic, Eigen::Dynamic> A(
224 out_channels, spatial_filter_size * in_channels);
226 A = (C * B.transpose()).
template cast<TOut>();
229 std::lock_guard<std::mutex> lock(filter_backprop_mutex);
231 for (
int j = 0; j < spatial_filter_size * in_channels; ++j)
232 for (
int i = 0; i < out_channels; ++i, ++linear_i) {
233 filter_backprop[linear_i] += A(i, j);
321 template <
class TFeat,
class TOut,
class TReal,
class TIndex>
323 const std::vector<int>& filter_dims,
325 const TReal* out_positions,
326 const TFeat* out_importance,
328 const TReal* inp_positions,
329 const TFeat* inp_features,
330 const TFeat* inp_neighbors_importance_sum,
331 const int64_t* inp_neighbors_row_splits,
332 size_t neighbors_index_size,
333 const TIndex* neighbors_index,
334 const TFeat* neighbors_importance,
335 const int64_t* neighbors_row_splits,
336 const TReal* extents,
337 const TReal* offsets,
338 const TFeat* out_features_gradient,
342 bool individual_extent,
343 bool isotropic_extent,
345 #define FN_PARAMETERS \
346 filter_backprop, filter_dims, num_out, out_positions, out_importance, \
347 num_inp, inp_positions, inp_features, \
348 inp_neighbors_importance_sum, inp_neighbors_row_splits, \
349 neighbors_index_size, neighbors_index, neighbors_importance, \
350 neighbors_row_splits, extents, offsets, out_features_gradient
352 #define CALL_TEMPLATE(INTERPOLATION, MAPPING, ALIGN_CORNERS, \
353 INDIVIDUAL_EXTENT, ISOTROPIC_EXTENT, NORMALIZE) \
354 if (INTERPOLATION == interpolation && MAPPING == coordinate_mapping && \
355 ALIGN_CORNERS == align_corners && \
356 INDIVIDUAL_EXTENT == individual_extent && \
357 ISOTROPIC_EXTENT == isotropic_extent && NORMALIZE == normalize) \
358 _CConvTransposeBackpropFilterCPU<TFeat, TOut, TReal, TIndex, \
359 INTERPOLATION, MAPPING, \
360 ALIGN_CORNERS, INDIVIDUAL_EXTENT, \
361 ISOTROPIC_EXTENT, NORMALIZE>( \
364 #define CALL_TEMPLATE2(INTERPOLATION, MAPPING) \
365 CALL_TEMPLATE(INTERPOLATION, MAPPING, true, true, true, true) \
366 CALL_TEMPLATE(INTERPOLATION, MAPPING, true, true, true, false) \
367 CALL_TEMPLATE(INTERPOLATION, MAPPING, true, true, false, true) \
368 CALL_TEMPLATE(INTERPOLATION, MAPPING, true, true, false, false) \
369 CALL_TEMPLATE(INTERPOLATION, MAPPING, true, false, true, true) \
370 CALL_TEMPLATE(INTERPOLATION, MAPPING, true, false, true, false) \
371 CALL_TEMPLATE(INTERPOLATION, MAPPING, true, false, false, true) \
372 CALL_TEMPLATE(INTERPOLATION, MAPPING, true, false, false, false) \
373 CALL_TEMPLATE(INTERPOLATION, MAPPING, false, true, true, true) \
374 CALL_TEMPLATE(INTERPOLATION, MAPPING, false, true, true, false) \
375 CALL_TEMPLATE(INTERPOLATION, MAPPING, false, true, false, true) \
376 CALL_TEMPLATE(INTERPOLATION, MAPPING, false, true, false, false) \
377 CALL_TEMPLATE(INTERPOLATION, MAPPING, false, false, true, true) \
378 CALL_TEMPLATE(INTERPOLATION, MAPPING, false, false, true, false) \
379 CALL_TEMPLATE(INTERPOLATION, MAPPING, false, false, false, true) \
380 CALL_TEMPLATE(INTERPOLATION, MAPPING, false, false, false, false)
382 #define CALL_TEMPLATE3(INTERPOLATION) \
383 CALL_TEMPLATE2(INTERPOLATION, CoordinateMapping::BALL_TO_CUBE_RADIAL) \
384 CALL_TEMPLATE2(INTERPOLATION, \
385 CoordinateMapping::BALL_TO_CUBE_VOLUME_PRESERVING) \
386 CALL_TEMPLATE2(INTERPOLATION, CoordinateMapping::IDENTITY)
388 #define CALL_TEMPLATE4 \
389 CALL_TEMPLATE3(InterpolationMode::LINEAR) \
390 CALL_TEMPLATE3(InterpolationMode::LINEAR_BORDER) \
391 CALL_TEMPLATE3(InterpolationMode::NEAREST_NEIGHBOR)
396 #undef CALL_TEMPLATE2
397 #undef CALL_TEMPLATE3
398 #undef CALL_TEMPLATE4
void _CConvTransposeBackpropFilterCPU(TOut *filter_backprop, const std::vector< int > &filter_dims, 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, const TFeat *out_features_gradient)
Definition: ContinuousConvTransposeBackpropFilter.h:51
InterpolationMode
Definition: ContinuousConvTypes.h:37
void CConvTransposeBackpropFilterCPU(TOut *filter_backprop, const std::vector< int > &filter_dims, 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, const TFeat *out_features_gradient, InterpolationMode interpolation, CoordinateMapping coordinate_mapping, bool align_corners, bool individual_extent, bool isotropic_extent, bool normalize)
Definition: ContinuousConvTransposeBackpropFilter.h:322
CoordinateMapping
Definition: ContinuousConvTypes.h:45
Definition: PinholeCameraIntrinsic.cpp:35
Class for computing interpolation weights.
Definition: CoordinateTransformation.h:204