Skip to content

eltwise_layer.cpp:116: error: (-215:Assertion failed) inputs[0] == inputs[i] in function 'getMemoryShapes' #15534

Closed

Description

@harristmv
System information (version)
  • OpenCV => : 4.1.1.26
  • Operating System / Platform => Ubuntu 18.04
  • Compiler => ython3.5
Detailed description

Hello I am having the following issue. I am trying to freeze EfficientNet taken from this repo and use the protobuf file for using it with OpenCV dnn module.

The model is a simple classification network. Right after the the Feature Extractor specified in the link, I just try add an extra dense & and an extra classification layer of num_classes.

I have frozen several networks and used them with the dnn library before and I know all the issues that can arise, which I have already tried.

Example code in python (also seen in attached script below):

net = cv2.dnn.readNetFromTensorflow('frozen.pb') 
x_cv = np.random.random((224, 224, 3)).astype(np.uint8)
blob = cv2.dnn.blobFromImage(x_cv, 1.0, (224, 224), (0, 0, 0))
net.setInput(blob)
# (1 x 200)
s = net.forward().shape
print(s)

However life is not that easy. This is the following error:

error: OpenCV(4.1.1) /io/opencv/modules/dnn/src/layers/eltwise_layer.cpp:116: error: (-215:Assertion failed) inputs[0] == inputs[i] in function 'getMemoryShapes'

Eventually I Found out that the problem is caused when using the Squeeze and excitation module (SE module for short). If I disable the flag & remove those modules from the network, the forward pass works.

In my opinion is the flow of the graph of the SE Module that is not acceptable from OpenCV from going to an average pooling to 1x1 convs and finally multiplying the features.

Some remarks:

I had this issue with SE modules in the past, with the difference that the model was using more basic operations such as:

  • reduce_mean across the features instead of average pooling

  • fully connected layers instead of 1x1 Convolutions, since they result in the same number of features.

Additionally:

  • I used the optimize_for_inference_lib tool with no success.

  • Using the following command: net.getUnconnectedOutLayersNames() gave me the following results:

    ['block1a_se_squeeze/Mean/flatten', 'block2a_se_squeeze/Mean/flatten', 'block2b_se_squeeze/Mean/flatten', 'block3a_se_squeeze/Mean/flatten', 'block3b_se_squeeze/Mean/flatten', 'block4a_se_squeeze/Mean/flatten', 'block4b_se_squeeze/Mean/flatten', 'block4c_se_squeeze/Mean/flatten', 'block5a_se_squeeze/Mean/flatten', 'block5b_se_squeeze/Mean/flatten', 'block5c_se_squeeze/Mean/flatten', 'block6a_se_squeeze/Mean/flatten', 'block6b_se_squeeze/Mean/flatten', 'block6c_se_squeeze/Mean/flatten', 'block6d_se_squeeze/Mean/flatten', 'block7a_se_squeeze/Mean/flatten', 'avg_pool/Mean/flatten', 'dense_1/Softmax']

Follow up

I would like to know if there is a possible way to add the SE Module either by

  • reshaping the network in a compatible manner
  • transforming the graph and using a .pbtxt file additionally
  • register the module as custom & override the getMemoryShapes()

Of course the easiest solution would be preferrable :)

Steps to reproduce

Running the attached .py (extension currently txt) file, will follow the pipeline:

  • Initialize EfficientnetB0 (smallest) with SE modules.
  • Create a dummy training set and run keras training fit() function.
  • Save the model as keras extension and reset graph
  • Load & freeze model with standard keras freezing method. You should see 344 variables frozen in the terminal.
  • Load model for OpenCV, create dummy blob & run net.forward(). It will crash with the aforementioned error.

opencv_efficientnet.txt

Making the model freezable.

If you jump to line 254 of the attached script, you can override the has_se=True to False. This will make the model compatible with OpenCV, proving the detailed description above that this is the factor that makes it show the assertion error.

