Skip to content

imgproc: optimise local cost computation in IntelligentScissorsMB::buildMap#21959

Merged
asmorkalov merged 1 commit intoopencv:4.xfrom
cpoerschke:4.x-intelligent-scissors-optimisation
Jun 8, 2023
Merged

imgproc: optimise local cost computation in IntelligentScissorsMB::buildMap#21959
asmorkalov merged 1 commit intoopencv:4.xfrom
cpoerschke:4.x-intelligent-scissors-optimisation

Conversation

@cpoerschke
Copy link
Copy Markdown
Contributor

@cpoerschke cpoerschke commented May 8, 2022

When reading the code I noticed a TODO(opt) comment, this pull request is for that.

https://github.com/opencv/opencv/blob/4.5.5/modules/imgproc/src/intelligent_scissors.cpp#L628

float cost = cost_q + local_cost(q, r);  // TODO(opt): compute partially until cost < cost_r

Notes

Test run snippet

$ export OPENCV_TEST_DATA_PATH=../opencv_extra/testdata

$./bin/opencv_perf_imgproc --gtest_filter="TestIntelligentScissorsMB*" 
...
[ RUN      ] TestIntelligentScissorsMB_buildMap_setComputeFullLocalCost.buildMap_setComputeFullLocalCost/0, where GetParam() = false
...
[ PERFSTAT ]    (samples=10   mean=5459.27   median=5452.70   min=5410.39   stddev=34.39 (0.6%))
[       OK ] TestIntelligentScissorsMB_buildMap_setComputeFullLocalCost.buildMap_setComputeFullLocalCost/0 (54604 ms)
[ RUN      ] TestIntelligentScissorsMB_buildMap_setComputeFullLocalCost.buildMap_setComputeFullLocalCost/1, where GetParam() = true
...
[ PERFSTAT ]    (samples=10   mean=5635.78   median=5634.03   min=5621.81   stddev=9.53 (0.2%))
[       OK ] TestIntelligentScissorsMB_buildMap_setComputeFullLocalCost.buildMap_setComputeFullLocalCost/1 (56360 ms)
...

Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
  • The PR is proposed to the proper branch
  • There is a reference to the original bug report and related work
  • There is accuracy test, performance test and test data in opencv_extra repository, if applicable
    Patch to opencv_extra has the same branch name.
  • The feature is well documented and sample code can be built with the project CMake

@cpoerschke
Copy link
Copy Markdown
Contributor Author

Here are some more notes on the optimisation.

The local_cost(p, q) is computed as the sum of three terms

weight_non_edge_compute * non_edge_feature.at<uchar>(q) +
weight_gradient_direction * fD +
weight_gradient_magnitude * fG

and the optimisation uses the non-negativity of the terms and the fact that we don't need the exact cost if the cost exceeds the threshold. E.g. if term1 increases the cost above the threshold then also adding the non-negative terms term2 and term3 cannot decrease the cost to below the threshold and so we can omit computing and adding term2 and term3.

Supporting links w.r.t. non-negativity of the terms:

@cpoerschke
Copy link
Copy Markdown
Contributor Author

Thanks for approving the CI run for this pull request!

Reviewing the "PR:4.x W10 / BuildAndTest (pull_request) Cancelled after 360m — BuildAndTest" outcome, this was

Run RET=$(git ls-remote --heads "https://github.com/cpoerschke/opencv_extra" "4.x-intelligent-scissors-optimisation") || true
Error: The operation was canceled.

and apparently because (at the time) there was no https://github.com/cpoerschke/opencv_extra fork in existence. Basic git documentation searching found no git ls-remote options or other git commands that would obviously handle this scenario and so I've just gone ahead and created a fork of the repo. Hope that helps? Thank you.

};

INSTANTIATE_TEST_CASE_P(/**/, Imgproc_IntelligentScissorsMB, testing::Combine(testing::Values( true, false ),
testing::Values( 1, 100 )));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What is the purpose of adding 100 iterations for the accuracy test?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It was a naive way of getting performance test numbers ... I have now in cd6ef45 added a separate performance test instead.

