multiclass_nms_compute.cc 3.4 KB
Newer Older
Y
Yan Chunwei 已提交
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 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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
// 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 "lite/kernels/arm/multiclass_nms_compute.h"
#include <string>
#include "lite/arm/math/funcs.h"

namespace paddle {
namespace lite {
namespace kernels {
namespace arm {

void MulticlassNmsCompute::Run() {
  auto& param = Param<operators::MulticlassNmsParam>();
  // bbox shape : N, M, 4
  // scores shape : N, C, M
  const float* bbox_data = param.bbox_data->data<float>();
  const float* conf_data = param.conf_data->data<float>();

  CHECK_EQ(param.bbox_data->dims().production() % 4, 0);

  std::vector<float> result;
  int N = param.bbox_data->dims()[0];
  int M = param.bbox_data->dims()[1];
  std::vector<int> priors(N, M);
  int class_num = param.conf_data->dims()[1];
  int background_label = param.background_label;
  int keep_top_k = param.keep_top_k;
  int nms_top_k = param.nms_top_k;
  float score_threshold = param.score_threshold;
  float nms_threshold = param.nms_threshold;
  float nms_eta = param.nms_eta;
  bool share_location = param.share_location;

  lite::arm::math::multiclass_nms(bbox_data,
                                  conf_data,
                                  &result,
                                  priors,
                                  class_num,
                                  background_label,
                                  keep_top_k,
                                  nms_top_k,
                                  score_threshold,
                                  nms_threshold,
                                  nms_eta,
                                  share_location);
  lite::LoD* lod = param.out->mutable_lod();
  std::vector<uint64_t> lod_info;
  lod_info.push_back(0);
  std::vector<float> result_corrected;
  int tmp_batch_id;
  uint64_t num = 0;
  for (int i = 0; i < result.size(); ++i) {
    if (i == 0) {
      tmp_batch_id = result[i];
    }
    if (i % 7 == 0) {
      if (result[i] == tmp_batch_id) {
        ++num;
      } else {
        lod_info.push_back(num);
        ++num;
        tmp_batch_id = result[i];
      }
    } else {
      result_corrected.push_back(result[i]);
    }
  }
  lod_info.push_back(num);

  (*lod).push_back(lod_info);
  param.out->Resize({static_cast<int64_t>(result_corrected.size() / 6), 6});
  float* out = param.out->mutable_data<float>();
  std::memcpy(
      out, result_corrected.data(), sizeof(float) * result_corrected.size());
}

}  // namespace arm
}  // namespace kernels
}  // namespace lite
}  // namespace paddle

REGISTER_LITE_KERNEL(multiclass_nms,
                     kARM,
                     kFloat,
                     kNCHW,
                     paddle::lite::kernels::arm::MulticlassNmsCompute,
                     def)
    .BindInput("BBoxes", {LiteType::GetTensorTy(TARGET(kARM))})
    .BindInput("Scores", {LiteType::GetTensorTy(TARGET(kARM))})
    .BindOutput("Out", {LiteType::GetTensorTy(TARGET(kARM))})
    .Finalize();