利用tensorboard查看pb模型圖

1. 模型圖和參數全保存在一個pb文件裏(即freeze出來的模型),查看模型圖代碼如下:

import tensorflow as tf
with tf.Session() as sess:
    model_filename ='model/freezon.pb'
    with tf.gfile.GFile(model_filename, 'rb') as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())
        g_in = tf.import_graph_def(graph_def)
     train_writer = tf.summary.FileWriter("./log")
     train_writer.add_graph(sess.graph)
        train_writer.flush()
        train_writer.close()

2. 使用estimator的export_saved_model或tf.saved_model保存的模型,包含saved_model.pb和varibles目錄,到處模型圖代碼如下:

import tensorflow as tf
from tensorflow.core.protobuf import saved_model_pb2
from tensorflow.python.util import compat
from tensorflow.python.framework import ops

model_path = "./1578003200/saved_model.pb"
with tf.Session(graph=ops.Graph()) as sess:
    with tf.gfile.GFile(model_path, "rb") as f:
        data = compat.as_bytes(f.read())
        sm = saved_model_pb2.SavedModel()
        sm.ParseFromString(data)
        g_in = tf.import_graph_def(sm.meta_graphs[0].graph_def)
        train_writer = tf.summary.FileWriter("./log")
        train_writer.add_graph(sess.graph)
        train_writer.flush()
        train_writer.close()

3. 使用tensorboard --logdir ./log --host 0.0.0.0就可以查看圖結構了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章