Open3D (C++ API)  0.12.0
NmsOpKernel.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 "open3d/ml/impl/misc/VoxelPooling.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 
32 // namespace for code that is common for all kernels
33 namespace nms_opkernel {
34 
35 class OutputAllocator {
36 public:
37  OutputAllocator(tensorflow::OpKernelContext* context) : context(context) {}
38 
39  void AllocKeepIndices(int64_t** ptr, int64_t num) {
40  using namespace tensorflow;
41  *ptr = nullptr;
42  Tensor* tensor = 0;
43  TensorShape shape({num});
44  OP_REQUIRES_OK(context, context->allocate_output(0, shape, &tensor));
45  auto flat_tensor = tensor->flat<int64>();
46  *ptr = (int64_t*)flat_tensor.data();
47  }
48 
49 private:
50  tensorflow::OpKernelContext* context;
51 };
52 
53 // Base class with common code for the OpKernel implementations
54 class NmsOpKernel : public tensorflow::OpKernel {
55 public:
56  explicit NmsOpKernel(tensorflow::OpKernelConstruction* construction)
57  : OpKernel(construction) {
58  OP_REQUIRES_OK(construction,
59  construction->GetAttr("nms_overlap_thresh",
60  &nms_overlap_thresh));
61  }
62 
63  void Compute(tensorflow::OpKernelContext* context) override {
64  using namespace tensorflow;
65  const Tensor& boxes = context->input(0);
66  const Tensor& scores = context->input(1);
67 
68  {
69  using namespace open3d::ml::op_util;
70  Dim num_points("num_points");
71  Dim five(5, "five");
72  CHECK_SHAPE(context, boxes, num_points, five);
73  CHECK_SHAPE(context, scores, num_points);
74  }
75 
76  Kernel(context, boxes, scores);
77  }
78 
79  // Function with the device specific code
80  virtual void Kernel(tensorflow::OpKernelContext* context,
81  const tensorflow::Tensor& boxes,
82  const tensorflow::Tensor& scores) = 0;
83 
84 protected:
85  float nms_overlap_thresh;
86 };
87 
88 } // namespace nms_opkernel
#define CHECK_SHAPE(tensor,...)
Definition: TorchHelper.h:204
Class for dimensions for which the value should be inferred.
Definition: ShapeChecking.h:69
Definition: ShapeChecking.h:35