本文介绍了Tensorflow的官方MNIST模型的训练准确性高但预测性能低的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm new to machine learning and I was following along with the Tensorflow official MNIST model (https://github.com/tensorflow/models/tree/master/official/mnist). After training the model for 3 epochs and getting accuracy results of over 98%, I decided to test the dataset with some of my own handwritten images that are very close to those found in the MNIST dataset:

    {'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

handwritten 7, predicted correctly as 7:https://storage.googleapis.com/imageexamples/example7.png

但是,正如您在下面看到的那样,这些预测大多是不正确的.谁能分享一些为什么会这样的见解?如果您需要其他任何信息,请告诉我.谢谢!

However, as you can see below, the predictions were mostly incorrect. Can anyone share some insight as to why this might be? If you want any other info, please let me know. Thanks!

[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存储库中给出的数组相同,所以我认为这不是问题所在:

Here is the script I used to convert pngs into npy arrays for testing. The resulting array for the provided '3' and '5' image is identical to the one given in the TF repository, so I don't think it's the issue:

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

除了历元数(以前是40,因为训练时间太长,而且在1历元后已经看到了很高的精度),我在MNIST官方模型中什么都没有改变.

I haven't changed anything in the official MNIST model except the number of epochs (previously 40, changed because it was taking so long to train and already seeing high accuracy after 1 epoch).

非常感谢!

推荐答案

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

The MNIST images are white-on-black; the images you've linked are black-on-white.

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

Unless there's a conversion step I missed, you'll want to invert the colors before attempting detection.

这篇关于Tensorflow的官方MNIST模型的训练准确性高但预测性能低的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-12 01:47