Skip to content

[dnn] fix high level api for python#19484

Merged
alalek merged 3 commits intoopencv:masterfrom
UnaNancyOwen:fix_highlevelapi
Feb 10, 2021
Merged

[dnn] fix high level api for python#19484
alalek merged 3 commits intoopencv:masterfrom
UnaNancyOwen:fix_highlevelapi

Conversation

@UnaNancyOwen
Copy link
Copy Markdown
Contributor

fix #19346
fix #19481

original opinion by @mikr. thanks! #19346 (comment)

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 other license that is incompatible with OpenCV
  • The PR is proposed to proper branch
  • There is reference to 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

@UnaNancyOwen UnaNancyOwen changed the title [dnn] fix high lebel api for python [dnn] fix high level api for python Feb 9, 2021
@VadimLevin
Copy link
Copy Markdown
Contributor

@UnaNancyOwen Thank you for contribution. Can you add simple tests to verify Python bindings functionality for exported classes are fixed?

@UnaNancyOwen
Copy link
Copy Markdown
Contributor Author

@VadimLevin Sorry, That could be difficult for me. I don't know how python / dnn-module testing in OpenCV.

@VadimLevin
Copy link
Copy Markdown
Contributor

VadimLevin commented Feb 9, 2021

@VadimLevin Sorry, That could be difficult for me. I don't know how python / dnn-module testing in OpenCV.

You can add a test to modules/dnn/misc/python/test/test_dnn.py as test_high_level_api(self) method of class dnn_test. To reproduce the issue before fix I think you can use models from opencv_extra repository. Before applied fix test should fail and after applied fix test should pass.

@asmorkalov asmorkalov added the pr: needs test New functionality requires minimal tests set label Feb 9, 2021
@UnaNancyOwen
Copy link
Copy Markdown
Contributor Author

UnaNancyOwen commented Feb 9, 2021

@VadimLevin I have written a test. Please check following.
And, I have one question. Do I need to upload model (.onnx) and reference (.npy) to opencv/opencv_extra?

def test_textdetection_model_db(self):
    img_path = self.find_dnn_file("dnn/text_det_test1.png")
    weights = self.find_dnn_file("onnx/models/DB_TD500_resnet50.onnx", required=False)
    ref = np.load(self.find_dnn_file("dnn/DB_TD500_resnet50_prob.npy"))
    if weights is None:
        raise unittest.SkipTest("Missing DNN test files (onnx/models/DB_TD500_resnet50.onnx). Verify OPENCV_DNN_TEST_DATA_PATH configuration parameter.")

    frame = cv.imread(img_path)
    model = cv.dnn_TextDetectionModel_DB(weights)
    model.setInputScale(1.0 / 255.0)
    model.setInputSize(736, 736)
    model.setInputMean(122.67891434, 116.66876762, 104.00698793)

    out = model.predict(frame)
    normAssert(self, out, ref)

@VadimLevin
Copy link
Copy Markdown
Contributor

@VadimLevin I have written a test. Please check following.
And, I have one question. Do I need to upload model (.onnx) and reference (.npy) to opencv/opencv_extra?

That's my suggestion to use model from opencv_extra, because the issue is related to the generated bindings and can be reproduced with any model. To avoid generating reference npy file you can use corresponding low level API cv::dnn::Net and compare results - they should be close to each other.

@UnaNancyOwen
Copy link
Copy Markdown
Contributor Author

UnaNancyOwen commented Feb 9, 2021

That's my suggestion to use model from opencv_extra, because the issue is related to the generated bindings and can be reproduced with any model.

(I don't understand how it works, but) is it okay if the model is described in opencv_extra/testdata/dnn/download_models.py?
DB_TD500_resnet50.onnx is described in here.
https://github.com/opencv/opencv_extra/blob/4.5.1/testdata/dnn/download_models.py#L872-L876

To avoid generating reference npy file you can use corresponding low level API cv::dnn::Net and compare results - they should be close to each other.

That's a good idea. I took that idea in test code.

def test_textdetection_model_db(self):
    img_path = self.find_dnn_file("dnn/text_det_test1.png")
    weights = self.find_dnn_file("dnn/onnx/models/DB_TD500_resnet50.onnx", required=False)
    if weights is None:
        raise unittest.SkipTest("Missing DNN test files (onnx/models/DB_TD500_resnet50.onnx). Verify OPENCV_DNN_TEST_DATA_PATH configuration parameter.")

    frame = cv.imread(img_path)
    scale = 1.0 / 255.0
    size = (736, 736)
    mean = (122.67891434, 116.66876762, 104.00698793)

    model = cv.dnn_TextDetectionModel_DB(weights)
    model.setInputParams(scale, size, mean)
    out = model.predict(frame)

    net = cv.dnn.readNet(weights)
    blob = cv.dnn.blobFromImage(frame, scale, size, mean)
    net.setInput(blob)
    ref = net.forward()

    normAssert(self, out, ref)

Comment on lines +211 to +213
model = cv.dnn_TextDetectionModel_DB(weights)
model.setInputParams(scale, size, mean)
out = model.predict(frame)
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.

This code shows that patch in .hpp file is not necessary to pass this test (test doesn't fail without the patch).

Please update test code (it should fail before and pass after the patch).

@alalek
Copy link
Copy Markdown
Member

alalek commented Feb 9, 2021

To avoid generating reference npy file you can use corresponding low level API cv::dnn::Net and compare results

There is strong recommendation to avoid accuracy checking in Python (and others) bindings tests.
Check only parameters passing and types/shapes of returned results (check binding shim code only).

@UnaNancyOwen
Copy link
Copy Markdown
Contributor Author

@alalek I will fix test to check type and shape of output.

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.

Well done! Thank you 👍

@alalek alalek merged commit 107f233 into opencv:master Feb 10, 2021
@alalek alalek mentioned this pull request Apr 9, 2021
a-sajjad72 pushed a commit to a-sajjad72/opencv that referenced this pull request Mar 30, 2023
* [dnn] fix high level api for python

* [dnn] add test_textdetection_model_db

* [dnn] fix textdetection test only check type and shape
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.

DeepLab v3 does not work with High Level API of Python Python binding for subclasses of TextDetectionModel crashes the program

4 participants