Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Queries about failed call “u2net_human_seg.onnx” in c++ #22

Closed
jsxyhelu opened this issue Nov 15, 2021 · 4 comments
Closed

Queries about failed call “u2net_human_seg.onnx” in c++ #22

jsxyhelu opened this issue Nov 15, 2021 · 4 comments

Comments

@jsxyhelu
Copy link

For the human-segmentation u2net, I have create scripts to port u2net to ONNX (u2net_human_seg-2onnx.py) and perform inference using OpenCV DNN (u2net_huam_seg-onnx-opencv.py). Get these scripts: https://github.com/fengyuentau/U-2-Net/tree/onnx_export.

However, the model itself is too large (168MB) to put into our zoo. The size of models in this zoo should be lower than 100MB.

Originally posted by @fengyuentau in #13 (comment)

@jsxyhelu
Copy link
Author

There is Python code that can be run for "u2net_human_seg. Onnx"

   # Inference
    net.setInput(blob)
    d0 = net.forward()
    # Norm
    pred = normPred(d0[:, 0, :, :])

There are problems in rewriting to C + +. My code is like this

    Mat inputBlob = blobFromImages(src, 1.0/255.0, Size(320, 320),Scalar(), true, false);
    net.setInput(inputBlob);
    Mat prob = net.forward();

The error echo is:

“[ INFO:0] global E:\Gitee\opencv_2021\opencv\modules\dnn\src\onnx\onnx_importer.cpp (395) cv::dnn::dnn4_v20201117::ONNXImporter::populateNet DNN/ONNX: loading ONNX v6 model produced by 'pytorch':1.8. Number of nodes = 1055, inputs = 1, outputs = 7
OpenCV(4.5.1-dev) Error: Assertion failed (total(os[i]) > 0) in cv::dnn::dnn4_v20201117::Net::Impl::getLayerShapesRecursively, file E:\Gitee\opencv_2021\opencv\modules\dnn\src\dnn.cpp, line 3520”

I tried to do some research, but I can't find the reason. I hope I can help with C + + calling. Thanks!

@fengyuentau
Copy link
Member

Can you provide the related cpp files so that I can help debugging?

@jsxyhelu
Copy link
Author

jsxyhelu commented Nov 15, 2021

OK,cpp code is simple


#include <iostream>
#include "opencv2/dnn.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/objdetect.hpp"
using namespace cv;
using namespace std;
using namespace cv::dnn;

int main(int argc, char ** argv)
{
	Net net = readNetFromONNX("e:/template/u2net_human_seg.onnx");
	Mat src = imread("e:/template/lena.jpg");
	if (net.empty()) {
		printf("read  model data failure...\n");
		return -1;
	}
	Mat inputBlob = blobFromImages(src, 1.0/255.0, Size(320, 320),Scalar(), true, false);
	net.setInput(inputBlob);
	Mat prob = net.forward();
	//ERROR
	/*
	[ INFO:0] global E:\Gitee\opencv_2021\opencv\modules\dnn\src\onnx\onnx_importer.cpp (395) cv::dnn::dnn4_v20201117::ONNXImporter::populateNet DNN/ONNX: loading ONNX v6 model produced by 'pytorch':1.8. Number of nodes = 1055, inputs = 1, outputs = 7
OpenCV(4.5.1-dev) Error: Assertion failed (total(os[i]) > 0) in cv::dnn::dnn4_v20201117::Net::Impl::getLayerShapesRecursively, file E:\Gitee\opencv_2021\opencv\modules\dnn\src\dnn.cpp, line 3520”
	*/
	return 0;
}

I think this is the same as this Python code

    net = cv.dnn.readNet(args.model)
    input_size = 320 # fixed
    # build blob using OpenCV
    img = cv.imread(args.input)
    blob = cv.dnn.blobFromImage(img, scalefactor=(1.0/255.0), size=(input_size, input_size), swapRB=True)
    # Inference
    net.setInput(blob)
    d0 = net.forward()

@ztxc
Copy link

ztxc commented Sep 8, 2023

hello, Is your problem solved?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants