1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-30 07:43:03 +04:00

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
This commit is contained in:
Vigh Sebastian
2026-05-26 18:09:44 +03:00
committed by GitHub
parent 05acc5d4c6
commit 8ec62ad346
2 changed files with 35 additions and 3 deletions
@@ -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<float>(backing_buf, out_onnx);
// G_API code
cv::Mat tensor(4, backing_buf.size.p, CV_8U, backing_buf.data);
G_API_NET(SqueezNet, <cv::GMat(cv::GMat)>, "squeeznet");
cv::GMat in;
cv::GMat out = cv::gapi::infer<SqueezNet>(in);
cv::GComputation comp(cv::GIn(in), cv::GOut(out));
auto net = cv::gapi::onnx::Params<SqueezNet> {
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");