density_prior_box.cc 3.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 25 26 27 28 29
// 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::graph::GraphGlobalMem;
using anakin::AK_FLOAT;
using anakin::saber::NV;
using anakin::saber::Shape;
using anakin::PTuple;

namespace paddle {
namespace inference {
namespace anakin {

30 31
void DensityPriorBoxOpConverter::operator()(const framework::proto::OpDesc& op,
                                            const framework::Scope& scope,
32 33 34 35 36 37 38 39 40 41 42 43 44
                                            bool test_mode) {
  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();

  auto op_name = op_desc.Type() + ":" + op_desc.Output("Boxes").front();

  auto fixed_sizes =
      boost::get<std::vector<float>>(op_desc.GetAttr("fixed_sizes"));
  auto fixed_ratios =
      boost::get<std::vector<float>>(op_desc.GetAttr("fixed_ratios"));
  auto densities = boost::get<std::vector<int>>(op_desc.GetAttr("densities"));
45 46 47 48
  std::vector<float> dens;
  for (auto& ele : densities) {
    dens.push_back(static_cast<float>(ele));
  }
49 50

  // lack flip
51
  // auto clip = boost::get<bool>(op_desc.GetAttr("clip"));
52
  auto variances = boost::get<std::vector<float>>(op_desc.GetAttr("variances"));
53 54 55
  for (auto& ele : variances) {
    LOG(INFO) << ele;
  }
56 57 58 59 60

  // 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"));
61 62 63 64 65
  PTuple<std::string> t_order;
  t_order.push_back("MIN");
  t_order.push_back("COM");
  t_order.push_back("MAX");

66 67 68 69 70 71
  std::vector<float> temp_v = {};

  engine_->AddOp(op_name, "PriorBox", {input_name, image_name}, {output_name});
  engine_->AddOpAttr<PTuple<float>>(op_name, "min_size", temp_v);
  engine_->AddOpAttr<PTuple<float>>(op_name, "max_size", temp_v);
  engine_->AddOpAttr<PTuple<float>>(op_name, "aspect_ratio", temp_v);
72 73 74 75 76
  engine_->AddOpAttr<PTuple<float>>(op_name, "fixed_size", fixed_sizes);
  engine_->AddOpAttr<PTuple<float>>(op_name, "fixed_ratio", fixed_ratios);
  engine_->AddOpAttr<PTuple<float>>(op_name, "density", dens);
  engine_->AddOpAttr(op_name, "is_flip", static_cast<bool>(false));
  engine_->AddOpAttr(op_name, "is_clip", static_cast<bool>(false));
77 78 79 80 81 82
  engine_->AddOpAttr<PTuple<float>>(op_name, "variance", variances);
  engine_->AddOpAttr(op_name, "img_h", static_cast<int>(0));
  engine_->AddOpAttr(op_name, "img_w", static_cast<int>(0));
  engine_->AddOpAttr(op_name, "step_h", step_h);
  engine_->AddOpAttr(op_name, "step_w", step_w);
  engine_->AddOpAttr(op_name, "offset", offset);
83
  engine_->AddOpAttr<PTuple<std::string>>(op_name, "order", t_order);
84 85 86 87 88 89 90
}

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

REGISTER_ANAKIN_OP_CONVERTER(density_prior_box, DensityPriorBoxOpConverter);