恢复已保存的TensorFlow模型以评估测试集

时间:2016-12-22 16:06:50

标签: tensorflow conv-neural-network

我在恢复TF模型和posts上的Google文档页面上看到了一些exporting graphs,但我认为我遗漏了一些内容。

我使用此Gist中的代码将模型与此utils文件一起保存到defines模型

现在我想恢复它并运行以前看不见的测试数据,如下所示:

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%。但是,如果我没有关闭Notebook并在训练模型后立即运行测试代码,那么我的 95%准确度。

这让我相信我没有正确加载模型?

3 个答案:

答案 0 :(得分:5)

这两个问题产生了问题:

loader.restore(sess, tf.train.latest_checkpoint('./'))
sess.run(tf.initialize_all_variables())   

第一行从检查点加载保存的模型。第二行重新初始化模型中的所有变量(例如权重矩阵,卷积滤波器和偏置矢量),通常是随机数,覆盖加载值。

解决方案很简单:删除第二行(sess.run(tf.initialize_all_variables())),评估将继续使用从检查点加载的训练值。

PS。这种变化很可能会给你一个关于“未初始化变量”的错误。在这种情况下,您应该执行sess.run(tf.initialize_all_variables())来初始化在执行loader.restore(sess, tf.train.latest_checkpoint('./'))之前检查点中未保存的任何变量。

答案 1 :(得分:2)

我有一个类似的问题,对我来说这可行:

with tf.Session() as sess:
    saver=tf.train.Saver(tf.all_variables())
    saver=tf.train.import_meta_graph('model.meta')
    saver.restore(sess,"model")

    test_accuracy = evaluate(X_test, y_test)

答案 2 :(得分:1)

答案发现public repository最终的工作方式如下:

save_path = saver.save(sess, '/home/ubuntu/gtsd-12-23-16.chkpt')
print("Model saved in file: %s" % save_path)
## later re-run code that creates 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)

saver = tf.train.Saver()
    with tf.Session() as sess:
        saver.restore(sess, '/home/ubuntu/gtsd-12-23-16.chkpt')
        print("Model restored.")
        test_accuracy = evaluate(X_test, y_test)
        print("Test Accuracy = {:.3f}".format(test_accuracy[0]*100))