如何获得estimator.predict来预测一个样本

时间:2018-05-09 11:06:33

标签: python tensorflow

我正在尝试让mnist cnn工作,以便一次对一个图像进行预测。我已经采用了tensorflow教程代码,并尝试将estimator.predict与模型一起使用,但我目前收到错误:

InvalidArgumentError (see above for traceback): Input to reshape is a tensor with 1 values, but the requested shape requires a multiple of 784
 [[Node: Reshape = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/device:GPU:0"](fifo_queue_DequeueUpTo/_53, Reshape/shape)]]

如果我打印给予预测输入函数的predict_data列表,它包含784个元素。

模型训练好并评估好。 该模型已经过培训,所以我在这里跳过了培训代码,但这就是我所拥有的:

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(
        model_fn=cnn_model_fn, model_dir="/tmp/mnist_convnet_model")

    # 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
#     train_input_fn = tf.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,
#         hooks=[logging_hook])

    # Evaluate the model and print results
#     eval_input_fn = tf.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]
    predict_input_fn = tf.estimator.inputs.numpy_input_fn(
        x={"x": predict_data},
        y=None,
        batch_size=1,
        num_epochs=1,
        shuffle=False,
        num_threads=1)

    predict_results = mnist_classifier.predict(predict_input_fn) 

    print(predict_data)
    for idx, prediction in enumerate(predict_results):
        print(idx)
        # print(prediction)

非常感谢任何帮助实现这项工作。

更新:我尝试了重塑,如下所示,但得到同样的错误。完整的痕迹是:

Traceback (most recent call last):
  File "C:\Users\artma\Miniconda3\envs\vpilot\lib\site-packages\tensorflow\python\client\session.py", line 1323, in _do_call
    return fn(*args)
  File "C:\Users\artma\Miniconda3\envs\vpilot\lib\site-packages\tensorflow\python\client\session.py", line 1302, in _run_fn
    status, run_metadata)
  File "C:\Users\artma\Miniconda3\envs\vpilot\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 473, in __exit__
    c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: Input to reshape is a tensor with 1 values, but the requested shape requires a multiple of 784
     [[Node: Reshape = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/device:GPU:0"](fifo_queue_DequeueUpTo/_53, Reshape/shape)]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\Workspace\eclipse\mnist_cnn\cnn_mnist.py", line 180, in <module>
    tf.app.run()
  File "C:\Users\artma\Miniconda3\envs\vpilot\lib\site-packages\tensorflow\python\platform\app.py", line 48, in run
    _sys.exit(main(_sys.argv[:1] + flags_passthrough))
  File "D:\Workspace\eclipse\mnist_cnn\cnn_mnist.py", line 170, in main
    for idx, prediction in enumerate(predict_results):
  File "C:\Users\artma\Miniconda3\envs\vpilot\lib\site-packages\tensorflow\python\estimator\estimator.py", line 420, in predict
    preds_evaluated = mon_sess.run(predictions)
  File "C:\Users\artma\Miniconda3\envs\vpilot\lib\site-packages\tensorflow\python\training\monitored_session.py", line 521, in run
    run_metadata=run_metadata)
  File "C:\Users\artma\Miniconda3\envs\vpilot\lib\site-packages\tensorflow\python\training\monitored_session.py", line 892, in run
    run_metadata=run_metadata)
  File "C:\Users\artma\Miniconda3\envs\vpilot\lib\site-packages\tensorflow\python\training\monitored_session.py", line 967, in run
    raise six.reraise(*original_exc_info)
  File "C:\Users\artma\Miniconda3\envs\vpilot\lib\site-packages\six.py", line 693, in reraise
    raise value
  File "C:\Users\artma\Miniconda3\envs\vpilot\lib\site-packages\tensorflow\python\training\monitored_session.py", line 952, in run
    return self._sess.run(*args, **kwargs)
  File "C:\Users\artma\Miniconda3\envs\vpilot\lib\site-packages\tensorflow\python\training\monitored_session.py", line 1024, in run
    run_metadata=run_metadata)
  File "C:\Users\artma\Miniconda3\envs\vpilot\lib\site-packages\tensorflow\python\training\monitored_session.py", line 827, in run
    return self._sess.run(*args, **kwargs)
  File "C:\Users\artma\Miniconda3\envs\vpilot\lib\site-packages\tensorflow\python\client\session.py", line 889, in run
    run_metadata_ptr)
  File "C:\Users\artma\Miniconda3\envs\vpilot\lib\site-packages\tensorflow\python\client\session.py", line 1120, in _run
    feed_dict_tensor, options, run_metadata)
  File "C:\Users\artma\Miniconda3\envs\vpilot\lib\site-packages\tensorflow\python\client\session.py", line 1317, in _do_run
    options, run_metadata)
  File "C:\Users\artma\Miniconda3\envs\vpilot\lib\site-packages\tensorflow\python\client\session.py", line 1336, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Input to reshape is a tensor with 1 values, but the requested shape requires a multiple of 784
     [[Node: Reshape = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/device:GPU:0"](fifo_queue_DequeueUpTo/_53, Reshape/shape)]]

Caused by op 'Reshape', defined at:
  File "D:\Workspace\eclipse\mnist_cnn\cnn_mnist.py", line 180, in <module>
    tf.app.run()
  File "C:\Users\artma\Miniconda3\envs\vpilot\lib\site-packages\tensorflow\python\platform\app.py", line 48, in run
    _sys.exit(main(_sys.argv[:1] + flags_passthrough))
  File "D:\Workspace\eclipse\mnist_cnn\cnn_mnist.py", line 170, in main
    for idx, prediction in enumerate(predict_results):
  File "C:\Users\artma\Miniconda3\envs\vpilot\lib\site-packages\tensorflow\python\estimator\estimator.py", line 411, in predict
    features, None, model_fn_lib.ModeKeys.PREDICT, self.config)
  File "C:\Users\artma\Miniconda3\envs\vpilot\lib\site-packages\tensorflow\python\estimator\estimator.py", line 694, in _call_model_fn
    model_fn_results = self._model_fn(features=features, **kwargs)
  File "D:\Workspace\eclipse\mnist_cnn\cnn_mnist.py", line 31, in cnn_model_fn
    input_layer = tf.reshape(features["x"], [-1, 28, 28, 1])
  File "C:\Users\artma\Miniconda3\envs\vpilot\lib\site-packages\tensorflow\python\ops\gen_array_ops.py", line 3937, in reshape
    "Reshape", tensor=tensor, shape=shape, name=name)
  File "C:\Users\artma\Miniconda3\envs\vpilot\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
    op_def=op_def)
  File "C:\Users\artma\Miniconda3\envs\vpilot\lib\site-packages\tensorflow\python\framework\ops.py", line 2956, in create_op
    op_def=op_def)
  File "C:\Users\artma\Miniconda3\envs\vpilot\lib\site-packages\tensorflow\python\framework\ops.py", line 1470, in __init__
    self._traceback = self._graph._extract_stack()  # pylint: disable=protected-access

InvalidArgumentError (see above for traceback): Input to reshape is a tensor with 1 values, but the requested shape requires a multiple of 784
     [[Node: Reshape = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/device:GPU:0"](fifo_queue_DequeueUpTo/_53, Reshape/shape)]]

更新:似乎破解了它。感谢xdurch0让我走上正轨。

2 个答案:

答案 0 :(得分:2)

predict_data只是一个784元素的向量。这将被视为包含784个元素(即不同的输入)的数据集,每个元素都是标量。你需要重塑你的predict_data到(1,784)让TF知道这是一个带有一个元素的数据集,它是一个784元素的向量。例如。 predict_data[np.newaxis, :]predict_data.reshape((1, 784))

答案 1 :(得分:1)

看来这一行:

predict_data = eval_data[1]

正在生成(784,)的np数组,甚至predict_data.reshape((1,784))也无法修复。

鉴于:

predict_data = eval_data[1:2]

生成一个(1,784)的np数组,estimator.predict现在很满意。