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
This commit is contained in:
alexlyulkov 2024-04-17 09:38:21 +03:00 committed by GitHub
parent 66fb5021e9
commit f9dd20eb07
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 8 deletions

View File

@ -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<int>(0);
startMat.convertTo(startMat, CV_32F);
float start = startMat.at<float>(0);
Mat limitMat = getBlob(node_proto, 1);
CV_Assert(limitMat.type() == CV_32SC1);
int limit = limitMat.at<int>(0);
limitMat.convertTo(limitMat, CV_32F);
float limit = limitMat.at<float>(0);
Mat deltaMat = getBlob(node_proto, 2);
CV_Assert(deltaMat.type() == CV_32SC1);
int delta = deltaMat.at<int>(0);
deltaMat.convertTo(deltaMat, CV_32F);
float delta = deltaMat.at<float>(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<int>(i) = start + (i * delta);
r.at<float>(i) = start + (i * delta);
}
addConstant(node_proto.output(0), r);
constBlobsExtraInfo.insert(std::make_pair(node_proto.output(0), TensorInfo(1)));

View File

@ -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)