Open3D (C++ API)  0.12.0
VoxelPoolingGradOpKernel.h
Go to the documentation of this file.
1 // The MIT License (MIT)
2 //
3 // Copyright (c) 2020 www.open3d.org
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 // IN THE SOFTWARE.
22 // ----------------------------------------------------------------------------
23 #pragma once
24 
26 #include "tensorflow/core/framework/op.h"
27 #include "tensorflow/core/framework/op_kernel.h"
28 #include "tensorflow/core/lib/core/errors.h"
29 
31 // namespace for code that is common for all kernels
32 namespace voxel_pooling_opkernel {
33 
34 template <class TReal, class TFeat>
35 class OutputAllocator {
36 public:
37  OutputAllocator(tensorflow::OpKernelContext* context) : context(context) {}
38 
39  void AllocPooledPositions(TReal** ptr, size_t num) {
40  using namespace tensorflow;
41  *ptr = nullptr;
42  Tensor* tensor = 0;
43  TensorShape shape({int64_t(num), 3});
44  OP_REQUIRES_OK(context, context->allocate_output(0, shape, &tensor));
45  auto flat_tensor = tensor->flat<TReal>();
46  *ptr = flat_tensor.data();
47  }
48 
49  void AllocPooledFeatures(TFeat** ptr, size_t num, int channels) {
50  using namespace tensorflow;
51  *ptr = nullptr;
52  Tensor* tensor = 0;
53  TensorShape shape({int64_t(num), channels});
54  OP_REQUIRES_OK(context, context->allocate_output(1, shape, &tensor));
55  auto flat_tensor = tensor->flat<TFeat>();
56  *ptr = flat_tensor.data();
57  }
58 
59 private:
60  tensorflow::OpKernelContext* context;
61 };
62 
63 // Base class with common code for the OpKernel implementations
64 class VoxelPoolingGradOpKernel : public tensorflow::OpKernel {
65 public:
66  explicit VoxelPoolingGradOpKernel(
67  tensorflow::OpKernelConstruction* construction)
68  : OpKernel(construction) {
69  using namespace tensorflow;
70  using namespace open3d::ml::impl;
71  std::string pos_fn_str;
72  OP_REQUIRES_OK(construction,
73  construction->GetAttr("position_fn", &pos_fn_str));
74 
75  if (pos_fn_str == "average")
76  position_fn = AVERAGE;
77  else if (pos_fn_str == "nearest_neighbor")
78  position_fn = NEAREST_NEIGHBOR;
79  else
80  position_fn = CENTER;
81 
82  std::string feat_fn_str;
83  OP_REQUIRES_OK(construction,
84  construction->GetAttr("feature_fn", &feat_fn_str));
85 
86  if (feat_fn_str == "average")
87  feature_fn = AVERAGE;
88  else if (feat_fn_str == "nearest_neighbor")
89  feature_fn = NEAREST_NEIGHBOR;
90  else
91  feature_fn = MAX;
92  }
93 
94  void Compute(tensorflow::OpKernelContext* context) override {
95  using namespace tensorflow;
96  using namespace open3d::ml::impl;
97 
98  const Tensor& positions = context->input(0);
99  OP_REQUIRES(
100  context, positions.shape().dims() == 2,
101  errors::InvalidArgument("positions must be a rank 2 tensor"));
102 
103  const Tensor& features = context->input(1);
104  OP_REQUIRES(
105  context, features.shape().dims() == 2,
106  errors::InvalidArgument("features must be a rank 2 tensor"));
107 
108  const Tensor& voxel_size = context->input(2);
109  OP_REQUIRES(
110  context, TensorShapeUtils::IsScalar(voxel_size.shape()),
111  errors::InvalidArgument("voxel_size must be a scalar, but is ",
112  voxel_size.shape().DebugString()));
113 
114  const Tensor& pooled_positions = context->input(3);
115  OP_REQUIRES(context, pooled_positions.shape().dims() == 2,
116  errors::InvalidArgument(
117  "pooled_positions must be a rank 2 tensor"));
118 
119  const Tensor& pooled_features_gradient = context->input(4);
120  OP_REQUIRES(
121  context, pooled_features_gradient.shape().dims() == 2,
122  errors::InvalidArgument(
123  "pooled_features_gradient must be a rank 2 tensor"));
124 
125  Tensor* features_backprop = nullptr;
126  OP_REQUIRES_OK(context, context->allocate_output(0, features.shape(),
127  &features_backprop));
128 
129  Kernel(context, *features_backprop, positions, features,
130  pooled_positions, pooled_features_gradient, voxel_size);
131  }
132 
133  // Function with the device specific code
134  virtual void Kernel(tensorflow::OpKernelContext* context,
135  tensorflow::Tensor& features_backprop,
136  const tensorflow::Tensor& positions,
137  const tensorflow::Tensor& features,
138  const tensorflow::Tensor& pooled_positions,
139  const tensorflow::Tensor& pooled_features_gradient,
140  const tensorflow::Tensor& voxel_size) = 0;
141 
142 protected:
145 };
146 
147 } // namespace voxel_pooling_opkernel
Definition: VoxelPooling.h:40
Definition: VoxelPooling.h:40
Definition: VoxelPooling.h:40
Definition: ContinuousConv.h:35
AccumulationFn
Definition: VoxelPooling.h:40
Definition: VoxelPooling.h:40