TensorFlow 加载并打印 PB 模型所有节点的方法

一只小鹿
一只小鹿
发布时间:2026-10-13| 4人浏览过
原创

本文介绍如何在 TensorFlow 中加载 .pb 格式的冻结模型文件,并通过遍历 GraphDef 节点列表来打印所有节点名称。提供了基于 Python 的完整代码示例,适用于模型结构分析与调试场景。

加载 PB 模型并提取节点信息

当仅拥有 .pb 模型文件时,需要一种方法来查看其内部结构。本文将展示如何加载该文件并提取所有节点信息。

Python 代码实现


from tensorflow.python.framework import tensor_util
from google.protobuf import text_format
import tensorflow as tf
from tensorflow.python.platform import gfile
from tensorflow.python.framework import tensor_util


GRAPH_PB_PATH = 'models/frozen_person_graph.pb' #path to your .pb file
with tf.Session() as sess:
	print("load graph")
	with gfile.FastGFile(GRAPH_PB_PATH,'rb') as f:
		graph_def = tf.GraphDef()
  # Note: one of the following two lines work if required libraries are available
		#text_format.Merge(f.read(), graph_def)
		graph_def.ParseFromString(f.read())
		tf.import_graph_def(graph_def, name='')
		for i,n in enumerate(graph_def.node):
			print("Name of the node - %s" % n.name)

以上关于 TensorFlow 实现打印 PB 模型所有节点的内容即为完整分享。希望此示例能为您提供参考,欢迎继续支持本站获取更多技术资讯。

相关标签:

免责声明:本文内容仅供参考,不代表本站观点。转载或引用请注明出处。

Python 相关产品和应用

全部下载
Selenium

Selenium

4.51.0 · Windows

用于浏览器自动化和 Web 测试的 Python 工具。

scikit-learn

scikit-learn

1.9.1 · Windows

常用 Python 机器学习算法与模型评估工具库。

Black

Black

26.10.1 · Windows

强调一致性的 Python 自动代码格式化工具。

Ruff

Ruff

0.17.1 · macOS

由 Rust 编写的高速 Python 代码检查和格式化工具。

pytest

pytest

9.1.1 · Windows

简洁而强大的 Python 自动化测试框架。

FastAPI

FastAPI

0.146.0 · Windows

高性能 Python API 框架,支持类型提示和自动接口文档。

Flask

Flask

3.1.3 · Windows

轻量灵活的 Python Web 应用框架。

Django

Django

6.1.2 · Windows

成熟的 Python Web 框架,适合构建内容站点和业务系统。

Spyder

Spyder

6.1.7 · Windows

面向科学计算与数据分析的 Python 集成开发环境。

SciPy

SciPy

1.18.1 · Windows

面向科学、工程和技术计算的 Python 算法库。