训练后量化后的故障加载模型

时间:2019-06-23 09:48:19

标签: tensorflow tensorflow-lite

我已经训练了一个模型,并将其转换为.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 <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.

当我在不应用任何训练后量化的情况下将模型转换为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')
])

如何应用训练后量化并加载模型而不会出现此错误?

1 个答案:

答案 0 :(得分:1)

也许您需要重建tflite运行时。可能太旧了,无法使用此模型。请参阅此处的说明:https://www.tensorflow.org/lite/guide/build_rpi