【问题标题】:How to get predictions for one image from the MNIST with TensorFlow?如何使用 TensorFlow 从 MNIST 获取一张图像的预测?
【发布时间】:2018-02-25 15:47:21
【问题描述】:

我按照本教程 https://www.tensorflow.org/tutorials/layers 训练了一个模型,用于识别 MNIST 集中的手写数字。

以下代码按预期工作,并为集合中的每个图像打印概率和类别

mnist = tf.contrib.learn.datasets.load_dataset("mnist")
train_data = mnist.train.images  # Returns np.array
tf.reset_default_graph()  
with tf.Session() as sess:

  mnist_classifier = tf.estimator.Estimator(model_fn=cnn_model_fn, model_dir="model/")

  pred = mnist_classifier.predict(input_fn=tf.estimator.inputs.numpy_input_fn(
      x={"x": train_data},
      shuffle=False))

  for p in pred:
    print(p)

但是,当我尝试仅使用

预测一张图像时
mnist_classifier.predict(input_fn=tf.estimator.inputs.numpy_input_fn(
          x={"x": train_data[0]},
          shuffle=False))

我的程序失败,TensorFlow 报告

InvalidArgumentError: Input to reshape is a tensor with 128 values,
but the requested shape requires a multiple of 784

这让我感到困惑,因为当我打印集合中第一张图像的长度时,它报告为 784

print("length of input: {}".format(len(train_data[0]))

如何仅获得一张图像的预测结果?

【问题讨论】:

    标签: python tensorflow mnist


    【解决方案1】:

    这很可能与您在创建单项数据集时删除了批处理维度有关。我的意思是你应该使用

    mnist_classifier.predict(input_fn=tf.estimator.inputs.numpy_input_fn(
          x={"x": np.array([train_data[0])]},
          shuffle=False))
    

    相反。请注意围绕train_data[0] 的附加列表。这将采用形状为 [1, 784] 的数组并创建一个包含一个元素的数据集,而这又是一个包含 784 个元素的向量。正如您现在的代码一样,您基本上是在创建一个包含 784 个元素的数据集,每个元素都是一个数字。这会导致形状不匹配。

    【讨论】:

      【解决方案2】:

      您也可以使用tf.expand_dimsdocumentation 说:

      如果您想将批量维度添加到单个元素,此操作很有用。例如,如果您有一个形状为[height, width, channels] 的图像,则可以将其与expand_dims(image, 0) 制作为一组图像,这将形成[1, height, width, channels] 的形状。

      【讨论】:

        猜你喜欢
        • 2016-08-24
        • 1970-01-01
        • 2021-09-27
        • 2017-06-16
        • 2018-09-17
        • 2023-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多