Activity

  1. dkurt commented on Sep 18, 2019

    @dkurt
    Member

    Load model for OpenCV, create dummy blob & run net.forward(). It will crash with the aforementioned error.

    Please provide the frozen model even with dummy weights (frozen.pb). The issue should be reproducible without training steps.

    net = cv2.dnn.readNetFromTensorflow('frozen.pb')

  2. added
    needs reproducer
    on Sep 18, 2019
  3. harristmv commented on Sep 18, 2019

    @harristmv
    Author

    Hello,

    Since I cannot upload the .pb file due to size, even after compressing, I am attaching a google drive link:

    https://drive.google.com/file/d/1CuNCRfdlNdvoTLoPl_E9bWC0RofuFhAJ/view

  4. harristmv commented on Sep 20, 2019

    @harristmv
    Author

    Update

    By doing some debugging, the layer that causes the problem is this:

    x = layers.multiply([x, se_tensor], name=prefix + 'se_excite')
    

    This causes the first SE Module the following:

    se_tensor is of shape [1, 1, 1, 32]
    x is of shape [1, 112, 112, 32]
    

    Keras allows the element-wise multiplication to happen by using the layers.multiply layer, but not OpenCV. OpenCV complains because the shapes internally are:

    x = [1, 32, 112, 112] # Channel first 
    se_tensor = [1, 32, 1, 1]
    

    And gives the above error in eltwise_layer.cpp.

    Already tried several stuff including:

    1. Reshaping the network to be dense layers.
      • This complains that internally does an ExpandDims to keep the flow of the graph, resulting in the
        following:
    cv2.error: OpenCV(4.1.1) /io/opencv/modules/dnn/src/dnn.cpp:525: error: (-2:Unspecified error) Can't create layer "ExpandDims" of type "ExpandDims" in function 'getLayerInstance'
    
    1. Tiling the se_tensor to become of shape [1, 112, 112, 32] with tf.keras.backend.tile
      • This complains that internally now doesn't have a Tile Layer and gives the same error as above:
    cv2.error: OpenCV(4.1.1) /io/opencv/modules/dnn/src/dnn.cpp:525: error: (-2:Unspecified error) Can't create layer "Facenet/lambda/Tile" of type "Tile" in function 'getLayerInstance'
    

    I tried to write the Tile Layer & register it as a custom layer with the following code:

    class TileLayer(object):
        def __init__(self, params, blobs):
            self.height = blobs[0][0][1]
            self.width = self.height
    
        def getMemoryShapes(self, inputs):
            _in, _out = inputs[0], inputs[1]
            return [[_in.shape[0], _in.shape[1], self.height, self.width]]
    
        def forward(self, inputs):
            return [np.tile(inputs[0], [1, 1, self.height, self.width])]
    

    I am aware the above code might not be correct completely. However I get the following error which I cannot debug:

    cv2.error: OpenCV(4.1.1) /io/opencv/modules/dnn/misc/python/pyopencv_dnn.hpp:146: error: (-213:The function/feature is not implemented) Failed to call "getMemoryShapes" method in function 'getMemoryShapes'
    

    Any leads/recommendations ?

  5. r0l1 commented on Dec 19, 2019

    @r0l1

    I am having the same problem with mobilenetv3 classification models.

  6. dkurt commented on Dec 19, 2019

    @dkurt
    Member

    @r0l1, As a workaround you may try to apply this patch:

    --- a/modules/dnn/src/tensorflow/tf_importer.cpp
    +++ b/modules/dnn/src/tensorflow/tf_importer.cpp
    @@ -1574,8 +1574,8 @@ void TFImporter::populateNet(Net dstNet)
                 }
                 else
                 {
    -                layerParams.set("operation", "prod");
    -                int id = dstNet.addLayer(name, "Eltwise", layerParams);
    +                layerParams.set("axis", 0);
    +                int id = dstNet.addLayer(name, "Scale", layerParams);
                     layer_id[name] = id;
     
                     for (int ii = 0; ii < layer.input_size(); ii++)

    Please note that this is a workaround and may break other networks.

  7. r0l1 commented on Dec 19, 2019

    @r0l1

    @dkurt thank you for the hint! However my models are ONNX models trained with mxnet. I'll have a look at the onnx importer.

    The error logs are slightly different:

    OpenCV(4.2.0-pre) /tmp/opencv/src/modules/dnn/src/layers/eltwise_layer.cpp:209: error: (-215:Assertion failed) inputs[0][j] == inputs[i][j] in function 'getMemoryShapes'
    
  8. added and removed
    needs reproducer
    on Feb 29, 2020
  9. dkurt commented on Mar 5, 2020

    @dkurt
    Member

    @r0l1, for ONNX model please try #16738

  10. r0l1 commented on Mar 7, 2020

    @r0l1

    @dkurt perfect! works now. Thank you!
    @harristmv could you please check if this fixes also your problem?

    Edit: Still broken for the openvino backend:

    OpenCV(4.2.0-dev) /tmp/opencv/opencv/modules/dnn/src/layers/scale_layer.cpp:234: error: (-215:Assertion failed) !blobs.empty() in function 'initInfEngine'
    
  11. dkurt commented on Mar 7, 2020

    @dkurt
    Member

    @r0l1, you need build with OpenVINO 2020.1 and see if nGraph backend enabled (see wiki page https://github.com/opencv/opencv/wiki/Intel%27s-Deep-Learning-Inference-Engine-backend). And use corresponding backend enum value (we will make it by default in the next release by #16746)

  12. r0l1 commented on Mar 7, 2020

    @r0l1

    @dkurt thanks. I am already using OpenVino 2020.1. Will try if it's compatible with the Myriad VPUs...

    Edit: I have set the environment variable to OPENCV_DNN_BACKEND_INFERENCE_ENGINE_TYPE="NGRAPH" and checked if OpenCV is build with ngraph, but the error is still the same. Did I forget a step?

  13. dkurt commented on Mar 8, 2020

    @dkurt
    Member

    Please check if you pulled the changes from #16753

  14. r0l1 commented on Mar 8, 2020

    @r0l1

    @dkurt I just pulled from the latest master branch and included also #16746 for the default backend change. Now everything works if I use the CPU inference engine backend. I'll test the Myriad target once I am back at work. Thank you for the great work imho. Things are progressing quite fast for OpenCV during the last year.

    Edit: Just checked the Myriad target. Works as expected.

  15. linked a pull request that will close this issueSupport for MobileNetV3-SSD from TensorFlow #16760on Mar 8, 2020
  16. dkurt commented on Mar 8, 2020

    @dkurt
    Member

    @harristmv, please test #16760. It now enables Squeeze & Excite block from MobileNet V3.

  17. harristmv commented on Mar 9, 2020

    @harristmv
    Author

    @dkurt I Applied the changes on 4.2.0 version, for EfficientNet (which I had issue with).

    Forward pass succeeds :)

    I will wait for this when this is fully merged, I suppose also on the latest version (4.2.0) right?

    Thanks for your efforts! :)

  18. dkurt commented on Mar 9, 2020

    @dkurt
    Member

    I will wait for this when this is fully merged, I suppose also on the latest version (4.2.0) right?

    4.2.0 has been already released: https://github.com/opencv/opencv/releases/tag/4.2.0

    So first we will merge the fix to 3.4 branch then port to master (weekly basis, like this one: #16753)

    Thanks for experiment!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions