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