我如何获得为单个图像预测的神经网络值?

How do i get the value of the neural network predicted for a single image?

我正在尝试创建一个简单的 python 脚本,它允许您放入手写数字的图片,NN 模型将尝试猜测到目前为止我拥有的数字是什么成功制作了模型并对其进行了测试,但是在测试单个图像时,我得到了这样的输出。

https://i.imgur.com/0GNMUPR.png

def make_pred():
    Tk().withdraw()
    filename = askopenfilename()
    #the array that will hold the values of image
    image = np.zeros(784)
    #read image
    gray = cv2.imread(filename, cv2.IMREAD_GRAYSCALE )
    #resize image and set background to black
    gray = cv2.resize(255-gray, (28,28))
    #making the image a one dimensional array of 784
    flatten = gray.flatten() / 255.0
    cv2.imshow("heh",gray)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    #saving the image and the correct value
    image = flatten
    prediction = neural_network_model(x)
    n_save = tf.train.Saver()
    with tf.Session() as sess2:        
        n_save.restore(sess2, './nn_test/nnmodel')
        print(sess2.run(prediction,feed_dict={x: [image], y:[7]}))

y 值为 7,因为这是我尝试使用的数字。

那么如何获取NN认为字符的值呢?

从你提供的信息来看有点难判断。但是,我认为您得到的输出很可能只是 softmax 输出层之前的 logits。

将此输出提供给 softmax 层,然后您将获得输出的概率分布。在您的特定情况下,softmax 输出:

[0., 0., 0., 0., 0., 1., 0., 0., 0., 0.]

现在只需要取这个tensor的argmax即可。在您的示例中,网络的预测是 5.

您错误地将logits作为网络输出的一个可能原因是大多数框架将softmax输出与损失函数结合在一起。因此,虽然损失函数确实将 logits 作为输入,但网络的实际输出仅在对 logits 应用 softmax 输出层后才给出。