如何将恢复的图的输出张量连接到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

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

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