From 8ec62ad3460fd4f371000157cf98cccaaa933303 Mon Sep 17 00:00:00 2001 From: Vigh Sebastian Date: Tue, 26 May 2026 18:09:44 +0300 Subject: [PATCH] Merge pull request #29134 from svigh:gapi_u8_nd_mats_onnx_layout_fix Merge pull request #29134 from svigh/gapi_u8_nd_mats_onnx_layout_fix Gapi u8 N-D mats onnx layout workaround #29134 ### 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 --- .../gapi/src/backends/onnx/gonnxbackend.cpp | 7 +++-- .../gapi/test/infer/gapi_infer_onnx_test.cpp | 31 +++++++++++++++++++ 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/modules/gapi/src/backends/onnx/gonnxbackend.cpp b/modules/gapi/src/backends/onnx/gonnxbackend.cpp index cbdd615c20..132a2d0db5 100644 --- a/modules/gapi/src/backends/onnx/gonnxbackend.cpp +++ b/modules/gapi/src/backends/onnx/gonnxbackend.cpp @@ -330,7 +330,8 @@ inline void preprocess(const cv::Mat& src, cv::Mat& dst) { // CNN input type const auto type = toCV(ti.type); - if (src.depth() != CV_8U) { + + if (src.depth() != CV_8U || src.dims > 2) { // Just pass the tensor as-is. // No layout or dimension transformations done here! // TODO: This needs to be aligned across all NN backends. @@ -338,10 +339,10 @@ inline void preprocess(const cv::Mat& src, if (tensor_dims.size() == ti.dims.size()) { for (size_t i = 0; i < ti.dims.size(); ++i) { GAPI_Assert((ti.dims[i] == -1 || ti.dims[i] == tensor_dims[i]) && - "Non-U8 tensor dimensions should match with all non-dynamic NN input dimensions"); + "Tensor dimensions should match with all non-dynamic NN input dimensions"); } } else { - GAPI_Error("Non-U8 tensor size should match with NN input"); + GAPI_Error("Tensor size should match with NN input"); } dst = src; diff --git a/modules/gapi/test/infer/gapi_infer_onnx_test.cpp b/modules/gapi/test/infer/gapi_infer_onnx_test.cpp index 6ca394aef2..605508481d 100644 --- a/modules/gapi/test/infer/gapi_infer_onnx_test.cpp +++ b/modules/gapi/test/infer/gapi_infer_onnx_test.cpp @@ -592,6 +592,37 @@ TEST_F(ONNXClassification, InferTensor) validate(); } +TEST_F(ONNXClassification, InferU8Tensor4D) +{ + // Create a U8 N-D (4D) tensor matching model input dims {1, 3, 224, 224}. + // This simulates tools like protopipe that feed pre-filled U8 N-D tensors + // directly to the ONNX backend (bypassing 2D image preprocessing). + // Without checking dims, preprocess() enters the image path and throws + // "Couldn't identify input tensor layout" because channels()==1 for N-D mats. + + useModel("classification/squeezenet/model/squeezenet1.0-9"); + + // ONNX_API code + cv::Mat backing_buf({1, 3, 224, 224}, CV_32F); + cv::randu(backing_buf, 0.f, 255.f); + infer(backing_buf, out_onnx); + + // G_API code + cv::Mat tensor(4, backing_buf.size.p, CV_8U, backing_buf.data); + G_API_NET(SqueezNet, , "squeeznet"); + cv::GMat in; + cv::GMat out = cv::gapi::infer(in); + cv::GComputation comp(cv::GIn(in), cv::GOut(out)); + auto net = cv::gapi::onnx::Params { + model_path + }.cfgNormalize({false}); + comp.apply(cv::gin(tensor), + cv::gout(out_gapi.front()), + cv::compile_args(cv::gapi::networks(net))); + // Validate + validate(); +} + TEST_F(ONNXClassification, InferROI) { useModel("classification/squeezenet/model/squeezenet1.0-9");