non_max_suppression.hpp
1 //*****************************************************************************
2 // Copyright 2017-2021 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
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
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
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
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
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
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 
239  namespace v5
240  {
241  /// \brief NonMaxSuppression operation
242  ///
243  class NGRAPH_API NonMaxSuppression : public Op
244  {
245  public:
246  NGRAPH_RTTI_DECLARATION;
247  enum class BoxEncodingType
248  {
249  CORNER,
250  CENTER
251  };
252 
253  NonMaxSuppression() = default;
254 
255  /// \brief Constructs a NonMaxSuppression operation with default values in the last
256  /// 4 inputs.
257  ///
258  /// \param boxes Node producing the box coordinates
259  /// \param scores Node producing the box scores
260  /// \param box_encoding Specifies the format of boxes data encoding
261  /// \param sort_result_descending Specifies whether it is necessary to sort selected
262  /// boxes across batches
263  /// \param output_type Specifies the output tensor type
265  const Output<Node>& scores,
266  const BoxEncodingType box_encoding = BoxEncodingType::CORNER,
267  const bool sort_result_descending = true,
268  const ngraph::element::Type& output_type = ngraph::element::i64);
269 
270  /// \brief Constructs a NonMaxSuppression operation with default values in the last.
271  /// 3 inputs.
272  ///
273  /// \param boxes Node producing the box coordinates
274  /// \param scores Node producing the box scores
275  /// \param max_output_boxes_per_class Node producing maximum number of boxes to be
276  /// selected per class
277  /// \param box_encoding Specifies the format of boxes data encoding
278  /// \param sort_result_descending Specifies whether it is necessary to sort selected
279  /// boxes across batches
280  /// \param output_type Specifies the output tensor type
282  const Output<Node>& scores,
283  const Output<Node>& max_output_boxes_per_class,
284  const BoxEncodingType box_encoding = BoxEncodingType::CORNER,
285  const bool sort_result_descending = true,
286  const ngraph::element::Type& output_type = ngraph::element::i64);
287 
288  /// \brief Constructs a NonMaxSuppression operation with default values in the last.
289  /// 2 inputs.
290  ///
291  /// \param boxes Node producing the box coordinates
292  /// \param scores Node producing the box scores
293  /// \param max_output_boxes_per_class Node producing maximum number of boxes to be
294  /// selected per class
295  /// \param iou_threshold Node producing intersection over union threshold
296  /// \param box_encoding Specifies the format of boxes data encoding
297  /// \param sort_result_descending Specifies whether it is necessary to sort selected
298  /// boxes across batches
299  /// \param output_type Specifies the output tensor type
301  const Output<Node>& scores,
302  const Output<Node>& max_output_boxes_per_class,
303  const Output<Node>& iou_threshold,
304  const BoxEncodingType box_encoding = BoxEncodingType::CORNER,
305  const bool sort_result_descending = true,
306  const ngraph::element::Type& output_type = ngraph::element::i64);
307 
308  /// \brief Constructs a NonMaxSuppression operation with default value in the last.
309  /// input.
310  ///
311  /// \param boxes Node producing the box coordinates
312  /// \param scores Node producing the box scores
313  /// \param max_output_boxes_per_class Node producing maximum number of boxes to be
314  /// selected per class
315  /// \param iou_threshold Node producing intersection over union threshold
316  /// \param score_threshold Node producing minimum score threshold
317  /// \param box_encoding Specifies the format of boxes data encoding
318  /// \param sort_result_descending Specifies whether it is necessary to sort selected
319  /// boxes across batches
320  /// \param output_type Specifies the output tensor type
322  const Output<Node>& scores,
323  const Output<Node>& max_output_boxes_per_class,
324  const Output<Node>& iou_threshold,
325  const Output<Node>& score_threshold,
326  const BoxEncodingType box_encoding = BoxEncodingType::CORNER,
327  const bool sort_result_descending = true,
328  const ngraph::element::Type& output_type = ngraph::element::i64);
329 
330  /// \brief Constructs a NonMaxSuppression operation.
331  ///
332  /// \param boxes Node producing the box coordinates
333  /// \param scores Node producing the box scores
334  /// \param max_output_boxes_per_class Node producing maximum number of boxes to be
335  /// selected per class
336  /// \param iou_threshold Node producing intersection over union threshold
337  /// \param score_threshold Node producing minimum score threshold
338  /// \param soft_nms_sigma Node specifying the sigma parameter for Soft-NMS
339  /// \param box_encoding Specifies the format of boxes data encoding
340  /// \param sort_result_descending Specifies whether it is necessary to sort selected
341  /// boxes across batches
342  /// \param output_type Specifies the output tensor type
344  const Output<Node>& scores,
345  const Output<Node>& max_output_boxes_per_class,
346  const Output<Node>& iou_threshold,
347  const Output<Node>& score_threshold,
348  const Output<Node>& soft_nms_sigma,
349  const BoxEncodingType box_encoding = BoxEncodingType::CORNER,
350  const bool sort_result_descending = true,
351  const ngraph::element::Type& output_type = ngraph::element::i64);
352 
353  bool visit_attributes(AttributeVisitor& visitor) override;
354  void validate_and_infer_types() override;
355 
356  std::shared_ptr<Node>
357  clone_with_new_inputs(const OutputVector& new_args) const override;
358 
359  BoxEncodingType get_box_encoding() const { return m_box_encoding; }
360  void set_box_encoding(const BoxEncodingType box_encoding)
361  {
362  m_box_encoding = box_encoding;
363  }
364  bool get_sort_result_descending() const { return m_sort_result_descending; }
365  void set_sort_result_descending(const bool sort_result_descending)
366  {
367  m_sort_result_descending = sort_result_descending;
368  }
369 
370  element::Type get_output_type() const { return m_output_type; }
371  void set_output_type(const element::Type& output_type)
372  {
373  m_output_type = output_type;
374  }
375  using Node::set_output_type;
376 
377  int64_t max_boxes_output_from_input() const;
378  float iou_threshold_from_input() const;
379  float score_threshold_from_input() const;
380  float soft_nms_sigma_from_input() const;
381  bool is_soft_nms_sigma_constant_and_default() const;
382 
383  protected:
384  BoxEncodingType m_box_encoding = BoxEncodingType::CORNER;
385  bool m_sort_result_descending = true;
386  ngraph::element::Type m_output_type = ngraph::element::i64;
387  void validate();
388  };
389  } // namespace v5
390  } // namespace op
391 
392  NGRAPH_API
393  std::ostream& operator<<(std::ostream& s,
394  const op::v1::NonMaxSuppression::BoxEncodingType& type);
395 
396  template <>
397  class NGRAPH_API AttributeAdapter<op::v1::NonMaxSuppression::BoxEncodingType>
398  : public EnumAttributeAdapterBase<op::v1::NonMaxSuppression::BoxEncodingType>
399  {
400  public:
401  AttributeAdapter(op::v1::NonMaxSuppression::BoxEncodingType& value)
403  {
404  }
405 
406  static constexpr DiscreteTypeInfo type_info{
407  "AttributeAdapter<op::v1::NonMaxSuppression::BoxEncodingType>", 1};
408  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
409  };
410 
411  NGRAPH_API
412  std::ostream& operator<<(std::ostream& s,
413  const op::v3::NonMaxSuppression::BoxEncodingType& type);
414 
415  template <>
416  class NGRAPH_API AttributeAdapter<op::v3::NonMaxSuppression::BoxEncodingType>
417  : public EnumAttributeAdapterBase<op::v3::NonMaxSuppression::BoxEncodingType>
418  {
419  public:
420  AttributeAdapter(op::v3::NonMaxSuppression::BoxEncodingType& value)
422  {
423  }
424 
425  static constexpr DiscreteTypeInfo type_info{
426  "AttributeAdapter<op::v3::NonMaxSuppression::BoxEncodingType>", 1};
427  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
428  };
429 
430  NGRAPH_API
431  std::ostream& operator<<(std::ostream& s,
432  const op::v5::NonMaxSuppression::BoxEncodingType& type);
433 
434  template <>
435  class NGRAPH_API AttributeAdapter<op::v5::NonMaxSuppression::BoxEncodingType>
436  : public EnumAttributeAdapterBase<op::v5::NonMaxSuppression::BoxEncodingType>
437  {
438  public:
439  AttributeAdapter(op::v5::NonMaxSuppression::BoxEncodingType& value)
441  {
442  }
443 
444  static constexpr DiscreteTypeInfo type_info{
445  "AttributeAdapter<op::v5::NonMaxSuppression::BoxEncodingType>", 1};
446  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
447  };
448 } // namespace ngraph
An AttributeAdapter "captures" an attribute as an AT& and makes it available as a ValueAccessor<VAT>.
Definition: attribute_adapter.hpp:171
Visits the attributes of a node, primarily for serialization-like tasks.
Definition: attribute_visitor.hpp:71
Access an enum via a string.
Definition: attribute_adapter.hpp:178
A handle for one of a node's outputs.
Definition: node_output.hpp:42
Definition: element_type.hpp:61
Root of all actual ops.
Definition: op.hpp:29
Elementwise addition operation.
Definition: non_max_suppression.hpp:30
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.
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.
const NodeTypeInfo & get_type_info() const override
Definition: non_max_suppression.hpp:39
void validate_and_infer_types() override
Verifies that attributes and inputs are consistent and computes output shapes and element types....
NonMaxSuppression operation.
Definition: non_max_suppression.hpp:104
void validate_and_infer_types() override
Verifies that attributes and inputs are consistent and computes output shapes and element types....
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.
const NodeTypeInfo & get_type_info() const override
Definition: non_max_suppression.hpp:113
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.
NonMaxSuppression operation.
Definition: non_max_suppression.hpp:190
void validate_and_infer_types() override
Verifies that attributes and inputs are consistent and computes output shapes and element types....
const NodeTypeInfo & get_type_info() const override
Definition: non_max_suppression.hpp:193
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.
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.
NonMaxSuppression operation.
Definition: non_max_suppression.hpp:244
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 with default value in the last. input.
void validate_and_infer_types() override
Verifies that attributes and inputs are consistent and computes output shapes and element types....
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 Output< Node > &soft_nms_sigma, 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.
NonMaxSuppression(const Output< Node > &boxes, const Output< Node > &scores, const Output< Node > &max_output_boxes_per_class, const Output< Node > &iou_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 with default values in the last. 2 inputs.
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 in the last 4 inputs.
NonMaxSuppression(const Output< Node > &boxes, const Output< Node > &scores, const Output< Node > &max_output_boxes_per_class, 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 in the last. 3 inputs.
The Intel nGraph C++ API.
Definition: attribute_adapter.hpp:28
Definition: type.hpp:39