【问题标题】:Exporting fine-tuned saved model to TensorFlow Lite error将微调保存的模型导出到 TensorFlow Lite 错误
【发布时间】:2021-02-24 12:59:34
【问题描述】:

我微调了 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

它似乎在抱怨输入形状('在 TF Lite 转换过程中要求 element_shape 是一维张量')​​。也许我应该在微调过程之前对模型进行一些修改?还是在那之后?

【问题讨论】:

    标签: tensorflow tensorflow2.0 tensorflow-lite object-detection-api


    【解决方案1】:

    你好,我在做同样的工作,遇到同样的错误,但我解决了。
    我转换的模型是SSD-Mobile-v2,而且我使用的是tensorflow 2_4,所以我相信这对你有用。

    你只需要新建一个conda环境(python 3.8就可以),然后安装tf-nightly:

    pip install tf-nightly  
    

    需要注意的是,tf-nightly 的版本必须 >= 2.5。
    一开始我用的是tf-nightly 2.3,又遇到了一个错误。然后我将它升级到2.5,转换器终于可以工作了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-16
      • 2021-06-07
      相关资源
      最近更新 更多