density_prior_box.cc 4.7 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 25
// 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 "paddle/fluid/inference/anakin/convert/density_prior_box.h"
#include <algorithm>
#include <map>
#include <vector>

using anakin::PTuple;

namespace paddle {
namespace inference {
namespace anakin {

26 27
template <typename TargetT, ::anakin::Precision PrecisionT>
void DensityPriorBoxOpConverter<TargetT, PrecisionT>::operator()(
28 29
    const framework::proto::OpDesc& op, const framework::BlockDesc& block_desc,
    const framework::Scope& scope, bool test_mode) {
30 31 32 33
  framework::OpDesc op_desc(op, nullptr);
  auto input_name = op_desc.Input("Input").front();
  auto image_name = op_desc.Input("Image").front();
  auto output_name = op_desc.Output("Boxes").front();
N
nhzlx 已提交
34 35
  auto op_type = op_desc.Type();
  auto op_name = op_type + ":" + op_desc.Output("Boxes").front();
36

N
nhzlx 已提交
37 38 39 40
  // only for density_prior_box
  std::vector<float> fixed_sizes = {};
  std::vector<float> fixed_ratios = {};
  std::vector<int> densities = {};
41

N
nhzlx 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
  std::vector<float> min_sizes = {};
  std::vector<float> max_sizes = {};
  std::vector<float> aspect_ratios = {};
  bool is_clip = false;
  bool is_flip = false;

  if (op_type == "density_prior_box") {
    fixed_sizes =
        boost::get<std::vector<float>>(op_desc.GetAttr("fixed_sizes"));
    fixed_ratios =
        boost::get<std::vector<float>>(op_desc.GetAttr("fixed_ratios"));
    densities = boost::get<std::vector<int>>(op_desc.GetAttr("densities"));
    is_clip = boost::get<bool>(op_desc.GetAttr("clip"));
  } else if (op_type == "prior_box") {
    min_sizes = boost::get<std::vector<float>>(op_desc.GetAttr("min_sizes"));
    max_sizes = boost::get<std::vector<float>>(op_desc.GetAttr("max_sizes"));
    aspect_ratios =
        boost::get<std::vector<float>>(op_desc.GetAttr("aspect_ratios"));
    is_clip = boost::get<bool>(op_desc.GetAttr("clip"));
    is_flip = boost::get<bool>(op_desc.GetAttr("flip"));
  }
63 64 65 66
  std::vector<float> dens;
  for (auto& ele : densities) {
    dens.push_back(static_cast<float>(ele));
  }
67 68 69 70 71 72 73

  auto variances = boost::get<std::vector<float>>(op_desc.GetAttr("variances"));

  // lack img_h, img_w
  auto step_h = boost::get<float>(op_desc.GetAttr("step_h"));
  auto step_w = boost::get<float>(op_desc.GetAttr("step_w"));
  auto offset = boost::get<float>(op_desc.GetAttr("offset"));
74 75 76 77 78
  PTuple<std::string> t_order;
  t_order.push_back("MIN");
  t_order.push_back("COM");
  t_order.push_back("MAX");

79 80
  std::vector<float> temp_v = {};

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
  this->engine_->AddOp(op_name, "PriorBox", {input_name, image_name},
                       {output_name});
  this->engine_->template AddOpAttr<PTuple<float>>(op_name, "min_size",
                                                   min_sizes);
  this->engine_->template AddOpAttr<PTuple<float>>(op_name, "max_size",
                                                   max_sizes);
  this->engine_->template AddOpAttr<PTuple<float>>(op_name, "aspect_ratio",
                                                   aspect_ratios);
  this->engine_->template AddOpAttr<PTuple<float>>(op_name, "fixed_size",
                                                   fixed_sizes);
  this->engine_->template AddOpAttr<PTuple<float>>(op_name, "fixed_ratio",
                                                   fixed_ratios);
  this->engine_->template AddOpAttr<PTuple<float>>(op_name, "density", dens);
  this->engine_->AddOpAttr(op_name, "is_flip", is_flip);
  this->engine_->AddOpAttr(op_name, "is_clip", is_clip);
  this->engine_->template AddOpAttr<PTuple<float>>(op_name, "variance",
                                                   variances);
  this->engine_->AddOpAttr(op_name, "img_h", static_cast<int>(0));
  this->engine_->AddOpAttr(op_name, "img_w", static_cast<int>(0));
  this->engine_->AddOpAttr(op_name, "step_h", step_h);
  this->engine_->AddOpAttr(op_name, "step_w", step_w);
  this->engine_->AddOpAttr(op_name, "offset", offset);
  this->engine_->template AddOpAttr<PTuple<std::string>>(op_name, "order",
                                                         t_order);
105 106 107 108 109 110 111
}

}  // namespace anakin
}  // namespace inference
}  // namespace paddle

REGISTER_ANAKIN_OP_CONVERTER(density_prior_box, DensityPriorBoxOpConverter);
N
nhzlx 已提交
112
REGISTER_ANAKIN_OP_CONVERTER(prior_box, DensityPriorBoxOpConverter);