将微调保存的模型导出到 TensorFlow Lite 错误
Exporting fine-tuned saved model to TensorFlow Lite error
我fine.tuned一个SSD模型来识别自定义对象。
我遵循了教程,运行 训练过程并导出了模型,我对其进行了推理测试,一切都很好。
所以,现在我的结构如下:
exported models/
|
---- SSD_custom_model/
|
--------checkpoint/
--------saved_model/
--------pipeline.config
我假设这就是 TensorFlow 文档中所谓的“已保存模型”。
因此,我想将此模型转换为 TensorFlow Lite 以在 Android 设备上进行测试,我查看了教程并正在尝试:
import tensorflow as tf
saved_model_dir = 'exported-models/SSD_custom_model/'
# # Convert the model
## I tried either just
# converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
## or, with more options
converter = tf.lite.TFLiteConverter.from_saved_model(
saved_model_dir, signature_keys=['serving_default'])
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.experimental_new_converter = True
converter.target_spec.supported_ops = [
tf.lite.OpsSet.TFLITE_BUILTINS, tf.lite.OpsSet.SELECT_TF_OPS]
tflite_model = converter.convert()
# Save the model.
with open('tflite/custom_model.tflite', 'wb') as f:
f.write(tflite_model)
我遇到了错误
File "/home/lews/anaconda3/envs/tf/lib/python3.8/site-packages/tensorflow/lite/python/convert.py", line 216, in toco_convert_protos
raise ConverterError(str(e))
tensorflow.lite.python.convert.ConverterError: <unknown>:0: error: loc(callsite(callsite("map/TensorArrayV2_1@__inference_call_func_11694" at "StatefulPartitionedCall@__inference_signature_wrapper_14068") at "StatefulPartitionedCall")): requires element_shape to be 1D tensor during TF Lite transformation pass
<unknown>:0: note: loc("StatefulPartitionedCall"): called from
<unknown>:0: error: loc(callsite(callsite("map/TensorArrayV2_1@__inference_call_func_11694" at "StatefulPartitionedCall@__inference_signature_wrapper_14068") at "StatefulPartitionedCall")): failed to legalize operation 'tf.TensorListReserve' that was explicitly marked illegal
<unknown>:0: note: loc("StatefulPartitionedCall"): called from
似乎是在抱怨输入形状 ('requires element_shape to be 1D tensor during TF Lite transformation pass')。也许我应该在微调过程之前对模型进行一些修改?还是在那之后?
你好,我在做同样的工作,遇到了同样的错误,但是我解决了。
我转换的模型是SSD-Mobile-v2,我用的是tensorflow2_4,所以我相信这对你有用。
你只需要新建一个conda环境(python3.8即可),然后安装tf-nightly:
pip install tf-nightly
需要注意的是tf-nightly的版本必须>=2.5。
一开始我用的是tf-nightly 2.3,又遇到了一个错误。然后我升级到2.5,转换器终于可以用了。
我fine.tuned一个SSD模型来识别自定义对象。 我遵循了教程,运行 训练过程并导出了模型,我对其进行了推理测试,一切都很好。 所以,现在我的结构如下:
exported models/
|
---- SSD_custom_model/
|
--------checkpoint/
--------saved_model/
--------pipeline.config
我假设这就是 TensorFlow 文档中所谓的“已保存模型”。 因此,我想将此模型转换为 TensorFlow Lite 以在 Android 设备上进行测试,我查看了教程并正在尝试:
import tensorflow as tf
saved_model_dir = 'exported-models/SSD_custom_model/'
# # Convert the model
## I tried either just
# converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
## or, with more options
converter = tf.lite.TFLiteConverter.from_saved_model(
saved_model_dir, signature_keys=['serving_default'])
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.experimental_new_converter = True
converter.target_spec.supported_ops = [
tf.lite.OpsSet.TFLITE_BUILTINS, tf.lite.OpsSet.SELECT_TF_OPS]
tflite_model = converter.convert()
# Save the model.
with open('tflite/custom_model.tflite', 'wb') as f:
f.write(tflite_model)
我遇到了错误
File "/home/lews/anaconda3/envs/tf/lib/python3.8/site-packages/tensorflow/lite/python/convert.py", line 216, in toco_convert_protos
raise ConverterError(str(e))
tensorflow.lite.python.convert.ConverterError: <unknown>:0: error: loc(callsite(callsite("map/TensorArrayV2_1@__inference_call_func_11694" at "StatefulPartitionedCall@__inference_signature_wrapper_14068") at "StatefulPartitionedCall")): requires element_shape to be 1D tensor during TF Lite transformation pass
<unknown>:0: note: loc("StatefulPartitionedCall"): called from
<unknown>:0: error: loc(callsite(callsite("map/TensorArrayV2_1@__inference_call_func_11694" at "StatefulPartitionedCall@__inference_signature_wrapper_14068") at "StatefulPartitionedCall")): failed to legalize operation 'tf.TensorListReserve' that was explicitly marked illegal
<unknown>:0: note: loc("StatefulPartitionedCall"): called from
似乎是在抱怨输入形状 ('requires element_shape to be 1D tensor during TF Lite transformation pass')。也许我应该在微调过程之前对模型进行一些修改?还是在那之后?
你好,我在做同样的工作,遇到了同样的错误,但是我解决了。
我转换的模型是SSD-Mobile-v2,我用的是tensorflow2_4,所以我相信这对你有用。
你只需要新建一个conda环境(python3.8即可),然后安装tf-nightly:
pip install tf-nightly
需要注意的是tf-nightly的版本必须>=2.5。
一开始我用的是tf-nightly 2.3,又遇到了一个错误。然后我升级到2.5,转换器终于可以用了。