我使用预训练的 InceptionV3 模型进行了学习迁移,并保存了 h5 模型文件。之后,我就可以做出预测了。 现在,我想使用 TFLiteConverter.convert() 方法将 h5 模型转换为 tflite 文件,如下所示:
converter = lite.TFLiteConverter.from_keras_model_file('keras.model.h5')
tflite_model = converter.convert()
但是我得到这个错误:
File "from_saved_model.py", line 28, in <module>
tflite_model = converter.convert()
File "C:\Anaconda3\lib\site-packages\tensorflow\contrib\lite\python\lite.py", line 409, in convert
"invalid shape '{1}'.".format(_tensor_name(tensor), shape))
ValueError: None is only supported in the 1st dimension. Tensor 'input_1' has invalid shape '[None, None, None, 3]'
我在 Windows 10 64 位上运行 Anaconda Python 3.6.8。预先感谢您的帮助!
最佳答案
将模型从 TensorFlow 转换为 TensorFlow Lite 时,只允许批量大小(索引 0)为None
。在调用 from_keras_model_file
以使输入数组形状有效时,您应该能够使用 input_shapes
参数。对于 InceptionV3 模型,input_shapes
参数通常是 {'Mul' : [1,299,299,3]}
。
TFLiteConverter.from_keras_model_file
的文档可用 here .接受的参数如下(从文档中复制):
from_keras_model_file(
cls,
model_file,
input_arrays=None,
input_shapes=None,
output_arrays=None
)
关于Tensorflow - h5 模型到 tflite 转换错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54211413/