apply partial sort to save computations#19552
Merged
alalek merged 5 commits intoopencv:3.4from Feb 22, 2021
WeiChungChang:partialSort
Merged
apply partial sort to save computations#19552alalek merged 5 commits intoopencv:3.4from WeiChungChang:partialSort
alalek merged 5 commits intoopencv:3.4from
WeiChungChang:partialSort
Conversation
added 4 commits
February 14, 2021 17:55
alalek
reviewed
Feb 19, 2021
Member
alalek
left a comment
There was a problem hiding this comment.
Thank you for contribution!
This patch should go into 3.4 branch first.
We will merge changes from 3.4 into master regularly (weekly/bi-weekly).
Please:
- change "base" branch of this PR: master => 3.4 (use "Edit" button near PR title)
- rebase your commits from master onto 3.4 branch. For example:
git rebase -i --onto upstream/3.4 upstream/master
(check list of your commits, save and quit (Esc + "wq" + Enter)
whereupstreamis configured by following this GitHub guide and fetched (git fetch upstream). - push rebased commits into source branch of your fork (with
--forceoption)
Note: no needs to re-open PR, apply changes "inplace".
| std::sort(scoreIndexPairs.begin(), scoreIndexPairs.end(), | ||
| util::SortScorePairDescend<std::pair<int, int> >); | ||
| // if _keepTopK * 8 <= scoreIndexPairs.size(), apply partial sort. | ||
| if ((_keepTopK << 3) > scoreIndexPairs.size()) { |
Member
There was a problem hiding this comment.
<< 3
modern compilers are smart enough, so just * 8 should work well too.
Member
|
Please remove unrelated commits (the first 2 commits) and squash commits into one (use |
Merged
Merged
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
According to experiment, for sorting std::vector, when the target to be sorted is less than 12.5% of the whole vector, apply partial_sort instead of sort benefits at most x8 speedup.
Typically within detection out layer, the output is far less than the proposals after NMS; for example, for faster RCNN model coming from TensorFlow, there are 90 (# of classes) * 100 proposals but outputs 100 outcomes only. Sometimes even after NMS, the proposals is still far largen than the number to keep. Ex, for maskRCNN, we keep top 100 but proposal after NMS is 1292, so the ratio is 100/1292 ~ 8% and partial sort is faster x 2 times than sort. In the case if we keep top 10 only, the ratio is 10/1292 < 1% so partial sort benefits more.
As the result, in this case, apply partial sort makes it faster x8 times than applying std::sort().
Following table shows the speedup of the partial sort ratio; from it we can see when ratio < 25%, partial sort is faster than sort.
To make it safe, 12.5% is chosen.
Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
Patch to opencv_extra has the same branch name.