-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
whyb
wants to merge
32
commits into
Tencent:master
Choose a base branch
from
whyb:yolov8example-improve
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
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 e48623d
Move class __ncnn_vulkan_instance_holder declaration from gpu.h to g…
whyb 1042565
Delete empty line changes
whyb a479422
Reimplement the sleep() and get_current_time() functions using modern…
whyb 6eb3bb7
Fixed build error for __riscv not support c++ 11 thread
whyb ed932ba
Add NCNN_SIMPLESTL Macro
whyb 00b9f28
Fix simple stl's compiler build error
whyb 67ef283
Fix linux-gcc-cpp03-nostdio-nostring-simplestl build error
whyb d1e079c
Use u_int64_t type parameters in linux-gcc-cpp03-nostdio-nostring-sim…
whyb 2ad85f0
change uint64_t&u_int64_t to unsigned long long int
whyb cec75bc
Remove include stdint.h and change function sleep() default paramete…
whyb 85c15a0
apply code-format changes
whyb be921fd
Update benchmark.cpp
nihui b25ac10
Merge branch 'Tencent:master' into master
whyb 98af54c
Merge branch 'Tencent:master' into master
whyb 27ba97b
Merge branch 'Tencent:master' into master
whyb 3110969
Merge branch 'Tencent:master' into master
whyb a1f84f8
Merge branch 'Tencent:master' into master
whyb 76941c6
Merge branch 'Tencent:master' into master
whyb 6cee97e
Merge branch 'Tencent:master' into master
whyb 56d61d8
Merge branch 'Tencent:master' into master
whyb 9ff751a
Merge branch 'Tencent:master' into master
whyb aa40554
Merge branch 'Tencent:master' into master
whyb fb19b98
Merge branch 'Tencent:master' into master
whyb e56cc3a
Merge branch 'Tencent:master' into master
whyb fe151a8
Merge branch 'Tencent:master' into master
whyb 4e91d49
Merge branch 'Tencent:master' into master
whyb 2e9e6c3
Merge branch 'Tencent:master' into master
whyb 173ae25
Merge branch 'Tencent:master' into master
whyb 8e30e72
Merge branch 'Tencent:master' into master
whyb 2880f80
Merge branch 'Tencent:master' into master
whyb c75882f
Using OpenMP to improve yolov8 post-processing efficiency
whyb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
|
@@ -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) | ||
{ | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What alternatives are there to achieve similar functionality?😭