如何将恢复的图的输出张量连接到TensorFlow中默认图的输入?

我是TensorFlow的新手,已经被这个问题困扰了几天。现在我有以下预训练模型(4个文件):

Classification.inception.model-27.data-0000-pf=00001Classification.inception.model-27.indexClassification.inception.model-27.metacheckpoint 

我可以在一个新的文件test.py中成功地将这个模型恢复为默认图:

with tf.Session() as sess:    new_restore = tf.train.import_meta_graph('Classification.inception.model-27.meta')    new_restore.restore(sess, tf.train.latest_checkpoint('/'))    graph = tf.get_default_graph()    input_data = graph.get_tensor_by_name('input_data')    output = graph.get_tensor_by_name('logits/BiasAdd:0')    ......    logits = sess.run(output, feed_dict = {input_data: mybatch})    ......

上述脚本运行良好,因为test.py与train.py是独立的。所以我通过这种方式得到的图只是默认图。

然而,我不知道如何将这个预训练模型整合到现有图中,即将张量“output”传递到一个新的网络(Python代码,而不是恢复的图)中,如下所示:

def main():    ### 在这里加载meta文件并恢复预训练图 #####    new_restore = tf.train.import_meta_graph('Classification.inception.model-27.meta')    new_restore.restore(sess, tf.train.latest_checkpoint('/'))    graph = tf.get_default_graph()    input_data = graph.get_tensor_by_name('input_data')    output1 = graph.get_tensor_by_name('logits/BiasAdd:0')    ......    with tf.Graph().as_default():        with tf.variable_scope(scope, 'InceptionResnetV1', [inputs], reuse=reuse):            with slim.arg_scope([slim.batch_norm, slim.dropout], is_training = is_training):                with slim.arg_scope([slim.conv2d, slim.max_pool2d, slim.avg_pool2d]):                    net = slim.conv2d(output1, 32, 3, stride = 2, scope= 'Conv2d_1a_3x3')

然而,当我将张量output1传递到slim.conv2d()时,出现了错误。错误信息是:

ValueError:Tensor(“InceptionResnetV1/Conv2d_1a_3x3/BatchNorm/AssignMovingAvg:0”. shape=(32,). dtype=float32_ref) is not an element of this graph

人们通常如何处理这种情况(从.meta文件恢复图并将其输出张量连接到当前默认图的输入)?

我在网上搜索了一些与我的问题类似的东西(即连接两个不同图的输入和输出张量TensorFlow)。但我认为这仍然相当不同。

此外,还有一些类似的方法可以恢复“.ckpt”文件,但我认为它们仍然不是我所寻找的。

任何评论和指导都将不胜感激。谢谢。


回答:

你的问题在于with tf.Graph().as_default():会覆盖你的旧图:

另一个典型的用法涉及tf.Graph.as_default上下文管理器,它会在上下文的生命周期内覆盖当前的默认图。

简单地删除这行代码以保留旧图,如下所示:

import tensorflow as tfimport numpy as npconst_input_dummy = np.random.randn(1, 28)# 创建图并保存所有内容x = tf.placeholder(dtype=tf.float32, shape=[1, 28], name='plhdr')y = tf.layers.dense(x, 2, name='logits')with tf.Session() as sess:    sess.run(tf.global_variables_initializer())    print(sess.run(y, {x: const_input_dummy}))    saver = tf.train.Saver()    saver.save(sess, './export/inception')# 重置到目前为止的所有内容(就像创建另一个脚本)tf.reset_default_graph()# 回答问题with tf.Session() as sess:    # 导入旧图结构    restorer = tf.train.import_meta_graph('./export/inception.meta')    # 获取从导入图中引用的张量    graph = tf.get_default_graph()    x = graph.get_tensor_by_name("plhdr:0")    y = graph.get_tensor_by_name('logits/BiasAdd:0')    # 添加一些新操作(和变量)    with tf.variable_scope('new_scope'):        y = tf.layers.dense(y, 1, name='other_layer')    # 初始化所有变量...    sess.run(tf.global_variables_initializer())    # ...然后从文件中恢复变量    restorer.restore(sess, tf.train.latest_checkpoint('./export'))    # 这将无错误地执行    print(sess.run(y, {x: const_input_dummy}))

通常情况下,没有必要维护多个图。所以我建议只使用一个图进行工作。

Related Posts

Keras Dense层输入未被展平

这是我的测试代码: from keras import…

无法将分类变量输入随机森林

我有10个分类变量和3个数值变量。我在分割后直接将它们…

如何在Keras中对每个输出应用Sigmoid函数?

这是我代码的一部分。 model = Sequenti…

如何选择类概率的最佳阈值?

我的神经网络输出是一个用于多标签分类的预测类概率表: …

在Keras中使用深度学习得到不同的结果

我按照一个教程使用Keras中的深度神经网络进行文本分…

‘MatMul’操作的输入’b’类型为float32,与参数’a’的类型float64不匹配

我写了一个简单的TensorFlow代码,但不断遇到T…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注