Open3D (C++ API)  0.11.0
BuildSpatialHashTableOpKernel.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 
25 #include "../TensorFlowHelper.h"
27 #include "tensorflow/core/framework/op.h"
28 #include "tensorflow/core/framework/op_kernel.h"
29 #include "tensorflow/core/lib/core/errors.h"
30 
31 class BuildSpatialHashTableOpKernel : public tensorflow::OpKernel {
32 public:
34  tensorflow::OpKernelConstruction* construction)
35  : OpKernel(construction) {
36  using namespace tensorflow;
37  using namespace open3d::ml::impl;
38 
39  OP_REQUIRES_OK(construction,
40  construction->GetAttr("max_hash_table_size",
42  }
43 
44  void Compute(tensorflow::OpKernelContext* context) override {
45  using namespace tensorflow;
46  using namespace open3d::ml::op_util;
47 
48  const Tensor& points = context->input(0);
49  const Tensor& radius = context->input(1);
50  OP_REQUIRES(context, TensorShapeUtils::IsScalar(radius.shape()),
51  errors::InvalidArgument("radius must be scalar, got shape ",
52  radius.shape().DebugString()));
53 
54  const Tensor& points_row_splits = context->input(2);
55 
56  const Tensor& hash_table_size_factor_tensor = context->input(3);
57  OP_REQUIRES(
58  context,
59  TensorShapeUtils::IsScalar(
60  hash_table_size_factor_tensor.shape()),
61  errors::InvalidArgument(
62  "hash_table_size_factor must be scalar, got shape ",
63  hash_table_size_factor_tensor.shape().DebugString()));
64  const double hash_table_size_factor =
65  hash_table_size_factor_tensor.scalar<double>()();
66 
67  Dim num_points("num_points");
68  Dim batch_size("batch_size");
69  CHECK_SHAPE(context, points, num_points, 3);
70  CHECK_SHAPE(context, points_row_splits, batch_size + 1);
71 
72  std::vector<uint32_t> hash_table_splits(batch_size.value() + 1, 0);
73  for (int i = 0; i < batch_size.value(); ++i) {
74  int64_t num_points_i = points_row_splits.flat<int64>()(i + 1) -
75  points_row_splits.flat<int64>()(i);
76 
77  int64_t hash_table_size = std::min<int64_t>(
78  std::max<int64_t>(hash_table_size_factor * num_points_i, 1),
80  hash_table_splits[i + 1] = hash_table_splits[i] + hash_table_size;
81  }
82 
83  Tensor* hash_table_index = 0;
84  TensorShape hash_table_index_shape({num_points.value()});
85  OP_REQUIRES_OK(context,
86  context->allocate_output(0, hash_table_index_shape,
87  &hash_table_index));
88 
89  Tensor* hash_table_cell_splits = 0;
90  TensorShape hash_table_cell_splits_shape(
91  {hash_table_splits.back() + 1});
92  OP_REQUIRES_OK(context,
93  context->allocate_output(1, hash_table_cell_splits_shape,
94  &hash_table_cell_splits));
95 
96  Tensor* out_hash_table_splits = 0;
97  TensorShape out_hash_table_splits_shape({batch_size.value() + 1});
98  OP_REQUIRES_OK(context,
99  context->allocate_output(2, out_hash_table_splits_shape,
100  &out_hash_table_splits));
101  for (size_t i = 0; i < hash_table_splits.size(); ++i) {
102  out_hash_table_splits->flat<uint32_t>()(i) = hash_table_splits[i];
103  }
104 
105  Kernel(context, points, radius, points_row_splits, hash_table_splits,
106  *hash_table_index, *hash_table_cell_splits);
107  }
108 
109  virtual void Kernel(tensorflow::OpKernelContext* context,
110  const tensorflow::Tensor& points,
111  const tensorflow::Tensor& radius,
112  const tensorflow::Tensor& points_row_splits,
113  const std::vector<uint32_t>& hash_table_splits,
114  tensorflow::Tensor& hash_table_index,
115  tensorflow::Tensor& hash_table_cell_splits) = 0;
116 
117 protected:
119 };
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 k4a_image_t image_handle uint8_t image_handle image_handle image_handle image_handle uint32_t
Definition: K4aPlugin.cpp:554
#define CHECK_SHAPE(tensor,...)
Definition: TorchHelper.h:204
Definition: BuildSpatialHashTableOpKernel.h:31
Class for dimensions for which the value should be inferred.
Definition: ShapeChecking.h:69
Definition: ContinuousConv.h:35
int64_t & value()
Definition: ShapeChecking.h:89
void Compute(tensorflow::OpKernelContext *context) override
Definition: BuildSpatialHashTableOpKernel.h:44
virtual void Kernel(tensorflow::OpKernelContext *context, const tensorflow::Tensor &points, const tensorflow::Tensor &radius, const tensorflow::Tensor &points_row_splits, const std::vector< uint32_t > &hash_table_splits, tensorflow::Tensor &hash_table_index, tensorflow::Tensor &hash_table_cell_splits)=0
int points
Definition: FilePCD.cpp:73
BuildSpatialHashTableOpKernel(tensorflow::OpKernelConstruction *construction)
Definition: BuildSpatialHashTableOpKernel.h:33
int max_hash_table_size
Definition: BuildSpatialHashTableOpKernel.h:118
Definition: ShapeChecking.h:35