I have used tensorflow to train a CNN model to do character classification. I have saved my best model using a tf.train.Saver() object. To do classification in an application, i use a function as can be seen below.

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

This function loads the saved model and uses it to classify the function input. The function works exactly as intended when called once. However, if I call it more than once during the same execution, it fails, throwing:

tensorflow.python.framework.errors_impl.NotFoundError: Key Variable_9 not found in checkpoint

Now, if this happened whenever the function was used, this would make sense to me, and I would assume there probably was something wrong with my model saving. But here it seems more like the function has state that persists outside of it, that prevents a second run. When looking at my code, however, I fail to see what this state could be. I do not overwrite my checkpoint file, so in theory, there should not be anything wrong with the loading of it.

Does anyone know what I am doing wrong?