non_max_suppression.hpp
1 // Copyright (C) 2018-2021 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 
5 #pragma once
6 
7 #include "ngraph/op/op.hpp"
8 
9 namespace ngraph
10 {
11  namespace op
12  {
13  namespace v1
14  {
15  /// \brief Elementwise addition operation.
16  ///
17  class NGRAPH_API NonMaxSuppression : public Op
18  {
19  public:
20  enum class BoxEncodingType
21  {
22  CORNER,
23  CENTER
24  };
25 
26  static constexpr NodeTypeInfo type_info{"NonMaxSuppression", 1};
27  const NodeTypeInfo& get_type_info() const override { return type_info; }
28  NonMaxSuppression() = default;
29 
30  /// \brief Constructs a NonMaxSuppression operation.
31  ///
32  /// \param boxes Node producing the box coordinates
33  /// \param scores Node producing the box scores
34  /// \param max_output_boxes_per_class Node producing maximum number of boxes to be
35  /// selected per class
36  /// \param iou_threshold Node producing intersection over union threshold
37  /// \param score_threshold Node producing minimum score threshold
38  /// \param box_encoding Specifies the format of boxes data encoding
40  const Output<Node>& scores,
41  const Output<Node>& max_output_boxes_per_class,
42  const Output<Node>& iou_threshold,
43  const Output<Node>& score_threshold,
44  const BoxEncodingType box_encoding = BoxEncodingType::CORNER,
45  const bool sort_result_descending = true);
46 
47  /// \brief Constructs a NonMaxSuppression operation with default values for the last
48  /// 3 inputs
49  ///
50  /// \param boxes Node producing the box coordinates
51  /// \param scores Node producing the box coordinates
52  /// \param box_encoding Specifies the format of boxes data encoding
53  /// \param sort_result_descending Specifies whether it is necessary to sort selected
54  /// boxes across batches
55  /// \param output_type Specifies the output tensor type
57  const Output<Node>& scores,
58  const BoxEncodingType box_encoding = BoxEncodingType::CORNER,
59  const bool sort_result_descending = true);
60 
61  bool visit_attributes(AttributeVisitor& visitor) override;
62  void validate_and_infer_types() override;
63 
64  std::shared_ptr<Node>
65  clone_with_new_inputs(const OutputVector& new_args) const override;
66 
67  BoxEncodingType get_box_encoding() const { return m_box_encoding; }
68  void set_box_encoding(const BoxEncodingType box_encoding)
69  {
70  m_box_encoding = box_encoding;
71  }
72  bool get_sort_result_descending() const { return m_sort_result_descending; }
73  void set_sort_result_descending(const bool sort_result_descending)
74  {
75  m_sort_result_descending = sort_result_descending;
76  }
77 
78  protected:
79  BoxEncodingType m_box_encoding = BoxEncodingType::CORNER;
80  bool m_sort_result_descending = true;
81 
82  private:
83  int64_t max_boxes_output_from_input() const;
84  };
85  } // namespace v1
86 
87  namespace v3
88  {
89  /// \brief NonMaxSuppression operation
90  ///
91  class NGRAPH_API NonMaxSuppression : public Op
92  {
93  public:
94  enum class BoxEncodingType
95  {
96  CORNER,
97  CENTER
98  };
99 
100  static constexpr NodeTypeInfo type_info{"NonMaxSuppression", 3};
101  const NodeTypeInfo& get_type_info() const override { return type_info; }
102  NonMaxSuppression() = default;
103 
104  /// \brief Constructs a NonMaxSuppression operation.
105  ///
106  /// \param boxes Node producing the box coordinates
107  /// \param scores Node producing the box scores
108  /// \param max_output_boxes_per_class Node producing maximum number of boxes to be
109  /// selected per class
110  /// \param iou_threshold Node producing intersection over union threshold
111  /// \param score_threshold Node producing minimum score threshold
112  /// \param box_encoding Specifies the format of boxes data encoding
113  /// \param sort_result_descending Specifies whether it is necessary to sort selected
114  /// boxes across batches
115  /// \param output_type Specifies the output tensor type
117  const Output<Node>& scores,
118  const Output<Node>& max_output_boxes_per_class,
119  const Output<Node>& iou_threshold,
120  const Output<Node>& score_threshold,
121  const BoxEncodingType box_encoding = BoxEncodingType::CORNER,
122  const bool sort_result_descending = true,
123  const ngraph::element::Type& output_type = ngraph::element::i64);
124 
125  /// \brief Constructs a NonMaxSuppression operation with default values for the last
126  /// 3 inputs
127  ///
128  /// \param boxes Node producing the box coordinates
129  /// \param scores Node producing the box coordinates
130  /// \param box_encoding Specifies the format of boxes data encoding
131  /// \param sort_result_descending Specifies whether it is necessary to sort selected
132  /// boxes across batches
133  /// \param output_type Specifies the output tensor type
135  const Output<Node>& scores,
136  const BoxEncodingType box_encoding = BoxEncodingType::CORNER,
137  const bool sort_result_descending = true,
138  const ngraph::element::Type& output_type = ngraph::element::i64);
139 
140  bool visit_attributes(AttributeVisitor& visitor) override;
141  void validate_and_infer_types() override;
142 
143  std::shared_ptr<Node>
144  clone_with_new_inputs(const OutputVector& new_args) const override;
145 
146  BoxEncodingType get_box_encoding() const { return m_box_encoding; }
147  void set_box_encoding(const BoxEncodingType box_encoding)
148  {
149  m_box_encoding = box_encoding;
150  }
151  bool get_sort_result_descending() const { return m_sort_result_descending; }
152  void set_sort_result_descending(const bool sort_result_descending)
153  {
154  m_sort_result_descending = sort_result_descending;
155  }
156 
157  element::Type get_output_type() const { return m_output_type; }
158  void set_output_type(const element::Type& output_type)
159  {
160  m_output_type = output_type;
161  }
162  using Node::set_output_type;
163 
164  protected:
165  BoxEncodingType m_box_encoding = BoxEncodingType::CORNER;
166  bool m_sort_result_descending = true;
167  ngraph::element::Type m_output_type = ngraph::element::i64;
168  void validate();
169  int64_t max_boxes_output_from_input() const;
170  };
171  } // namespace v3
172 
173  namespace v4
174  {
175  /// \brief NonMaxSuppression operation
176  ///
178  {
179  public:
180  static constexpr NodeTypeInfo type_info{"NonMaxSuppression", 4};
181  const NodeTypeInfo& get_type_info() const override { return type_info; }
182  NonMaxSuppression() = default;
183 
184  /// \brief Constructs a NonMaxSuppression operation.
185  ///
186  /// \param boxes Node producing the box coordinates
187  /// \param scores Node producing the box scores
188  /// \param max_output_boxes_per_class Node producing maximum number of boxes to be
189  /// selected per class
190  /// \param iou_threshold Node producing intersection over union threshold
191  /// \param score_threshold Node producing minimum score threshold
192  /// \param box_encoding Specifies the format of boxes data encoding
193  /// \param sort_result_descending Specifies whether it is necessary to sort selected
194  /// boxes across batches
195  /// \param output_type Specifies the output tensor type
197  const Output<Node>& scores,
198  const Output<Node>& max_output_boxes_per_class,
199  const Output<Node>& iou_threshold,
200  const Output<Node>& score_threshold,
201  const BoxEncodingType box_encoding = BoxEncodingType::CORNER,
202  const bool sort_result_descending = true,
203  const ngraph::element::Type& output_type = ngraph::element::i64);
204 
205  /// \brief Constructs a NonMaxSuppression operation with default values for the last
206  /// 3 inputs
207  ///
208  /// \param boxes Node producing the box coordinates
209  /// \param scores Node producing the box coordinates
210  /// \param box_encoding Specifies the format of boxes data encoding
211  /// \param sort_result_descending Specifies whether it is necessary to sort selected
212  /// boxes across batches
213  /// \param output_type Specifies the output tensor type
215  const Output<Node>& scores,
216  const BoxEncodingType box_encoding = BoxEncodingType::CORNER,
217  const bool sort_result_descending = true,
218  const ngraph::element::Type& output_type = ngraph::element::i64);
219 
220  void validate_and_infer_types() override;
221 
222  std::shared_ptr<Node>
223  clone_with_new_inputs(const OutputVector& new_args) const override;
224  };
225  } // namespace v4
226 
227  namespace v5
228  {
229  /// \brief NonMaxSuppression operation
230  ///
231  class NGRAPH_API NonMaxSuppression : public Op
232  {
233  public:
234  NGRAPH_RTTI_DECLARATION;
235  enum class BoxEncodingType
236  {
237  CORNER,
238  CENTER
239  };
240 
241  NonMaxSuppression() = default;
242 
243  /// \brief Constructs a NonMaxSuppression operation with default values in the last
244  /// 4 inputs.
245  ///
246  /// \param boxes Node producing the box coordinates
247  /// \param scores Node producing the box scores
248  /// \param box_encoding Specifies the format of boxes data encoding
249  /// \param sort_result_descending Specifies whether it is necessary to sort selected
250  /// boxes across batches
251  /// \param output_type Specifies the output tensor type
253  const Output<Node>& scores,
254  const BoxEncodingType box_encoding = BoxEncodingType::CORNER,
255  const bool sort_result_descending = true,
256  const ngraph::element::Type& output_type = ngraph::element::i64);
257 
258  /// \brief Constructs a NonMaxSuppression operation with default values in the last.
259  /// 3 inputs.
260  ///
261  /// \param boxes Node producing the box coordinates
262  /// \param scores Node producing the box scores
263  /// \param max_output_boxes_per_class Node producing maximum number of boxes to be
264  /// selected per class
265  /// \param box_encoding Specifies the format of boxes data encoding
266  /// \param sort_result_descending Specifies whether it is necessary to sort selected
267  /// boxes across batches
268  /// \param output_type Specifies the output tensor type
270  const Output<Node>& scores,
271  const Output<Node>& max_output_boxes_per_class,
272  const BoxEncodingType box_encoding = BoxEncodingType::CORNER,
273  const bool sort_result_descending = true,
274  const ngraph::element::Type& output_type = ngraph::element::i64);
275 
276  /// \brief Constructs a NonMaxSuppression operation with default values in the last.
277  /// 2 inputs.
278  ///
279  /// \param boxes Node producing the box coordinates
280  /// \param scores Node producing the box scores
281  /// \param max_output_boxes_per_class Node producing maximum number of boxes to be
282  /// selected per class
283  /// \param iou_threshold Node producing intersection over union threshold
284  /// \param box_encoding Specifies the format of boxes data encoding
285  /// \param sort_result_descending Specifies whether it is necessary to sort selected
286  /// boxes across batches
287  /// \param output_type Specifies the output tensor type
289  const Output<Node>& scores,
290  const Output<Node>& max_output_boxes_per_class,
291  const Output<Node>& iou_threshold,
292  const BoxEncodingType box_encoding = BoxEncodingType::CORNER,
293  const bool sort_result_descending = true,
294  const ngraph::element::Type& output_type = ngraph::element::i64);
295 
296  /// \brief Constructs a NonMaxSuppression operation with default value in the last.
297  /// input.
298  ///
299  /// \param boxes Node producing the box coordinates
300  /// \param scores Node producing the box scores
301  /// \param max_output_boxes_per_class Node producing maximum number of boxes to be
302  /// selected per class
303  /// \param iou_threshold Node producing intersection over union threshold
304  /// \param score_threshold Node producing minimum score threshold
305  /// \param box_encoding Specifies the format of boxes data encoding
306  /// \param sort_result_descending Specifies whether it is necessary to sort selected
307  /// boxes across batches
308  /// \param output_type Specifies the output tensor type
310  const Output<Node>& scores,
311  const Output<Node>& max_output_boxes_per_class,
312  const Output<Node>& iou_threshold,
313  const Output<Node>& score_threshold,
314  const BoxEncodingType box_encoding = BoxEncodingType::CORNER,
315  const bool sort_result_descending = true,
316  const ngraph::element::Type& output_type = ngraph::element::i64);
317 
318  /// \brief Constructs a NonMaxSuppression operation.
319  ///
320  /// \param boxes Node producing the box coordinates
321  /// \param scores Node producing the box scores
322  /// \param max_output_boxes_per_class Node producing maximum number of boxes to be
323  /// selected per class
324  /// \param iou_threshold Node producing intersection over union threshold
325  /// \param score_threshold Node producing minimum score threshold
326  /// \param soft_nms_sigma Node specifying the sigma parameter for Soft-NMS
327  /// \param box_encoding Specifies the format of boxes data encoding
328  /// \param sort_result_descending Specifies whether it is necessary to sort selected
329  /// boxes across batches
330  /// \param output_type Specifies the output tensor type
332  const Output<Node>& scores,
333  const Output<Node>& max_output_boxes_per_class,
334  const Output<Node>& iou_threshold,
335  const Output<Node>& score_threshold,
336  const Output<Node>& soft_nms_sigma,
337  const BoxEncodingType box_encoding = BoxEncodingType::CORNER,
338  const bool sort_result_descending = true,
339  const ngraph::element::Type& output_type = ngraph::element::i64);
340 
341  bool visit_attributes(AttributeVisitor& visitor) override;
342  void validate_and_infer_types() override;
343 
344  std::shared_ptr<Node>
345  clone_with_new_inputs(const OutputVector& new_args) const override;
346 
347  BoxEncodingType get_box_encoding() const { return m_box_encoding; }
348  void set_box_encoding(const BoxEncodingType box_encoding)
349  {
350  m_box_encoding = box_encoding;
351  }
352  bool get_sort_result_descending() const { return m_sort_result_descending; }
353  void set_sort_result_descending(const bool sort_result_descending)
354  {
355  m_sort_result_descending = sort_result_descending;
356  }
357 
358  element::Type get_output_type() const { return m_output_type; }
359  void set_output_type(const element::Type& output_type)
360  {
361  m_output_type = output_type;
362  }
363  using Node::set_output_type;
364 
365  int64_t max_boxes_output_from_input() const;
366  float iou_threshold_from_input() const;
367  float score_threshold_from_input() const;
368  float soft_nms_sigma_from_input() const;
369  bool is_soft_nms_sigma_constant_and_default() const;
370 
371  protected:
372  BoxEncodingType m_box_encoding = BoxEncodingType::CORNER;
373  bool m_sort_result_descending = true;
374  ngraph::element::Type m_output_type = ngraph::element::i64;
375  void validate();
376  };
377  } // namespace v5
378  } // namespace op
379 
380  NGRAPH_API
381  std::ostream& operator<<(std::ostream& s,
382  const op::v1::NonMaxSuppression::BoxEncodingType& type);
383 
384  template <>
385  class NGRAPH_API AttributeAdapter<op::v1::NonMaxSuppression::BoxEncodingType>
386  : public EnumAttributeAdapterBase<op::v1::NonMaxSuppression::BoxEncodingType>
387  {
388  public:
389  AttributeAdapter(op::v1::NonMaxSuppression::BoxEncodingType& value)
391  {
392  }
393 
394  static constexpr DiscreteTypeInfo type_info{
395  "AttributeAdapter<op::v1::NonMaxSuppression::BoxEncodingType>", 1};
396  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
397  };
398 
399  NGRAPH_API
400  std::ostream& operator<<(std::ostream& s,
401  const op::v3::NonMaxSuppression::BoxEncodingType& type);
402 
403  template <>
404  class NGRAPH_API AttributeAdapter<op::v3::NonMaxSuppression::BoxEncodingType>
405  : public EnumAttributeAdapterBase<op::v3::NonMaxSuppression::BoxEncodingType>
406  {
407  public:
408  AttributeAdapter(op::v3::NonMaxSuppression::BoxEncodingType& value)
410  {
411  }
412 
413  static constexpr DiscreteTypeInfo type_info{
414  "AttributeAdapter<op::v3::NonMaxSuppression::BoxEncodingType>", 1};
415  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
416  };
417 
418  NGRAPH_API
419  std::ostream& operator<<(std::ostream& s,
420  const op::v5::NonMaxSuppression::BoxEncodingType& type);
421 
422  template <>
423  class NGRAPH_API AttributeAdapter<op::v5::NonMaxSuppression::BoxEncodingType>
424  : public EnumAttributeAdapterBase<op::v5::NonMaxSuppression::BoxEncodingType>
425  {
426  public:
427  AttributeAdapter(op::v5::NonMaxSuppression::BoxEncodingType& value)
429  {
430  }
431 
432  static constexpr DiscreteTypeInfo type_info{
433  "AttributeAdapter<op::v5::NonMaxSuppression::BoxEncodingType>", 1};
434  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
435  };
436 } // namespace ngraph
An AttributeAdapter "captures" an attribute as an AT& and makes it available as a ValueAccessor<VAT>.
Definition: attribute_adapter.hpp:161
Visits the attributes of a node, primarily for serialization-like tasks.
Definition: attribute_visitor.hpp:59
Access an enum via a string.
Definition: attribute_adapter.hpp:168
A handle for one of a node's outputs.
Definition: node_output.hpp:33
Definition: element_type.hpp:51
Root of all actual ops.
Definition: op.hpp:17
Elementwise addition operation.
Definition: non_max_suppression.hpp:18
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:27
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:92
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:101
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:178
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:181
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:232
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:16
Definition: type.hpp:27