Trouble loading model after Post-training quantisation

Viewed 1051

I've trained up a model and converted it to a .tflite model. I have done post train quantization with the following:

import tensorflow as tf
converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
converter.optimizations = [tf.lite.Optimize.OPTIMIZE_FOR_SIZE]
tflite_quant_model = converter.convert()

But when I try to do inference using the model on a RaspberryPi I get the following error

Traceback (most recent call last):
File "tf_lite_test.py", line 8, in <module>
interpreter = tf.lite.Interpreter(model_path="converted_from_h5_model_with_quants.tflite")
File "/home/pi/.local/lib/python3.5/site-packages/tensorflow/lite/python/interpreter.py", line 46, in __init__
model_path))
ValueError: Didn't find op for builtin opcode 'CONV_2D' version '2'
Registration failed.

When I convert the model to tflite without applying any Post-training quantization I get no errors. This is the code I use to covert the model without applying Post-training quantization.

import tensorflow as tf
converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
tflite_quant_model = converter.convert()

This is my model:

model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(32, (3,3), activation='relu', input_shape=(IMG_SHAPE, IMG_SHAPE, 3)),
tf.keras.layers.MaxPooling2D(2, 2),

tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),

tf.keras.layers.Conv2D(128, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),

tf.keras.layers.Dropout(0.5),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(3, activation='softmax')
])

How do I apply Post-training quantization and load a model without getting this error?

1 Answers
Related