Open3D (C++ API)  0.18.0
ContinuousConvTransposeBackpropFilterOpKernel.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2023 www.open3d.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
11 #include "tensorflow/core/framework/op.h"
12 #include "tensorflow/core/framework/op_kernel.h"
13 #include "tensorflow/core/lib/core/errors.h"
14 
15 template <class TIndex>
17  : public tensorflow::OpKernel {
18 public:
20  tensorflow::OpKernelConstruction* construction)
21  : OpKernel(construction) {
22  using namespace tensorflow;
23  using namespace open3d::ml::impl;
24  OP_REQUIRES_OK(construction,
25  construction->GetAttr("align_corners", &align_corners));
26  OP_REQUIRES_OK(construction,
27  construction->GetAttr("normalize", &normalize));
28 
29  std::string interpolation_str;
30  OP_REQUIRES_OK(construction, construction->GetAttr("interpolation",
31  &interpolation_str));
32 
33  if (interpolation_str == "linear")
34  interpolation = InterpolationMode::LINEAR;
35  else if (interpolation_str == "linear_border")
36  interpolation = InterpolationMode::LINEAR_BORDER;
37  else
39 
40  std::string mapping_str;
41  OP_REQUIRES_OK(construction, construction->GetAttr("coordinate_mapping",
42  &mapping_str));
43 
44  if (mapping_str == "ball_to_cube_radial")
45  coordinate_mapping = CoordinateMapping::BALL_TO_CUBE_RADIAL;
46  else if (mapping_str == "ball_to_cube_volume_preserving")
48  CoordinateMapping::BALL_TO_CUBE_VOLUME_PRESERVING;
49  else
50  coordinate_mapping = CoordinateMapping::IDENTITY;
51 
52  OP_REQUIRES_OK(construction, construction->GetAttr("max_temp_mem_MB",
53  &max_temp_mem_MB));
54  }
55 
56  void Compute(tensorflow::OpKernelContext* context) override {
57  using namespace tensorflow;
58  static_assert(sizeof(int64) == sizeof(int64_t),
59  "int64 type is not compatible");
60  const Tensor& filter = context->input(0);
61 
62  const Tensor& out_positions = context->input(1);
63  OP_REQUIRES(context,
64  out_positions.shape().dim_size(0) <=
65  std::numeric_limits<TIndex>::max(),
66  errors::InvalidArgument("Too many output points"));
67 
68  const Tensor& out_importance = context->input(2);
69  OP_REQUIRES(context,
70  out_importance.shape().dim_size(0) == 0 ||
71  out_importance.shape().dim_size(0) ==
72  out_positions.shape().dim_size(0),
73  errors::InvalidArgument("length of out_importance must "
74  "match the number of output points "
75  "or must be 0"));
76 
77  const Tensor& extents = context->input(3);
78 
79  const Tensor& offset = context->input(4);
80  OP_REQUIRES(context, offset.shape().dims() == 1,
81  errors::InvalidArgument("offset must be a rank 1 tensor"));
82  OP_REQUIRES(context, offset.shape().dim_size(0) == 3,
83  errors::InvalidArgument("offset length must be 3"));
84 
85  const Tensor& inp_positions = context->input(5);
86  OP_REQUIRES(context,
87  inp_positions.shape().dim_size(0) <=
88  std::numeric_limits<TIndex>::max(),
89  errors::InvalidArgument("Too many input points"));
90 
91  const Tensor& inp_features = context->input(6);
92 
93  const Tensor& inp_neighbors_importance_sum = context->input(7);
94 
95  const Tensor& inp_neighbors_row_splits = context->input(8);
96 
97  const Tensor& neighbors_index = context->input(9);
98 
99  const Tensor& neighbors_importance = context->input(10);
100 
101  const Tensor& neighbors_row_splits = context->input(11);
102 
103  const Tensor& out_features_gradient = context->input(12);
104 
105  OP_REQUIRES(context, extents.shape().dims() == 2,
106  errors::InvalidArgument("extents must be a rank 2 tensor"));
107  OP_REQUIRES(context,
108  extents.shape().dim_size(0) ==
109  inp_positions.shape().dim_size(0) ||
110  extents.shape().dim_size(0) == 1,
111  errors::InvalidArgument("number of extents must match the "
112  "number of inp_positions or must "
113  "be 1"));
114  OP_REQUIRES(context,
115  extents.shape().dim_size(1) == 3 ||
116  extents.shape().dim_size(1) == 1,
117  errors::InvalidArgument(
118  "number of components for extents must be 3 or 1"));
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(
128  context,
129  inp_neighbors_importance_sum.shape().dim_size(0) ==
130  inp_positions.shape().dim_size(0) ||
131  inp_neighbors_importance_sum.shape().dim_size(0) == 0,
132  errors::InvalidArgument(
133  "first dim of inp_neighbors_importance_sum does not "
134  "match the first dim of inp_positions",
135  inp_neighbors_importance_sum.shape().dim_size(0), " ",
136  inp_positions.shape().dim_size(0)));
137 
138  OP_REQUIRES(context,
139  out_positions.shape().dim_size(0) ==
140  out_importance.shape().dim_size(0) ||
141  out_importance.shape().dim_size(0) == 0,
142  errors::InvalidArgument("first dim of out_positions does "
143  "not match the first dim of "
144  "out_importance"));
145 
146  OP_REQUIRES(context,
147  neighbors_importance.shape().dim_size(0) ==
148  neighbors_index.shape().dim_size(0) ||
149  neighbors_importance.shape().dim_size(0) == 0,
150  errors::InvalidArgument("first dim of neighbors_importance "
151  "does not match the first dim of "
152  "neighbors_index"));
153 
154  OP_REQUIRES(
155  context,
156  filter.shape().dim_size(3) == inp_features.shape().dim_size(1),
157  errors::InvalidArgument("number of input channels in filter "
158  "and inp_features does not match"));
159 
160  OP_REQUIRES(context,
161  out_features_gradient.shape().dim_size(0) ==
162  out_positions.shape().dim_size(0),
163  errors::InvalidArgument("first dim of out_positions, does "
164  "not match the first dim of "
165  "out_features_gradient"));
166 
167  TensorShape filter_backprop_shape(filter.shape());
168  Tensor* filter_backprop = nullptr;
169  OP_REQUIRES_OK(context,
170  context->allocate_output(0, filter_backprop_shape,
171  &filter_backprop));
172 
173  std::vector<int> filter_dims({
174  int(filter.shape().dim_size(0)),
175  int(filter.shape().dim_size(1)),
176  int(filter.shape().dim_size(2)),
177  int(filter.shape().dim_size(3)),
178  int(filter.shape().dim_size(4)),
179  });
180 
181  bool individual_extents = extents.shape().dim_size(0) ==
182  out_positions.shape().dim_size(0) &&
183  extents.shape().dim_size(0) > 1;
184 
185  bool isotropic_extents = extents.shape().dim_size(1) == 1;
186 
187  bool point_importances = out_importance.shape().dim_size(0) != 0;
188 
189  bool has_neighbors_importances =
190  neighbors_importance.shape().dim_size(0) != 0;
191 
192  Kernel(context, filter, out_positions, out_importance, extents, offset,
193  inp_positions, inp_features, inp_neighbors_importance_sum,
194  inp_neighbors_row_splits, neighbors_index, neighbors_importance,
195  neighbors_row_splits, out_features_gradient, filter_dims,
196  individual_extents, isotropic_extents, point_importances,
197  has_neighbors_importances, *filter_backprop);
198  }
199 
200  virtual void Kernel(tensorflow::OpKernelContext* context,
201  const tensorflow::Tensor& filter,
202  const tensorflow::Tensor& out_positions,
203  const tensorflow::Tensor& out_importance,
204  const tensorflow::Tensor& extents,
205  const tensorflow::Tensor& offset,
206  const tensorflow::Tensor& inp_positions,
207  const tensorflow::Tensor& inp_features,
208  const tensorflow::Tensor& inp_neighbors_importance_sum,
209  const tensorflow::Tensor& inp_neighbors_row_splits,
210  const tensorflow::Tensor& neighbors_index,
211  const tensorflow::Tensor& neighbors_importance,
212  const tensorflow::Tensor& neighbors_row_splits,
213  const tensorflow::Tensor& out_features_gradient,
214  const std::vector<int>& filter_dims,
215  const bool individual_extents,
216  const bool isotropic_extents,
217  const bool point_importances,
218  const bool has_neighbors_importances,
219  tensorflow::Tensor& filter_backprop) = 0;
220 
221 public:
223  bool normalize;
227 };
ImGuiContext * context
Definition: Window.cpp:76
Definition: ContinuousConvTransposeBackpropFilterOpKernel.h:17
ContinuousConvTransposeBackpropFilterOpKernel(tensorflow::OpKernelConstruction *construction)
Definition: ContinuousConvTransposeBackpropFilterOpKernel.h:19
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 tensorflow::Tensor &out_features_gradient, 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 &filter_backprop)=0
bool align_corners
Definition: ContinuousConvTransposeBackpropFilterOpKernel.h:222
void Compute(tensorflow::OpKernelContext *context) override
Definition: ContinuousConvTransposeBackpropFilterOpKernel.h:56
open3d::ml::impl::InterpolationMode interpolation
Definition: ContinuousConvTransposeBackpropFilterOpKernel.h:224
open3d::ml::impl::CoordinateMapping coordinate_mapping
Definition: ContinuousConvTransposeBackpropFilterOpKernel.h:225
bool normalize
Definition: ContinuousConvTransposeBackpropFilterOpKernel.h:223
int max_temp_mem_MB
Definition: ContinuousConvTransposeBackpropFilterOpKernel.h:226
int offset
Definition: FilePCD.cpp: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:474
Definition: ContinuousConv.h:16
InterpolationMode
Definition: ContinuousConvTypes.h:18
@ NEAREST_NEIGHBOR
Definition: VoxelPooling.h:21
CoordinateMapping
Definition: ContinuousConvTypes.h:26