Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve yolov8 post-processing efficiency. #5658

Open
wants to merge 32 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
7db5444
Add get_gpu_instance() function and Organized the instance class codes.
whyb Apr 17, 2023
e48623d
Move class __ncnn_vulkan_instance_holder declaration from gpu.h to g…
whyb Apr 17, 2023
1042565
Delete empty line changes
whyb Apr 17, 2023
a479422
Reimplement the sleep() and get_current_time() functions using modern…
whyb Apr 28, 2023
6eb3bb7
Fixed build error for __riscv not support c++ 11 thread
whyb Apr 28, 2023
ed932ba
Add NCNN_SIMPLESTL Macro
whyb May 5, 2023
00b9f28
Fix simple stl's compiler build error
whyb May 12, 2023
67ef283
Fix linux-gcc-cpp03-nostdio-nostring-simplestl build error
whyb May 12, 2023
d1e079c
Use u_int64_t type parameters in linux-gcc-cpp03-nostdio-nostring-sim…
whyb May 15, 2023
2ad85f0
change uint64_t&u_int64_t to unsigned long long int
whyb May 15, 2023
cec75bc
Remove include stdint.h and change function sleep() default paramete…
whyb May 16, 2023
85c15a0
apply code-format changes
whyb May 16, 2023
be921fd
Update benchmark.cpp
nihui May 16, 2023
b25ac10
Merge branch 'Tencent:master' into master
whyb May 30, 2023
98af54c
Merge branch 'Tencent:master' into master
whyb May 31, 2023
27ba97b
Merge branch 'Tencent:master' into master
whyb Jun 14, 2023
3110969
Merge branch 'Tencent:master' into master
whyb Jun 26, 2023
a1f84f8
Merge branch 'Tencent:master' into master
whyb Jun 26, 2023
76941c6
Merge branch 'Tencent:master' into master
whyb Aug 7, 2023
6cee97e
Merge branch 'Tencent:master' into master
whyb Oct 10, 2023
56d61d8
Merge branch 'Tencent:master' into master
whyb Oct 12, 2023
9ff751a
Merge branch 'Tencent:master' into master
whyb Oct 27, 2023
aa40554
Merge branch 'Tencent:master' into master
whyb Nov 9, 2023
fb19b98
Merge branch 'Tencent:master' into master
whyb Dec 18, 2023
e56cc3a
Merge branch 'Tencent:master' into master
whyb Dec 25, 2023
fe151a8
Merge branch 'Tencent:master' into master
whyb May 9, 2024
4e91d49
Merge branch 'Tencent:master' into master
whyb Jun 11, 2024
2e9e6c3
Merge branch 'Tencent:master' into master
whyb Jul 3, 2024
173ae25
Merge branch 'Tencent:master' into master
whyb Aug 19, 2024
8e30e72
Merge branch 'Tencent:master' into master
whyb Aug 20, 2024
2880f80
Merge branch 'Tencent:master' into master
whyb Aug 29, 2024
c75882f
Using OpenMP to improve yolov8 post-processing efficiency
whyb Aug 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 44 additions & 27 deletions examples/yolov8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@
#include <opencv2/highgui/highgui.hpp>
#include <float.h>
#include <stdio.h>
#if NCNN_SIMPLEOMP
#include "simpleomp.h"
#else
#include <omp.h>
#endif

#define MAX_STRIDE 32

Expand Down Expand Up @@ -173,35 +178,47 @@ static void parse_yolov8_detections(
std::vector<Object> detections;
cv::Mat output = cv::Mat((int)num_channels, (int)num_anchors, CV_32F, inputs).t();

for (int i = 0; i < num_anchors; i++)
const size_t stride = num_anchors;
const size_t num_threads = omp_get_max_threads();
const size_t chunk_size = stride / num_threads;
#pragma omp parallel shared(detections)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but simpleomp does not support shared clause

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but simpleomp does not support shared clause

What alternatives are there to achieve similar functionality?😭

{
const float* row_ptr = output.row(i).ptr<float>();
const float* bboxes_ptr = row_ptr;
const float* scores_ptr = row_ptr + 4;
const float* max_s_ptr = std::max_element(scores_ptr, scores_ptr + num_labels);
float score = *max_s_ptr;
if (score > confidence_threshold)
const size_t thread_id = omp_get_thread_num();
const size_t start_idx = thread_id * chunk_size;
const size_t end_idx = (thread_id == num_threads - 1) ? stride : (start_idx + chunk_size);
for (int i = start_idx; i < end_idx; i++)
{
float x = *bboxes_ptr++;
float y = *bboxes_ptr++;
float w = *bboxes_ptr++;
float h = *bboxes_ptr;

float x0 = clampf((x - 0.5f * w), 0.f, (float)infer_img_width);
float y0 = clampf((y - 0.5f * h), 0.f, (float)infer_img_height);
float x1 = clampf((x + 0.5f * w), 0.f, (float)infer_img_width);
float y1 = clampf((y + 0.5f * h), 0.f, (float)infer_img_height);

cv::Rect_<float> bbox;
bbox.x = x0;
bbox.y = y0;
bbox.width = x1 - x0;
bbox.height = y1 - y0;
Object object;
object.label = max_s_ptr - scores_ptr;
object.prob = score;
object.rect = bbox;
detections.push_back(object);
const float* row_ptr = output.row(i).ptr<float>();
const float* bboxes_ptr = row_ptr;
const float* scores_ptr = row_ptr + 4;
const float* max_s_ptr = std::max_element(scores_ptr, scores_ptr + num_labels);
float score = *max_s_ptr;
if (score > confidence_threshold)
{
float x = *bboxes_ptr++;
float y = *bboxes_ptr++;
float w = *bboxes_ptr++;
float h = *bboxes_ptr;

float x0 = clampf((x - 0.5f * w), 0.f, (float)infer_img_width);
float y0 = clampf((y - 0.5f * h), 0.f, (float)infer_img_height);
float x1 = clampf((x + 0.5f * w), 0.f, (float)infer_img_width);
float y1 = clampf((y + 0.5f * h), 0.f, (float)infer_img_height);

cv::Rect_<float> bbox;
bbox.x = x0;
bbox.y = y0;
bbox.width = x1 - x0;
bbox.height = y1 - y0;
Object object;
object.label = max_s_ptr - scores_ptr;
object.prob = score;
object.rect = bbox;
#pragma omp critical
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but simpleomp does not support critical clause

{
detections.push_back(object);
}
}
}
}
objects = detections;
Expand Down