detection_kernel.h 7.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#pragma once

J
jameswu2014 已提交
17
#include <memory>
18 19 20 21 22 23 24 25 26 27 28 29 30
#include <vector>
#include "framework/operator.h"
#include "operators/op_param.h"

namespace paddle_mobile {
namespace operators {

#ifdef ANCHOR_GENERATOR_OP
template <typename Dtype>
class AnchorGeneratorParam : public OpParam {
 public:
  AnchorGeneratorParam(const VariableNameMap &inputs,
                       const VariableNameMap &outputs,
31 32 33 34
                       const AttributeMap &attrs, Scope *scope)
      : OpParam(inputs, outputs, attrs, scope) {
    input_ =
        OpParam::GetVarValue<framework::LoDTensor>("Input", inputs, *scope);
35
    output_anchors_ =
36 37 38
        OpParam::GetVarValue<framework::LoDTensor>("Anchors", outputs, *scope);
    output_variances_ = OpParam::GetVarValue<framework::LoDTensor>(
        "Variances", outputs, *scope);
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

    anchor_sizes_ = OpParam::GetAttr<std::vector<float>>("anchor_sizes", attrs);
    aspect_ratios_ =
        OpParam::GetAttr<std::vector<float>>("aspect_ratios", attrs);
    variances_ = OpParam::GetAttr<std::vector<float>>("variances", attrs);
    stride_ = OpParam::GetAttr<std::vector<float>>("stride", attrs);
    offset_ = OpParam::GetAttr<float>("offset", attrs);
  }

 public:
  // input
  framework::Tensor *input_;
  // outputs
  framework::Tensor *output_anchors_;
  framework::Tensor *output_variances_;

  std::vector<float> anchor_sizes_;
  std::vector<float> aspect_ratios_;
  std::vector<float> variances_;
  std::vector<float> stride_;
  float offset_;
};

DECLARE_KERNEL(AnchorGenerator, AnchorGeneratorParam);
#endif

#ifdef PROPOSAL_OP
template <typename Dtype>
class ProposalParam : public OpParam {
 public:
  ProposalParam(const VariableNameMap &inputs, const VariableNameMap &outputs,
70 71
                const AttributeMap &attrs, Scope *scope)
      : OpParam(inputs, outputs, attrs, scope) {
Z
zhangyang0701 已提交
72
    scores_ =
73 74 75
        OpParam::GetVarValue<framework::LoDTensor>("Scores", inputs, *scope);
    bbox_deltas_ = OpParam::GetVarValue<framework::LoDTensor>("BboxDeltas",
                                                              inputs, *scope);
Z
zhangyang0701 已提交
76
    im_info_ =
77
        OpParam::GetVarValue<framework::LoDTensor>("ImInfo", inputs, *scope);
78
    anchors_ =
79
        OpParam::GetVarValue<framework::LoDTensor>("Anchors", inputs, *scope);
80
    variances_ =
81
        OpParam::GetVarValue<framework::LoDTensor>("Variances", inputs, *scope);
82 83

    rpn_rois_ =
84
        OpParam::GetVarValue<framework::LoDTensor>("RpnRois", outputs, *scope);
85
    rpn_probs_ = OpParam::GetVarValue<framework::LoDTensor>("RpnRoiProbs",
86
                                                            outputs, *scope);
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

    pre_nms_topn_ = OpParam::GetAttr<int>("pre_nms_topN", attrs);
    post_nms_topn_ = OpParam::GetAttr<int>("post_nms_topN", attrs);
    nms_thresh_ = OpParam::GetAttr<float>("nms_thresh", attrs);
    min_size_ = OpParam::GetAttr<float>("min_size", attrs);
    eta_ = OpParam::GetAttr<float>("eta", attrs);
  }

 public:
  framework::Tensor *scores_;
  framework::Tensor *bbox_deltas_;
  framework::Tensor *im_info_;
  framework::Tensor *anchors_;
  framework::Tensor *variances_;

102 103
  std::shared_ptr<Tensor> score_index_;

104 105 106 107 108 109 110 111
  framework::LoDTensor *rpn_rois_;
  framework::LoDTensor *rpn_probs_;

