【问题标题】:Problem converting a saved_model.pb file to .tflite file with custom shapes将 saved_model.pb 文件转换为具有自定义形状的 .tflite 文件时出现问题
【发布时间】:2020-01-19 21:41:28
【问题描述】:

我在 Windows 10 上使用 Tensorflow 2,我从 TensorFlow Detection Model Zoo. 下载了一个模型

我使用的模型是ssd.mobilenetv2.oid4

型号详情如下:

[<tf.Tensor 'image_tensor:0' shape=(None, None, None, 3) dtype=uint8>]

注意:我还提供了 freeze_inference_graph.pb 以及配置文件和检查点。

我使用 TensorFlowLiteConverter Snippet 将 saved_model.pb 文件转换为具有自定义形状的 .tflite:

import tensorflow as tf

input_dir = "D:\\Models\\ssd_mobilenet_v2_oid_v4_2018_12_12\\saved_model"

model = tf.saved_model.load(input_dir)
concrete_func = model.signatures[
tf.saved_model.DEFAULT_SERVING_SIGNATURE_DEF_KEY]
concrete_func.inputs[0].set_shape([None, None, None, 3])
converter = tf.lite.TFLiteConverter.from_concrete_functions([concrete_func])
tflite_model = converter.convert()

我收到以下错误:

Traceback (most recent call last):
File "C:\Users\Bhavin\Desktop\TensorFlow_pb_converter.py", line 10, in <module>
tflite_model = converter.convert()
File "C:\Users\Bhavin\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow_core\lite\python\lite.py", line 428, in convert
"invalid shape '{1}'.".format(_get_tensor_name(tensor), shape_list))
ValueError: None is only supported in the 1st dimension. Tensor 'image_tensor' has invalid shape '[None, None, None, 3]'

我尝试使用 toco 和 tflite_convert 但我得到了同样的错误。

我做错了什么以及如何将此 pb 文件转换为 tflite 文件?

【问题讨论】:

  • 您的问题解决了吗?我也有同样的需求。@Renegade

标签: tensorflow2.0 tensorflow-lite


【解决方案1】:

目前 tensorflow Lite 不支持转换动态形状,除了第一维。 考虑设置确切的形状而不是“无”

【讨论】:

  • 我将大小设置为 1,300,300,3,错误得到解决,但随后出现数据类型错误。模型的数据类型是 UINT8,但错误指出代码需要 FLOAT。我检查了互联网,但没有得到任何尝试使用 python 将模型转换为具有特定数据类型的 tflite 的示例。
  • 您可能缺少设置输入类型。见tensorflow.org/api_docs/python/tf/compat/v1/lite/…
  • 现在可以了吗?我只有一个动态形状的 saved_model.pb。如何将其转换为.tflite 文件?@KarimNosseir
  • TFLite 现在支持动态形状输入。遵循指南tensorflow.org/lite/convert#python_api 您是否尝试过但失败了?转换为 TFLite 文件时最好使用保存的模型格式
猜你喜欢
  • 1970-01-01
  • 2018-07-27
  • 1970-01-01
  • 1970-01-01
  • 2011-03-25
  • 2018-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多