Open3D (C++ API)  0.19.0
BuildSpatialHashTableOpKernel.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.open3d.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <cstdint>
11 
12 #include "../TensorFlowHelper.h"
14 #include "tensorflow/core/framework/op.h"
15 #include "tensorflow/core/framework/op_kernel.h"
16 #include "tensorflow/core/lib/core/errors.h"
17 
18 class BuildSpatialHashTableOpKernel : public tensorflow::OpKernel {
19 public:
21  tensorflow::OpKernelConstruction* construction)
22  : OpKernel(construction) {
23  using namespace tensorflow;
24 
25  OP_REQUIRES_OK(construction,
26  construction->GetAttr("max_hash_table_size",
28  }
29 
30  void Compute(tensorflow::OpKernelContext* context) override {
31  using namespace tensorflow;
32  using namespace open3d::ml::op_util;
33 
34  const Tensor& points = context->input(0);
35  const Tensor& radius = context->input(1);
36  OP_REQUIRES(context, TensorShapeUtils::IsScalar(radius.shape()),
37  absl::InvalidArgumentError(
38  std::string("radius must be scalar, got shape ") +
39  radius.shape().DebugString()));
40 
41  const Tensor& points_row_splits = context->input(2);
42 
43  const Tensor& hash_table_size_factor_tensor = context->input(3);
44  OP_REQUIRES(
45  context,
46  TensorShapeUtils::IsScalar(
47  hash_table_size_factor_tensor.shape()),
48  absl::InvalidArgumentError(
49  std::string("hash_table_size_factor must be scalar, "
50  "got shape ") +
51  hash_table_size_factor_tensor.shape().DebugString()));
52  const double hash_table_size_factor =
53  hash_table_size_factor_tensor.scalar<double>()();
54 
55  Dim num_points("num_points");
56  Dim batch_size("batch_size");
57  CHECK_SHAPE(context, points, num_points, 3);
58  CHECK_SHAPE(context, points_row_splits, batch_size + 1);
59 
60  std::vector<uint32_t> hash_table_splits(batch_size.value() + 1, 0);
61  for (int i = 0; i < batch_size.value(); ++i) {
62  int64_t num_points_i = points_row_splits.flat<int64_t>()(i + 1) -
63  points_row_splits.flat<int64_t>()(i);
64 
65  int64_t hash_table_size = std::min<int64_t>(
66  std::max<int64_t>(hash_table_size_factor * num_points_i, 1),
68  hash_table_splits[i + 1] = hash_table_splits[i] + hash_table_size;
69  }
70 
71  Tensor* hash_table_index = 0;
72  TensorShape hash_table_index_shape({num_points.value()});
73  OP_REQUIRES_OK(context,
74  context->allocate_output(0, hash_table_index_shape,
75  &hash_table_index));
76 
77  Tensor* hash_table_cell_splits = 0;
78  TensorShape hash_table_cell_splits_shape(
79  {hash_table_splits.back() + 1});
80  OP_REQUIRES_OK(context,
81  context->allocate_output(1, hash_table_cell_splits_shape,
82  &hash_table_cell_splits));
83 
84  Tensor* out_hash_table_splits = 0;
85  TensorShape out_hash_table_splits_shape({batch_size.value() + 1});
86  OP_REQUIRES_OK(context,
87  context->allocate_output(2, out_hash_table_splits_shape,
88  &out_hash_table_splits));
89  for (size_t i = 0; i < hash_table_splits.size(); ++i) {
90  out_hash_table_splits->flat<uint32_t>()(i) = hash_table_splits[i];
91  }
92 
93  Kernel(context, points, radius, points_row_splits, hash_table_splits,
94  *hash_table_index, *hash_table_cell_splits);
95  }
96 
97  virtual void Kernel(tensorflow::OpKernelContext* context,
98  const tensorflow::Tensor& points,
99  const tensorflow::Tensor& radius,
100  const tensorflow::Tensor& points_row_splits,
101  const std::vector<uint32_t>& hash_table_splits,
102  tensorflow::Tensor& hash_table_index,
103  tensorflow::Tensor& hash_table_cell_splits) = 0;
104 
105 protected:
107 };
#define CHECK_SHAPE(tensor,...)
Definition: TorchHelper.h:190
ImGuiContext * context
Definition: Window.cpp:76
Definition: BuildSpatialHashTableOpKernel.h:18
int max_hash_table_size
Definition: BuildSpatialHashTableOpKernel.h:106
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
void Compute(tensorflow::OpKernelContext *context) override
Definition: BuildSpatialHashTableOpKernel.h:30
BuildSpatialHashTableOpKernel(tensorflow::OpKernelConstruction *construction)
Definition: BuildSpatialHashTableOpKernel.h:20
Class for dimensions for which the value should be inferred.
Definition: ShapeChecking.h:50
int64_t & value()
Definition: ShapeChecking.h:70
int points
Definition: FilePCD.cpp:54
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:548
Definition: ShapeChecking.h:16