picodet_postprocess.cc 4.2 KB
Newer Older
G
Guanghua Yu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
//   Copyright (c) 2021 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.
G
Guanghua Yu 已提交
14 15 16
//
// The code is based on:
// https://github.com/RangiLyu/nanodet/blob/main/demo_mnn/nanodet_mnn.cpp
G
Guanghua Yu 已提交
17 18 19 20 21 22

#include "include/picodet_postprocess.h"

namespace PaddleDetection {

float fast_exp(float x) {
G
Guanghua Yu 已提交
23 24 25 26 27 28
  union {
    uint32_t i;
    float f;
  } v{};
  v.i = (1 << 23) * (1.4426950409 * x + 126.93490512f);
  return v.f;
G
Guanghua Yu 已提交
29 30 31 32
}

template <typename _Tp>
int activation_function_softmax(const _Tp *src, _Tp *dst, int length) {
G
Guanghua Yu 已提交
33 34
  const _Tp alpha = *std::max_element(src, src + length);
  _Tp denominator{0};
G
Guanghua Yu 已提交
35

G
Guanghua Yu 已提交
36 37 38 39
  for (int i = 0; i < length; ++i) {
    dst[i] = fast_exp(src[i] - alpha);
    denominator += dst[i];
  }
G
Guanghua Yu 已提交
40

G
Guanghua Yu 已提交
41 42 43
  for (int i = 0; i < length; ++i) {
    dst[i] /= denominator;
  }
G
Guanghua Yu 已提交
44

G
Guanghua Yu 已提交
45
  return 0;
G
Guanghua Yu 已提交
46 47 48
}

// PicoDet decode
G
Guanghua Yu 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62
PaddleDetection::ObjectResult
disPred2Bbox(const float *&dfl_det, int label, float score, int x, int y,
             int stride, std::vector<float> im_shape, int reg_max) {
  float ct_x = (x + 0.5) * stride;
  float ct_y = (y + 0.5) * stride;
  std::vector<float> dis_pred;
  dis_pred.resize(4);
  for (int i = 0; i < 4; i++) {
    float dis = 0;
    float *dis_after_sm = new float[reg_max + 1];
    activation_function_softmax(dfl_det + i * (reg_max + 1), dis_after_sm,
                                reg_max + 1);
    for (int j = 0; j < reg_max + 1; j++) {
      dis += j * dis_after_sm[j];
G
Guanghua Yu 已提交
63
    }
G
Guanghua Yu 已提交
64 65 66 67 68 69 70 71
    dis *= stride;
    dis_pred[i] = dis;
    delete[] dis_after_sm;
  }
  int xmin = (int)(std::max)(ct_x - dis_pred[0], .0f);
  int ymin = (int)(std::max)(ct_y - dis_pred[1], .0f);
  int xmax = (int)(std::min)(ct_x + dis_pred[2], (float)im_shape[0]);
  int ymax = (int)(std::min)(ct_y + dis_pred[3], (float)im_shape[1]);
G
Guanghua Yu 已提交
72

G
Guanghua Yu 已提交
73 74 75 76
  PaddleDetection::ObjectResult result_item;
  result_item.rect = {xmin, ymin, xmax, ymax};
  result_item.class_id = label;
  result_item.confidence = score;
G
Guanghua Yu 已提交
77

G
Guanghua Yu 已提交
78
  return result_item;
G
Guanghua Yu 已提交
79 80
}

G
Guanghua Yu 已提交
81 82 83 84 85 86
void PicoDetPostProcess(std::vector<PaddleDetection::ObjectResult> *results,
                        std::vector<const float *> outs,
                        std::vector<int> fpn_stride,
                        std::vector<float> im_shape,
                        std::vector<float> scale_factor, float score_threshold,
                        float nms_threshold, int num_class, int reg_max) {
G
Guanghua Yu 已提交
87 88 89 90
  std::vector<std::vector<PaddleDetection::ObjectResult>> bbox_results;
  bbox_results.resize(num_class);
  int in_h = im_shape[0], in_w = im_shape[1];
  for (int i = 0; i < fpn_stride.size(); ++i) {
G
Guanghua Yu 已提交
91 92
    int feature_h = ceil((float)in_h / fpn_stride[i]);
    int feature_w = ceil((float)in_w / fpn_stride[i]);
G
Guanghua Yu 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105 106
    for (int idx = 0; idx < feature_h * feature_w; idx++) {
      const float *scores = outs[i] + (idx * num_class);

      int row = idx / feature_w;
      int col = idx % feature_w;
      float score = 0;
      int cur_label = 0;
      for (int label = 0; label < num_class; label++) {
        if (scores[label] > score) {
          score = scores[label];
          cur_label = label;
        }
      }
      if (score > score_threshold) {
G
Guanghua Yu 已提交
107 108 109 110 111
        const float *bbox_pred =
            outs[i + fpn_stride.size()] + (idx * 4 * (reg_max + 1));
        bbox_results[cur_label].push_back(
            disPred2Bbox(bbox_pred, cur_label, score, col, row, fpn_stride[i],
                         im_shape, reg_max));
G
Guanghua Yu 已提交
112 113 114 115 116 117 118
      }
    }
  }
  for (int i = 0; i < (int)bbox_results.size(); i++) {
    PaddleDetection::nms(bbox_results[i], nms_threshold);

    for (auto box : bbox_results[i]) {
G
Guanghua Yu 已提交
119 120 121 122 123
      box.rect[0] = box.rect[0] / scale_factor[1];
      box.rect[2] = box.rect[2] / scale_factor[1];
      box.rect[1] = box.rect[1] / scale_factor[0];
      box.rect[3] = box.rect[3] / scale_factor[0];
      results->push_back(box);
G
Guanghua Yu 已提交
124 125 126 127
    }
  }
}

G
Guanghua Yu 已提交
128
} // namespace PaddleDetection