From f9dd20eb07210f4759889f667fc8fc1ad90f55da Mon Sep 17 00:00:00 2001 From: alexlyulkov Date: Wed, 17 Apr 2024 09:38:21 +0300 Subject: [PATCH] Merge pull request #25414 from alexlyulkov:al/range-fixed Fixed ONNX range layer #25414 Partially address https://github.com/opencv/opencv/issues/25363 Fixed ONNX range layer. It should support any input type. Added tests (extra [PR](https://github.com/opencv/opencv_extra/pull/1170)) ### 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. - [x] The feature is well documented and sample code can be built with the project CMake --- modules/dnn/src/onnx/onnx_importer.cpp | 16 ++++++++-------- modules/dnn/test/test_onnx_importer.cpp | 6 ++++++ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/modules/dnn/src/onnx/onnx_importer.cpp b/modules/dnn/src/onnx/onnx_importer.cpp index f0b2febb88..b09a9ed360 100644 --- a/modules/dnn/src/onnx/onnx_importer.cpp +++ b/modules/dnn/src/onnx/onnx_importer.cpp @@ -3005,22 +3005,22 @@ void ONNXImporter::parseRange(LayerParams& layerParams, const opencv_onnx::NodeP CV_Assert(const_id.size() == 3); Mat startMat = getBlob(node_proto, 0); - CV_Assert(startMat.type() == CV_32SC1); - int start = startMat.at(0); + startMat.convertTo(startMat, CV_32F); + float start = startMat.at(0); Mat limitMat = getBlob(node_proto, 1); - CV_Assert(limitMat.type() == CV_32SC1); - int limit = limitMat.at(0); + limitMat.convertTo(limitMat, CV_32F); + float limit = limitMat.at(0); Mat deltaMat = getBlob(node_proto, 2); - CV_Assert(deltaMat.type() == CV_32SC1); - int delta = deltaMat.at(0); + deltaMat.convertTo(deltaMat, CV_32F); + float delta = deltaMat.at(0); int number_of_elements = std::max(int(std::ceil((limit - start) / delta)), 0); - Mat r(number_of_elements, 1, CV_32SC1); + Mat r(1, number_of_elements, CV_32FC1); // should be 1d tensor, but Mat doesn't support it for (int i = 0; i < number_of_elements; i++) { - r.at(i) = start + (i * delta); + r.at(i) = start + (i * delta); } addConstant(node_proto.output(0), r); constBlobsExtraInfo.insert(std::make_pair(node_proto.output(0), TensorInfo(1))); diff --git a/modules/dnn/test/test_onnx_importer.cpp b/modules/dnn/test/test_onnx_importer.cpp index 669d363afa..82b10fb1ba 100644 --- a/modules/dnn/test/test_onnx_importer.cpp +++ b/modules/dnn/test/test_onnx_importer.cpp @@ -804,6 +804,12 @@ TEST_P(Test_ONNX_layers, CumSumExclusiveInplace) testONNXModels("cumsum_exclusive_inplace"); } +TEST_P(Test_ONNX_layers, Range) +{ + testONNXModels("range_float"); + testONNXModels("range_float_negative"); +} + TEST_P(Test_ONNX_layers, Eltwise3D) { #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_LT(2021040000)