Tensorflow 讀取ckpt文件中的tensor操作
在使用pre-train model時候,我們需要restore variables from checkpoint files.
經(jīng)常出現(xiàn)在checkpoint 中找不到”Tensor name not found”.
這時候需要查看一下ckpt中到底有哪些變量
import os
from tensorflow.python import pywrap_tensorflow
checkpoint_path = os.path.join(model_dir, "model.ckpt")
# Read data from checkpoint file
reader = pywrap_tensorflow.NewCheckpointReader(checkpoint_path)
var_to_shape_map = reader.get_variable_to_shape_map()
# Print tensor name and values
for key in var_to_shape_map:
print("tensor_name: ", key)
print(reader.get_tensor(key))
可以顯示ckpt中的tensor名字和值,當然也可以用pycharm調試。
補充:tensorflow中讀取模型中保存的值, tf.train.NewCheckpointReader
使用tf.trian.NewCheckpointReader(model_dir)
一個標準的模型文件有一下文件, model_dir就是MyModel(沒有后綴)
checkpoint Model.meta Model.data-00000-of-00001 Model.index
import tensorflow as tf
import pprint # 使用pprint 提高打印的可讀性
NewCheck =tf.train.NewCheckpointReader("model")
打印模型中的所有變量
print("debug_string:\n")
pprint.pprint(NewCheck.debug_string().decode("utf-8"))

其中有3個字段, 分別是名字, 數(shù)據(jù)類型, shape
獲取變量中的值
print("get_tensor:\n")
pprint.pprint(NewCheck.get_tensor("D/conv2d/bias"))

print("get_variable_to_dtype_map\n")
pprint.pprint(NewCheck.get_variable_to_dtype_map())
print("get_variable_to_shape_map\n")
pprint.pprint(NewCheck.get_variable_to_shape_map())
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關文章
Python解決%matplotlib inline標紅底報錯問題
在使用非Jupyter環(huán)境如Spyder或PyCharm時,%matplotlib inline會因為是Jupyter特有的魔法命令而導致報錯,這條命令是用于Jupyter Notebook或Jupyter Qt Console中,主要作用是將matplotlib的圖表直接嵌入到Notebook中顯示2024-09-09
python ndarray數(shù)組對象特點及實例分享
在本篇文章里小編給大家分享的是一篇關于python ndarray數(shù)組對象特點及實例相關內容,有需要的朋友們跟著學習下。2021-10-10
Python3.5編程實現(xiàn)修改IIS WEB.CONFIG的方法示例
這篇文章主要介紹了Python3.5編程實現(xiàn)修改IIS WEB.CONFIG的方法,涉及Python針對xml格式文件的讀寫以及節(jié)點操作相關技巧,需要的朋友可以參考下2017-08-08

