加載pb模型文件,並輸出定義
model = 'model.pb'
with tf.Session() as sess:
with open(model, 'rb') as model_file:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
print(graph_def)
採用上述的方式可以在新的會話中重新加載本地的模型文件(pb),然後二進制解析後,輸出可以看到結果。但是如果網絡層結構十分複雜,那麼這種顯示方式就會比較難以閱讀。
加載pb模型文件,並使用Tensorboard可視化
import tensorflow as tf
from tensorflow.python.platform import gfile
model = '1.pb'
graph = tf.get_default_graph()
graph_def = graph.as_graph_def()
graph_def.ParseFromString(gfile.FastGFile(model, 'rb').read())
tf.import_graph_def(graph_def, name='graph')
summaryWriter = tf.summary.FileWriter('log/', graph)
然後會在你的log文件夾下面生成文件。在終端中執行
tensorboard --logdir DIR --host IP --port PORT
一般情況下,不設置host和port,就會在localhost:6006啓動。DIR是路徑(不加引號)。
上面的例子:
tensorboard --logdir log
然後在瀏覽器中訪問localhost:6006就可以可視化你的網絡結構了。
打印pb模型文件中的tensor名字及shape
# coding:utf-8
import tensorflow as tf
from tensorflow.python.platform import gfile
tf.reset_default_graph() # 重置計算圖
output_graph_path = '1.pb'
with tf.Session() as sess:
tf.global_variables_initializer().run()
output_graph_def = tf.GraphDef()
# 獲得默認的圖
graph = tf.get_default_graph()
with gfile.FastGFile(output_graph_path, 'rb') as f:
output_graph_def.ParseFromString(f.read())
_ = tf.import_graph_def(output_graph_def, name="")
# 得到當前圖有幾個操作節點
print("%d ops in the final graph." % len(output_graph_def.node))
tensor_name = [tensor.name for tensor in output_graph_def.node]
print(tensor_name)
print('---------------------------')
# 在log_graph文件夾下生產日誌文件,可以在tensorboard中可視化模型
# summaryWriter = tf.summary.FileWriter('log_graph/', graph)
for op in graph.get_operations():
# print出tensor的name和值
print(op.name, op.values())