prior_box_compute.cc 5.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Copyright (c) 2019 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 <string>
#include <vector>

C
chonwhite 已提交
18 19 20
#include "lite/backends/fpga/KD/debugger.hpp"
#include "lite/kernels/fpga/prior_box_compute.h"

21 22 23 24 25 26 27
namespace paddle {
namespace lite {
namespace kernels {
namespace fpga {

using float16 = zynqmp::float16;

C
chonwhite 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
inline void ExpandAspectRatios(const std::vector<float>& input_aspect_ratior,
                               bool flip,
                               std::vector<float>* output_aspect_ratior) {
  constexpr float epsilon = 1e-6;
  output_aspect_ratior->clear();
  output_aspect_ratior->push_back(1.0f);
  for (size_t i = 0; i < input_aspect_ratior.size(); ++i) {
    float ar = input_aspect_ratior[i];
    bool already_exist = false;
    for (size_t j = 0; j < output_aspect_ratior->size(); ++j) {
      if (fabs(ar - output_aspect_ratior->at(j)) < epsilon) {
        already_exist = true;
        break;
      }
    }
    if (!already_exist) {
      output_aspect_ratior->push_back(ar);
      if (flip) {
        output_aspect_ratior->push_back(1.0f / ar);
      }
    }
  }
}

52 53
void PriorBoxCompute::PrepareForRun() {
  auto& param = this->Param<param_t>();
C
chonwhite 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
  bool is_flip = param.flip;
  bool is_clip = param.clip;
  std::vector<float> min_size = param.min_sizes;
  std::vector<float> max_size = param.max_sizes;
  std::vector<float> aspect_ratio = param.aspect_ratios;
  std::vector<float> variance = param.variances_;
  int img_w = param.img_w;
  int img_h = param.img_h;
  float step_w = param.step_w;
  float step_h = param.step_h;
  float offset = param.offset;
  std::vector<float> aspect_ratios_vec;
  ExpandAspectRatios(aspect_ratio, is_flip, &aspect_ratios_vec);
  size_t prior_num = aspect_ratios_vec.size() * min_size.size();
  prior_num += max_size.size();
  std::vector<std::string> order = param.order;
  bool min_max_aspect_ratios_order = param.min_max_aspect_ratios_order;
71

C
chonwhite 已提交
72 73 74 75 76 77 78 79 80
  int win1 = param.input->dims()[3];
  int hin1 = param.input->dims()[2];

  DDim shape_out({hin1, win1, prior_num, 4});
  param.boxes->Resize(shape_out);
  param.variances->Resize(shape_out);

  param.boxes->mutable_data<float>();
  param.variances->mutable_data<float>();
81 82 83 84 85 86 87 88 89 90
  // ====================================================
  zynqmp::PriorBoxParam& priobox_param = pe_.param();
  priobox_param.input = param.input->ZynqTensor();
  priobox_param.image = param.image->ZynqTensor();
  priobox_param.outputBoxes = param.boxes->ZynqTensor();
  priobox_param.outputVariances = param.variances->ZynqTensor();
  priobox_param.minSizes = param.min_sizes;
  priobox_param.maxSizes = param.max_sizes;
  priobox_param.aspectRatios = param.aspect_ratios;
  priobox_param.variances = param.variances_;
C
chonwhite 已提交
91
  priobox_param.minMaxAspectRatiosOrder = min_max_aspect_ratios_order;
92 93 94 95 96 97 98 99 100 101
  priobox_param.flip = param.flip;
  priobox_param.clip = param.clip;
  priobox_param.stepW = param.step_w;
  priobox_param.stepH = param.step_h;
  priobox_param.offset = param.offset;

  pe_.init();
  pe_.apply();
}

C
chonwhite 已提交
102 103 104 105 106 107 108 109 110 111
void PriorBoxCompute::Run() {
  pe_.dispatch();
#ifdef FPGA_PRINT_TENSOR
  zynqmp::PriorBoxParam& priobox_param = pe_.param();
  Debugger::get_instance().registerOutput("pb_boxes",
                                          priobox_param.outputBoxes);
  Debugger::get_instance().registerOutput("pb_variances",
                                          priobox_param.outputVariances);
#endif
}
112 113 114 115 116 117 118 119 120 121 122 123

}  // namespace fpga
}  // namespace kernels
}  // namespace lite
}  // namespace paddle

REGISTER_LITE_KERNEL(prior_box,
                     kFPGA,
                     kFP16,
                     kNHWC,
                     paddle::lite::kernels::fpga::PriorBoxCompute,
                     def)
C
chonwhite 已提交
124 125 126 127 128 129 130 131
    .BindInput("Input",
               {LiteType::GetTensorTy(TARGET(kFPGA),
                                      PRECISION(kFP16),
                                      DATALAYOUT(kNHWC))})
    .BindInput("Image",
               {LiteType::GetTensorTy(TARGET(kFPGA),
                                      PRECISION(kFP16),
                                      DATALAYOUT(kNHWC))})
132 133 134
    .BindOutput("Boxes", {LiteType::GetTensorTy(TARGET(kARM))})
    .BindOutput("Variances", {LiteType::GetTensorTy(TARGET(kARM))})
    .Finalize();
C
chonwhite 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150

// REGISTER_LITE_KERNEL(prior_box,
//                      kFPGA,
//                      kFP16,
//                      kNHWC,
//                      paddle::lite::kernels::fpga::PriorBoxCompute,
//                      def)
//     .BindInput("Input", {LiteType::GetTensorTy(TARGET(kFPGA),
//                                       PRECISION(kFP16),
//                                       DATALAYOUT(kNHWC))})
//     .BindInput("Image", {LiteType::GetTensorTy(TARGET(kFPGA),
//                                       PRECISION(kFP16),
//                                       DATALAYOUT(kNHWC))})
//     .BindOutput("Boxes", {LiteType::GetTensorTy(TARGET(kARM))})
//     .BindOutput("Variances", {LiteType::GetTensorTy(TARGET(kARM))})
//     .Finalize();