输入手写数字输出识别结果——分析篇

前面主要实现了手写数字输出识别结果,下面谈谈对其的理解;

在反向传播backward函数中,读入mnist函数集

def main():
    #读入mnist
    mnist = input_data.read_data_sets("./data/", one_hot=True)
    #反向传播
    backward(mnist)

 其余步骤与mnist手写数字识别准确率一致

最重要步骤:mnista_app.py中

def application():
    #输入要识别的几张图片
    testNum = input("input the number of test pictures:")
    for i in range(testNum):
        #给出待识别图片的路径和名称
        testPic = raw_input("the path of test picture:")
        #图片预处理
        testPicArr = pre_pic(testPic)
        #获取预测结果
        preValue = restore_model(testPicArr)
        print "The prediction number is:", preValue

def main():
    application()
其中:

任务分成两个函数完成
1)testPicArr = pre_pic(testPic)对手写数字图片做预处理
2)preValue = restore_model(testPicArr) 将符合神经网络输入要求的图片喂给复现的神经网络模型,输出预测值
函数代码:

def restore_model(testPicArr):
    #利用tf.Graph()复现之前定义的计算图
    with tf.Graph().as_default() as tg:
        x = tf.placeholder(tf.float32, [None, mnist_forward.INPUT_NODE])
        #调用mnist_forward文件中的前向传播过程forword()函数
        y = mnist_forward.forward(x, None)
        #得到概率最大的预测值
        preValue = tf.argmax(y, 1)

        #实例化具有滑动平均的saver对象
        variable_averages = tf.train.ExponentialMovingAverage(mnist_backward.MOVING_AVERAGE_DECAY)
         variables_to_restore = variable_averages.variables_to_restore()
         saver = tf.train.Saver(variables_to_restore)

        with tf.Session() as sess:
            #通过ckpt获取最新保存的模型
            ckpt = tf.train.get_checkpoint_state(mnist_backward.MODEL_SAVE_PATH)
            if ckpt and ckpt.model_checkpoint_path:
                saver.restore(sess, ckpt.model_checkpoint_path)
        
                preValue = sess.run(preValue, feed_dict={x:testPicArr})
                return preValue
            else:
                print("No checkpoint file found")
                return -1

#预处理,包括resize,转变灰度图,二值化
def pre_pic(picName):
    img = Image.open(picName)
    reIm = img.resize((28,28), Image.ANTIALIAS)
    im_arr = np.array(reIm.convert('L'))
    #对图片做二值化处理(这样以滤掉噪声,另外调试中可适当调节阈值)
    threshold = 50
    #模型的要求是黑底白字,但输入的图是白底黑字,所以需要对每个像素点的值改为255减去原值以得到互补的反色。
    for i in range(28):
        for j in range(28):
            im_arr[i][j] = 255 - im_arr[i][j]
             if (im_arr[i][j] < threshold):
                 im_arr[i][j] = 0
            else: im_arr[i][j] = 255
    #把图片形状拉成1行784列,并把值变为浮点型(因为要求像素点是0-1 之间的浮点数)
    nm_arr = im_arr.reshape([1, 784])
    nm_arr = nm_arr.astype(np.float32)
    #接着让现有的RGB图从0-255之间的数变为0-1之间的浮点数
    img_ready = np.multiply(nm_arr, 1.0/255.0)

    return img_ready

1)main 函数中的 application 函数:输入要识别的几张图片(注意要给出待识别图片的路径和名称)。
2)代码处理过程:
(1)模型的要求是黑底白字,但输入的图是白底黑字,所以需要对每个像素点的值改为 255 减去原值以得到互补的反色。 让图片只有纯白色和纯黑色
(2)对图片做二值化处理(这样以滤掉噪声,另外调试中可适当调节阈值)。
(3)把图片形状拉成 1 行 784 列,并把值变为浮点型(因为要求像素点是 0-1之间的浮点数)。 
(4)接着让现有的 RGB 图从 0-255 之间的数变为 0-1 之间的浮点数。
(5)运行完成后返回到 main 函数。
(6)计算求得输出 y,y 的最大值所对应的列表索引号就是预测结果。

由于此次的数据集使用的是mnist数据集+二值化处理图片,对自己手写图片的预测错误率高;下篇讲一讲自定义数据集

原文地址:https://www.cnblogs.com/fcfc940503/p/11014200.html