29 #include <tbb/parallel_for.h> 39 template <
class TReal,
44 bool INDIVIDUAL_EXTENT,
45 bool ISOTROPIC_EXTENT,
49 const std::vector<int>& filter_dims,
52 const TReal* out_positions,
53 const TReal* out_importance,
55 const TReal* inp_positions,
56 const TReal* inp_features,
57 const TReal* inp_neighbors_importance_sum,
58 const int64_t* inp_neighbors_row_splits,
59 size_t neighbors_index_size,
60 const TIndex* neighbor_index,
61 const TReal* neighbor_importance,
62 const int64_t* neighbors_row_splits,
64 const TReal* offsets) {
65 const bool NEIGHBOR_IMPORTANCE = inp_neighbors_importance_sum;
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(TReal) * 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<TReal, Eigen::Dynamic, Eigen::Dynamic> B(
87 in_channels * spatial_filter_size, range_length);
90 typedef Eigen::Array<TReal, VECSIZE, Eigen::Dynamic> Matrix;
91 Matrix infeat(VECSIZE, in_channels);
93 Eigen::Array<TReal, 3, 1> offsets_(offsets[0], offsets[1],
96 Eigen::Array<TReal, VECSIZE, 3> inv_extents;
97 if (INDIVIDUAL_EXTENT ==
false) {
98 if (ISOTROPIC_EXTENT) {
99 inv_extents = 1 / extents[0];
101 inv_extents.col(0) = 1 / extents[0];
102 inv_extents.col(1) = 1 / extents[1];
103 inv_extents.col(2) = 1 / extents[2];
107 for (
size_t out_idx = r.begin(); out_idx != r.end();
109 const int out_col = out_idx - r.begin();
110 const size_t neighbor_start = neighbors_row_splits[out_idx];
111 const size_t neighbor_end =
112 (out_idx + 1 < num_out
113 ? neighbors_row_splits[out_idx + 1]
114 : neighbors_index_size);
116 typename InterpolationVec_t::Weight_t interp_weights;
117 typename InterpolationVec_t::Idx_t interp_indices;
119 int vec_valid_count = 0;
127 for (
size_t n = neighbor_start; n < neighbor_end; ++n) {
128 const size_t inp_idx = neighbor_index[n];
130 const int i = vec_valid_count;
131 x(i) = out_positions[out_idx * 3 + 0] -
132 inp_positions[inp_idx * 3 + 0];
133 y(i) = out_positions[out_idx * 3 + 1] -
134 inp_positions[inp_idx * 3 + 1];
135 z(i) = out_positions[out_idx * 3 + 2] -
136 inp_positions[inp_idx * 3 + 2];
138 if (INDIVIDUAL_EXTENT) {
139 if (ISOTROPIC_EXTENT) {
140 inv_extents.row(i) = 1 / extents[inp_idx];
143 1 / extents[3 * inp_idx + 0];
145 1 / extents[3 * inp_idx + 1];
147 1 / extents[3 * inp_idx + 2];
151 TReal n_importance = NEIGHBOR_IMPORTANCE
152 ? neighbor_importance[n]
154 for (
int ic = 0; ic < in_channels; ++ic)
156 inp_features[inp_idx * in_channels + ic] *
160 TReal normalizer = 1;
161 if (NEIGHBOR_IMPORTANCE) {
162 if (inp_neighbors_importance_sum[inp_idx] != 0)
163 normalizer /= inp_neighbors_importance_sum
166 size_t num_inp_neighbors;
167 const size_t inp_neighbor_start =
168 inp_neighbors_row_splits[inp_idx];
169 const size_t inp_neighbor_end =
170 inp_neighbors_row_splits[inp_idx + 1];
172 inp_neighbor_end - inp_neighbor_start;
173 if (num_inp_neighbors > 0)
174 normalizer /= num_inp_neighbors;
176 for (
int ic = 0; ic < in_channels; ++ic)
177 infeat(i, ic) *= normalizer;
181 if (vec_valid_count == VECSIZE ||
182 n + 1 == neighbor_end) {
183 ComputeFilterCoordinates<ALIGN_CORNERS, MAPPING>(
184 x, y, z, filter_size_xyz, inv_extents,
186 interpolation.Interpolate(
187 interp_weights, interp_indices, x, y, z,
188 filter_size_xyz, in_channels);
189 for (
int k = 0; k < vec_valid_count; ++k) {
190 for (
int j = 0; j < InterpolationVec_t::Size();
192 for (
int ic = 0; ic < in_channels; ++ic)
193 B(interp_indices(j, k) + ic, out_col) +=
194 interp_weights(j, k) *
204 Eigen::Map<
const Eigen::Matrix<TReal, Eigen::Dynamic,
206 A(filter, out_channels,
207 spatial_filter_size * in_channels);
208 Eigen::Map<Eigen::Matrix<TReal, Eigen::Dynamic, Eigen::Dynamic>>
209 C(out_features + (r.begin() * out_channels),
210 out_channels, range_length);
213 if (out_importance) {
214 for (
int i = 0; i < range_length; ++i)
215 C.col(i) *= out_importance[r.begin() + i];
296 template <
class TReal,
class TIndex>
298 const std::vector<int>& filter_dims,
301 const TReal* out_positions,
302 const TReal* out_importance,
304 const TReal* inp_positions,
305 const TReal* inp_features,
306 const TReal* inp_neighbors_importance_sum,
307 const int64_t* inp_neighbors_row_splits,
308 size_t neighbors_index_size,
309 const TIndex* neighbor_index,
310 const TReal* neighbor_importance,
311 const int64_t* neighbors_row_splits,
312 const TReal* extents,
313 const TReal* offsets,
317 bool individual_extent,
318 bool isotropic_extent,
320 #define FN_PARAMETERS \ 321 out_features, filter_dims, filter, num_out, out_positions, out_importance, \ 322 num_inp, inp_positions, inp_features, \ 323 inp_neighbors_importance_sum, inp_neighbors_row_splits, \ 324 neighbors_index_size, neighbor_index, neighbor_importance, \ 325 neighbors_row_splits, extents, offsets 327 #define CALL_TEMPLATE(INTERPOLATION, MAPPING, ALIGN_CORNERS, \ 328 INDIVIDUAL_EXTENT, ISOTROPIC_EXTENT, NORMALIZE) \ 329 if (INTERPOLATION == interpolation && MAPPING == coordinate_mapping && \ 330 ALIGN_CORNERS == align_corners && \ 331 INDIVIDUAL_EXTENT == individual_extent && \ 332 ISOTROPIC_EXTENT == isotropic_extent && NORMALIZE == normalize) \ 333 _CConvTransposeComputeFeaturesCPU< \ 334 TReal, TIndex, INTERPOLATION, MAPPING, ALIGN_CORNERS, \ 335 INDIVIDUAL_EXTENT, ISOTROPIC_EXTENT, NORMALIZE>( \ 338 #define CALL_TEMPLATE2(INTERPOLATION, MAPPING) \ 339 CALL_TEMPLATE(INTERPOLATION, MAPPING, true, true, true, true) \ 340 CALL_TEMPLATE(INTERPOLATION, MAPPING, true, true, true, false) \ 341 CALL_TEMPLATE(INTERPOLATION, MAPPING, true, true, false, true) \ 342 CALL_TEMPLATE(INTERPOLATION, MAPPING, true, true, false, false) \ 343 CALL_TEMPLATE(INTERPOLATION, MAPPING, true, false, true, true) \ 344 CALL_TEMPLATE(INTERPOLATION, MAPPING, true, false, true, false) \ 345 CALL_TEMPLATE(INTERPOLATION, MAPPING, true, false, false, true) \ 346 CALL_TEMPLATE(INTERPOLATION, MAPPING, true, false, false, false) \ 347 CALL_TEMPLATE(INTERPOLATION, MAPPING, false, true, true, true) \ 348 CALL_TEMPLATE(INTERPOLATION, MAPPING, false, true, true, false) \ 349 CALL_TEMPLATE(INTERPOLATION, MAPPING, false, true, false, true) \ 350 CALL_TEMPLATE(INTERPOLATION, MAPPING, false, true, false, false) \ 351 CALL_TEMPLATE(INTERPOLATION, MAPPING, false, false, true, true) \ 352 CALL_TEMPLATE(INTERPOLATION, MAPPING, false, false, true, false) \ 353 CALL_TEMPLATE(INTERPOLATION, MAPPING, false, false, false, true) \ 354 CALL_TEMPLATE(INTERPOLATION, MAPPING, false, false, false, false) 356 #define CALL_TEMPLATE3(INTERPOLATION) \ 357 CALL_TEMPLATE2(INTERPOLATION, CoordinateMapping::BALL_TO_CUBE_RADIAL) \ 358 CALL_TEMPLATE2(INTERPOLATION, \ 359 CoordinateMapping::BALL_TO_CUBE_VOLUME_PRESERVING) \ 360 CALL_TEMPLATE2(INTERPOLATION, CoordinateMapping::IDENTITY) 362 #define CALL_TEMPLATE4 \ 363 CALL_TEMPLATE3(InterpolationMode::LINEAR) \ 364 CALL_TEMPLATE3(InterpolationMode::LINEAR_BORDER) \ 365 CALL_TEMPLATE3(InterpolationMode::NEAREST_NEIGHBOR) 370 #undef CALL_TEMPLATE2 371 #undef CALL_TEMPLATE3 372 #undef CALL_TEMPLATE4
InterpolationMode
Definition: ContinuousConvTypes.h:37
Class for computing interpolation weights.
Definition: CoordinateTransformation.h:204
CoordinateMapping
Definition: ContinuousConvTypes.h:45
Definition: PinholeCameraIntrinsic.cpp:35
void CConvTransposeComputeFeaturesCPU(TReal *out_features, const std::vector< int > &filter_dims, const TReal *filter, size_t num_out, const TReal *out_positions, const TReal *out_importance, size_t num_inp, const TReal *inp_positions, const TReal *inp_features, const TReal *inp_neighbors_importance_sum, const int64_t *inp_neighbors_row_splits, size_t neighbors_index_size, const TIndex *neighbor_index, const TReal *neighbor_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:297
void _CConvTransposeComputeFeaturesCPU(TReal *out_features, const std::vector< int > &filter_dims, const TReal *filter, size_t num_out, const TReal *out_positions, const TReal *out_importance, size_t num_inp, const TReal *inp_positions, const TReal *inp_features, const TReal *inp_neighbors_importance_sum, const int64_t *inp_neighbors_row_splits, size_t neighbors_index_size, const TIndex *neighbor_index, const TReal *neighbor_importance, const int64_t *neighbors_row_splits, const TReal *extents, const TReal *offsets)
Definition: ContinuousConvTranspose.h:47