【问题标题】:Converting saved_model.pb to model.tflite将 saved_model.pb 转换为 model.tflite
【发布时间】:2020-12-18 12:05:37
【问题描述】:

张量流版本:2.2.0

操作系统:Windows 10

我正在尝试将 saved_model.pb 转换为 tflite 文件。

这是我正在运行的代码:

import tensorflow as tf

# Convert
converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir='C:\Data\TFOD\models\ssd_mobilenet_v2_quantized')
tflite_model = converter.convert()


fo = open("model.tflite", "wb")
fo.write(tflite_model)
fo.close

此代码在转换时出错:

  File "C:\Users\Mr.Ace\AppData\Roaming\Python\Python38\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\Mr.Ace\AppData\Roaming\Python\Python38\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 "c:/Data/TFOD/convert.py", line 13, in <module>
    tflite_model = converter.convert()
  File "C:\Users\Mr.Ace\AppData\Roaming\Python\Python38\site-packages\tensorflow\lite\python\lite.py", line 1076, in convert
    return super(TFLiteConverterV2, self).convert()
  File "C:\Users\Mr.Ace\AppData\Roaming\Python\Python38\site-packages\tensorflow\lite\python\lite.py", line 899, in convert
    return super(TFLiteFrozenGraphConverterV2,
  File "C:\Users\Mr.Ace\AppData\Roaming\Python\Python38\site-packages\tensorflow\lite\python\lite.py", line 629, in convert
    result = _toco_convert_impl(
  File "C:\Users\Mr.Ace\AppData\Roaming\Python\Python38\site-packages\tensorflow\lite\python\convert.py", line 569, in toco_convert_impl
    data = toco_convert_protos(
  File "C:\Users\Mr.Ace\AppData\Roaming\Python\Python38\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>

【问题讨论】:

  • 我知道可以使用 tf-nightly 来解决这个问题,但我不想使用它。
  • 构建模型的 tf 版本是什么?
  • TensorFlow 版本为 2.2.0
  • 所以您正在使用从 TF2 模型动物园下载的模型进行对象检测。
  • 是的,我正在使用新模型动物园的更新,文件夹名称是旧模型名称,但模型是最新的 tfv2

标签: python tensorflow tensorflow2.0 tensorflow-lite


【解决方案1】:

好的,我终于解决了!

我所做的是使用 tf-nightly 并使用以下 Python 脚本:

import tensorflow as tf

saved_model_dir = "C:/Data/TFOD/models/ssd_mobilenet_v2_quantized/tflite"
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()

fo = open(
    "C:/Data/TFOD/models/ssd_mobilenet_v2_quantized/tflite/model.tflite", "wb")
fo.write(tflite_model)
fo.close

这解决了问题,您可以转换为 .tflite

【讨论】:

    【解决方案2】:

    Tensorflow 在model/object_detection 文件夹中提供了一个名为export_tflite_ssd_graph.py 的python 文件,可用于将保存的模型转换为tflite 格式。

    This 是该文件的 GitHub 链接。下载models目录的时候就下载好了。

    如何使用它:

    python object_detection/export_tflite_ssd_graph.py \
        --pipeline_config_path path/to/ssd_mobilenet.config \
        --trained_checkpoint_prefix path/to/model.ckpt \
        --output_directory path/to/exported_model_directory
    

    预期的输出将在目录中
    path/to/exported_model_directory(如果不存在则创建)
    内容:

    • tflite_graph.pbtxt
    • tflite_graph.pb

    完整使用可以阅读文件里面的cmets。

    【讨论】:

    • 嗨 Aniket,感谢您的快速回答。我收到此错误:RuntimeError: tf.placeholder() is not compatible with eager execution。我需要禁用急切执行吗?
    • 尝试禁用它。
    • 好的,我使用tf.compat.v1.disable_eager_execution() 禁用了它,现在还有另一个问题:NameError: name 'graph_matcher' is not defined。调用tf.app.run(main) 时会发生这种情况。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-18
    • 2021-11-18
    • 2020-04-17
    • 1970-01-01
    • 1970-01-01
    • 2014-01-17
    相关资源
    最近更新 更多