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