2

我是新来的神经网络。我已经通过TensorFlow mninst ML初学者去Tensor Flow Mninst外部图像预测不起作用

使用tensorflow基本MNIST教程

,并试图使用外部图像在这里输入的形象描述

我已经更新由tensorflow

提供的MNIST例如获得预测
On top of that i have added few things : 
1. Saving trained models locally 
2. loading the saved models. 
3. preprocessing the image into 28 * 28. 

i have attached the image for reference 

1. while training the models, save it locally. So i can reuse it at any point of time. 
2. once after training, loading the models. 
3. creating an external image via gimp which contains any one values ranging from [0 - 9] 
4. using opencv to convert the image into 28 * 28 image and reversing the bit as well. 
5. Then trying to predict. 

我可以训练模型并妥善保存。

我得到的预测是不正确的。

查找我的代码下面

1.TrainSimple.py

# Load MNIST Data 
    from tensorflow.examples.tutorials.mnist import input_data 
    mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) 

    from random import randint 
    from scipy import misc 

    # Start TensorFlow InteractiveSession 
    import tensorflow as tf 
    sess = tf.InteractiveSession() 

    # Placeholders 
    x = tf.placeholder(tf.float32, shape=[None, 784]) 
    y_ = tf.placeholder(tf.float32, shape=[None, 10]) 

    # Variables 
    W = tf.Variable(tf.zeros([784,10])) 
    b = tf.Variable(tf.zeros([10])) 

    sess.run(tf.initialize_all_variables()) 

    # Predicted Class and Cost Function 
    y = tf.nn.softmax(tf.matmul(x,W) + b) 
    cross_entropy = -tf.reduce_sum(y_*tf.log(y)) 

    saver = tf.train.Saver() # defaults to saving all variables 

    # GradientDescentOptimizer 
    train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy) 


    # Train the Model 

     for i in range(40000): 
      if (i + 1) == 40000 : 
       saver.save(sess, "/Users/xxxx/Desktop/TensorFlow/"+"/model.ckpt", global_step=i) 
     batch = mnist.train.next_batch(50) 
     train_step.run(feed_dict={x: batch[0], y_: batch[1]}) 

    # Evaluate the Model 
    correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1)) 

    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 

    print(accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels})) 
  • loadImageAndPredict.py

    from random import randint 
    from scipy import misc 
    import numpy as np 
    import cv2 
    
    def preProcess(invert_file): 
        print "preprocessing the images" + invert_file 
        image=cv2.imread(invert_file,0) 
        ret,image_thresh = cv2.threshold(image,127,255,cv2.THRESH_BINARY) 
        l,b=image.shape 
    
        fr=0 
        lr=0 
        fc=0 
        lc=0 
    
        i=0 
        while len(set(image_thresh[i,]))==1: 
         i+=1 
        fr=i 
    
        i=0 
        while len(set(image_thresh[-1+i,]))==1: 
         i-=1 
        lr=i+l 
    
        j=0 
        while len(set(image_thresh[0:,j]))==1: 
         j+=1 
        fc=j 
    
        j=0 
        while len(set(image_thresh[0:,-1+j]))==1: 
         j-=1 
        lc=j+b 
    
        image_crop=image_thresh[fr:lr,fc:lc] 
        image_padded= cv2.copyMakeBorder(image_crop,5,5,5,5,cv2.BORDER_CONSTANT,value=255) 
        image_resized = cv2.resize(image_padded, (28, 28)) 
        image_resized = (255-image_resized) 
        cv2.imwrite(invert_file, image_resized) 
    
    import tensorflow as tf 
    sess = tf.InteractiveSession() 
    
    # Placeholders 
    x = tf.placeholder(tf.float32, shape=[None, 784]) 
    y_ = tf.placeholder(tf.float32, shape=[None, 10]) 
    
    # # Variables 
    W = tf.Variable(tf.zeros([784,10])) 
    b = tf.Variable(tf.zeros([10])) 
    
    
    
    # Predicted Class and Cost Function 
    y = tf.nn.softmax(tf.matmul(x,W) + b) 
    cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1])) 
    
    saver = tf.train.Saver() # defaults to saving all variables - in this case w and b 
    
    # Train the Model 
    # GradientDescentOptimizer 
    train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy) 
    flag_1 = 0 
    
    # create an an array where we can store 1 picture 
    images = np.zeros((1,784)) 
    # and the correct values 
    correct_vals = np.zeros((1,10)) 
    
    
    preProcess("4_white.png") 
    
    gray = cv2.imread("4_white.png", 0) 
    
    flatten = gray.flatten()/255.0 
    """ 
    we need to store the flatten image and generate 
    the correct_vals array 
    correct_val for a digit (9) would be 
    [0,0,0,0,0,0,0,0,0,1] 
    """ 
    images[0] = flatten 
    # print images[0] 
    print len(images[0]) 
    
    sess.run(tf.initialize_all_variables()) 
    
    ckpt = tf.train.get_checkpoint_state("/Users/xxxx/Desktop/TensorFlow") 
    if ckpt and ckpt.model_checkpoint_path: 
        saver.restore(sess, ckpt.model_checkpoint_path) 
        my_classification = sess.run(tf.argmax(y, 1), feed_dict={x: [images[0]]}) 
        print 'Neural Network predicted', my_classification[0], "for your digit" 
    
  • 我不确定我做了什么错误。

    认为简单的模型可能不起作用我已经使用这个卷积代码来预测。

    https://github.com/tensorflow/tensorflow/blob/master/tensorflow/models/image/mnist/convolutional.py

    即使不能正确地预测:(

    +0

    [Tensor Flow Mninst示例预测使用外部图像不起作用]的可能重复(http://stackoverflow.com/questions/ 36865622/tensor-flow-mninst-example-prediction-using-external-image-does-not-work) – DavidW

    回答

    0

    有些事情要检查:

    1. 请问你训练亏损下去
    2. 你得到训练数据集精度高? ?
    3. 您是否对验证数据集(预留的部分训练集?)有较高的准确性?
    4. 你对目标数据集的准确性是否高?

    如果您的培训损失较低(1.),那么您没有学习,并且需要尝试不同的超参数,例如学习率。

    如果你有高(2.)和低(3.),你就是过度适应,并且需要训练时间更短,或者有更高的正则化惩罚。如果你有高(3.)和低(4.),你的训练集并不代表你的实际组合。需要使您的训练集更具代表性,或者至少比较难,例如,通过添加扭曲

    +0

    我开始使用这个https://www.tensorflow.org/versions/r0.8/tutorials/index.html 在上面的教程中,我编写了代码来获取外部图像,并将其传递到numpy.array中所需格式(28 * 28)的预测变量。 所以它可以正确预测数量。 –