Skip to content

dynamic batch-size onnx problem #22086

@whuhit

Description

@whuhit
System information (version)
  • OpenCV == 4.6.0
  • Operating System / Platform => Windows 64 Bit
  • Compiler => python3.8
Detailed description

I try to use cv2.dnn.readNetFromONNX to load an onnx segmentation model, this onnx model has a dynamic batch size. when i set batch-size=1, it worked. but when i use another batch size value, i got an exception: (python and c++ opencv both have this problem)

[ERROR:0@0.041] global D:\a\opencv-python\opencv-python\opencv\modules\dnn\src\net_impl.cpp (1171) cv::dnn::dnn4_v20220524::Net::Impl::getLayerShapesRecursively OPENCV/DNN: [Convolution]:(onnx_node!Conv_12): getMemoryShapes() throws exception. inputs=1 outputs=0/1 blobs=2
[ERROR:0@0.041] global D:\a\opencv-python\opencv-python\opencv\modules\dnn\src\net_impl.cpp (1174) cv::dnn::dnn4_v20220524::Net::Impl::getLayerShapesRecursively     input[0] = [ 2 1 16 1 1 ]
[ERROR:0@0.041] global D:\a\opencv-python\opencv-python\opencv\modules\dnn\src\net_impl.cpp (1182) cv::dnn::dnn4_v20220524::Net::Impl::getLayerShapesRecursively     blobs[0] = CV_32FC1 [ 8 16 1 1 ]
[ERROR:0@0.041] global D:\a\opencv-python\opencv-python\opencv\modules\dnn\src\net_impl.cpp (1182) cv::dnn::dnn4_v20220524::Net::Impl::getLayerShapesRecursively     blobs[1] = CV_32FC1 [ 8 1 ]
[ERROR:0@0.041] global D:\a\opencv-python\opencv-python\opencv\modules\dnn\src\net_impl.cpp (1184) cv::dnn::dnn4_v20220524::Net::Impl::getLayerShapesRecursively Exception message: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\dnn\src\layers\convolution_layer.cpp:405: error: (-2:Unspecified error) Number of input channels should be multiple of 16 but got 1 in function 'cv::dnn::ConvolutionLayerImpl::getMemoryShapes'

Traceback (most recent call last):
  File "E:/01_code/AEye/deploy/opencv/cls_cv_dnn.py", line 17, in <module>
    out = net.forward()
cv2.error: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\dnn\src\layers\convolution_layer.cpp:405: error: (-2:Unspecified error) Number of input channels should be multiple of 16 but got 1 in function 'cv::dnn::ConvolutionLayerImpl::getMemoryShapes'

Steps to reproduce
import cv2
import numpy as np

batch_size = 2

net = cv2.dnn.readNetFromONNX("dynamic_batch_seg.onnx")
image = cv2.imread("test.bmp", -1)
images = [image]*batch_size
blob = cv2.dnn.blobFromImages(images, 1.0/255, size=(256, 256))
print(blob.shape)  # (batch_size, 1, 256, 256)
net.setInput(blob)
out = net.forward()
out = out[0].astype(np.uint8)*255
cv2.imshow("image", image)
cv2.imshow("out", out)
cv2.waitKey(0)

test onnx model and test data can be found here: https://drive.google.com/drive/folders/16XEir77vtXAXwLC4oOAywDnMiYnLH5bg?usp=sharing

Issue submission checklist
  • I checked the problem with documentation, FAQ, open issues,
    forum.opencv.org, Stack Overflow, etc and have not found any solution
  • I updated to the latest OpenCV version and the issue is still there
  • There is reproducer code and related data files: videos, images, onnx, etc

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions