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");