@asmorkalov asmorkalov modified the milestones: 4.7.0, 4.8.0 Dec 12, 2022
@asmorkalov asmorkalov requested review from asmorkalov and removed request for alalek May 23, 2023 06:43
@asmorkalov
Copy link
Copy Markdown
Contributor

@cpoerschke Thanks a lot for the patch! Couple of questions for the solution:

  • If I understand correctly, the optimized branch produces the same result. In case if we rich threshold with some part of cost sum, then we've got what we want. cost_map contains only values bellow threshold and half-baked cost values that are above threshold are not added there. Why do you need special flag to switch behaviour?
  • Could you modify some (not all) tests to check contour values, but not only contour size? It allows to check item 1.

@asmorkalov asmorkalov self-assigned this May 23, 2023
@cpoerschke
Copy link
Copy Markdown
Contributor Author

Hi @asmorkalov - thanks for the review!

  • If I understand correctly, ... Why do you need special flag to switch behaviour?

Your understanding is correct. The special flag (during development) helps compare performance and accuracy via the parameterised tests but yes beyond that there is technically no need for the flag to remain. Sometimes flags can help preserve existing behaviour for APIs for backwards compatibility between versions or so but that is not the case here I think.

  • Could you modify some (not all) tests to check contour values, but not only contour size? It allows to check item 1.

Have just added a commit that modifies (so far only) one test to check ca. 5+5% of the contour values, at the beginning and the end of the contour.

@asmorkalov
Copy link
Copy Markdown
Contributor

I propose 2 things:

  • Dump full contour to file with FileStorage (yaml or yaml.gz) and check full contour in several tests or all tests. You need to create PR to opencv_extra repo with the same branch name. If the branches match they are testsed together by CI.
  • Remove flag and stay with optimized branch. I do not see reason to presume original behavior, if they are equivalent. Test with full contour check ensures it.

@cpoerschke
Copy link
Copy Markdown
Contributor Author

I propose 2 things:

  • Dump full contour to file with FileStorage (yaml or yaml.gz) and check full contour in several tests or all tests. You need to create PR to opencv_extra repo with the same branch name. If the branches match they are testsed together by CI.
    ...

#23688 opened for the first thing - .xml files seemed more common than .yaml files and so I went with that for now. opencv/opencv_extra#1065 is the matching PR for the opencv_extra repo.

@cpoerschke
Copy link
Copy Markdown
Contributor Author

I propose 2 things:

  • ...
  • Remove flag and stay with optimized branch. I do not see reason to presume original behavior, if they are equivalent. Test with full contour check ensures it.

a01344e commit added for the second thing.

@asmorkalov
Copy link
Copy Markdown
Contributor

I propose to restore perf test. We can use summary.py and other scripts to compare performance numbers for different versions, e.g. gefore and after patch.

asmorkalov pushed a commit that referenced this pull request May 29, 2023
imgproc: add basic IntelligentScissorsMB performance test #23698

Adding basic performance test that can be used before and after the #21959 changes etc. as per @asmorkalov's #21959 (comment) comment.

### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [X] I agree to contribute to the project under Apache 2 License.
- [X] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [X] The PR is proposed to the proper branch
- [ ] There is a reference to the original bug report and related work
- [X] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
asmorkalov pushed a commit that referenced this pull request Jun 7, 2023
imgproc: add contour values check to IntelligentScissorsMB tests

Preparation for the #21959 changes as per @asmorkalov's #21959 (comment) suggestion.

### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [X] I agree to contribute to the project under Apache 2 License.
- [X] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [X] The PR is proposed to the proper branch
- [ ] There is a reference to the original bug report and related work
- [X] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
@asmorkalov
Copy link
Copy Markdown
Contributor

@cpoerschke Thanks a lot for the contribution! I merged accuracy and performance tests. Could you rebase the PR with optimization on top of current 4.x and squash commits?

@cpoerschke cpoerschke force-pushed the 4.x-intelligent-scissors-optimisation branch from a01344e to f597838 Compare June 7, 2023 21:50
@cpoerschke
Copy link
Copy Markdown
Contributor Author

