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

Jos*_*yne 1 python machine-learning mnist conv-neural-network tensorflow

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

    {'loss': 0.03686057, 'global_step': 2400, 'accuracy': 0.98729998}
Run Code Online (Sandbox Code Playgroud)

手写 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]]
Run Code Online (Sandbox Code Playgroud)

这是我用来将 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 
Run Code Online (Sandbox Code Playgroud)

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

非常感谢!

AKX*_*AKX 5

MNIST 图像是黑底白字;您链接的图像是黑白的。

除非我遗漏了一个转换步骤,否则您将需要在尝试检测之前反转颜色。