Content-Length: 325071 | pFad | http://github.com/opencv/opencv/pull/27063

54 Updated TopK Handling in ONNX Importer by tony19053000 · Pull Request #27063 · opencv/opencv · GitHub
Skip to content

Updated TopK Handling in ONNX Importer #27063

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

Closed
wants to merge 2 commits into from

Conversation

tony19053000
Copy link

Fixed the issue with the ONNX TopK operator in OpenCV.

The TopK operator needs a value k to work. Before, OpenCV expected k to always be set as a constant value.
If k was provided as an input (instead of a constant), OpenCV would fail with an error saying k was missing.
The fix allows OpenCV to read k from the input if it’s available.
If k is still missing after checking the inputs, OpenCV will now give a clear error message.

@tony19053000
Copy link
Author

@asmorkalov Sir, can you please assign me some issue? It would be really helpful.

@asmorkalov asmorkalov added feature category: dnn (onnx) ONNX suport issues in DNN module labels Mar 14, 2025
@asmorkalov asmorkalov added this to the 4.12.0 milestone Mar 14, 2025
@asmorkalov asmorkalov requested a review from Abdurrahheem March 14, 2025 05:59
@asmorkalov asmorkalov added the pr: needs test New functionality requires minimal tests set label Mar 14, 2025
@asmorkalov asmorkalov requested a review from vpisarev March 14, 2025 05:59
@asmorkalov
Copy link
Contributor

Related fix for 5.x and dynamic shapes support: #26731

@tony19053000
Copy link
Author

@asmorkalov Sir, how to fix this error?

@asmorkalov
Copy link
Contributor

I converted our top_k.onnx model to oppset 11 to get the layer with two inputs and get the following error for the patch:

[ RUN      ] Test_ONNX_layers.TopK/0, where GetParam() = OCV/OCL
[ WARN:0@0.168] global onnx_importer.cpp:3145 parseTopK OnnxImporter/TopK: K is not constant, falling back to attribute value if available
[ERROR:0@0.168] global onnx_importer.cpp:1036 handleNode DNN/ONNX: ERROR during processing node with 2 inputs and 2 outputs: [TopK]:(onnx_node_output_0!values) from domain='ai.onnx'
unknown file: Failure
C++ exception with description "OpenCV(4.12.0-dev) /home/ksenia/Projects/opencv/modules/dnn/src/onnx/onnx_importer.cpp:1058: error: (-2:Unspecified error) in function 'handleNode'
> Node [TopK@ai.onnx]:(onnx_node_output_0!values) parse error: OpenCV(4.12.0-dev) /home/ksenia/Projects/opencv/modules/dnn/src/onnx/onnx_importer.cpp:3149: error: (-2:Unspecified error) in function 'void cv::dnn::dnn4_v20241223::ONNXImporter::parseTopK(cv::dnn::dnn4_v20241223::LayerParams&, const opencv_onnx::NodeProto&)'
> > OnnxImporter/TopK: K parameter is required but missing:
> >     'k != -1 || K != -1' must be 'true'
> " thrown in the test body.
[  FAILED  ] Test_ONNX_layers.TopK/0, where GetParam() = OCV/OCL (3 ms)
[ RUN      ] Test_ONNX_layers.TopK/1, where GetParam() = OCV/OCL_FP16
[ WARN:0@0.172] global onnx_importer.cpp:3145 parseTopK OnnxImporter/TopK: K is not constant, falling back to attribute value if available
[ERROR:0@0.172] global onnx_importer.cpp:1036 handleNode DNN/ONNX: ERROR during processing node with 2 inputs and 2 outputs: [TopK]:(onnx_node_output_0!values) from domain='ai.onnx'
unknown file: Failure
C++ exception with description "OpenCV(4.12.0-dev) /home/ksenia/Projects/opencv/modules/dnn/src/onnx/onnx_importer.cpp:1058: error: (-2:Unspecified error) in function 'handleNode'
> Node [TopK@ai.onnx]:(onnx_node_output_0!values) parse error: OpenCV(4.12.0-dev) /home/ksenia/Projects/opencv/modules/dnn/src/onnx/onnx_importer.cpp:3149: error: (-2:Unspecified error) in function 'void cv::dnn::dnn4_v20241223::ONNXImporter::parseTopK(cv::dnn::dnn4_v20241223::LayerParams&, const opencv_onnx::NodeProto&)'
> > OnnxImporter/TopK: K parameter is required but missing:
> >     'k != -1 || K != -1' must be 'true'
> " thrown in the test body.
[  FAILED  ] Test_ONNX_layers.TopK/1, where GetParam() = OCV/OCL_FP16 (5 ms)
[ RUN      ] Test_ONNX_layers.TopK/2, where GetParam() = OCV/CPU
[ WARN:0@0.173] global onnx_importer.cpp:3145 parseTopK OnnxImporter/TopK: K is not constant, falling back to attribute value if available
[ERROR:0@0.173] global onnx_importer.cpp:1036 handleNode DNN/ONNX: ERROR during processing node with 2 inputs and 2 outputs: [TopK]:(onnx_node_output_0!values) from domain='ai.onnx'
unknown file: Failure
C++ exception with description "OpenCV(4.12.0-dev) /home/ksenia/Projects/opencv/modules/dnn/src/onnx/onnx_importer.cpp:1058: error: (-2:Unspecified error) in function 'handleNode'
> Node [TopK@ai.onnx]:(onnx_node_output_0!values) parse error: OpenCV(4.12.0-dev) /home/ksenia/Projects/opencv/modules/dnn/src/onnx/onnx_importer.cpp:3149: error: (-2:Unspecified error) in function 'void cv::dnn::dnn4_v20241223::ONNXImporter::parseTopK(cv::dnn::dnn4_v20241223::LayerParams&, const opencv_onnx::NodeProto&)'
> > OnnxImporter/TopK: K parameter is required but missing:
> >     'k != -1 || K != -1' must be 'true'
> " thrown in the test body.
[  FAILED  ] Test_ONNX_layers.TopK/2, where GetParam() = OCV/CPU (0 ms)
[----------] 3 tests from Test_ONNX_layers (8 ms total)

[----------] Global test environment tear-down
[==========] 3 tests from 1 test case ran. (8 ms total)
[  PASSED  ] 0 tests.
[  FAILED  ] 3 tests, listed below:
[  FAILED  ] Test_ONNX_layers.TopK/0, where GetParam() = OCV/OCL
[  FAILED  ] Test_ONNX_layers.TopK/1, where GetParam() = OCV/OCL_FP16
[  FAILED  ] Test_ONNX_layers.TopK/2, where GetParam() = OCV/CPU

 3 FAILED TESTS

@asmorkalov
Copy link
Contributor

@asmorkalov
Copy link
Contributor

Conversion script: import sys

import onnx
from onnx import version_converter

model = onnx.load(sys.argv[1])
converted_model = version_converter.convert_version(model, 11)
onnx.save(converted_model, sys.argv[2])

@fengyuentau
Copy link
Member

It is not possible to fix this issue in 4.x as 4.x does not support dynamic input shape. You need to do it in 5.x along with corresponding changes in topk_layer.cpp.

@asmorkalov asmorkalov closed this Mar 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
category: dnn (onnx) ONNX suport issues in DNN module feature pr: needs test New functionality requires minimal tests set
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/opencv/opencv/pull/27063

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy