參考鏈接:https://juejin.im/post/6844903693184172040
查看模型的Signature簽名
Tensorflow提供了一個工具
- 如果你下載了Tensorflow的源碼,可以找到這樣一個文件,./tensorflow/python/tools/saved_model_cli.py
- 如果你安裝了tensorflow,也可以用下邊的命令查看tensorflow源碼位置和版本:
import tensorflow as tf
print tf.__path__
print tf.__version__
你可以加上-h參數查看saved_model_cli.py腳本的幫助信息:
usage: saved_model_cli.py [-h] [-v] {show,run,scan} ...
saved_model_cli: Command-line interface for SavedModel
optional arguments:
-h, --help show this help message and exit
-v, --version show program's version number and exit
commands:
valid commands
{show,run,scan} additional help
如果你安裝
指定SavedModel模所在的位置,我們就可以顯示SavedModel的模型信息:
python path/to/tensorflow/python/tools/saved_model_cli.py show --dir ./model/ --all
顯示類似結果
MetaGraphDef with tag-set: 'serve' contains the following SignatureDefs:
signature_def['predict']:
The given SavedModel SignatureDef contains the following input(s):
inputs['myInput'] tensor_info:
dtype: DT_FLOAT
shape: (-1, 784)
name: myInput:0
The given SavedModel SignatureDef contains the following output(s):
outputs['myOutput'] tensor_info:
dtype: DT_FLOAT
shape: (-1, 10)
name: Softmax:0
Method name is: tensorflow/serving/predict
查看模型的計算圖
了解tensflow的人可能知道TensorBoard是一個非常強大的工具,能夠顯示很多模型信息,其中包括計算圖。問題是,TensorBoard需要模型訓練時的log,如果這個SavedModel模型是別人訓練好的呢?辦法也不是沒有,我們可以寫一段代碼,加載這個模型,然后輸出summary info,代碼如下:
import tensorflow as tf
import sys
from tensorflow.python.platform import gfile
from tensorflow.core.protobuf import saved_model_pb2
from tensorflow.python.util import compat
with tf.Session() as sess:
model_filename ='./model/saved_model.pb'
with gfile.FastGFile(model_filename, 'rb') as f:
data = compat.as_bytes(f.read())
sm = saved_model_pb2.SavedModel()
sm.ParseFromString(data)
if 1 != len(sm.meta_graphs):
print('More than one graph found. Not sure which to write')
sys.exit(1)
g_in = tf.import_graph_def(sm.meta_graphs[0].graph_def)
LOGDIR='./logdir'
train_writer = tf.summary.FileWriter(LOGDIR)
train_writer.add_graph(sess.graph)
train_writer.flush()
train_writer.close()
代碼中,將匯總信息輸出到logdir,接着啟動TensorBoard,加上上面的logdir:
tensorboard --logdir ./logdir
在瀏覽器中輸入地址: http://127.0.0.1:6006/ ,就可以看到如下的計算圖: