恢复已保存的 Tensorflow .pb 模型的权重 [英] restoring weights of an already saved Tensorflow .pb model

查看:42
本文介绍了恢复已保存的 Tensorflow .pb 模型的权重的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里看到了很多关于恢复已保存的 TF 模型的帖子,但没有一个能回答我的问题.使用 TF 1.0.0

I have seen many posts about restoring an already saved TF models here, but none could answer my question. Using TF 1.0.0

特别是,我有兴趣看到 inceptionv3 模型的权重,该模型在 .pb 文件 这里.我设法使用一小段 Python 代码将其恢复,并且可以访问 tensorboard 中的图形高级视图:

Specifically, I am interested in seeing the weights for inceptionv3 model which is publicly available in .pb file here. I managed to restore it back using a small chunk of Python code and can access the graphs high-level view in tensorboard:

from tensorflow.python.platform import gfile

INCEPTION_LOG_DIR = '/tmp/inception_v3_log'

if not os.path.exists(INCEPTION_LOG_DIR):
    os.makedirs(INCEPTION_LOG_DIR)
with tf.Session() as sess:
    model_filename = './model/tensorflow_inception_v3_stripped_optimized_quantized.pb'
    with gfile.FastGFile(model_filename, 'rb') as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())
        _= tf.import_graph_def(graph_def,name='')
    writer = tf.train.SummaryWriter(INCEPTION_LOG_DIR, graph_def)
    writer=tf.summary.FileWriter(INCEPTION_LOG_DIR, graph_def)
    writer.close()

但是,我无法访问任何图层的权重.

However, I failed to access any layers' weights.

tensors= tf.import_graph_def(graph_def,name='')

返回空,即使我添加了任意的 return_elements=.它有任何重量吗?如果是,这里的适当程序是什么?谢谢.

returns empty, even if I add the arbitrary return_elements=. Does it have any weights at all? If yes, what is the appropriate procedure here? Thanks.

推荐答案

使用此代码打印张量的值:

use this code to print your tensor's value :

with tf.Session() as sess:
    print sess.run('your_tensor_name')

您可以使用此代码检索张量名称:

you can use this code to retrieve tensor names:

    op = sess.graph.get_operations()
    for m in op : 
    print(m.values())

这篇关于恢复已保存的 Tensorflow .pb 模型的权重的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