You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将TensorFlow frozen graph转换为可在安卓运行的TensorFlow Lite模型?

问题解答

1. 如何获取TF Detection Zoo模型的输入输出张量与形状配置

  • 你之前使用的层信息查询方法输出结果不准确,可使用以下代码遍历冻结图的所有算子和对应张量形状,筛选正确的输入输出:
import tensorflow as tf
def print_all_tensors(pb_path):
    with tf.gfile.GFile(pb_path, "rb") as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())
    with tf.Graph().as_default() as graph:
        tf.import_graph_def(graph_def, name="")
    # 打印所有算子名、输出张量形状
    for op in graph.get_operations():
        print(f"算子名: {op.name}, 输出形状: {op.outputs[0].shape}")
  • 原生TF1 Detection Zoo的模型不存在normalized_input_image_tensor这个张量,这个是做过TFLite适配导出后的模型才有的归一化后输入张量,原生模型的输入张量通常为image_tensor,形状为[1, 输入高度, 输入宽度, 3],输入数据为0-255范围的uint8类型。
  • 你之前用到的TFLite_Detection_PostProcess系列输出张量是TFLite自定义后处理OP的输出,原生冻结图没有这个OP,需要先完成TFLite适配的预导出步骤才能生成对应张量。
  • 原生冻结图的默认输出张量为detection_boxes、detection_scores、detection_classes、num_detections四个。

2. Faster RCNN系列模型能否转换为TFLite格式

  • 公开的常规Faster RCNN模型不适合转换为移动端可用的TFLite格式,官方TFLite Task库原生仅支持SSD系列检测模型。Faster RCNN包含大量动态控制流、动态形状的算子,即使强行转换为TFLite文件,也无法在移动端解释器中正常运行,也无法直接适配官方的TFLite检测示例应用。

3. Missing 'operators' section in subgraph报错解决方法

该报错本质是转换得到的TFLite模型结构损坏,没有可执行的算子,可按以下顺序排查解决:

  • 转换时补充OP兼容配置,允许自定义OP和TensorFlow内置OP fallback:
import tensorflow as tf

converter = tf.lite.TFLiteConverter.from_frozen_graph(
    graph_def_file='frozen_graph.pb',
    input_shapes = {'image_tensor':[1, 300, 300, 3]},
    input_arrays = ['image_tensor'],
    output_arrays = ['detection_boxes','detection_scores', 'detection_classes','num_detections']
)
# 新增以下两行配置
converter.allow_custom_ops = True
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS, tf.lite.OpsSet.SELECT_TF_OPS]
tflite_model = converter.convert()

with open('model.tflite', 'wb') as f:
  f.write(tflite_model)
  • 不要使用TF2直接转换TF1的Faster RCNN冻结图,这类模型本身不兼容移动端TFLite部署,建议更换为SSD系列模型,先完成TFLite适配的预导出步骤后再做转换。
  • 如果使用官方示例应用部署,必须选择SSD系列模型,且导出时需要开启TFLite后处理适配,否则输出结构不匹配也会导致模型加载失败。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.23 19:06:00