恢复保存的TensorFlow模型以对测试集进行评估 [英] Restoring saved TensorFlow model to evaluate on test set
问题描述
我已经看到一些帖子关于恢复 TF $ c的帖子 $ c>模型和上的
Google
文档页面导出图形,但我认为我缺少了一些东西。
I have seen a few posts on restoring TF
models and the Google
doc page on exporting graphs but I think I am missing something.
I use the code in this Gist to save the model along with this utils file to which defines the model
现在,我想还原它并在以前看不见的测试数据中运行,如下所示:
Now I would like to restore it and run in a previously unseen test data as follows:
def evaluate(X_data, y_data):
num_examples = len(X_data)
total_accuracy = 0
total_loss = 0
sess = tf.get_default_session()
acc_steps = len(X_data) // BATCH_SIZE
for i in range(acc_steps):
batch_x, batch_y = next_batch(X_val, Y_val, BATCH_SIZE)
loss, accuracy = sess.run([loss_value, acc], feed_dict={
images_placeholder: batch_x,
labels_placeholder: batch_y,
keep_prob: 0.5
})
total_accuracy += (accuracy * len(batch_x))
total_loss += (loss * len(batch_x))
return (total_accuracy / num_examples, total_loss / num_examples)
## re-execute the code that defines the model
# Image Tensor
images_placeholder = tf.placeholder(tf.float32, shape=[None, 32, 32, 3], name='x')
gray = tf.image.rgb_to_grayscale(images_placeholder, name='gray')
gray /= 255.
# Label Tensor
labels_placeholder = tf.placeholder(tf.float32, shape=(None, 43), name='y')
# dropout Tensor
keep_prob = tf.placeholder(tf.float32, name='drop')
# construct model
logits = inference(gray, keep_prob)
# calculate loss
loss_value = loss(logits, labels_placeholder)
# training
train_op = training(loss_value, 0.001)
# accuracy
acc = accuracy(logits, labels_placeholder)
with tf.Session() as sess:
loader = tf.train.import_meta_graph('gtsd.meta')
loader.restore(sess, tf.train.latest_checkpoint('./'))
sess.run(tf.initialize_all_variables())
test_accuracy = evaluate(X_test, y_test)
print("Test Accuracy = {:.3f}".format(test_accuracy[0]))
我得到的测试准确度仅为 3%。但是,如果我在训练模型后不关闭笔记本电脑并立即运行测试代码,则可以达到 95%的精度。
I'm getting a test accuracy of only 3%. However If I don't close the Notebook and run the test code immediately after training the model, I get a 95% accuracy.
这使我相信我没有正确加载模型?
This leads me to believe I'm not loading the model correctly?
推荐答案
问题来自这两行:
loader.restore(sess, tf.train.latest_checkpoint('./'))
sess.run(tf.initialize_all_variables())
第一行从检查点加载保存的模型。第二行重新初始化模型中的所有变量(例如权重矩阵,卷积滤波器和偏差矢量),通常是随机数,然后覆盖
The first line loads the saved model from a checkpoint. The second line re-initializes all of the variables in the model (such as the weight matrices, convolutional filters, and bias vectors), usually to random numbers, and overwrites the loaded values.
解决方案很简单:删除第二行( sess.run(tf.initialize_all_variables())
),然后将从检查点加载的训练值进行评估。
The solution is simple: delete the second line (sess.run(tf.initialize_all_variables())
) and evaluation will proceed with the trained values loaded from the checkpoint.
PS。此更改极有可能给您带来有关未初始化的变量的错误。在这种情况下,您应该执行 sess.run(tf.initialize_all_variables())
来初始化未保存在检查点之前的变量,然后执行 loader.restore(sess,tf.train.latest_checkpoint('./'))
。
PS. There is a small chance that this change will give you an error about "uninitialized variables". In that case, you should execute sess.run(tf.initialize_all_variables())
to initialize any variables not saved in the checkpoint before executing loader.restore(sess, tf.train.latest_checkpoint('./'))
.
这篇关于恢复保存的TensorFlow模型以对测试集进行评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!