Files
FastDeploy/fastdeploy/vision/detection/contrib/rknpu2/utils.cc
Zheng_Bicheng 188dcedc02 [RKNN2] Fix bugs (#851)
* 修复picodet格式

* * 修正错误文档
* 修复rknpu2 backend后端的部分错误
* 更新pphumanseg example格式

* * 更新pphumanseg example格式

* * 更新picodet example格式

* * 更新scrfd example格式

* * 更新ppseg rknpu2 python example中的错误

* * 修复代码格式问题

* * 修复代码格式问题

* * 修复代码格式问题

* * 修复代码格式问题

* * 修复代码格式问题

* * 修复代码格式问题

Co-authored-by: Jason <jiangjiajun@baidu.com>
2022-12-12 15:37:31 +08:00

93 lines
3.2 KiB
C++

// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. //NOLINT
//
// 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 "fastdeploy/vision/detection/contrib/rknpu2/utils.h"
namespace fastdeploy {
namespace vision {
namespace detection {
float Clamp(float val, int min, int max) {
return val > min ? (val < max ? val : max) : min;
}
float Sigmoid(float x) { return 1.0 / (1.0 + expf(-x)); }
float UnSigmoid(float y) { return -1.0 * logf((1.0 / y) - 1.0); }
inline int32_t __clip(float val, float min, float max) {
float f = val <= min ? min : (val >= max ? max : val);
return f;
}
int8_t QntF32ToAffine(float f32, int32_t zp, float scale) {
float dst_val = (f32 / scale) + zp;
int8_t res = (int8_t)__clip(dst_val, -128, 127);
return res;
}
float DeqntAffineToF32(int8_t qnt, int32_t zp, float scale) {
return ((float)qnt - (float)zp) * scale;
}
static float CalculateOverlap(float xmin0, float ymin0, float xmax0,
float ymax0, float xmin1, float ymin1,
float xmax1, float ymax1) {
float w = fmax(0.f, fmin(xmax0, xmax1) - fmax(xmin0, xmin1) + 1.0);
float h = fmax(0.f, fmin(ymax0, ymax1) - fmax(ymin0, ymin1) + 1.0);
float i = w * h;
float u = (xmax0 - xmin0 + 1.0) * (ymax0 - ymin0 + 1.0) +
(xmax1 - xmin1 + 1.0) * (ymax1 - ymin1 + 1.0) - i;
return u <= 0.f ? 0.f : (i / u);
}
int NMS(int valid_count, std::vector<float>& output_locations,
std::vector<int>& class_id, std::vector<int>& order, float threshold,
bool class_agnostic) {
// printf("class_agnostic: %d\n", class_agnostic);
for (int i = 0; i < valid_count; ++i) {
if (order[i] == -1) {
continue;
}
int n = order[i];
for (int j = i + 1; j < valid_count; ++j) {
int m = order[j];
if (m == -1) {
continue;
}
if (!class_agnostic && class_id[n] != class_id[m]) {
continue;
}
float xmin0 = output_locations[n * 4 + 0];
float ymin0 = output_locations[n * 4 + 1];
float xmax0 = output_locations[n * 4 + 0] + output_locations[n * 4 + 2];
float ymax0 = output_locations[n * 4 + 1] + output_locations[n * 4 + 3];
float xmin1 = output_locations[m * 4 + 0];
float ymin1 = output_locations[m * 4 + 1];
float xmax1 = output_locations[m * 4 + 0] + output_locations[m * 4 + 2];
float ymax1 = output_locations[m * 4 + 1] + output_locations[m * 4 + 3];
float iou = CalculateOverlap(xmin0, ymin0, xmax0, ymax0, xmin1, ymin1,
xmax1, ymax1);
if (iou > threshold) {
order[j] = -1;
}
}
}
return 0;
}
} // namespace detection
} // namespace vision
} // namespace fastdeploy