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

如何获取TensorFlow模型结构摘要,加载导出模型调用summary报错如何解决?

报错原因

你通过tf.saved_model.load加载的是TFOD API导出的部署用SavedModel格式,本质是序列化的推理计算图和权重的打包文件,返回的_UserObject是TensorFlow对非原生Keras实例的包装类,本身就没有summary()方法,所以会触发属性不存在的报错。


解决方法

分两种场景处理:

场景1:保留了训练时的pipeline配置和checkpoint文件(推荐)

这种方式可以拿到完整的分层模型结构,和Model Zoo官方定义的结构完全一致:

  1. 准备好训练时生成的pipeline.config文件,以及对应步数的checkpoint文件(通常命名为ckpt-xxx.index、ckpt-xxx.data-00000-of-00001)
  2. 运行如下代码加载模型:
import tensorflow as tf
from object_detection.builders import model_builder
from object_detection.utils import config_util

# 加载训练配置
configs = config_util.get_configs_from_pipeline_file("替换为你的pipeline.config路径")
model_config = configs["model"]

# 构建模型实例
detection_model = model_builder.build(model_config=model_config, is_training=False)

# 加载训练好的权重
ckpt = tf.train.Checkpoint(model=detection_model)
ckpt.restore("替换为你的checkpoint路径,例如./train/ckpt-5000").expect_partial()
  1. 直接调用方法输出结构:
# 打印模型摘要
detection_model.summary()

# 可选:导出模型结构图片
tf.keras.utils.plot_model(detection_model, to_file="model_arch.png", show_shapes=True, show_layer_names=True)

场景2:只有导出的SavedModel,没有训练配置和checkpoint

这种方式只能拿到粗略的输入输出结构,内部分层会显示为匿名层,仅做参考:

import tensorflow as tf

# 加载SavedModel
loaded_model = tf.saved_model.load("替换为你的SavedModel路径")
infer_func = loaded_model.signatures["serving_default"]

# 封装为Keras模型
inputs = tf.keras.Input(shape=(None, None, 3), dtype=tf.uint8)
outputs = infer_func(inputs)
keras_model = tf.keras.Model(inputs=inputs, outputs=outputs)

# 打印摘要
keras_model.summary()

注意事项

  • 不需要加载训练过程中的动态模型,也不需要重新训练,只要有对应训练产出的pipeline配置和checkpoint就能直接还原完整结构。
  • 部分大模型的summary层数过多,可以通过detection_model.summary(line_length=150)调整行宽提升可读性。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 10:45:04