30 #include "tensorflow/core/framework/op.h"
31 #include "tensorflow/core/framework/op_kernel.h"
32 #include "tensorflow/core/lib/core/errors.h"
36 namespace voxel_pooling_opkernel {
38 template <
class TReal,
class TFeat>
39 class OutputAllocator {
43 void AllocPooledPositions(TReal** ptr,
size_t num) {
44 using namespace tensorflow;
47 TensorShape shape({int64_t(num), 3});
48 OP_REQUIRES_OK(
context,
context->allocate_output(0, shape, &tensor));
49 auto flat_tensor = tensor->flat<TReal>();
50 *ptr = flat_tensor.data();
53 void AllocPooledFeatures(TFeat** ptr,
size_t num,
int channels) {
54 using namespace tensorflow;
57 TensorShape shape({int64_t(num), channels});
58 OP_REQUIRES_OK(
context,
context->allocate_output(1, shape, &tensor));
59 auto flat_tensor = tensor->flat<TFeat>();
60 *ptr = flat_tensor.data();
64 tensorflow::OpKernelContext*
context;
68 class VoxelPoolingGradOpKernel :
public tensorflow::OpKernel {
70 explicit VoxelPoolingGradOpKernel(
71 tensorflow::OpKernelConstruction* construction)
72 : OpKernel(construction) {
73 using namespace tensorflow;
75 std::string pos_fn_str;
76 OP_REQUIRES_OK(construction,
77 construction->GetAttr(
"position_fn", &pos_fn_str));
79 if (pos_fn_str ==
"average")
81 else if (pos_fn_str ==
"nearest_neighbor")
86 std::string feat_fn_str;
87 OP_REQUIRES_OK(construction,
88 construction->GetAttr(
"feature_fn", &feat_fn_str));
90 if (feat_fn_str ==
"average")
92 else if (feat_fn_str ==
"nearest_neighbor")
98 void Compute(tensorflow::OpKernelContext*
context)
override {
99 using namespace tensorflow;
102 const Tensor& positions =
context->input(0);
104 context, positions.shape().dims() == 2,
105 errors::InvalidArgument(
"positions must be a rank 2 tensor"));
107 const Tensor& features =
context->input(1);
109 context, features.shape().dims() == 2,
110 errors::InvalidArgument(
"features must be a rank 2 tensor"));
112 const Tensor& voxel_size =
context->input(2);
114 context, TensorShapeUtils::IsScalar(voxel_size.shape()),
115 errors::InvalidArgument(
"voxel_size must be a scalar, but is ",
116 voxel_size.shape().DebugString()));
118 const Tensor& pooled_positions =
context->input(3);
119 OP_REQUIRES(
context, pooled_positions.shape().dims() == 2,
120 errors::InvalidArgument(
121 "pooled_positions must be a rank 2 tensor"));
123 const Tensor& pooled_features_gradient =
context->input(4);
125 context, pooled_features_gradient.shape().dims() == 2,
126 errors::InvalidArgument(
127 "pooled_features_gradient must be a rank 2 tensor"));
129 Tensor* features_backprop =
nullptr;
130 OP_REQUIRES_OK(
context,
context->allocate_output(0, features.shape(),
131 &features_backprop));
133 Kernel(
context, *features_backprop, positions, features,
134 pooled_positions, pooled_features_gradient, voxel_size);
138 virtual void Kernel(tensorflow::OpKernelContext*
context,
139 tensorflow::Tensor& features_backprop,
140 const tensorflow::Tensor& positions,
141 const tensorflow::Tensor& features,
142 const tensorflow::Tensor& pooled_positions,
143 const tensorflow::Tensor& pooled_features_gradient,
144 const tensorflow::Tensor& voxel_size) = 0;
ImGuiContext * context
Definition: Window.cpp:95
Definition: ContinuousConv.h:35
AccumulationFn
Definition: VoxelPooling.h:40
@ CENTER
Definition: VoxelPooling.h:40
@ NEAREST_NEIGHBOR
Definition: VoxelPooling.h:40
@ MAX
Definition: VoxelPooling.h:40
@ AVERAGE
Definition: VoxelPooling.h:40