Open3D (C++ API)
ContinuousConvOpKernel.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 
30 template <class TIndex>
31 class ContinuousConvOpKernel : public tensorflow::OpKernel {
32 public:
34  tensorflow::OpKernelConstruction* construction)
35  : OpKernel(construction) {
36  using namespace tensorflow;
37  using namespace open3d::ml::impl;
38  OP_REQUIRES_OK(construction,
39  construction->GetAttr("align_corners", &align_corners));
40  OP_REQUIRES_OK(construction,
41  construction->GetAttr("normalize", &normalize));
42 
43  std::string interpolation_str;
44  OP_REQUIRES_OK(construction, construction->GetAttr("interpolation",
45  &interpolation_str));
46 
47  if (interpolation_str == "linear")
48  interpolation = InterpolationMode::LINEAR;
49  else if (interpolation_str == "linear_border")
50  interpolation = InterpolationMode::LINEAR_BORDER;
51  else
53 
54  std::string mapping_str;
55  OP_REQUIRES_OK(construction, construction->GetAttr("coordinate_mapping",
56  &mapping_str));
57 
58  if (mapping_str == "ball_to_cube_radial")
59  coordinate_mapping = CoordinateMapping::BALL_TO_CUBE_RADIAL;
60  else if (mapping_str == "ball_to_cube_volume_preserving")
62  CoordinateMapping::BALL_TO_CUBE_VOLUME_PRESERVING;
63  else
64  coordinate_mapping = CoordinateMapping::IDENTITY;
65 
66  OP_REQUIRES_OK(construction, construction->GetAttr("max_temp_mem_MB",
67  &max_temp_mem_MB));
68  }
69 
70  void Compute(tensorflow::OpKernelContext* context) override {
71  using namespace tensorflow;
72  static_assert(sizeof(int64) == sizeof(int64_t),
73  "int64 type is not compatible");
74  const Tensor& filter = context->input(0);
75 
76  const Tensor& out_positions = context->input(1);
77  OP_REQUIRES(context,
78  out_positions.shape().dim_size(0) <=
79  std::numeric_limits<TIndex>::max(),
80  errors::InvalidArgument("Too many output points"));
81 
82  const Tensor& extents = context->input(2);
83  OP_REQUIRES(context, extents.shape().dims() == 2,
84  errors::InvalidArgument("extents must be a rank 2 tensor"));
85  OP_REQUIRES(context,
86  extents.shape().dim_size(0) ==
87  out_positions.shape().dim_size(0) ||
88  extents.shape().dim_size(0) == 1,
89  errors::InvalidArgument("number of extents must match the "
90  "number of out_positions or must "
91  "be 1"));
92  OP_REQUIRES(context,
93  extents.shape().dim_size(1) == 3 ||
94  extents.shape().dim_size(1) == 1,
95  errors::InvalidArgument(
96  "number of components for extents must be 3 or 1"));
97 
98  const Tensor& offset = context->input(3);
99  OP_REQUIRES(context, offset.shape().dims() == 1,
100  errors::InvalidArgument("offset must be a rank 1 tensor"));
101  OP_REQUIRES(context, offset.shape().dim_size(0) == 3,
102  errors::InvalidArgument("offset length must be 3"));
103 
104  const Tensor& inp_positions = context->input(4);
105  OP_REQUIRES(context,
106  inp_positions.shape().dim_size(0) <=
107  std::numeric_limits<TIndex>::max(),
108  errors::InvalidArgument("Too many input points"));
109 
110  const Tensor& inp_features = context->input(5);
111 
112  const Tensor& inp_importance = context->input(6);
113 
114  const Tensor& neighbors_index = context->input(7);
115 
116  const Tensor& neighbors_importance = context->input(8);
117 
118  const Tensor& neighbors_row_splits = context->input(9);
119 
120  OP_REQUIRES(
121  context,
122  inp_positions.shape().dim_size(0) ==
123  inp_features.shape().dim_size(0),
124  errors::InvalidArgument("first dim of inp_positions does not "
125  "match the first dim of inp_features"));
126 
127  OP_REQUIRES(context,
128  inp_positions.shape().dim_size(0) ==
129  inp_importance.shape().dim_size(0) ||
130  inp_importance.shape().dim_size(0) == 0,
131  errors::InvalidArgument("first dim of inp_positions does "
132  "not match the first dim of "
133  "inp_importance"));
134 
135  OP_REQUIRES(context,
136  neighbors_importance.shape().dim_size(0) ==
137  neighbors_index.shape().dim_size(0) ||
138  neighbors_importance.shape().dim_size(0) == 0,
139  errors::InvalidArgument("first dim of neighbors_importance "
140  "does not match the first dim of "
141  "neighbors_index"));
142 
143  OP_REQUIRES(
144  context,
145  filter.shape().dim_size(3) == inp_features.shape().dim_size(1),
146  errors::InvalidArgument("number of input channels in filter "
147  "and inp_features does not match"));
148 
149  TensorShape out_features_shape({out_positions.shape().dim_size(0),
150  filter.shape().dim_size(4)});
151  Tensor* out_features = nullptr;
152  OP_REQUIRES_OK(context, context->allocate_output(0, out_features_shape,
153  &out_features));
154 
155  std::vector<int> filter_dims({
156  int(filter.shape().dim_size(0)),
157  int(filter.shape().dim_size(1)),
158  int(filter.shape().dim_size(2)),
159  int(filter.shape().dim_size(3)),
160  int(filter.shape().dim_size(4)),
161  });
162 
163  bool individual_extents = extents.shape().dim_size(0) ==
164  out_positions.shape().dim_size(0) &&
165  extents.shape().dim_size(0) > 1;
166 
167  bool isotropic_extents = extents.shape().dim_size(1) == 1;
168 
169  bool point_importances = inp_importance.shape().dim_size(0) != 0;
170 
171  bool has_neighbors_importances =
172  neighbors_importance.shape().dim_size(0) != 0;
173 
174  Kernel(context, filter, out_positions, extents, offset, inp_positions,
175  inp_features, inp_importance, neighbors_index,
176  neighbors_importance, neighbors_row_splits, filter_dims,
177  individual_extents, isotropic_extents, point_importances,
178  has_neighbors_importances, *out_features);
179  }
180 
181  virtual void Kernel(tensorflow::OpKernelContext* context,
182  const tensorflow::Tensor& filter,
183  const tensorflow::Tensor& out_positions,
184  const tensorflow::Tensor& extents,
185  const tensorflow::Tensor& offset,
186  const tensorflow::Tensor& inp_positions,
187  const tensorflow::Tensor& inp_features,
188  const tensorflow::Tensor& inp_importance,
189  const tensorflow::Tensor& neighbors_index,
190  const tensorflow::Tensor& neighbors_importance,
191  const tensorflow::Tensor& neighbors_row_splits,
192  const std::vector<int>& filter_dims,
193  const bool individual_extents,
194  const bool isotropic_extents,
195  const bool point_importances,
196  const bool has_neighbors_importances,
197  tensorflow::Tensor& out_features) = 0;
198 
199 public:
201  bool normalize;
205 };
Definition: VoxelPooling.h:40
void Compute(tensorflow::OpKernelContext *context) override
Definition: ContinuousConvOpKernel.h:70
int max_temp_mem_MB
Definition: ContinuousConvOpKernel.h:204
Definition: ContinuousConvOpKernel.h:31
virtual void Kernel(tensorflow::OpKernelContext *context, const tensorflow::Tensor &filter, const tensorflow::Tensor &out_positions, const tensorflow::Tensor &extents, const tensorflow::Tensor &offset, const tensorflow::Tensor &inp_positions, const tensorflow::Tensor &inp_features, const tensorflow::Tensor &inp_importance, const tensorflow::Tensor &neighbors_index, const tensorflow::Tensor &neighbors_importance, const tensorflow::Tensor &neighbors_row_splits, const std::vector< int > &filter_dims, const bool individual_extents, const bool isotropic_extents, const bool point_importances, const bool has_neighbors_importances, tensorflow::Tensor &out_features)=0
int offset
Definition: FilePCD.cpp:64
InterpolationMode
Definition: ContinuousConvTypes.h:37
Definition: ContinuousConv.h:35
CoordinateMapping
Definition: ContinuousConvTypes.h:45
const char const char value recording_handle imu_sample recording_handle uint8_t size_t data_size k4a_record_configuration_t config target_format k4a_capture_t capture_handle k4a_imu_sample_t imu_sample playback_handle k4a_logging_message_cb_t void min_level device_handle k4a_imu_sample_t timeout_in_ms capture_handle capture_handle capture_handle image_handle temperature_c int
Definition: K4aPlugin.cpp:476
bool normalize
Definition: ContinuousConvOpKernel.h:201
bool align_corners
Definition: ContinuousConvOpKernel.h:200
ContinuousConvOpKernel(tensorflow::OpKernelConstruction *construction)
Definition: ContinuousConvOpKernel.h:33
open3d::ml::impl::InterpolationMode interpolation
Definition: ContinuousConvOpKernel.h:202
open3d::ml::impl::CoordinateMapping coordinate_mapping
Definition: ContinuousConvOpKernel.h:203