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名字和值,當(dāng)然也可以用pycharm調(diào)試。
補充: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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
django 實現(xiàn)celery動態(tài)設(shè)置周期任務(wù)執(zhí)行時間
今天小編就為大家分享一篇django 實現(xiàn)celery動態(tài)設(shè)置周期任務(wù)執(zhí)行時間,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11
Python 中使用 argparse 解析命令行參數(shù)
這篇文章主要介紹了Python 中使用 argparse 解析命令行參數(shù),argparse 模塊是一個強大的命令行參數(shù)解析器,還有很多功能沒能在這里介紹。下面文化在哪個詳細介紹該內(nèi)容,需要的朋友可以參考一下2021-11-11
python實現(xiàn)RGB與YCBCR顏色空間轉(zhuǎn)換
這篇文章主要介紹了python實現(xiàn)RGB與YCBCR顏色空間轉(zhuǎn)換,RGB與YCbCr顏色空間概念的與變換關(guān)系,包括內(nèi)容灰度值和亮度的關(guān)系、RGB顏色空間與顏色控制、YCbCr顏色空間及與RGB的變換關(guān)系,需要的小伙伴可以參考一下2022-03-03
Tensorflow 多線程與多進程數(shù)據(jù)加載實例
今天小編就為大家分享一篇Tensorflow 多線程與多進程數(shù)據(jù)加載實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02
Django debug為True時,css加載失敗的解決方案
這篇文章主要介紹了Django debug為True時,css加載失敗的解決方案,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04
關(guān)于Python turtle庫使用時坐標的確定方法
這篇文章主要介紹了關(guān)于Python turtle庫使用時坐標的確定方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03

