Open3D (C++ API)  0.15.1
BuildSpatialHashTableOpKernel.h
Go to the documentation of this file.
1// ----------------------------------------------------------------------------
2// - Open3D: www.open3d.org -
3// ----------------------------------------------------------------------------
4// The MIT License (MIT)
5//
6// Copyright (c) 2018-2021 www.open3d.org
7//
8// Permission is hereby granted, free of charge, to any person obtaining a copy
9// of this software and associated documentation files (the "Software"), to deal
10// in the Software without restriction, including without limitation the rights
11// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12// copies of the Software, and to permit persons to whom the Software is
13// furnished to do so, subject to the following conditions:
14//
15// The above copyright notice and this permission notice shall be included in
16// all copies or substantial portions of the Software.
17//
18// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24// IN THE SOFTWARE.
25// ----------------------------------------------------------------------------
26
27#pragma once
28
29#include "../TensorFlowHelper.h"
31#include "tensorflow/core/framework/op.h"
32#include "tensorflow/core/framework/op_kernel.h"
33#include "tensorflow/core/lib/core/errors.h"
34
35class BuildSpatialHashTableOpKernel : public tensorflow::OpKernel {
36public:
38 tensorflow::OpKernelConstruction* construction)
39 : OpKernel(construction) {
40 using namespace tensorflow;
41
42 OP_REQUIRES_OK(construction,
43 construction->GetAttr("max_hash_table_size",
45 }
46
47 void Compute(tensorflow::OpKernelContext* context) override {
48 using namespace tensorflow;
49 using namespace open3d::ml::op_util;
50
51 const Tensor& points = context->input(0);
52 const Tensor& radius = context->input(1);
53 OP_REQUIRES(context, TensorShapeUtils::IsScalar(radius.shape()),
54 errors::InvalidArgument("radius must be scalar, got shape ",
55 radius.shape().DebugString()));
56
57 const Tensor& points_row_splits = context->input(2);
58
59 const Tensor& hash_table_size_factor_tensor = context->input(3);
60 OP_REQUIRES(
61 context,
62 TensorShapeUtils::IsScalar(
63 hash_table_size_factor_tensor.shape()),
64 errors::InvalidArgument(
65 "hash_table_size_factor must be scalar, got shape ",
66 hash_table_size_factor_tensor.shape().DebugString()));
67 const double hash_table_size_factor =
68 hash_table_size_factor_tensor.scalar<double>()();
69
70 Dim num_points("num_points");
71 Dim batch_size("batch_size");
72 CHECK_SHAPE(context, points, num_points, 3);
73 CHECK_SHAPE(context, points_row_splits, batch_size + 1);
74
75 std::vector<uint32_t> hash_table_splits(batch_size.value() + 1, 0);
76 for (int i = 0; i < batch_size.value(); ++i) {
77 int64_t num_points_i = points_row_splits.flat<int64>()(i + 1) -
78 points_row_splits.flat<int64>()(i);
79
80 int64_t hash_table_size = std::min<int64_t>(
81 std::max<int64_t>(hash_table_size_factor * num_points_i, 1),
83 hash_table_splits[i + 1] = hash_table_splits[i] + hash_table_size;
84 }
85
86 Tensor* hash_table_index = 0;
87 TensorShape hash_table_index_shape({num_points.value()});
88 OP_REQUIRES_OK(context,
89 context->allocate_output(0, hash_table_index_shape,
90 &hash_table_index));
91
92 Tensor* hash_table_cell_splits = 0;
93 TensorShape hash_table_cell_splits_shape(
94 {hash_table_splits.back() + 1});
95 OP_REQUIRES_OK(context,
96 context->allocate_output(1, hash_table_cell_splits_shape,
97 &hash_table_cell_splits));
98
99 Tensor* out_hash_table_splits = 0;
100 TensorShape out_hash_table_splits_shape({batch_size.value() + 1});
101 OP_REQUIRES_OK(context,
102 context->allocate_output(2, out_hash_table_splits_shape,
103 &out_hash_table_splits));
104 for (size_t i = 0; i < hash_table_splits.size(); ++i) {
105 out_hash_table_splits->flat<uint32_t>()(i) = hash_table_splits[i];
106 }
107
108 Kernel(context, points, radius, points_row_splits, hash_table_splits,
109 *hash_table_index, *hash_table_cell_splits);
110 }
111
112 virtual void Kernel(tensorflow::OpKernelContext* context,
113 const tensorflow::Tensor& points,
114 const tensorflow::Tensor& radius,
115 const tensorflow::Tensor& points_row_splits,
116 const std::vector<uint32_t>& hash_table_splits,
117 tensorflow::Tensor& hash_table_index,
118 tensorflow::Tensor& hash_table_cell_splits) = 0;
119
120protected:
122};
#define CHECK_SHAPE(tensor,...)
Definition: TorchHelper.h:205
ImGuiContext * context
Definition: Window.cpp:95
Definition: BuildSpatialHashTableOpKernel.h:35
int max_hash_table_size
Definition: BuildSpatialHashTableOpKernel.h:121
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:47
BuildSpatialHashTableOpKernel(tensorflow::OpKernelConstruction *construction)
Definition: BuildSpatialHashTableOpKernel.h:37
Class for dimensions for which the value should be inferred.
Definition: ShapeChecking.h:69
int64_t & value()
Definition: ShapeChecking.h:89
int points
Definition: FilePCD.cpp:73
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:567
Definition: ShapeChecking.h:35