11 #include "tensorflow/core/framework/op.h"
12 #include "tensorflow/core/framework/op_kernel.h"
13 #include "tensorflow/core/lib/core/errors.h"
17 namespace voxel_pooling_opkernel {
19 template <
class TReal,
class TFeat>
20 class OutputAllocator {
24 void AllocPooledPositions(TReal** ptr,
size_t num) {
25 using namespace tensorflow;
28 TensorShape shape({int64_t(num), 3});
29 OP_REQUIRES_OK(
context,
context->allocate_output(0, shape, &tensor));
30 auto flat_tensor = tensor->flat<TReal>();
31 *ptr = flat_tensor.data();
34 void AllocPooledFeatures(TFeat** ptr,
size_t num,
int channels) {
35 using namespace tensorflow;
38 TensorShape shape({int64_t(num), channels});
39 OP_REQUIRES_OK(
context,
context->allocate_output(1, shape, &tensor));
40 auto flat_tensor = tensor->flat<TFeat>();
41 *ptr = flat_tensor.data();
45 tensorflow::OpKernelContext*
context;
49 class VoxelPoolingGradOpKernel :
public tensorflow::OpKernel {
51 explicit VoxelPoolingGradOpKernel(
52 tensorflow::OpKernelConstruction* construction)
53 : OpKernel(construction) {
54 using namespace tensorflow;
56 std::string pos_fn_str;
57 OP_REQUIRES_OK(construction,
58 construction->GetAttr(
"position_fn", &pos_fn_str));
60 if (pos_fn_str ==
"average")
62 else if (pos_fn_str ==
"nearest_neighbor")
67 std::string feat_fn_str;
68 OP_REQUIRES_OK(construction,
69 construction->GetAttr(
"feature_fn", &feat_fn_str));
71 if (feat_fn_str ==
"average")
73 else if (feat_fn_str ==
"nearest_neighbor")
79 void Compute(tensorflow::OpKernelContext*
context)
override {
80 using namespace tensorflow;
83 const Tensor& positions =
context->input(0);
85 context, positions.shape().dims() == 2,
86 errors::InvalidArgument(
"positions must be a rank 2 tensor"));
88 const Tensor& features =
context->input(1);
90 context, features.shape().dims() == 2,
91 errors::InvalidArgument(
"features must be a rank 2 tensor"));
93 const Tensor& voxel_size =
context->input(2);
95 context, TensorShapeUtils::IsScalar(voxel_size.shape()),
96 errors::InvalidArgument(
"voxel_size must be a scalar, but is ",
97 voxel_size.shape().DebugString()));
99 const Tensor& pooled_positions =
context->input(3);
100 OP_REQUIRES(
context, pooled_positions.shape().dims() == 2,
101 errors::InvalidArgument(
102 "pooled_positions must be a rank 2 tensor"));
104 const Tensor& pooled_features_gradient =
context->input(4);
106 context, pooled_features_gradient.shape().dims() == 2,
107 errors::InvalidArgument(
108 "pooled_features_gradient must be a rank 2 tensor"));
110 Tensor* features_backprop =
nullptr;
111 OP_REQUIRES_OK(
context,
context->allocate_output(0, features.shape(),
112 &features_backprop));
114 Kernel(
context, *features_backprop, positions, features,
115 pooled_positions, pooled_features_gradient, voxel_size);
119 virtual void Kernel(tensorflow::OpKernelContext*
context,
120 tensorflow::Tensor& features_backprop,
121 const tensorflow::Tensor& positions,
122 const tensorflow::Tensor& features,
123 const tensorflow::Tensor& pooled_positions,
124 const tensorflow::Tensor& pooled_features_gradient,
125 const tensorflow::Tensor& voxel_size) = 0;
ImGuiContext * context
Definition: Window.cpp:76
Definition: ContinuousConv.h:16
AccumulationFn
Definition: VoxelPooling.h:21
@ CENTER
Definition: VoxelPooling.h:21
@ NEAREST_NEIGHBOR
Definition: VoxelPooling.h:21
@ MAX
Definition: VoxelPooling.h:21
@ AVERAGE
Definition: VoxelPooling.h:21