Open3D (C++ API)
ContinuousConvTransposeOpKernel.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 ContinuousConvTransposeOpKernel : 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& out_importance = context->input(2);
83  OP_REQUIRES(context,
84  out_importance.shape().dim_size(0) == 0 ||
85  out_importance.shape().dim_size(0) ==
86  out_positions.shape().dim_size(0),
87  errors::InvalidArgument("length of out_importance must "
88  "match the number of output points "
89  "or must be 0"));
90 
91  const Tensor& extents = context->input(3);
92 
93  const Tensor& offset = context->input(4);
94  OP_REQUIRES(context, offset.shape().dims() == 1,
95  errors::InvalidArgument("offset must be a rank 1 tensor"));
96  OP_REQUIRES(context, offset.shape().dim_size(0) == 3,
97  errors::InvalidArgument("offset length must be 3"));
98 
99  const Tensor& inp_positions = context->input(5);
100  OP_REQUIRES(context,
101  inp_positions.shape().dim_size(0) <=
102  std::numeric_limits<TIndex>::max(),
103  errors::InvalidArgument("Too many input points"));
104 
105  const Tensor& inp_features = context->input(6);
106 
107  // not used in forward pass
108  // const Tensor& inp_neighbors_index = context->input(7);
109 
110  const Tensor& inp_neighbors_importance_sum = context->input(8);
111 
112  const Tensor& inp_neighbors_row_splits = context->input(9);
113 
114  const Tensor& neighbors_index = context->input(10);
115 
116  const Tensor& neighbors_importance = context->input(11);
117 
118  const Tensor& neighbors_row_splits = context->input(12);
119 
120  OP_REQUIRES(context, extents.shape().dims() == 2,
121  errors::InvalidArgument("extents must be a rank 2 tensor"));
122  OP_REQUIRES(context,
123  extents.shape().dim_size(0) ==
124  inp_positions.shape().dim_size(0) ||
125  extents.shape().dim_size(0) == 1,
126  errors::InvalidArgument("number of extents must match the "
127  "number of inp_positions or must "
128  "be 1"));
129  OP_REQUIRES(context,
130  extents.shape().dim_size(1) == 3 ||
131  extents.shape().dim_size(1) == 1,
132  errors::InvalidArgument(
133  "number of components for extents must be 3 or 1"));
134 
135  OP_REQUIRES(
136  context,
137  inp_positions.shape().dim_size(0) ==
138  inp_features.shape().dim_size(0),
139  errors::InvalidArgument("first dim of inp_positions does not "
140  "match the first dim of inp_features"));
141 
142  OP_REQUIRES(
143  context,
144  inp_neighbors_importance_sum.shape().dim_size(0) ==
145  inp_positions.shape().dim_size(0) ||
146  inp_neighbors_importance_sum.shape().dim_size(0) == 0,
147  errors::InvalidArgument(
148  "first dim of inp_neighbors_importance_sum does not "
149  "match the first dim of inp_positions",
150  inp_neighbors_importance_sum.shape().dim_size(0), " ",
151  inp_positions.shape().dim_size(0)));
152 
153  OP_REQUIRES(context,
154  out_positions.shape().dim_size(0) ==
155  out_importance.shape().dim_size(0) ||
156  out_importance.shape().dim_size(0) == 0,
157  errors::InvalidArgument("first dim of out_positions does "
158  "not match the first dim of "
159  "out_importance"));
160 
161  OP_REQUIRES(context,
162  neighbors_importance.shape().dim_size(0) ==
163  neighbors_index.shape().dim_size(0) ||
164  neighbors_importance.shape().dim_size(0) == 0,
165  errors::InvalidArgument("first dim of neighbors_importance "
166  "does not match the first dim of "
167  "neighbors_index"));
168 
169  OP_REQUIRES(
170  context,
171  filter.shape().dim_size(3) == inp_features.shape().dim_size(1),
172  errors::InvalidArgument("number of input channels in filter "
173  "and inp_features does not match"));
174 
175  TensorShape out_features_shape({out_positions.shape().dim_size(0),
176  filter.shape().dim_size(4)});
177  Tensor* out_features = nullptr;
178  OP_REQUIRES_OK(context, context->allocate_output(0, out_features_shape,
179  &out_features));
180 
181  std::vector<int> filter_dims({
182  int(filter.shape().dim_size(0)),
183  int(filter.shape().dim_size(1)),
184  int(filter.shape().dim_size(2)),
185  int(filter.shape().dim_size(3)),
186  int(filter.shape().dim_size(4)),
187  });
188 
189  bool individual_extents = extents.shape().dim_size(0) ==
190  out_positions.shape().dim_size(0) &&
191  extents.shape().dim_size(0) > 1;
192 
193  bool isotropic_extents = extents.shape().dim_size(1) == 1;
194 
195  bool point_importances = out_importance.shape().dim_size(0) != 0;
196 
197  bool has_neighbors_importances =
198  neighbors_importance.shape().dim_size(0) != 0;
199 
200  Kernel(context, filter, out_positions, out_importance, extents, offset,
201  inp_positions, inp_features, inp_neighbors_importance_sum,
202  inp_neighbors_row_splits, neighbors_index, neighbors_importance,
203  neighbors_row_splits, filter_dims, individual_extents,
204  isotropic_extents, point_importances, has_neighbors_importances,
205  *out_features);
206  }
207 
208  virtual void Kernel(tensorflow::OpKernelContext* context,
209  const tensorflow::Tensor& filter,
210  const tensorflow::Tensor& out_positions,
211  const tensorflow::Tensor& out_importance,
212  const tensorflow::Tensor& extents,
213  const tensorflow::Tensor& offset,
214  const tensorflow::Tensor& inp_positions,
215  const tensorflow::Tensor& inp_features,
216  const tensorflow::Tensor& inp_neighbors_importance_sum,
217  const tensorflow::Tensor& inp_neighbors_row_splits,
218  const tensorflow::Tensor& neighbors_index,
219  const tensorflow::Tensor& neighbors_importance,
220  const tensorflow::Tensor& neighbors_row_splits,
221  const std::vector<int>& filter_dims,
222  const bool individual_extents,
223  const bool isotropic_extents,
224  const bool point_importances,
225  const bool has_neighbors_importances,
226  tensorflow::Tensor& out_features) = 0;
227 
228 public:
230  bool normalize;
234 };
Definition: VoxelPooling.h:40
int offset
Definition: FilePCD.cpp:64
ContinuousConvTransposeOpKernel(tensorflow::OpKernelConstruction *construction)
Definition: ContinuousConvTransposeOpKernel.h:33
Definition: ContinuousConvTransposeOpKernel.h:31
bool normalize
Definition: ContinuousConvTransposeOpKernel.h:230
InterpolationMode
Definition: ContinuousConvTypes.h:37
open3d::ml::impl::InterpolationMode interpolation
Definition: ContinuousConvTransposeOpKernel.h:231
Definition: ContinuousConv.h:35
bool align_corners
Definition: ContinuousConvTransposeOpKernel.h:229
void Compute(tensorflow::OpKernelContext *context) override
Definition: ContinuousConvTransposeOpKernel.h:70
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
open3d::ml::impl::CoordinateMapping coordinate_mapping
Definition: ContinuousConvTransposeOpKernel.h:232
int max_temp_mem_MB
Definition: ContinuousConvTransposeOpKernel.h:233
virtual void Kernel(tensorflow::OpKernelContext *context, const tensorflow::Tensor &filter, const tensorflow::Tensor &out_positions, const tensorflow::Tensor &out_importance, const tensorflow::Tensor &extents, const tensorflow::Tensor &offset, const tensorflow::Tensor &inp_positions, const tensorflow::Tensor &inp_features, const tensorflow::Tensor &inp_neighbors_importance_sum, const tensorflow::Tensor &inp_neighbors_row_splits, 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