我是tensorflow的新手。我正在学习本教程。而且我采用了tensorflow cnn tutorial code,并且已经训练了CNN模型。我正在尝试使用此模型和estimator.predict预测测试集和训练集之外的一幅图像。但是我遇到了一些麻烦(从检查点恢复失败。)
我在主要功能后面添加了代码。该模型已经过训练,因此这里跳过了训练代码。
def main(unused_argv):
# Load training and eval data
mnist = tf.contrib.learn.datasets.load_dataset("mnist")
train_data = mnist.train.images # Returns np.array
train_labels = np.asarray(mnist.train.labels, dtype=np.int32)
eval_data = mnist.test.images # Returns np.array
eval_labels = np.asarray(mnist.test.labels, dtype=np.int32)
# Create the Estimator
mnist_classifier = tf.estimator.Estimator( # mnist_classifier
model_fn=cnn_model_fn, model_dir="models/cnn")
# Set up logging for predictions
# Log the values in the "Softmax" tensor with label "probabilities"
tensors_to_log = {"probabilities": "softmax_tensor"}
logging_hook = tf.train.LoggingTensorHook(
tensors=tensors_to_log, every_n_iter=50)
# # Train the model The model is already trained so I have skipped the training code here
# train_input_fn = tf.compat.v1.estimator.inputs.numpy_input_fn(
# x={"x": train_data},
# y=train_labels,
# batch_size=100,
# num_epochs=None,
# shuffle=True)
# mnist_classifier.train(
# input_fn=train_input_fn,
# steps=20000, # 20000
# hooks=[logging_hook])
#
# # Evaluate the model and print results
# eval_input_fn = tf.compat.v1.estimator.inputs.numpy_input_fn(
# x={"x": eval_data}, y=eval_labels, num_epochs=1, shuffle=False)
# eval_results = mnist_classifier.evaluate(input_fn=eval_input_fn)
# print(eval_results)
#predict_data = eval_data[1]
#here is my code
predict_data = np.random.rand(784) #this random list representsa 784 pixel image
predict_data = np.array(predict_data)
predict_data = np.reshape(predict_data, (1, 784))
pred_input_fn = tf.estimator.inputs.numpy_input_fn(
x={"x": predict_data},
shuffle=False)
pred_results = mnist_classifier.predict(input_fn=pred_input_fn)
print(next(pred_results))
if __name__ == "__main__":
tf.app.run()
这是错误
InvalidArgumentError (see above for traceback): Restoring from checkpoint failed. This is most likely due to a mismatch between the current graph and the graph from the checkpoint. Please ensure that you have not altered the graph expected based on the checkpoint.
但是如果我使用测试集中已经存在的图像,它将很好地工作。
predict_data = eval_data[1]
predict_data = np.reshape(predict_data, (1, 784))
我应该怎么做才能使该模式在测试集和训练集之外的一幅图像上进行预测?
我们将不胜感激。