Skip to content

DNN: Add New API blobFromImageParam#22750

Merged
asmorkalov merged 2 commits intoopencv:4.xfrom
zihaomu:improve_blobFromImage
Apr 21, 2023
Merged

DNN: Add New API blobFromImageParam#22750
asmorkalov merged 2 commits intoopencv:4.xfrom
zihaomu:improve_blobFromImage

Conversation

@zihaomu
Copy link
Copy Markdown
Member

@zihaomu zihaomu commented Nov 4, 2022

The purpose of this PR:

  1. Add new API blobFromImageParam to extend blobFromImage API. It can support the different datalayout (NCHW or NHWC), and letter_box.
  2. blobFromImage can output CV_16F

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

@zihaomu zihaomu force-pushed the improve_blobFromImage branch 2 times, most recently from 7b47836 to e4abcec Compare November 4, 2022 05:53
@zihaomu zihaomu force-pushed the improve_blobFromImage branch 5 times, most recently from 60209c1 to d280eab Compare November 24, 2022 08:25
@zihaomu
Copy link
Copy Markdown
Member Author

zihaomu commented Nov 24, 2022

Hi, the updated code has re-used old blobFromImage API, only change is convert scalefactor from double to Scalar. And this change brokes the java binding, shown as follows:

    [mkdir] Created dir: /home/ci/build/java_test/build/classes
    [javac] Compiling 58 source files to /home/ci/build/java_test/build/classes
    [javac] /home/ci/build/java_test/src/org/opencv/test/dnn/DnnListRegressionTest.java:69: error: incompatible types: double cannot be converted to Scalar
    [javac]         Mat inputBlob = Dnn.blobFromImage(image, 1.0, new Size(224, 224), new Scalar(0), true, true);
    [javac]                                                  ^
    [javac] /home/ci/build/java_test/src/org/opencv/test/dnn/DnnTensorFlowTest.java:98: error: incompatible types: double cannot be converted to Scalar
    [javac]         Mat inputBlob = Dnn.blobFromImage(image, 1.0, new Size(224, 224), new Scalar(0), true, true);
    [javac]                                                  ^
    [javac] Note: /home/ci/build/java_test/src/org/opencv/test/dnn/DnnTensorFlowTest.java uses or overrides a deprecated API.
    [javac] Note: Recompile with -Xlint:deprecation for details.
    [javac] Note: Some input files use unchecked or unsafe operations.
    [javac] Note: Recompile with -Xlint:unchecked for details.
    [javac] Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
    [javac] 2 errors

Is there any solution?
Is it possible to map one cpp API of blobFromImage into two java APi?

@zihaomu zihaomu requested a review from vpisarev November 25, 2022 06:03
@zihaomu zihaomu force-pushed the improve_blobFromImage branch from a5c6ceb to 520b472 Compare November 25, 2022 06:55
Copy link
Copy Markdown
Member

@alalek alalek left a comment

Choose a reason for hiding this comment

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

As we have a new version with "parameters" do we really need to update old versions blobFromImage()? We could deprecate them and suggest using a new variant.

@asmorkalov
Copy link
Copy Markdown
Contributor

You can generate extra overloads in Java with ManualFuncs option of Java bindigs generator. See modules/core/misc/java/gen_dict.json as example. Java tests for all manual functions are required.

@zihaomu zihaomu force-pushed the improve_blobFromImage branch 4 times, most recently from b80264b to 716ba23 Compare November 30, 2022 03:54
@zihaomu zihaomu force-pushed the improve_blobFromImage branch 2 times, most recently from 972d8ce to 12acf76 Compare February 15, 2023 08:29
@zihaomu zihaomu changed the title DNN: improve blobFromImage API DNN: Extending blobFromImage through New API blobFromImageParam Feb 15, 2023
@zihaomu zihaomu changed the title DNN: Extending blobFromImage through New API blobFromImageParam DNN: Add New API blobFromImageParam Feb 15, 2023
@zihaomu
Copy link
Copy Markdown
Member Author

zihaomu commented Feb 15, 2023

As we have a new version with "parameters" do we really need to update old versions blobFromImage()? We could deprecate them and suggest using a new variant.

