Tensorflow模型多次加载失败

Tensorflow模型多次加载失败,tensorflow,Tensorflow,我使用tensorflow来训练CNN模型来进行字符分类。我使用tf.train.Saver()对象保存了我最好的模型。为了在应用程序中进行分类,我使用了如下所示的函数 def classify_chars(images): # Create the model x = tf.placeholder(tf.float32, [None, 400]) # Build the graph for the deep net y_conv, keep_prob = _d

我使用tensorflow来训练CNN模型来进行字符分类。我使用tf.train.Saver()对象保存了我最好的模型。为了在应用程序中进行分类,我使用了如下所示的函数

def classify_chars(images):
    # Create the model
    x = tf.placeholder(tf.float32, [None, 400])

    # Build the graph for the deep net
    y_conv, keep_prob = _deepnn(x)

    # Define classification
    letter_class = tf.argmax(y_conv, 1)
    confidence = tf.reduce_max(tf.nn.softmax(y_conv), 1)

    # Enable saving and loading of variables
    saver = tf.train.Saver()

    with tf.Session() as sess:
        # Restore variables from disk.
        saver.restore(sess, os.path.join(M_PATH, "char_model.ckpt"))
        print("Model restored.")

        images_classes = letter_class.eval(feed_dict={x: images})
        images_confidences = confidence.eval(feed_dict={x: images})

    return images_classes, images_confidences
此函数加载保存的模型并使用它对函数输入进行分类。当调用一次时,该函数完全按照预期工作。但是,如果我在同一执行过程中多次调用它,它将失败,抛出:

tensorflow.python.framework.errors_impl.NotFoundError: Key Variable_9 not found in checkpoint
现在,如果每次使用函数时都会发生这种情况,这对我来说是有意义的,我会假设我的模型保存可能有问题。但在这里,函数的状态更像是保持在它的外部,从而阻止了第二次运行。但是,当查看我的代码时,我看不到这种状态可能是什么。我不会覆盖我的检查点文件,所以从理论上讲,加载它应该不会有任何问题


有人知道我做错了什么吗?

tf.saver将全局步骤添加到检查点名称中,可能是您应该检查这是否是问题所在。

结果表明,此问题是由于未在函数调用之间重置默认图造成的。如果TensorFlow已经有一个图,尝试再次加载同一个图会导致此错误。这可以通过添加

tf.reset_default_graph()

还原图形之前。

我不知道这会有什么问题?在您的checkpoints文件夹中,tensorflow会自动创建一个名为checkpoint的文件,该文件包含以下条目model\u checkpoint\u path和所有model\u checkpoint\u路径,尝试使用saver.restore从那里获取模型路径(会话,检查点。模型检查点路径)