广志是我爸爸 2017-05-13 16:24 采纳率: 0%
浏览 5140
已结题

Tensorflow实现手写数字识别,使用训练模型进行预测时,为什么精确度远不如训练精确度??

用BP神经网络算法,基于Tensorflow训练了mnist数据集,在训练的python脚本中可以得到test上的精确度为96%,然后在另一个python脚本中,恢复出这个模型,输入手写数字的图片进行预测,100张图片识别的精确度只有70%多,请教各路大神帮忙解决一下,预测脚本的代码如下:

import sys
import tensorflow as tf
import os
from PIL import Image, ImageFilter
from pylab import *

def predictint(imvalue):

    with tf.Graph().as_default():

        def addlayer(input_data,insize,outsize,act_function=None):
          W=tf.Variable(tf.random_normal([insize,outsize]))
          b=tf.Variable(tf.zeros([outsize]))+0.1
          out_data=tf.matmul(input_data,W)+b
          if act_function==None:
        return out_data
          elif act_function=="relu":
        return tf.nn.relu(out_data)
          elif act_function=="softmax":
                #result=tf.nn.softmax(out_data)
        return tf.nn.softmax(out_data)
          else:
        return tf.nn.sigmoid(out_data)
        x_input=tf.placeholder(tf.float32,[None,784])
        #y_input=tf.placeholder(tf.float32,[None,10])
        l1=addlayer(x_input,784,64,act_function="relu")
        l2=addlayer(l1,64,10,act_function="softmax")

        init_op = tf.initialize_all_variables()
        saver = tf.train.Saver()

        with tf.Session() as sess:
               sess.run(init_op)
               saver.restore(sess, "./model.ckpt")

        prediction=tf.argmax(l2,1)
        return prediction.eval(feed_dict={x_input: [imvalue]}, session=sess)


def imageprepare(argv):
    im = Image.open(argv).convert('L')
    width = float(im.size[0])
    height = float(im.size[1])
    newImage = Image.new('L', (28, 28), (255)) #creates white canvas of 28x28 pixels

    if width > height: #check which dimension is bigger
        #Width is bigger. Width becomes 20 pixels.
        nheight = int(round((20.0/width*height),0)) #resize height according to ratio width
        if (nheigth == 0): #rare case but minimum is 1 pixel
            nheigth = 1  
        # resize and sharpen
        img = im.resize((20,nheight), Image.ANTIALIAS).filter(ImageFilter.SHARPEN)
        wtop = int(round(((28 - nheight)/2),0)) #caculate horizontal pozition
        newImage.paste(img, (4, wtop)) #paste resized image on white canvas
    else:
        #Height is bigger. Heigth becomes 20 pixels. 
        nwidth = int(round((20.0/height*width),0)) #resize width according to ratio height
        if (nwidth == 0): #rare case but minimum is 1 pixel
            nwidth = 1
         # resize and sharpen
        img = im.resize((nwidth,20), Image.ANTIALIAS).filter(ImageFilter.SHARPEN)
        wleft = int(round(((28 - nwidth)/2),0)) #caculate vertical pozition
        newImage.paste(img, (wleft, 4)) #paste resized image on white canvas

    newImage.show()

    tv = list(newImage.getdata()) #get pixel values

    #normalize pixels to 0 and 1. 0 is pure white, 1 is pure black.
    tva = [ (255-x)*1.0/255.0 for x in tv] 
    return tva
    #print(tva)

def main(argv):

    imvalue = imageprepare(argv)
    predint = predictint(imvalue)
    print (predint[0]) #first value in list
    f1 = open('/home/wch/MNIST_data/traindata_predict.txt','a')
    f1.writelines('%d  ' % predint[0])
    f1.close()



def VisitDir(path):
    for root,dirs,files in os.walk(path):
        for filepath in files:
            print(os.path.join(root,filepath))
            main(os.path.join(root,filepath))

if __name__ == "__main__":

    path = r"/home/wch/MNIST_data/data_convert2"
    VisitDir(path)
  • 写回答

2条回答 默认 最新

  • silent56_th 2017-06-15 11:10
    关注

    数据库不一样准确率自然不一样吧。而且100张从统计学意义并不大。MNIST中对于数字图像也做了一些预处理的,如果是自己的图片的话可能也需要同样进行处理的。

    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向