Open3D (C++ API)  0.16.0
VoxelPoolingGradOpKernel.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 VoxelPoolingGradOpKernel : public tensorflow::OpKernel {
69public:
70 explicit VoxelPoolingGradOpKernel(
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
98 void Compute(tensorflow::OpKernelContext* context) override {
99 using namespace tensorflow;
100 using namespace open3d::ml::impl;
101
102 const Tensor& positions = context->input(0);
103 OP_REQUIRES(
104 context, positions.shape().dims() == 2,
105 errors::InvalidArgument("positions must be a rank 2 tensor"));
106
107 const Tensor& features = context->input(1);
108 OP_REQUIRES(
109 context, features.shape().dims() == 2,
110 errors::InvalidArgument("features must be a rank 2 tensor"));
111
112 const Tensor& voxel_size = context->input(2);
113 OP_REQUIRES(
114 context, TensorShapeUtils::IsScalar(voxel_size.shape()),
115 errors::InvalidArgument("voxel_size must be a scalar, but is ",
116 voxel_size.shape().DebugString()));
117
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"));
122
123 const Tensor& pooled_features_gradient = context->input(4);
124 OP_REQUIRES(
125 context, pooled_features_gradient.shape().dims() == 2,
126 errors::InvalidArgument(
127 "pooled_features_gradient must be a rank 2 tensor"));
128
129 Tensor* features_backprop = nullptr;
130 OP_REQUIRES_OK(context, context->allocate_output(0, features.shape(),
131 &features_backprop));
132
133 Kernel(context, *features_backprop, positions, features,
134 pooled_positions, pooled_features_gradient, voxel_size);
135 }
136
137 // Function with the device specific code
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;
145
146protected:
149};
150
151} // 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