Tensorflow官方MNIST模型训练准确率高但预测性能低

High training accuracy but low prediction performance for Tensorflow's official MNIST model

我是机器学习的新手,我一直在关注 Tensorflow 官方 MNIST 模型 (https://github.com/tensorflow/models/tree/master/official/mnist)。在训练模型 3 个 epoch 并获得超过 98% 的准确率结果后,我决定用一些我自己的手写图像来测试数据集,这些图像与 MNIST 数据集中的图像非常接近:

    {'loss': 0.03686057, 'global_step': 2400, 'accuracy': 0.98729998}

手写1,预测为2:https://storage.googleapis.com/imageexamples/example1.png

手写4,预测为5: https://storage.googleapis.com/imageexamples/example4.png

手写7,预测正确为7: https://storage.googleapis.com/imageexamples/example7.png

但是,正如您在下面看到的,预测大多不正确。任何人都可以分享一些关于为什么会这样的见解吗?如果您需要任何其他信息,请告诉我。谢谢!

[2 5 7]
Result for output key probabilities:
[[  1.47042423e-01   1.40417784e-01   2.80471593e-01   1.18162427e-02
    1.71029475e-02   1.15245730e-01   9.41787264e-04   1.71402004e-02
    2.61987478e-01   7.83374347e-03]
 [  3.70134876e-05   3.59491096e-03   1.70885725e-03   3.44008535e-01
    1.75098982e-02   6.24581575e-01   1.02930271e-05   3.97418407e-05
    7.59732258e-03   9.11886105e-04]
 [  7.62941269e-03   7.74145573e-02   1.42017215e-01   4.73754480e-03
    3.75231934e-06   7.16139004e-03   4.40478354e-04   7.60131121e-01
    4.09408152e-04   5.51677040e-05]]

这是我用来将png转换为npy数组进行测试的脚本。所提供的“3”和“5”图像的结果数组与 TF 存储库中给出的数组相同,所以我认为这不是问题所在:

def main(unused_argv):

output = []
images = []

filename_generate = True
index = 0

if FLAGS.images is not None:
    images = str.split(FLAGS.images)
if FLAGS.output is not "": # check for output names and make sure outputs map to images
    output = str.split(FLAGS.output)
    filename_generate = False
    if len(output) != len(images):
        raise ValueError('The number of image files and output files must be the same.')

if FLAGS.batch == "True":
    combined_arr = np.array([]) # we'll be adding up arrays

for image_name in images:
    input_image = Image.open(image_name).convert('L') # convert to grayscale
    input_image = input_image.resize((28, 28)) # resize the image, if needed
    width, height = input_image.size
    data_image = array('B')
    pixel = input_image.load()
    for x in range(0,width):
        for y in range(0,height):
            data_image.append(pixel[y,x]) # use the MNIST format
    np_image = np.array(data_image)
    img_arr = np.reshape(np_image, (1, 28, 28))
    img_arr = img_arr/float(255) # use scale of [0, 1]
    if FLAGS.batch != "True":
        if filename_generate:
            np.save("image"+str(index), img_arr) # save each image with random filenames
        else:
            np.save(output[index], img_arr) # save each image with chosen filenames
        index = index+1
    else:
        if combined_arr.size == 0:
            combined_arr = img_arr
        else:
            combined_arr = np.concatenate((combined_arr, img_arr), axis=0) # add all image arrays to one array
if FLAGS.batch == "True":
    if filename_generate:
        np.save("images"+str(index), combined_arr) # save batched images with random filename
    else:
        np.save(output[0], combined_arr) # save batched images with chosen filename 

我没有改变官方 MNIST 模型中的任何东西,除了 epoch 的数量(之前是 40,改变是因为训练花了很长时间并且在 1 epoch 后已经看到了高精度)。

非常感谢!

MNIST 图像是 white-on-black;您链接的图片是 black-on-white.

除非我错过了转换步骤,否则您需要在尝试检测之前反转颜色。

MNIST 图像像素值范围从 0.00 到 1.00 从黑色到白色。通常当你使用你自己的图像时,它会是 0 到 255。我不确定我是否错过了任何将它映射回 0.0-1.0 的编码。你可以尝试打印出存储你的像素值的数组和确保白色像素接近或等于 1.0,黑色像素接近或等于 0.0。