Open3D (C++ API)  0.11.0
VoxelPoolingOpKernel.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 VoxelPoolingOpKernel : public tensorflow::OpKernel {
65 public:
66  explicit VoxelPoolingOpKernel(
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  OP_REQUIRES_OK(construction, construction->GetAttr("debug", &debug));
94  }
95 
96  void Compute(tensorflow::OpKernelContext* context) override {
97  using namespace tensorflow;
98  using namespace open3d::ml::impl;
99  const Tensor& positions = context->input(0);
100  OP_REQUIRES(
101  context, positions.shape().dims() == 2,
102  errors::InvalidArgument("positions must be a rank 2 tensor"));
103 
104  const Tensor& features = context->input(1);
105  OP_REQUIRES(
106  context, features.shape().dims() == 2,
107  errors::InvalidArgument("features must be a rank 2 tensor"));
108 
109  const Tensor& voxel_size = context->input(2);
110  OP_REQUIRES(
111  context, TensorShapeUtils::IsScalar(voxel_size.shape()),
112  errors::InvalidArgument("voxel_size must be a scalar, but is ",
113  voxel_size.shape().DebugString()));
114 
115  Kernel(context, positions, features, voxel_size);
116  }
117 
118  // Function with the device specific code
119  virtual void Kernel(tensorflow::OpKernelContext* context,
120  const tensorflow::Tensor& positions,
121  const tensorflow::Tensor& features,
122  const tensorflow::Tensor& voxel_size) = 0;
123 
124 protected:
127  bool debug;
128 };
129 
130 } // 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