non_max_suppression.hpp
1 //*****************************************************************************
2 // Copyright 2017-2020 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //*****************************************************************************
16 
17 #pragma once
18 
19 #include "ngraph/op/op.hpp"
20 
21 namespace ngraph
22 {
23  namespace op
24  {
25  namespace v1
26  {
27  /// \brief Elementwise addition operation.
28  ///
29  class NGRAPH_API NonMaxSuppression : public Op
30  {
31  public:
32  enum class BoxEncodingType
33  {
34  CORNER,
35  CENTER
36  };
37 
38  static constexpr NodeTypeInfo type_info{"NonMaxSuppression", 1};
39  const NodeTypeInfo& get_type_info() const override { return type_info; }
40  NonMaxSuppression() = default;
41 
42  /// \brief Constructs a NonMaxSuppression operation.
43  ///
44  /// \param boxes Node producing the box coordinates
45  /// \param scores Node producing the box scores
46  /// \param max_output_boxes_per_class Node producing maximum number of boxes to be
47  /// selected per class
48  /// \param iou_threshold Node producing intersection over union threshold
49  /// \param score_threshold Node producing minimum score threshold
50  /// \param box_encoding Specifies the format of boxes data encoding
51  NonMaxSuppression(const Output<Node>& boxes,
52  const Output<Node>& scores,
53  const Output<Node>& max_output_boxes_per_class,
54  const Output<Node>& iou_threshold,
55  const Output<Node>& score_threshold,
56  const BoxEncodingType box_encoding = BoxEncodingType::CORNER,
57  const bool sort_result_descending = true);
58 
59  /// \brief Constructs a NonMaxSuppression operation with default values for the last
60  /// 3 inputs
61  ///
62  /// \param boxes Node producing the box coordinates
63  /// \param scores Node producing the box coordinates
64  /// \param box_encoding Specifies the format of boxes data encoding
65  /// \param sort_result_descending Specifies whether it is necessary to sort selected
66  /// boxes across batches
67  /// \param output_type Specifies the output tensor type
68  NonMaxSuppression(const Output<Node>& boxes,
69  const Output<Node>& scores,
70  const BoxEncodingType box_encoding = BoxEncodingType::CORNER,
71  const bool sort_result_descending = true);
72 
73  bool visit_attributes(AttributeVisitor& visitor) override;
74  void validate_and_infer_types() override;
75 
76  std::shared_ptr<Node>
77  clone_with_new_inputs(const OutputVector& new_args) const override;
78 
79  BoxEncodingType get_box_encoding() const { return m_box_encoding; }
80  void set_box_encoding(const BoxEncodingType box_encoding)
81  {
82  m_box_encoding = box_encoding;
83  }
84  bool get_sort_result_descending() const { return m_sort_result_descending; }
85  void set_sort_result_descending(const bool sort_result_descending)
86  {
87  m_sort_result_descending = sort_result_descending;
88  }
89 
90  protected:
91  BoxEncodingType m_box_encoding = BoxEncodingType::CORNER;
92  bool m_sort_result_descending = true;
93 
94  private:
95  int64_t max_boxes_output_from_input() const;
96  };
97  } // namespace v1
98 
99  namespace v3
100  {
101  /// \brief NonMaxSuppression operation
102  ///
103  class NGRAPH_API NonMaxSuppression : public Op
104  {
105  public:
106  enum class BoxEncodingType
107  {
108  CORNER,
109  CENTER
110  };
111 
112  static constexpr NodeTypeInfo type_info{"NonMaxSuppression", 3};
113  const NodeTypeInfo& get_type_info() const override { return type_info; }
114  NonMaxSuppression() = default;
115 
116  /// \brief Constructs a NonMaxSuppression operation.
117  ///
118  /// \param boxes Node producing the box coordinates
119  /// \param scores Node producing the box scores
120  /// \param max_output_boxes_per_class Node producing maximum number of boxes to be
121  /// selected per class
122  /// \param iou_threshold Node producing intersection over union threshold
123  /// \param score_threshold Node producing minimum score threshold
124  /// \param box_encoding Specifies the format of boxes data encoding
125  /// \param sort_result_descending Specifies whether it is necessary to sort selected
126  /// boxes across batches
127  /// \param output_type Specifies the output tensor type
128  NonMaxSuppression(const Output<Node>& boxes,
129  const Output<Node>& scores,
130  const Output<Node>& max_output_boxes_per_class,
131  const Output<Node>& iou_threshold,
132  const Output<Node>& score_threshold,
133  const BoxEncodingType box_encoding = BoxEncodingType::CORNER,
134  const bool sort_result_descending = true,
135  const ngraph::element::Type& output_type = ngraph::element::i64);
136 
137  /// \brief Constructs a NonMaxSuppression operation with default values for the last
138  /// 3 inputs
139  ///
140  /// \param boxes Node producing the box coordinates
141  /// \param scores Node producing the box coordinates
142  /// \param box_encoding Specifies the format of boxes data encoding
143  /// \param sort_result_descending Specifies whether it is necessary to sort selected
144  /// boxes across batches
145  /// \param output_type Specifies the output tensor type
146  NonMaxSuppression(const Output<Node>& boxes,
147  const Output<Node>& scores,
148  const BoxEncodingType box_encoding = BoxEncodingType::CORNER,
149  const bool sort_result_descending = true,
150  const ngraph::element::Type& output_type = ngraph::element::i64);
151 
152  bool visit_attributes(AttributeVisitor& visitor) override;
153  void validate_and_infer_types() override;
154 
155  std::shared_ptr<Node>
156  clone_with_new_inputs(const OutputVector& new_args) const override;
157 
158  BoxEncodingType get_box_encoding() const { return m_box_encoding; }
159  void set_box_encoding(const BoxEncodingType box_encoding)
160  {
161  m_box_encoding = box_encoding;
162  }
163  bool get_sort_result_descending() const { return m_sort_result_descending; }
164  void set_sort_result_descending(const bool sort_result_descending)
165  {
166  m_sort_result_descending = sort_result_descending;
167  }
168 
169  element::Type get_output_type() const { return m_output_type; }
170  void set_output_type(const element::Type& output_type)
171  {
172  m_output_type = output_type;
173  }
174  using Node::set_output_type;
175 
176  protected:
177  BoxEncodingType m_box_encoding = BoxEncodingType::CORNER;
178  bool m_sort_result_descending = true;
179  ngraph::element::Type m_output_type = ngraph::element::i64;
180  void validate();
181  int64_t max_boxes_output_from_input() const;
182  };
183  } // namespace v3
184 
185  namespace v4
186  {
187  /// \brief NonMaxSuppression operation
188  ///
190  {
191  public:
192  static constexpr NodeTypeInfo type_info{"NonMaxSuppression", 4};
193  const NodeTypeInfo& get_type_info() const override { return type_info; }
194  NonMaxSuppression() = default;
195 
196  /// \brief Constructs a NonMaxSuppression operation.
197  ///
198  /// \param boxes Node producing the box coordinates
199  /// \param scores Node producing the box scores
200  /// \param max_output_boxes_per_class Node producing maximum number of boxes to be
201  /// selected per class
202  /// \param iou_threshold Node producing intersection over union threshold
203  /// \param score_threshold Node producing minimum score threshold
204  /// \param box_encoding Specifies the format of boxes data encoding
205  /// \param sort_result_descending Specifies whether it is necessary to sort selected
206  /// boxes across batches
207  /// \param output_type Specifies the output tensor type
208  NonMaxSuppression(const Output<Node>& boxes,
209  const Output<Node>& scores,
210  const Output<Node>& max_output_boxes_per_class,
211  const Output<Node>& iou_threshold,
212  const Output<Node>& score_threshold,
213  const BoxEncodingType box_encoding = BoxEncodingType::CORNER,
214  const bool sort_result_descending = true,
215  const ngraph::element::Type& output_type = ngraph::element::i64);
216 
217  /// \brief Constructs a NonMaxSuppression operation with default values for the last
218  /// 3 inputs
219  ///
220  /// \param boxes Node producing the box coordinates
221  /// \param scores Node producing the box coordinates
222  /// \param box_encoding Specifies the format of boxes data encoding
223  /// \param sort_result_descending Specifies whether it is necessary to sort selected
224  /// boxes across batches
225  /// \param output_type Specifies the output tensor type
226  NonMaxSuppression(const Output<Node>& boxes,
227  const Output<Node>& scores,
228  const BoxEncodingType box_encoding = BoxEncodingType::CORNER,
229  const bool sort_result_descending = true,
230  const ngraph::element::Type& output_type = ngraph::element::i64);
231 
232  void validate_and_infer_types() override;
233 
234  std::shared_ptr<Node>
235  clone_with_new_inputs(const OutputVector& new_args) const override;
236  };
237  } // namespace v4
238  } // namespace op
239 
240  NGRAPH_API
241  std::ostream& operator<<(std::ostream& s,
242  const op::v1::NonMaxSuppression::BoxEncodingType& type);
243 
244  template <>
245  class NGRAPH_API AttributeAdapter<op::v1::NonMaxSuppression::BoxEncodingType>
246  : public EnumAttributeAdapterBase<op::v1::NonMaxSuppression::BoxEncodingType>
247  {
248  public:
249  AttributeAdapter(op::v1::NonMaxSuppression::BoxEncodingType& value)
250  : EnumAttributeAdapterBase<op::v1::NonMaxSuppression::BoxEncodingType>(value)
251  {
252  }
253 
254  static constexpr DiscreteTypeInfo type_info{
255  "AttributeAdapter<op::v1::NonMaxSuppression::BoxEncodingType>", 1};
256  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
257  };
258 
259  NGRAPH_API
260  std::ostream& operator<<(std::ostream& s,
261  const op::v3::NonMaxSuppression::BoxEncodingType& type);
262 
263  template <>
264  class NGRAPH_API AttributeAdapter<op::v3::NonMaxSuppression::BoxEncodingType>
265  : public EnumAttributeAdapterBase<op::v3::NonMaxSuppression::BoxEncodingType>
266  {
267  public:
268  AttributeAdapter(op::v3::NonMaxSuppression::BoxEncodingType& value)
269  : EnumAttributeAdapterBase<op::v3::NonMaxSuppression::BoxEncodingType>(value)
270  {
271  }
272 
273  static constexpr DiscreteTypeInfo type_info{
274  "AttributeAdapter<op::v3::NonMaxSuppression::BoxEncodingType>", 1};
275  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
276  };
277 } // namespace ngraph
ngraph::op::v3::NonMaxSuppression::NonMaxSuppression
NonMaxSuppression(const Output< Node > &boxes, const Output< Node > &scores, const BoxEncodingType box_encoding=BoxEncodingType::CORNER, const bool sort_result_descending=true, const ngraph::element::Type &output_type=ngraph::element::i64)
Constructs a NonMaxSuppression operation with default values for the last 3 inputs.
ngraph::op::v4::NonMaxSuppression::validate_and_infer_types
void validate_and_infer_types() override
Verifies that attributes and inputs are consistent and computes output shapes and element types....
ngraph::op::v4::NonMaxSuppression::get_type_info
const NodeTypeInfo & get_type_info() const override
Definition: non_max_suppression.hpp:193
ngraph::op::v1::NonMaxSuppression
Elementwise addition operation.
Definition: non_max_suppression.hpp:30
ngraph::op::v3::NonMaxSuppression::validate_and_infer_types
void validate_and_infer_types() override
Verifies that attributes and inputs are consistent and computes output shapes and element types....
ngraph::op::v1::NonMaxSuppression::get_type_info
const NodeTypeInfo & get_type_info() const override
Definition: non_max_suppression.hpp:39
ngraph::op::v4::NonMaxSuppression::NonMaxSuppression
NonMaxSuppression(const Output< Node > &boxes, const Output< Node > &scores, const Output< Node > &max_output_boxes_per_class, const Output< Node > &iou_threshold, const Output< Node > &score_threshold, const BoxEncodingType box_encoding=BoxEncodingType::CORNER, const bool sort_result_descending=true, const ngraph::element::Type &output_type=ngraph::element::i64)
Constructs a NonMaxSuppression operation.
ngraph::element::Type
Definition: element_type.hpp:61
ngraph::op::v1::NonMaxSuppression::validate_and_infer_types
void validate_and_infer_types() override
Verifies that attributes and inputs are consistent and computes output shapes and element types....
ngraph::DiscreteTypeInfo
Definition: type.hpp:39
ngraph
The Intel nGraph C++ API.
Definition: attribute_adapter.hpp:28
ngraph::op::v1::NonMaxSuppression::NonMaxSuppression
NonMaxSuppression(const Output< Node > &boxes, const Output< Node > &scores, const Output< Node > &max_output_boxes_per_class, const Output< Node > &iou_threshold, const Output< Node > &score_threshold, const BoxEncodingType box_encoding=BoxEncodingType::CORNER, const bool sort_result_descending=true)
Constructs a NonMaxSuppression operation.
ngraph::op::v4::NonMaxSuppression
NonMaxSuppression operation.
Definition: non_max_suppression.hpp:190
ngraph::op::v3::NonMaxSuppression::get_type_info
const NodeTypeInfo & get_type_info() const override
Definition: non_max_suppression.hpp:113
ngraph::AttributeVisitor
Visits the attributes of a node, primarily for serialization-like tasks.
Definition: attribute_visitor.hpp:70
ngraph::op::v3::NonMaxSuppression
NonMaxSuppression operation.
Definition: non_max_suppression.hpp:104
ngraph::op::v1::NonMaxSuppression::NonMaxSuppression
NonMaxSuppression(const Output< Node > &boxes, const Output< Node > &scores, const BoxEncodingType box_encoding=BoxEncodingType::CORNER, const bool sort_result_descending=true)
Constructs a NonMaxSuppression operation with default values for the last 3 inputs.
ngraph::op::v3::NonMaxSuppression::NonMaxSuppression
NonMaxSuppression(const Output< Node > &boxes, const Output< Node > &scores, const Output< Node > &max_output_boxes_per_class, const Output< Node > &iou_threshold, const Output< Node > &score_threshold, const BoxEncodingType box_encoding=BoxEncodingType::CORNER, const bool sort_result_descending=true, const ngraph::element::Type &output_type=ngraph::element::i64)
Constructs a NonMaxSuppression operation.
ngraph::op::v4::NonMaxSuppression::NonMaxSuppression
NonMaxSuppression(const Output< Node > &boxes, const Output< Node > &scores, const BoxEncodingType box_encoding=BoxEncodingType::CORNER, const bool sort_result_descending=true, const ngraph::element::Type &output_type=ngraph::element::i64)
Constructs a NonMaxSuppression operation with default values for the last 3 inputs.
ngraph::op::Op
Root of all actual ops.
Definition: op.hpp:29