这是TensorFlow文档提供的用于将tf2模型转换为tflite的脚本:https://www.tensorflow.org/lite/convert#cmdline
我只将路径添加到了SavedModel目录。
import tensorflow as tf
# Convert the model
converter = tf.lite.TFLiteConverter.from_saved_model('./exported-models/my_model/saved_model') # path to the SavedModel directory
tflite_model = converter.convert().
# Save the model.
with open('model.tflite', 'wb') as f:
f.write(tflite_model)
我在tflite_model = converter.convert()时遇到错误。我应该添加一些东西吗?如果我删除点(。),那么我会遇到以下错误:
Traceback (most recent call last):
File "C:\Users\eddie\anaconda3\envs\tf2.3\lib\site-packages\tensorflow\lite\python\convert.py", line 196, in toco_convert_protos
model_str = wrap_toco.wrapped_toco_convert(model_flags_str,
File "C:\Users\eddie\anaconda3\envs\tf2.3\lib\site-packages\tensorflow\lite\python\wrap_toco.py", line 32, in wrapped_toco_convert
return _pywrap_toco_api.TocoConvert(
Exception: <unknown>:0: error: loc("Func/StatefulPartitionedCall/input/_0"): requires all operands and results to have compatible element types
<unknown>:0: note: loc("Func/StatefulPartitionedCall/input/_0"): see current operation: %1 = "tf.Identity"(%arg0) {device = ""} : (tensor<1x?x?x3x!tf.quint8>) -> tensor<1x?x?x3xui8>
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "./converter.py", line 5, in <module>
tflite_model = converter.convert()
File "C:\Users\eddie\anaconda3\envs\tf2.3\lib\site-packages\tensorflow\lite\python\lite.py", line 1076, in convert
return super(TFLiteConverterV2, self).convert()
File "C:\Users\eddie\anaconda3\envs\tf2.3\lib\site-packages\tensorflow\lite\python\lite.py", line 899, in convert
return super(TFLiteFrozenGraphConverterV2,
File "C:\Users\eddie\anaconda3\envs\tf2.3\lib\site-packages\tensorflow\lite\python\lite.py", line 629, in convert
result = _toco_convert_impl(
File "C:\Users\eddie\anaconda3\envs\tf2.3\lib\site-packages\tensorflow\lite\python\convert.py", line 569, in toco_convert_impl
data = toco_convert_protos(
File "C:\Users\eddie\anaconda3\envs\tf2.3\lib\site-packages\tensorflow\lite\python\convert.py", line 202, in toco_convert_protos
raise ConverterError(str(e))
tensorflow.lite.python.convert.ConverterError: <unknown>:0: error: loc("Func/StatefulPartitionedCall/input/_0"): requires all operands and results to have compatible element types
<unknown>:0: note: loc("Func/StatefulPartitionedCall/input/_0"): see current operation: %1 = "tf.Identity"(%arg0) {device = ""} : (tensor<1x?x?x3x!tf.quint8>) -> tensor<1x?x?x3xui8>