You need to enable JavaScript to run this app. 转换faced目标检测模型为TFLite模型时出现protobuf解析错误
优惠活动
大模型
产品
解决方案
定价
更多

转换faced目标检测模型为TFLite模型时出现protobuf解析错误

问题根因

该报错并非需要你手动修改数据传输的类型配置,核心原因是你当前使用的TensorFlow 2.x版本,与faced模型导出时使用的TensorFlow 1.x版本格式不兼容,旧版SavedModel的protobuf结构在新版TF的解析逻辑下会触发格式校验失败。

解决方案

提供两种可直接落地的处理路径,按需选择即可:

路径1:使用TensorFlow 1.15版本完成转换(兼容性最高)

faced项目基于TensorFlow 1.x开发,TensorFlow 1.15是1.x分支的最终版本,兼容旧版SavedModel格式同时内置TFLite转换能力。

  1. 新建兼容环境:
conda create -n tf115 python=3.7
conda activate tf115
pip install tensorflow==1.15
  1. 使用适配TF1.15的转换代码:
import tensorflow as tf

# 转换模型
saved_model_dir = '/directory_previous_to_saved_model.pb'
converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
# 遇到自定义算子时可打开下方两个配置
# converter.allow_custom_ops = True
# converter.target_ops = [tf.lite.OpsSet.TFLITE_BUILTINS, tf.lite.OpsSet.SELECT_TF_OPS]
tflite_model = converter.convert()

# 保存模型
with open('/directory/faced.tflite', 'wb') as f:
  f.write(tflite_model)

路径2:在现有TensorFlow 2.x环境下转换

通过TF2提供的向前兼容接口先加载旧版模型,重新导出为TF2兼容的SavedModel格式后再转TFLite:

import tensorflow as tf

# 第一步:加载TF1.x模型并重新导出为兼容格式
saved_model_dir = '/directory_previous_to_saved_model.pb'
export_dir = './tf2_compatible_saved_model'

with tf.compat.v1.Session() as sess:
    # 加载旧版SavedModel
    tf.compat.v1.saved_model.loader.load(sess, [tf.saved_model.tag_constants.SERVING], saved_model_dir)
    # 重新导出为TF2兼容格式
    builder = tf.compat.v1.saved_model.builder.SavedModelBuilder(export_dir)
    builder.add_meta_graph_and_variables(sess, [tf.saved_model.tag_constants.SERVING])
    builder.save()

# 第二步:转换兼容格式的模型为TFLite
converter = tf.lite.TFLiteConverter.from_saved_model(export_dir)
# 遇到自定义算子时可打开下方配置
# converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS, tf.lite.OpsSet.SELECT_TF_OPS]
tflite_model = converter.convert()

with open('/directory/faced.tflite', 'wb') as f:
  f.write(tflite_model)
注意事项
  • 转换前先校验saved_model.pb文件完整性,确认文件下载/传输过程中没有损坏,避免因文件本身错误触发解析失败
  • 若转换过程中出现算子不支持报错,可开启代码中注释的自定义算子/SELECT_TF_OPS配置

内容的提问来源于stack exchange,提问作者GoodMFM

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.09 13:15:03