roi_align.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 v3
26  {
27  class NGRAPH_API ROIAlign : public Op
28  {
29  public:
30  static constexpr NodeTypeInfo type_info{"ROIAlign", 3};
31  const NodeTypeInfo& get_type_info() const override { return type_info; }
32  enum class PoolingMode
33  {
34  AVG,
35  MAX
36  };
37 
38  ROIAlign() = default;
39  /// \brief Constructs a ROIAlign node matching the ONNX ROIAlign specification
40  ///
41  /// \param input Input feature map {N, C, H, W}
42  /// \param rois Regions of interest to pool over
43  /// \param batch_indices Indices of images in the batch matching
44  /// the number or ROIs
45  /// \param pooled_h Height of the ROI output features
46  /// \param pooled_w Width of the ROI output features
47  /// \param sampling_ratio Number of sampling points used to compute
48  /// an output element
49  /// \param spatial_scale Spatial scale factor used to translate ROI coordinates
50  /// \param mode Method of pooling - 'avg' or 'max'
51  ROIAlign(const Output<Node>& input,
52  const Output<Node>& rois,
53  const Output<Node>& batch_indices,
54  const int pooled_h,
55  const int pooled_w,
56  const int sampling_ratio,
57  const float spatial_scale,
58  const std::string& mode);
59 
60  ROIAlign(const Output<Node>& input,
61  const Output<Node>& rois,
62  const Output<Node>& batch_indices,
63  const int pooled_h,
64  const int pooled_w,
65  const int sampling_ratio,
66  const float spatial_scale,
67  const PoolingMode mode);
68 
69  virtual void validate_and_infer_types() override;
70  virtual bool visit_attributes(AttributeVisitor& visitor) override;
71  virtual std::shared_ptr<Node>
72  clone_with_new_inputs(const OutputVector& new_args) const override;
73 
74  int get_pooled_h() const { return m_pooled_h; }
75  int get_pooled_w() const { return m_pooled_w; }
76  int get_sampling_ratio() const { return m_sampling_ratio; }
77  float get_spatial_scale() const { return m_spatial_scale; }
78  PoolingMode get_mode() const { return m_mode; }
79  bool evaluate(const HostTensorVector& outputs,
80  const HostTensorVector& inputs) const override;
81 
82  private:
83  PoolingMode mode_from_string(const std::string& mode) const;
84 
85  private:
86  int m_pooled_h;
87  int m_pooled_w;
88  int m_sampling_ratio;
89  float m_spatial_scale;
90  PoolingMode m_mode;
91  };
92  } // namespace v3
93  using v3::ROIAlign;
94  } // namespace op
95 
96  std::ostream& operator<<(std::ostream& s, const op::v3::ROIAlign::PoolingMode& mode);
97 
98  template <>
99  class NGRAPH_API AttributeAdapter<op::v3::ROIAlign::PoolingMode>
100  : public EnumAttributeAdapterBase<op::v3::ROIAlign::PoolingMode>
101  {
102  public:
103  AttributeAdapter(op::v3::ROIAlign::PoolingMode& value)
105  {
106  }
107 
108  static constexpr DiscreteTypeInfo type_info{
109  "AttributeAdapter<op::v3::ROIAlign::PoolingMode>", 3};
110  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
111  };
112 } // 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
Root of all actual ops.
Definition: op.hpp:29
Definition: roi_align.hpp:28
bool evaluate(const HostTensorVector &outputs, const HostTensorVector &inputs) const override
Evaluates the op on input_values putting results in output_values.
virtual void validate_and_infer_types() override
Verifies that attributes and inputs are consistent and computes output shapes and element types....
ROIAlign(const Output< Node > &input, const Output< Node > &rois, const Output< Node > &batch_indices, const int pooled_h, const int pooled_w, const int sampling_ratio, const float spatial_scale, const std::string &mode)
Constructs a ROIAlign node matching the ONNX ROIAlign specification.
const NodeTypeInfo & get_type_info() const override
Definition: roi_align.hpp:31
The Intel nGraph C++ API.
Definition: attribute_adapter.hpp:28
Definition: type.hpp:39