在冻结的Tensorflow模型中替换节点 [英] Replacing a node in a frozen Tensorflow model

查看:202
本文介绍了在冻结的Tensorflow模型中替换节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个frozen inference graph存储在.pb file中,该frozen inference graph是通过freeze_graph函数从trained Tensorflow model获取的.

I have a frozen inference graph stored in a .pb file, which was obtained from a trained Tensorflow model by the freeze_graph function.

为简单起见,假设,我想将模型中的某些sigmoid activations更改为tanh activations(并且我们不讨论这是否是一个好主意).

Suppose, for simplicity, that I would like to change some of the sigmoid activations in the model to tanh activations (and let's not discuss whether this is a good idea).

如何仅访问.pb文件中的冻结图形而又无法重新训练模型呢?

我知道Graph Editor library in tf.contrib,它应该能够执行这种工作,但是我无法在文档中找到一种简单的方法来实现此目的.

I am aware of the Graph Editor library in tf.contrib, which should be able to do this kind of job, but I wasn't able to figure out a simple way to do this in the documentation.

推荐答案

解决方案是使用import_graph_def:

import tensorflow as tf
sess = tf.Session()

def load_graph(frozen_graph_filename):
    with tf.gfile.GFile(frozen_graph_filename, "rb") as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())
    with tf.Graph().as_default() as graph:
        tf.import_graph_def(graph_def, name='')
    return graph
graph_model = load_graph("frozen_inference_graph.pb")
graph_model_def = graph_model.as_graph_def()

graph_new = tf.Graph()
graph_new.as_default()
my_new_tensor = # whatever
tf.import_graph_def(graph_model_def, name='', input_map={"tensor_to_replace": my_new_tensor})
#do somthing with your new graph

在这里,我写了一篇发表它

这篇关于在冻结的Tensorflow模型中替换节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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