@cpoerschke Thanks a lot for the contribution! I merged accuracy and performance tests. Could you rebase the PR with optimization on top of current 4.x and squash commits?

Thanks @asmorkalov for the review inputs! I've rebased and squashed the commits.

Also ran the performance test on the commit before and the commit itself - unless I'm misreading the numbers the optimisation actually makes things slower now. Would like to look into that more thoroughly, perhaps I missed something when removing the switch flag? The accuracy tests pass.

@cpoerschke cpoerschke marked this pull request as draft June 7, 2023 22:01
@asmorkalov
Copy link
Copy Markdown
Contributor

I see optimization effect, but the mentioned function is not bootle neck:

python3 ../opencv-master/modules/ts/misc/summary.py ./IntelligentScissorsMB-4.x.xml ./IntelligentScissorsMB-opt.xml 

Geometric mean (ms)

             Name of Test              IntelligentScissorsMB-4.x IntelligentScissorsMB-opt IntelligentScissorsMB-opt
                                                                                                      vs            
                                                                                           IntelligentScissorsMB-4.x
                                                                                                  (x-factor)        
buildMap::TestIntelligentScissorsMB::0         7995.334                  7819.149                    1.02           
buildMap::TestIntelligentScissorsMB::1         7988.302                  7816.922                    1.02  

Copy link
Copy Markdown
Contributor

@asmorkalov asmorkalov left a comment

Choose a reason for hiding this comment

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

👍

@asmorkalov asmorkalov marked this pull request as ready for review June 8, 2023 13:44
@asmorkalov asmorkalov merged commit 5d913f4 into opencv:4.x Jun 8, 2023
@asmorkalov asmorkalov mentioned this pull request Jul 12, 2023
advait-0 pushed a commit to advait-0/opencv that referenced this pull request Aug 31, 2023
imgproc: add contour values check to IntelligentScissorsMB tests

Preparation for the opencv#21959 changes as per @asmorkalov's opencv#21959 (comment) suggestion.

### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [X] I agree to contribute to the project under Apache 2 License.
- [X] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [X] The PR is proposed to the proper branch
- [ ] There is a reference to the original bug report and related work
- [X] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
thewoz pushed a commit to thewoz/opencv that referenced this pull request Jan 4, 2024
imgproc: add basic IntelligentScissorsMB performance test opencv#23698

Adding basic performance test that can be used before and after the opencv#21959 changes etc. as per @asmorkalov's opencv#21959 (comment) comment.

### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [X] I agree to contribute to the project under Apache 2 License.
- [X] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [X] The PR is proposed to the proper branch
- [ ] There is a reference to the original bug report and related work
- [X] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
thewoz pushed a commit to thewoz/opencv that referenced this pull request Jan 4, 2024
imgproc: add contour values check to IntelligentScissorsMB tests

Preparation for the opencv#21959 changes as per @asmorkalov's opencv#21959 (comment) suggestion.

### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [X] I agree to contribute to the project under Apache 2 License.
- [X] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [X] The PR is proposed to the proper branch
- [ ] There is a reference to the original bug report and related work
- [X] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
thewoz pushed a commit to thewoz/opencv that referenced this pull request May 29, 2024
imgproc: add basic IntelligentScissorsMB performance test opencv#23698

Adding basic performance test that can be used before and after the opencv#21959 changes etc. as per @asmorkalov's opencv#21959 (comment) comment.

### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [X] I agree to contribute to the project under Apache 2 License.
- [X] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [X] The PR is proposed to the proper branch
- [ ] There is a reference to the original bug report and related work
- [X] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
thewoz pushed a commit to thewoz/opencv that referenced this pull request May 29, 2024
imgproc: add contour values check to IntelligentScissorsMB tests

Preparation for the opencv#21959 changes as per @asmorkalov's opencv#21959 (comment) suggestion.

### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [X] I agree to contribute to the project under Apache 2 License.
- [X] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [X] The PR is proposed to the proper branch
- [ ] There is a reference to the original bug report and related work
- [X] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants