1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-21 19:33: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
@@ -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;
@@ -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");