yolo_box.cc 6.2 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// 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.

15 16
#include "lite/backends/arm/math/yolo_box.h"
#include "lite/backends/arm/math/funcs.h"
Y
Yan Chunwei 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

namespace paddle {
namespace lite {
namespace arm {
namespace math {

namespace {
inline float sigmoid(float x) { return 1.f / (1.f + expf(-x)); }

inline void get_yolo_box(float* box,
                         const float* x,
                         const int* anchors,
                         int i,
                         int j,
                         int an_idx,
                         int grid_size,
                         int input_size,
                         int index,
                         int stride,
                         int img_height,
37 38 39 40 41 42
                         int img_width,
                         float scale,
                         float bias) {
  box[0] = (i + sigmoid(x[index]) * scale + bias) * img_width / grid_size;
  box[1] =
      (j + sigmoid(x[index + stride]) * scale + bias) * img_height / grid_size;
Y
Yan Chunwei 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
  box[2] = std::exp(x[index + 2 * stride]) * anchors[2 * an_idx] * img_width /
           input_size;
  box[3] = std::exp(x[index + 3 * stride]) * anchors[2 * an_idx + 1] *
           img_height / input_size;
}

inline int get_entry_index(int batch,
                           int an_idx,
                           int hw_idx,
                           int an_num,
                           int an_stride,
                           int stride,
                           int entry) {
  return (batch * an_num + an_idx) * an_stride + entry * stride + hw_idx;
}

inline void calc_detection_box(float* boxes,
                               float* box,
                               const int box_idx,
                               const int img_height,
63 64
                               const int img_width,
                               bool clip_bbox) {
Y
Yan Chunwei 已提交
65 66 67 68 69
  boxes[box_idx] = box[0] - box[2] / 2;
  boxes[box_idx + 1] = box[1] - box[3] / 2;
  boxes[box_idx + 2] = box[0] + box[2] / 2;
  boxes[box_idx + 3] = box[1] + box[3] / 2;

70 71 72 73 74 75 76 77 78 79 80 81
  if (clip_bbox) {
    boxes[box_idx] =
        boxes[box_idx] > 0 ? boxes[box_idx] : static_cast<float>(0);
    boxes[box_idx + 1] =
        boxes[box_idx + 1] > 0 ? boxes[box_idx + 1] : static_cast<float>(0);
    boxes[box_idx + 2] = boxes[box_idx + 2] < img_width - 1
                             ? boxes[box_idx + 2]
                             : static_cast<float>(img_width - 1);
    boxes[box_idx + 3] = boxes[box_idx + 3] < img_height - 1
                             ? boxes[box_idx + 3]
                             : static_cast<float>(img_height - 1);
  }
Y
Yan Chunwei 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
}

inline void calc_label_score(float* scores,
                             const float* input,
                             const int label_idx,
                             const int score_idx,
                             const int class_num,
                             const float conf,
                             const int stride) {
  for (int i = 0; i < class_num; i++) {
    scores[score_idx + i] = conf * sigmoid(input[label_idx + i * stride]);
  }
}
}  // namespace

void yolobox(lite::Tensor* X,
             lite::Tensor* ImgSize,
             lite::Tensor* Boxes,
             lite::Tensor* Scores,
             std::vector<int> anchors,
             int class_num,
             float conf_thresh,
104 105 106 107
             int downsample_ratio,
             bool clip_bbox,
             float scale,
             float bias) {
Y
Yan Chunwei 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120
  const int n = X->dims()[0];
  const int h = X->dims()[2];
  const int w = X->dims()[3];
  const int b_num = Boxes->dims()[1];
  const int an_num = anchors.size() / 2;
  int X_size = downsample_ratio * h;

  const int stride = h * w;
  const int an_stride = (class_num + 5) * stride;

  auto anchors_data = anchors.data();

  const float* X_data = X->data<float>();
J
juncaipeng 已提交
121
  int* ImgSize_data = ImgSize->mutable_data<int>();
Y
Yan Chunwei 已提交
122 123

  float* Boxes_data = Boxes->mutable_data<float>();
124
  memset(Boxes_data, 0, Boxes->numel() * sizeof(float));
Y
Yan Chunwei 已提交
125 126

  float* Scores_data = Scores->mutable_data<float>();
127
  memset(Scores_data, 0, Scores->numel() * sizeof(float));
Y
Yan Chunwei 已提交
128 129 130

  float box[4];
  for (int i = 0; i < n; i++) {
J
juncaipeng 已提交
131 132
    int img_height = ImgSize_data[2 * i];
    int img_width = ImgSize_data[2 * i + 1];
Y
Yan Chunwei 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156

    for (int j = 0; j < an_num; j++) {
      for (int k = 0; k < h; k++) {
        for (int l = 0; l < w; l++) {
          int obj_idx =
              get_entry_index(i, j, k * w + l, an_num, an_stride, stride, 4);
          float conf = sigmoid(X_data[obj_idx]);
          if (conf < conf_thresh) {
            continue;
          }

          int box_idx =
              get_entry_index(i, j, k * w + l, an_num, an_stride, stride, 0);
          get_yolo_box(box,
                       X_data,
                       anchors_data,
                       l,
                       k,
                       j,
                       h,
                       X_size,
                       box_idx,
                       stride,
                       img_height,
157 158 159
                       img_width,
                       scale,
                       bias);
Y
Yan Chunwei 已提交
160
          box_idx = (i * b_num + j * stride + k * w + l) * 4;
161 162
          calc_detection_box(
              Boxes_data, box, box_idx, img_height, img_width, clip_bbox);
Y
Yan Chunwei 已提交
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183

          int label_idx =
              get_entry_index(i, j, k * w + l, an_num, an_stride, stride, 5);
          int score_idx = (i * b_num + j * stride + k * w + l) * class_num;
          calc_label_score(Scores_data,
                           X_data,
                           label_idx,
                           score_idx,
                           class_num,
                           conf,
                           stride);
        }
      }
    }
  }
}

}  // namespace math
}  // namespace arm
}  // namespace lite
}  // namespace paddle