cancel
Showing results for 
Search instead for 
Did you mean: 

VL53L8CX Hand Posture - can't do quantization.

Shion
Associate II

Error output when trying to quantize CNN2d_ST_HandPosture_8classes_hand_posture_ST_VL53L8CX_handposture_dataset.h5 in STM Edge AI Developer Cloud. For example, the error "Could not find cuda drivers on your machine, GPU will not be used." Why do I need cuda drivers on my machine when I am running in the cloud? If anyone knows the cause of this error, please let us know.

1 ACCEPTED SOLUTION

Accepted Solutions
Julian E.
ST Employee

Hello @Shion,

 

The "Could not find cuda drivers on your machine, GPU will not be used." is just a warning, it is not the blocking point here. As you say, you don't need to install cuda on your pc.

 

The model is quite small even unquantized.

If you really need to quantize it, you can do it manually, for example:

import tensorflow as tf

# Load the Keras model from .h5 file
h5_model_path = 'CNN2D_ST_HandPosture_8classes.h5'
model = tf.keras.models.load_model(h5_model_path)

# Convert to TFLite model with post-training dynamic range quantization
converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.optimizations = [tf.lite.Optimize.DEFAULT]

# Convert the model
tflite_model = converter.convert()

# Save the quantized model to file
tflite_model_path = 'CNN2D_ST_HandPosture_8classes_quantized.tflite'
with open(tflite_model_path, 'wb') as f:
    f.write(tflite_model)

print(f"Quantized model saved to {tflite_model_path}")

 

The model is smaller when quantized: 70kb -> 7kb. (h5 model -> tflitem model)

I let you verify this after convertion to C code.

 

In terms of inference time, it is the same:

Not quantized: 

JulianE_1-1754551782045.png

Quantized (with above script):

JulianE_2-1754552093374.png

 

In term of accuracy, it needs to be tested, I think that if model zoo does not quantize it, it may indicate that you should not do it. But you can try (in the example script, it is quantized with random data. Quantizing it with real data is Highly recommended).

 

Have a good day.

Julian


In order to give better visibility on the answered topics, please click on 'Accept as Solution' on the reply which solved your issue or answered your question.

View solution in original post

3 REPLIES 3
Julian E.
ST Employee

Hello @Shion,

 

The "Could not find cuda drivers on your machine, GPU will not be used." is just a warning, it is not the blocking point here. As you say, you don't need to install cuda on your pc.

 

The model is quite small even unquantized.

If you really need to quantize it, you can do it manually, for example:

import tensorflow as tf

# Load the Keras model from .h5 file
h5_model_path = 'CNN2D_ST_HandPosture_8classes.h5'
model = tf.keras.models.load_model(h5_model_path)

# Convert to TFLite model with post-training dynamic range quantization
converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.optimizations = [tf.lite.Optimize.DEFAULT]

# Convert the model
tflite_model = converter.convert()

# Save the quantized model to file
tflite_model_path = 'CNN2D_ST_HandPosture_8classes_quantized.tflite'
with open(tflite_model_path, 'wb') as f:
    f.write(tflite_model)

print(f"Quantized model saved to {tflite_model_path}")

 

The model is smaller when quantized: 70kb -> 7kb. (h5 model -> tflitem model)

I let you verify this after convertion to C code.

 

In terms of inference time, it is the same:

Not quantized: 

JulianE_1-1754551782045.png

Quantized (with above script):

JulianE_2-1754552093374.png

 

In term of accuracy, it needs to be tested, I think that if model zoo does not quantize it, it may indicate that you should not do it. But you can try (in the example script, it is quantized with random data. Quantizing it with real data is Highly recommended).

 

Have a good day.

Julian


In order to give better visibility on the answered topics, please click on 'Accept as Solution' on the reply which solved your issue or answered your question.
Shion
Associate II

Thank you for your comments! To be honest, I don't really understand ST Edge AI Developer Cloud, and I was just trying to confirm the flow by quantizing various models. I apologize for saying something inappropriate. Thank you very much for your detailed response.

No problem, feel free to ask anything.

 

Have a good day,

Julian


In order to give better visibility on the answered topics, please click on 'Accept as Solution' on the reply which solved your issue or answered your question.