Attention! This forum will be made read-only by Dec-20. Please migrate to https://forum.opencv.org. Most of existing active users should've received invitation by e-mail.
Ask Your Question
0

Can't load freezed and optimized Tensorflow 2.3.0 ResNet like model

asked 2020-10-20 02:30:23 -0500

Boris Brodski gravatar image

updated 2020-10-23 02:03:56 -0500

I have a ResNet type of model, that I have simplified and that I would like to use with OpenCV.

Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_1 (InputLayer)            [(None, 50, 50, 1)]  0                                            
__________________________________________________________________________________________________
batch_normalization (BatchNorma (None, 50, 50, 1)    4           input_1[0][0]                    
__________________________________________________________________________________________________
conv2d (Conv2D)                 (None, 50, 50, 20)   200         batch_normalization[0][0]        
__________________________________________________________________________________________________
re_lu (ReLU)                    (None, 50, 50, 20)   0           conv2d[0][0]                     
__________________________________________________________________________________________________
batch_normalization_1 (BatchNor (None, 50, 50, 20)   80          re_lu[0][0]                      
__________________________________________________________________________________________________

...

conv2d_13 (Conv2D)              (None, 13, 13, 80)   57680       batch_normalization_12[0][0]     
__________________________________________________________________________________________________
add_5 (Add)                     (None, 13, 13, 80)   0           conv2d_14[0][0]                  
                                                                 conv2d_13[0][0]                  
__________________________________________________________________________________________________
re_lu_12 (ReLU)                 (None, 13, 13, 80)   0           add_5[0][0]                      
__________________________________________________________________________________________________
batch_normalization_13 (BatchNo (None, 13, 13, 80)   320         re_lu_12[0][0]                   
__________________________________________________________________________________________________
average_pooling2d (AveragePooli (None, 3, 3, 80)     0           batch_normalization_13[0][0]     
__________________________________________________________________________________________________
flatten (Flatten)               (None, 720)          0           average_pooling2d[0][0]          
__________________________________________________________________________________________________
dense (Dense)                   (None, 2)            1442        flatten[0][0]                    
==================================================================================================

I followed the tutorial to freeze the model and to optimize it using Tensorflow 1.5 tensorflow.python.tools.optimize_for_inference tool.

All steps worked fine and now I can load my model with OpenCV both with

net = cv2.dnn.readNetFromTensorflow("optmized_graph.pb", "optmized_graph.pbtxt")
# or
net = cv2.dnn.readNet("optmized_graph.pb", "optmized_graph.pbtxt")

But I'm getting some weird error, when I try to get the predictions:

image = np.random.rand(1, 50, 50).astype("float32")
net.setInput(image, 'x')

# or

image = np.random.rand(1, 50, 50, 1).astype("float32")
net.setInput(image, 'x')

The error is

[ERROR:0] global /tmp/pip-req-build-6amqbhlx/opencv/modules/dnn/src/dnn.cpp (3441) getLayerShapesRecursively OPENCV/DNN: [Convolution]:(mask-try4/conv2d/Conv2D): ge
tMemoryShapes() throws exception. inputs=1 outputs=0/1 blobs=2
[ERROR:0] global /tmp/pip-req-build-6amqbhlx/opencv/modules/dnn/src/dnn.cpp (3447) getLayerShapesRecursively     input[0] = [ 1 50 ]
[ERROR:0] global /tmp/pip-req-build-6amqbhlx/opencv/modules/dnn/src/dnn.cpp (3455) getLayerShapesRecursively     blobs[0] = CV_32FC1 [ 20 1 3 3 ]
[ERROR:0] global /tmp/pip-req-build-6amqbhlx/opencv/modules/dnn/src/dnn.cpp (3455) getLayerShapesRecursively     blobs[1] = CV_32FC1 [ 20 1 ]
[ERROR:0] global /tmp/pip-req-build-6amqbhlx/opencv/modules/dnn/src/dnn.cpp (3457) getLayerShapesRecursively Exception message: OpenCV(4.4.0) /tmp/pip-req-build-6am
qbhlx/opencv/modules/dnn/src/layers/convolution_layer.cpp:348: error: (-215:Assertion failed) ngroups > 0 && inpCn % ngroups == 0 && outCn % ngroups == 0 in functio
n 'getMemoryShapes'

Traceback (most recent call last):
  File "test_with_opencv.py", line 17, in <module>
    detections = net.forward()
cv2.error: OpenCV(4.4.0) /tmp/pip-req-build-6amqbhlx/opencv/modules/dnn/src/layers/convolution_layer.cpp:348: error: (-215:Assertion failed) ngroups > 0 && inpCn %
ngroups == 0 && outCn % ngroups == 0 in function 'getMemoryShapes'

My questions are:

  • What I'm doing wrong?
  • How can I overcome the error and get the model to work?
  • My the shape in the error message is input[0] = [ 1 50 ]? (I'm passing (1,50,50))

Versions:

  • Tensorflow: 2.3.0
  • OpenCV: 4.4.0.44
  • Python: 3.6.9

Post on StackOverflow: https://stackoverflow.com/questions/6...

edit retag flag offensive close merge delete

Comments

imho, you have to pass 4 dimensional input to net.setInput()

berak gravatar imageberak ( 2020-10-20 03:18:58 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2020-10-20 15:24:36 -0500

Boris Brodski gravatar image

updated 2020-10-22 04:51:21 -0500

After being hinted by @berak, that the input should be 4 dimensional, I found out, that

image = np.random.rand(1, 1, 50, 50)

works just fine!

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2020-10-20 02:30:23 -0500

Seen: 44 times

Last updated: Oct 22