Agree with @alalek. I propose that this PR only add new features to the new API blobFromImageParam, without changing the scalefactor type at old API (blobFromImage). Since the blobFromImage has too many arguments, and the future new features will be added to the blobFromImageParam.

@zihaomu zihaomu force-pushed the improve_blobFromImage branch from 12acf76 to a58ce0c Compare February 15, 2023 11:12
@zihaomu zihaomu force-pushed the improve_blobFromImage branch from edd832e to b8d1f84 Compare March 1, 2023 03:48
@asmorkalov
Copy link
Copy Markdown
Contributor

@zihaomu @vpisarev friendly reminder.

@zihaomu zihaomu force-pushed the improve_blobFromImage branch 3 times, most recently from 6816dbd to 5c0ed4f Compare April 12, 2023 07:22
@zihaomu zihaomu force-pushed the improve_blobFromImage branch 2 times, most recently from b386d90 to 957cd82 Compare April 17, 2023 03:35
@zihaomu zihaomu force-pushed the improve_blobFromImage branch from 957cd82 to b4f6383 Compare April 18, 2023 05:44
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.

👍

@vpisarev vpisarev self-requested a review April 19, 2023 06:23
@vpisarev
Copy link
Copy Markdown
Contributor

@dkurt, can you please tell if "planar" layout used in TFLite importer is the same thing as "ND" layout used in other formats?

@dkurt
Copy link
Copy Markdown
Member

dkurt commented Apr 19, 2023

Do we really need to introduce legacy TensorFlow NHWC data layout? I think it may introduce more confusion than strict NCHW input.

Also, I'm not sure about preserving ratio during scale (crop/letter_box). They are different in different applications. For example, EfficientDet from TensorFlow:

h, w = img.shape[0:2]
# 1. Resize and keep aspect ratio
assert(w >= h)
h = int(h / w * args.height)
inp = cv.resize(img.astype(np.float32), (args.width, h))  # It's important to perform resize for fp32
# 2. Zero padding to the bottom
inp = np.pad(inp, ((0, args.height - h), (0, 0), (0, 0)), 'constant')
inp = np.expand_dims(inp.transpose(2, 0, 1), axis=0)

source

@dkurt, can you please tell if "planar" layout used in TFLite importer is the same thing as "ND" layout used in other formats?

@vpisarev, TFLite's planar is the same as TensorFlow's planar. It's used in case of 4D->3D or 2D reshaping when OpenCV data layout matches TensorFlow during import.

@zihaomu zihaomu force-pushed the improve_blobFromImage branch from a8c3f22 to 30aeda5 Compare April 21, 2023 09:25
@asmorkalov
Copy link
Copy Markdown
Contributor

[  FAILED  ] blobFromImageWithParams_4ch.letter_box

@zihaomu zihaomu force-pushed the improve_blobFromImage branch from 30aeda5 to 9450d87 Compare April 21, 2023 14:57
@zihaomu
Copy link
Copy Markdown
Member Author

zihaomu commented Apr 21, 2023

Hi @asmorkalov, thanks for your reminder, updated.

@asmorkalov asmorkalov merged commit 601778e into opencv:4.x Apr 21, 2023
@asmorkalov asmorkalov added this to the 4.8.0 milestone Apr 21, 2023
@asmorkalov asmorkalov mentioned this pull request May 31, 2023
thewoz pushed a commit to thewoz/opencv that referenced this pull request Jan 4, 2024
DNN: Add New API blobFromImageParam opencv#22750

The purpose of this PR:

1. Add new API `blobFromImageParam` to extend `blobFromImage` API. It can support the different data layout (NCHW or NHWC), and letter_box.
2. ~~`blobFromImage` can output `CV_16F`~~

### 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
- [ ] 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
DNN: Add New API blobFromImageParam opencv#22750

The purpose of this PR:

1. Add new API `blobFromImageParam` to extend `blobFromImage` API. It can support the different data layout (NCHW or NHWC), and letter_box.
2. ~~`blobFromImage` can output `CV_16F`~~

### 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
- [ ] 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.

5 participants