  int pre_nms_topn_;
  int post_nms_topn_;
  float nms_thresh_;
  float min_size_;
  float eta_;
112 113 114 115
#ifdef PADDLE_MOBILE_FPGA
  std::shared_ptr<Tensor> float_score, float_bbox;
  fpga::BypassArgs score_arg, bbox_arg;
#endif
116 117 118 119 120 121 122 123 124 125
};

DECLARE_KERNEL(Proposal, ProposalParam);
#endif

#ifdef PSROI_POOL_OP
template <typename Dtype>
class PSRoiPoolParam : public OpParam {
 public:
  PSRoiPoolParam(const VariableNameMap &inputs, const VariableNameMap &outputs,
126 127 128
                 const AttributeMap &attrs, Scope *scope)
      : OpParam(inputs, outputs, attrs, scope) {
    input_x_ = OpParam::GetVarValue<framework::LoDTensor>("X", inputs, *scope);
129
    input_rois_ =
130 131 132
        OpParam::GetVarValue<framework::LoDTensor>("ROIs", inputs, *scope);
    output_ =
        OpParam::GetVarValue<framework::LoDTensor>("Out", outputs, *scope);
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147

    output_channels_ = OpParam::GetAttr<int>("output_channels", attrs);
    pooled_height_ = OpParam::GetAttr<int>("pooled_height", attrs);
    pooled_width_ = OpParam::GetAttr<int>("pooled_width", attrs);
    spatial_scale_ = OpParam::GetAttr<float>("spatial_scale", attrs);
  }

 public:
  framework::Tensor *input_x_;
  framework::LoDTensor *input_rois_;
  framework::Tensor *output_;
  int output_channels_;
  int pooled_height_;
  int pooled_width_;
  float spatial_scale_;
148 149 150 151
#ifdef PADDLE_MOBILE_FPGA
  std::shared_ptr<Tensor> float_input, float_output;
  fpga::BypassArgs input_arg, output_arg;
#endif
152 153 154 155 156
};

DECLARE_KERNEL(PSRoiPool, PSRoiPoolParam);
#endif

157 158 159 160
#ifdef ROIALIGN_POOL_OP
template <typename Dtype>
class RoiAlignPoolParam : public OpParam {
 public:
J
jameswu2014 已提交
161 162 163 164
  RoiAlignPoolParam(const VariableNameMap &inputs,
                    const VariableNameMap &outputs, const AttributeMap &attrs,
                    Scope *scope)
      : OpParam(inputs, outputs, attrs, scope) {
165 166 167
    input_x_ = OpParam::GetVarValue<framework::LoDTensor>("X", inputs, *scope);
    input_rois_ =
        OpParam::GetVarValue<framework::LoDTensor>("ROIs", inputs, *scope);
J
jameswu2014 已提交
168 169
    output_ =
        OpParam::GetVarValue<framework::LoDTensor>("Out", outputs, *scope);
170 171 172 173

    pooled_height_ = OpParam::GetAttr<int>("pooled_height", attrs);
    pooled_width_ = OpParam::GetAttr<int>("pooled_width", attrs);
    spatial_scale_ = OpParam::GetAttr<float>("spatial_scale", attrs);
J
jameswu2014 已提交
174
    sampling_ratio_ = OpParam::GetAttr<float>("sampling_ratio", attrs);
175 176 177 178 179 180 181 182 183 184 185
  }

 public:
  framework::Tensor *input_x_;
  framework::LoDTensor *input_rois_;
  framework::Tensor *output_;
  int pooled_height_;
  int pooled_width_;
  float spatial_scale_;
  int sampling_ratio_;
#ifdef PADDLE_MOBILE_FPGA
J
jameswu2014 已提交
186 187
  std::shared_ptr<Tensor> float_input, float_output;
  fpga::BypassArgs input_arg, output_arg;
188 189 190 191 192 193
#endif
};

DECLARE_KERNEL(RoiAlignPool, RoiAlignPoolParam);
#endif

H
hjchen2 已提交
194 195 196 197 198 199
#ifdef ROI_PERSPECTIVE_OP
template <typename Dtype>
class RoiPerspectiveParam : public OpParam {
 public:
  RoiPerspectiveParam(const VariableNameMap &inputs,
                      const VariableNameMap &outputs, const AttributeMap &attrs,
200 201 202
                      Scope *scope)
      : OpParam(inputs, outputs, attrs, scope) {
    input_x_ = OpParam::GetVarValue<framework::LoDTensor>("X", inputs, *scope);
H
hjchen2 已提交
203
    input_rois_ =
204 205 206
        OpParam::GetVarValue<framework::LoDTensor>("ROIs", inputs, *scope);
    output_ =
        OpParam::GetVarValue<framework::LoDTensor>("Out", outputs, *scope);
H
hjchen2 已提交
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225

    spatial_scale_ = OpParam::GetAttr<float>("spatial_scale", attrs);
    transformed_height_ = OpParam::GetAttr<int>("transformed_height", attrs);
    transformed_width_ = OpParam::GetAttr<int>("transformed_width", attrs);
  }

 public:
  framework::Tensor *input_x_;
  framework::LoDTensor *input_rois_;
  framework::Tensor *output_;

  float spatial_scale_;
  int transformed_height_;
  int transformed_width_;
};

DECLARE_KERNEL(RoiPerspective, RoiPerspectiveParam);
#endif

226 227
}  // namespace operators
}  // namespace paddle_mobile