Batch forward onnx model

Hello,
I’m running YOLO model on CPP code using opencv::dnn.

when forwarding single image using the code -

vector<Mat> pre_process(cv::Mat input_image)
{
	// Convert to blob.
	Mat blob;
	dnn::blobFromImage(input_image, blob, 1.0 / 255, Size(INPUT_WIDTH, INPUT_HEIGHT), Scalar(), true, false);
	net.setInput(blob);
	// Forward propagate.
	vector<Mat> outputs;
	net.forward(outputs, net.getUnconnectedOutLayersNames());
	return outputs;

}

it works perfectly fine.
However, when trying to forward multiple images in a batch, using the code

vector <vector<Mat>> pre_process(vector <cv::Mat> input_images)
{
	// Convert to blob.
	Mat blob;
	dnn::blobFromImages(input_images, blob, 1.0 / 255, Size(INPUT_WIDTH, INPUT_HEIGHT), Scalar(), true, false);
	net.setInput(blob);
	// Forward propagate.
	vector <vector<Mat>> AllOutputs;
	net.forward(AllOutputs, net.getUnconnectedOutLayersNames());
	return AllOutputs;
}

I get the following error:

[ERROR:0@8.011] global net_impl.cpp:1165 cv::dnn::dnn4_v20240521::Net::Impl::getLayerShapesRecursively OPENCV/DNN: [Permute]:(onnx_node!/model.24/Transpose): getMemoryShapes() throws exception. inputs=1 outputs=0/1 blobs=0

[ERROR:0@8.012] global net_impl.cpp:1168 cv::dnn::dnn4_v20240521::Net::Impl::getLayerShapesRecursively input[0] = [ 135 1 3 7 80 80 ]

[ERROR:0@8.012] global net_impl.cpp:1178 cv::dnn::dnn4_v20240521::Net::Impl::getLayerShapesRecursively Exception message: OpenCV(4.10.0) C:\opencv\opencv-4.10.0\modules\dnn\src\layers\permute_layer.cpp:162: error: (-215:Assertion failed) (int)_numAxes == inputs[0].size() in function ‘cv::dnn::PermuteLayerImpl::getMemoryShapes’

any idea how to process multiple images as a batch?

thanks :slight_smile:

are they all the same size and such?

sure. all the same size