为了账号安全,请及时绑定邮箱和手机立即绑定

将 save_model.pb 转换为 model.tflite

将 save_model.pb 转换为 model.tflite

海绵宝宝撒 2023-07-05 10:08:10
张量流版本:2.2.0操作系统:Windows 10我正在尝试将 saving_model.pb 转换为 tflite 文件。这是我正在运行的代码:import tensorflow as tf# Convertconverter = 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(
查看完整描述

2 回答

?
慕尼黑8549860

TA贡献1818条经验 获得超11个赞

好吧,我终于解决了!


我所做的是使用 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


查看完整回答
反对 回复 2023-07-05
?
沧海一幻觉

TA贡献1824条经验 获得超5个赞

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


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

如需完整使用,您可以阅读文件内的注释。


查看完整回答
反对 回复 2023-07-05
  • 2 回答
  • 0 关注
  • 112 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信