Assertion fail in DNN module after opencv-python 4.5.3.56

hi, all.

I made a dnn example that recognizes Korean character.
The deep learning model used in this program was trained using the source code released by IBM.

The trained model was saved into a .pb file using the code like this:

    output_graph_def = graph_util.convert_variables_to_constants(
        sess, sess.graph_def, [output_node_name])
    with gfile.FastGFile('./korean_recognition.pb', 'wb') as f:
        f.write(output_graph_def.SerializeToString())

The deep learning network’s input is a 64x64 grayscale image.
So, in the dnn example program, I created a blob using the below, where img is a 400x400 grayscale image.

    img = np.zeros((400, 400), dtype=np.uint8)
	# draw character using mouse...

    blob = cv2.dnn.blobFromImage(img, 1, (64, 64))
    net.setInput(blob)
    out = net.forward() # out.shape=(1, 256)

It was confirmed that this code works well in OpenCV 4.2.
However, it does not work in the latest version of OpenCV.

Upon investigation, no error occurs until opencv-python==4.5.2.54, and the following error occurs after opencv-python==4.5.3.36.

C:\coding\tf>python dnnkorean.py
(1, 1, 64, 64) <class 'numpy.ndarray'> float32
[ERROR:0@2.393] 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]:(Conv2D): getMemoryShapes() throws exception. inputs=1 outputs=0/1 blobs=1
[ERROR:0@2.395] global D:\a\opencv-python\opencv-python\opencv\modules\dnn\src\net_impl.cpp (1174) cv::dnn::dnn4_v20220524::Net::Impl:: getLayerShapesRecursively input[0] = [ 1 64 64 1 ]
[ERROR:0@2.397] 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 [ 32 1 5 5 ]
[ERROR:0@2.399] 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:406: error: (-215:Assertion failed) ngroups > 0 && inpCn % ngroups == 0 && outCn % ngroups == 0 in function 'cv::dnn::ConvolutionLayerImpl::getMemoryShapes'

Traceback (most recent call last):
  File "dnnkorean.py", line 64, in <module>
    out = net.forward() # out.shape=(1, 256)
cv2.error: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\dnn\src\layers\convolution_layer.cpp:406: error: (-215:Assertion failed) ngroups > 0 && inpCn % ngroups == 0 && outCn % ngroups == 0 in function 'cv::dnn::ConvolutionLayerImpl::getMemoryShapes'

Can you figure out the cause?

1 Like

few thoughts:

  • do you have a link to the pb for us ??
  • are you sure, this model is using NCHW input ?
    (what blobFromImage returns)
    (python code from the orig repo looks more like it is NHWC)
    you could try to resize the image to 64x64, then try
    blob = np.expand_dims(img, 0) (instead of blobFromImage)
    to get to the expected [1,64,64,1] shape in the 1st convolution
  • not really sure, but there is a (maybe) related issue here: Lost support of tf nhwc models · Issue #21290 · opencv/opencv · GitHub

Hi, berak,

I uploaded my example files in this link:

I tested it again and found that the error occured from ‘opencv-python==4.5.2.54’.
Here is the tested commands and the results:

C:\coding\tf\dnnkorean>pip install opencv-python==4.5.1.48
Collecting opencv-python==4.5.1.48
  Using cached opencv_python-4.5.1.48-cp38-cp38-win_amd64.whl (34.9 MB)
Requirement already satisfied: numpy>=1.17.3 in c:\users\sunkyoo\appdata\local\programs\python\python38\lib\site-packages (from opencv-python==4.5.1.48) (1.22.3)
Installing collected packages: opencv-python
  Attempting uninstall: opencv-python
    Found existing installation: opencv-python 4.5.2.54
    Uninstalling opencv-python-4.5.2.54:
      Successfully uninstalled opencv-python-4.5.2.54
Successfully installed opencv-python-4.5.1.48

C:\coding\tf\dnnkorean>python dnnkorean.py
(1, 1, 64, 64) <class 'numpy.ndarray'> float32
가 (100.00%)

C:\coding\tf\dnnkorean>pip install opencv-python==4.5.2.54
Collecting opencv-python==4.5.2.54
  Using cached opencv_python-4.5.2.54-cp38-cp38-win_amd64.whl (34.7 MB)
Requirement already satisfied: numpy>=1.17.3 in c:\users\sunkyoo\appdata\local\programs\python\python38\lib\site-packages (from opencv-python==4.5.2.54) (1.22.3)
Installing collected packages: opencv-python
  Attempting uninstall: opencv-python
    Found existing installation: opencv-python 4.5.1.48
    Uninstalling opencv-python-4.5.1.48:
      Successfully uninstalled opencv-python-4.5.1.48
Successfully installed opencv-python-4.5.2.54

C:\coding\tf\dnnkorean>python dnnkorean.py
(1, 1, 64, 64) <class 'numpy.ndarray'> float32
[ERROR:0] global C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-_8k9tw8n\opencv\modules\dnn\src\dnn.cpp (3512) cv::dnn::dnn4_v20210301::Net::Impl::getLayerShapesRecursively OPENCV/DNN: [Convolution]:(Conv2D): getMemoryShapes() throws exception. inputs=1 outputs=0/1 blobs=1
[ERROR:0] global C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-_8k9tw8n\opencv\modules\dnn\src\dnn.cpp (3515) cv::dnn::dnn4_v20210301::Net::Impl::getLayerShapesRecursively     input[0] = [ 1 64 64 1 ]
[ERROR:0] global C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-_8k9tw8n\opencv\modules\dnn\src\dnn.cpp (3523) cv::dnn::dnn4_v20210301::Net::Impl::getLayerShapesRecursively     blobs[0] = CV_32FC1 [ 32 1 5 5 ]
[ERROR:0] global C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-_8k9tw8n\opencv\modules\dnn\src\dnn.cpp (3525) cv::dnn::dnn4_v20210301::Net::Impl::getLayerShapesRecursively Exception message: OpenCV(4.5.2) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-_8k9tw8n\opencv\modules\dnn\src\layers\convolution_layer.cpp:387: error: (-215:Assertion failed) ngroups > 0 && inpCn % ngroups == 0 && outCn % ngroups == 0 in function 'cv::dnn::ConvolutionLayerImpl::getMemoryShapes'

Traceback (most recent call last):
  File "dnnkorean.py", line 63, in <module>
    out = net.forward()  # out.shape=(1, 256)
cv2.error: OpenCV(4.5.2) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-_8k9tw8n\opencv\modules\dnn\src\layers\convolution_layer.cpp:387: error: (-215:Assertion failed) ngroups > 0 && inpCn % ngroups == 0 && outCn % ngroups == 0 in function 'cv::dnn::ConvolutionLayerImpl::getMemoryShapes'


C:\coding\tf\dnnkorean>

I think the link in your comment is very similar to my case.
But, I’m not good at analysing pb file, so please check the uploaded files ans share you opinion.

Thanks a lot.