Skip to main content
rickforescue
Associate II
March 16, 2022
Solved

TOOL ERROR: operands could not be broadcast together with shapes (32,7200) (32,)

  • March 16, 2022
  • 9 replies
  • 5394 views

0693W00000Kcsu9QAB.pngI have a convolutional model with convolutional layers bach normalization layers and Dense layers at the end. The model is converted to a tflite model. The inferencing works perfect on computer using tflite but When I try to deploy it on the nucleo h743zi2 I get this error.

The network layers ans its shape look like it is shown in the pic. Has anyone come across this problem?

As far my understanding goes, I did not do wrong model creation. It is some bad interpretation from STM Cube library.

Additional Info: I am using STM Cube AI version 7.1.0

Thanks in advance

Rick

    Best answer by fauvarque.daniel

    The problem comes from the optimization to fold the batch normalization.

    With the undocumented option "--optimize.fold_batchnorm False" the model is analyzed correctly.

    You can pass the option directly to the stm32ai command line or if you are using X-Cube-AI inside STM32CubeMX you can add this option in the first screen of the advanced parameter window

    Regards

    Daniel

    9 replies

    fauvarque.daniel
    ST Employee
    March 16, 2022

    Can you share the model so I can reproduce and have the development team fix the problem ?

    Thanks in advance

    Regards

    Daniel

    rickforescue
    Associate II
    March 16, 2022

    Hello @fauvarque.daniel​,

    Thanks for quicl reply. Should I share you the keras .h5 file ?

    fauvarque.daniel
    ST Employee
    March 16, 2022

    yes please

    rickforescue
    Associate II
    March 16, 2022

    Here is the .h5 keras file in zipped format. The model is relatively big to fit in internal flash. I quantized it using tflite Let me know if you have some other questions

    Thanks

    Rick

    fauvarque.daniel
    ST Employee
    March 16, 2022

    If I may, if you could provide also the quantized tflite so I have exactly the file you are using.

    Daniel

    fauvarque.daniel
    ST Employee
    March 16, 2022

    I've reproduced the problem with the h5, I let you know if there is a workaround

    rickforescue
    Associate II
    March 16, 2022

    Ok Thankyou @fauvarque.daniel​ 

    fauvarque.daniel
    fauvarque.danielBest answer
    ST Employee
    March 17, 2022

    The problem comes from the optimization to fold the batch normalization.

    With the undocumented option "--optimize.fold_batchnorm False" the model is analyzed correctly.

    You can pass the option directly to the stm32ai command line or if you are using X-Cube-AI inside STM32CubeMX you can add this option in the first screen of the advanced parameter window

    Regards

    Daniel

    DanF
    Associate II
    November 1, 2023

    Is this still the correct syntax? I am using Cube AI 8.1.0 and when I add: --optimize.fold_batchnorm False" I get an unrecognized argument error. 

    DanF
    Associate II
    November 1, 2023

    A little more information. The error itself only occurs when I attempt to quantize the model into 8 bit data types by adding these lines when creating the TF Lite model:

     

    converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
    converter.inference_input_type = tf.int8 # or tf.uint8
    converter.inference_output_type = tf.int8 # or tf.uint8
     
    Without those lines there is no TOOL ERROR at all.
     
    Update: It's only this line causing the problem:
    converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
    I suspect that STM code is using some other ops and thus this fails. 
    rickforescue
    Associate II
    March 18, 2022

    Thanks a lot @fauvarque.daniel​ . The solution works. :)

    Well, I am curious. Can you give little bit more insight about it. What do you mean by folding the bachnorm ?

    Thanks

    Rick

    fauvarque.daniel
    ST Employee
    March 18, 2022

    During code generation there is an optimization phase that can merge some layers, a typical case is a Conv2D followed by a batchNormalization followed by a ReLU, The optimized graph will just have a Conv2D

    For example for this part of the model

    0693W00000KdCYTQA3.jpgAfter the optimizer the model will look like

    0693W00000KdCZaQAN.jpg 

    Regards

    Daniel

    rickforescue
    Associate II
    March 21, 2022

    Thanks. I see. Its interesting inisight.