Align fastdeploy prediction precision with yolov5 (#11)

* Align fastdeploy prediction precision with yolov5

* Change name of Sort function to SortDetectionResult

* Add stride max_wh is_mini_pad property in __init__.py and unify format of getting image width and length

* Change mergesort.cc to sort_det_res.cc
This commit is contained in:
huangjianhui
2022-07-08 15:52:22 +08:00
committed by GitHub
parent 2b51f0efbc
commit 7d13491879
11 changed files with 151 additions and 65 deletions

View File

@@ -22,13 +22,13 @@ namespace utils {
// The implementation refers to
// https://github.com/PaddlePaddle/PaddleDetection/blob/release/2.4/deploy/cpp/src/utils.cc
void NMS(DetectionResult* result, float iou_threshold) {
result->Sort();
utils::SortDetectionResult(result);
std::vector<float> area_of_boxes(result->boxes.size());
std::vector<int> suppressed(result->boxes.size(), 0);
for (size_t i = 0; i < result->boxes.size(); ++i) {
area_of_boxes[i] = (result->boxes[i][2] - result->boxes[i][0] + 1) *
(result->boxes[i][3] - result->boxes[i][1] + 1);
area_of_boxes[i] = (result->boxes[i][2] - result->boxes[i][0]) *
(result->boxes[i][3] - result->boxes[i][1]);
}
for (size_t i = 0; i < result->boxes.size(); ++i) {
@@ -43,12 +43,11 @@ void NMS(DetectionResult* result, float iou_threshold) {
float ymin = std::max(result->boxes[i][1], result->boxes[j][1]);
float xmax = std::min(result->boxes[i][2], result->boxes[j][2]);
float ymax = std::min(result->boxes[i][3], result->boxes[j][3]);
float overlap_w = std::max(0.0f, xmax - xmin + 1);
float overlap_h = std::max(0.0f, ymax - ymin + 1);
float overlap_w = std::max(0.0f, xmax - xmin);
float overlap_h = std::max(0.0f, ymax - ymin);
float overlap_area = overlap_w * overlap_h;
float overlap_ratio =
overlap_area /
(area_of_boxes[i] + area_of_boxes[j] - overlap_area + 1e-06);
overlap_area / (area_of_boxes[i] + area_of_boxes[j] - overlap_area);
if (overlap_ratio > iou_threshold) {
suppressed[j] = 1;
}
@@ -67,6 +66,6 @@ void NMS(DetectionResult* result, float iou_threshold) {
}
}
} // namespace utils
} // namespace vision
} // namespace fastdeploy
} // namespace utils
} // namespace vision
} // namespace fastdeploy

View File

@@ -0,0 +1,77 @@
// Copyright (c) 2022 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 "fastdeploy/vision/utils/utils.h"
namespace fastdeploy {
namespace vision {
namespace utils {
void Merge(DetectionResult* result, size_t low, size_t mid, size_t high) {
std::vector<std::array<float, 4>>& boxes = result->boxes;
std::vector<float>& scores = result->scores;
std::vector<int32_t>& label_ids = result->label_ids;
std::vector<std::array<float, 4>> temp_boxes(boxes);
std::vector<float> temp_scores(scores);
std::vector<int32_t> temp_label_ids(label_ids);
size_t i = low;
size_t j = mid + 1;
size_t k = i;
for (; i <= mid && j <= high; k++) {
if (temp_scores[i] >= temp_scores[j]) {
scores[k] = temp_scores[i];
label_ids[k] = temp_label_ids[i];
boxes[k] = temp_boxes[i];
i++;
} else {
scores[k] = temp_scores[j];
label_ids[k] = temp_label_ids[j];
boxes[k] = temp_boxes[j];
j++;
}
}
while (i <= mid) {
scores[k] = temp_scores[i];
label_ids[k] = temp_label_ids[i];
boxes[k] = temp_boxes[i];
k++;
i++;
}
while (j <= high) {
scores[k] = temp_scores[j];
label_ids[k] = temp_label_ids[j];
boxes[k] = temp_boxes[j];
k++;
j++;
}
}
void MergeSort(DetectionResult* result, size_t low, size_t high) {
if (low < high) {
size_t mid = (high - low) / 2 + low;
MergeSort(result, low, mid);
MergeSort(result, mid + 1, high);
Merge(result, low, mid, high);
}
}
void SortDetectionResult(DetectionResult* result) {
size_t low = 0;
size_t high = result->scores.size() - 1;
MergeSort(result, low, high);
}
} // namespace utils
} // namespace vision
} // namespace fastdeploy

View File

@@ -14,11 +14,11 @@
#pragma once
#include <set>
#include <vector>
#include "fastdeploy/core/fd_tensor.h"
#include "fastdeploy/utils/utils.h"
#include "fastdeploy/vision/common/result.h"
#include <set>
#include <vector>
namespace fastdeploy {
namespace vision {
@@ -53,6 +53,9 @@ std::vector<int32_t> TopKIndices(const T* array, int array_size, int topk) {
void NMS(DetectionResult* output, float iou_threshold = 0.5);
} // namespace utils
} // namespace vision
} // namespace fastdeploy
// MergeSort
void SortDetectionResult(DetectionResult* output);
} // namespace utils
} // namespace vision
} // namespace fastdeploy