detection_ops.cpp 4.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/* 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. */

#include "operators/detection_ops.h"
#include <vector>

namespace paddle_mobile {
namespace operators {

#ifdef ANCHOR_GENERATOR_OP
template <typename DeviceType, typename T>
void AnchorGeneratorOp<DeviceType, T>::InferShape() const {
  const auto &input_dims = this->param_.input_->dims();
Z
zhangyang0701 已提交
25
  // DLOG << "AnchorGenerator input dim =" << input_dims.size();
26 27 28 29 30 31 32 33 34 35 36 37 38 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
  PADDLE_MOBILE_ENFORCE(input_dims.size() == 4, "The layout of input is NCHW.");
  const auto &anchor_sizes = this->param_.anchor_sizes_;
  const auto &aspect_ratios = this->param_.aspect_ratios_;

  size_t num_anchors = aspect_ratios.size() * anchor_sizes.size();
  std::vector<int64_t> dim_vec(4);
  dim_vec[0] = input_dims[2];
  dim_vec[1] = input_dims[3];
  dim_vec[2] = num_anchors;
  dim_vec[3] = 4;

  this->param_.output_anchors_->Resize(framework::make_ddim(dim_vec));
  this->param_.output_variances_->Resize(framework::make_ddim(dim_vec));
}
#endif

#ifdef PROPOSAL_OP
template <typename DeviceType, typename T>
void ProposalOp<DeviceType, T>::InferShape() const {
  this->param_.rpn_rois_->Resize(framework::make_ddim({-1, 4}));
  this->param_.rpn_probs_->Resize(framework::make_ddim({-1, 1}));
}
#endif

#ifdef PSROI_POOL_OP
template <typename DeviceType, typename T>
void PSRoiPoolOp<DeviceType, T>::InferShape() const {
  const auto &rois_dims = this->param_.input_rois_->dims();
  const int pooled_height = this->param_.pooled_height_;
  const int pooled_width = this->param_.pooled_width_;
  const int output_channels = this->param_.output_channels_;

  auto out_dims = this->param_.input_x_->dims();
  out_dims[0] = rois_dims[0];
  out_dims[1] =
      output_channels;  // input_dims[1] / (pooled_height * pooled_width);
  out_dims[2] = pooled_height;
  out_dims[3] = pooled_width;
  this->param_.output_->Resize(out_dims);
}
#endif

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
#ifdef ROIALIGN_POOL_OP
template <typename DeviceType, typename T>
void RoiAlignPoolOp<DeviceType, T>::InferShape() const {
  const auto &rois_dims = this->param_.input_rois_->dims();
  const int pooled_height = this->param_.pooled_height_;
  const int pooled_width = this->param_.pooled_width_;

  auto out_dims = this->param_.input_x_->dims();
  out_dims[0] = rois_dims[0];
 // out_dims[1] =
 //     output_channels;  // input_dims[1] / (pooled_height * pooled_width);
  out_dims[2] = pooled_height;
  out_dims[3] = pooled_width;
  this->param_.output_->Resize(out_dims);
}
#endif


H
hjchen2 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
#ifdef ROI_PERSPECTIVE_OP
template <typename DeviceType, typename T>
void RoiPerspectiveOp<DeviceType, T>::InferShape() const {
  const auto &input_dims = this->param_.input_x_->dims();
  const auto &rois_dims = this->param_.input_rois_->dims();
  const int transformed_height = this->param_.transformed_height_;
  const int transformed_width = this->param_.transformed_width_;
  std::vector<int64_t> out_dims_v({rois_dims[0],   // num_rois
                                   input_dims[1],  // channels
                                   static_cast<int64_t>(transformed_height),
                                   static_cast<int64_t>(transformed_width)});
  auto out_dims = framework::make_ddim(out_dims_v);
  this->param_.output_->Resize(out_dims);
}
#endif

102 103 104 105 106 107 108 109 110 111 112 113 114 115
}  // namespace operators
}  // namespace paddle_mobile

namespace ops = paddle_mobile::operators;
#ifdef PADDLE_MOBILE_CPU
#ifdef ANCHOR_GENERATOR_OP
REGISTER_OPERATOR_CPU(anchor_generator, ops::AnchorGeneratorOp);
#endif
#ifdef PROPOSAL_OP
REGISTER_OPERATOR_CPU(generate_proposals, ops::ProposalOp);
#endif
#ifdef PSROI_POOL_OP
REGISTER_OPERATOR_CPU(psroi_pool, ops::PSRoiPoolOp);
#endif
H
hjchen2 已提交
116 117 118
#ifdef ROI_PERSPECTIVE_OP
REGISTER_OPERATOR_CPU(roi_perspective_transform, ops::RoiPerspectiveOp);
#endif
119
#endif
Z
zhangyang0701 已提交
120 121 122 123 124 125 126 127 128 129 130

#ifdef PADDLE_MOBILE_FPGA
#ifdef ANCHOR_GENERATOR_OP
REGISTER_OPERATOR_FPGA(anchor_generator, ops::AnchorGeneratorOp);
#endif
#ifdef PROPOSAL_OP
REGISTER_OPERATOR_FPGA(generate_proposals, ops::ProposalOp);
#endif
#ifdef PSROI_POOL_OP
REGISTER_OPERATOR_FPGA(psroi_pool, ops::PSRoiPoolOp);
#endif
131 132 133 134
#ifdef ROIALIGN_POOL_OP
REGISTER_OPERATOR_FPGA(roialign_pool, ops::RoiAlignPoolOp);
#endif

Z
zhangyang0701 已提交
135
#endif