我有一个frozen inference graph
存储在.pb file
中,该trained Tensorflow model
是freeze_graph
函数从sigmoid activations
获得的。
假设,为简单起见,我想将模型中的部分tanh activations
更改为Graph Editor library in tf.contrib
(并且不要讨论是否这是一个好主意)。
如何才能只访问.pb文件中的冻结图表,并且无法重新训练模型?
我知道<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<header id="header" class="hoc clear">
<!-- ################################################################################################ -->
<nav id="mainav" class="fl_left">
<ul class="clear">
<li class="#{funksionsBean.getIndex()}"><a href="index.xhtml">Faqja Kryesore</a></li>
<li class="#{funksionsBean.getDestinations()}"><a href="destinations.xhtml">Destinacionet Turistike</a></li>
<li class="#{funksionsBean.getContact()}"><a href="contact.xhtml">Na kontaktoni</a></li>
</ul>
</nav>
<div id="logo" class="fl_right">
<h:form id="accountForm">
<h:panelGrid columns="2" style="background: transparent"
rendered="#{!clientBean.logged}">
<p:commandButton value="HYR" styleClass="login"
onclick="PF('logInDialog').show();" />
<p:commandButton value="REGJISTROHU" styleClass="login" />
</h:panelGrid>
<h:panelGrid columns="3" style="background: transparent"
rendered="#{clientBean.logged}">
<h:outputLabel
value="PERSHENDETJE, #{clientBean.client.getClient_name()}"
styleClass="pershendetje"></h:outputLabel>
<p:commandButton value="Llogaria" styleClass="login"
action="llogaria.xhtml?faces-redirect=true" />
<p:commandButton value="DIL" styleClass="login"
action="#{clientBean.LogOut}" />
</h:panelGrid>
<p:dialog header="Ju lutem fusni te dhenat" widgetVar="logInDialog"
minHeight="40">
<h:panelGrid id="inputPanel" columns="2" cellpadding="10"
styleClass="logInPannel">
<h:outputLabel value="Email:" />
<h:inputText value="#{clientBean.email}" />
<h:outputLabel value="Password:" />
<h:inputSecret value="#{clientBean.password}"></h:inputSecret>
</h:panelGrid>
<h:commandButton action="#{clientBean.LogIn}" value="Hyr"></h:commandButton>
</p:dialog>
</h:form>
</div>
</header>
应该可以做这种工作,但我无法在文档中找到一种简单的方法。
答案 0 :(得分:1)
解决方案是使用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
Here我写了一篇关于它的帖子
答案 1 :(得分:0)
* .pb文件包含SavedModel协议缓冲区。您应该可以使用SavedModel loader加载它。您也可以使用SavedModel CLI进行操作。 SavedModels的完整文档是here。
答案 2 :(得分:0)
你可以试试这个:
graph = load_graph(filename)
graph_def = graph.as_graph_def()
# if ReLu op is at node 161
graph_def.node[161].op="tanh"
tf.train.write_graph(graph_def, path2savfrozn, "altered_frozen.pb", False)
请知道它是否有效。
答案 3 :(得分:0)
遵循这些原则应该可以起作用:
graph_def = tf.GraphDef()
with open('frozen_inference.pb', 'rb') as f:
graph_def.ParseFromString(f.read())
with tf.Graph().as_default() as graph:
importer.import_graph_def(graph_def, name='')
new_model = tf.GraphDef()
with tf.Session(graph=graph) as sess:
for n in sess.graph_def.node:
if n.op == 'Sigmoid':
nn = new_model.node.add()
nn.op = 'Tanh'
nn.name = n.name
for i in n.input:
nn.input.extend([i])
else:
nn = new_model.node.add()
nn.CopyFrom(n)