我训练了一个模型并将其转换为.tflite模型。 我用以下方法完成了列车后量化:
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()
但是当我尝试在RaspberryPi上使用模型进行推理时,我得到以下错误
Traceback (most recent call last):
File "tf_lite_test.py", line 8, in
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.
当我将模型转换为tflite而不应用任何训练后量化时,我没有错误。 这是我用来转换模型而不应用训练后量化的代码。
import tensorflow as tf
converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
tflite_quant_model = converter.convert()
这是我的模特:
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')
])
如何应用训练后量化并加载模型而不会出现此错误?