tensorflow 实现从checkpoint中获取graph信息

所属分类: 脚本专栏 / python 阅读数: 2008
收藏 0 赞 0 分享

代码:

import tensorflow as tf
 
sess = tf.Session()
check_point_path = 'variables' 
saver = tf.train.import_meta_graph('variables/save_variables.ckpt.meta')
 
saver.restore(sess, tf.train.latest_checkpoint(check_point_path))
 
graph = tf.get_default_graph()
 
#print(graph.get_operations())
 
#with open('op.txt','a') as f:
# f.write(str(graph.get_operations()))
op1 = graph.get_tensor_by_name('fully_connected/biases:0')
print(op1)

使用函数graph.get_operations()获取ckpt.meta中保存的graph中的所有operation,而tensor_name为'op_name:0'。

然后使用graph.get_tensor_by_name('op_name:0') 获取tensor信息。

代码从ckpt文件中获取保存的variable的数据(tensor的name和value):

import os
import tensorflow as tf
from tensorflow.python import pywrap_tensorflow
check_point_path = 'variables'
#checkpoint_path = os.path.join(logs_train_dir, 'model.ckpt')
ckpt = tf.train.get_checkpoint_state(checkpoint_dir=check_point_path)
checkpoint_path = os.path.join('.', ckpt.model_checkpoint_path)
#print(ckpt.model_checkpoint_path)
reader = pywrap_tensorflow.NewCheckpointReader(checkpoint_path)
var_to_shape_map = reader.get_variable_to_shape_map()
for key in var_to_shape_map:
 print("tensor_name: ", key)
 #print(reader.get_tensor(key))

法二:

from tensorflow.python.tools.inspect_checkpoint import print_tensors_in_checkpoint_file
 
print_tensors_in_checkpoint_file("variables/save_variables.ckpt",tensor_name='', all_tensors=False, all_tensor_names=False)

注意:tf.train.latest_checkpoint(check_point_path) 方法用来获取最后一次ckeckpoint的路径,等价于

ckpt = tf.train.get_checkpoint_state(check_point_path)
ckpt.model_checkpoint_path

不能将tf.train.latest_checkpoint与tf.train.get_checkpoint_state 搞混了

以上这篇tensorflow 实现从checkpoint中获取graph信息就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

QML用PathView实现轮播图

这篇文章主要为大家详细介绍了QML用PathView实现轮播图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Opencv图像处理:如何判断图片里某个颜色值占的比例

这篇文章主要介绍了Opencv图像处理:如何判断图片里某个颜色值占的比例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

python golang中grpc 使用示例代码详解

这篇文章主要介绍了python golang中grpc 使用,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

浅谈python opencv对图像颜色通道进行加减操作溢出

这篇文章主要介绍了浅谈python opencv对图像颜色通道进行加减操作溢出,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

解决python运行启动报错问题

这篇文章主要介绍了解决python运行启动报错问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Python常见反爬虫机制解决方案

这篇文章主要介绍了Python常见反爬虫机制解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享

解决pycharm导入本地py文件时,模块下方出现红色波浪线的问题

这篇文章主要介绍了解决pycharm导入本地py文件时,模块下方出现红色波浪线的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

pycharm设置默认的UTF-8编码模式的方法详解

这篇文章主要介绍了pycharm设置默认的UTF-8编码模式,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

浅谈Pycharm的项目文件名是红色的原因及解决方式

这篇文章主要介绍了浅谈Pycharm的项目文件名是红色的原因及解决方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Python网络爬虫四大选择器用法原理总结

这篇文章主要介绍了Python网络爬虫四大选择器用法原理总结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多