mnist,自己手写一个数字总是验证不正确

jupyter上运行结果如下

导入数据

(x_train,y_train), (x_test,y_test) = mnist.load_data()
x_train = x_train.reshape(x_train.shape[0],28,28,1).astype('float32')
x_test = x_test.reshape(x_test.shape[0],28,28,1).astype('float32')

x_train /= 255
x_test /= 255

y_train = np_utils.to_categorical(y_train,10)
y_test = np_utils.to_categorical(y_test,10)

训练网络

model = Sequential()

model.add(Conv2D(filters = 64, kernel_size = (3,3), activation = 'relu', input_shape = (28,28,1)))
model.add(MaxPooling2D(pool_size = (2,2)))

model.add(Conv2D(filters = 64, kernel_size = (3,3), activation = 'relu'))
model.add(MaxPooling2D(pool_size = (2,2)))
model.add(Dropout(0.5))

model.add(Flatten())

model.add(Dense(128, activation = 'relu'))
model.add(Dense(10,activation = 'softmax'))

model.compile(loss = 'categorical_crossentropy', optimizer = Adadelta(), metrics = ['accuracy'])

model.fit(x_train,y_train,batch_size=100,epochs=20)

训练结果

图片说明

用自己手写的数字验证结果总是同一个数字

# 将图片转为灰度图并调整为28*28大小
def convert_gray(f, **args):
    rgb=io.imread(f)   
    gray=color.rgb2gray(rgb)     
    dst=transform.resize(gray,(28,28))       
    return dst

test_gray_resize = convert_gray('number3.png')

图片说明

图片说明

运行结果如上,我手写的数字3,预测结果是6,后面不管我用哪个自己手写的图片验证,预测结果都是6,刚开始做深度学习,出现bug真是愁好几天,大神求解惑

http://blog.csdn.net/sparta_117/article/details/66965760

参考:基于tensorflow的MNIST手写字识别
http://www.jianshu.com/p/4195577585e6

问题得到解决,是resize图片时出错

楼主,我用的PIL.Image模块,也碰到了同样的问题,不管写入什么都是5。代码如下:

 from PIL import Image
import numpy as np
import tensorflow as tf
import mnist_backward
import mnist_forward

def restore_model(testPicArr):
    with tf.Graph().as_default() as tg:
        x = tf.placeholder(tf.float32,[None,mnist_forward.INPUT_NODE])
        y = mnist_forward.forward(x,None)
        preValue = tf.argmax(y,1)

        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 = 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 exist!")
                return (-1)

def pre_pic(picName):
    img = Image.open(picName)
    reIm = img.resize((28,28),Image.ANTIALIAS)
    im_arr = np.array(reIm.convert('L'))
    threshold = 500
    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

    nm_arr = im_arr.reshape([1,784])
    nm_arr = nm_arr.astype(np.float32)
    img_ready = np.multiply(nm_arr,1.0/255.0)
    return (img_ready)

def application():
    testNum = input("input the number of test pictures :")
    testNum = int(testNum)
    for i in range(testNum):
        testPic = input("the path of test pictures :")
        testPicArr = pre_pic(testPic)
        preValue = restore_model(testPicArr)
        print("the prediction number is : ",preValue)

def main():
    application()

if __name__ == "__main__":
    main()


能解答下吗?

我已经找到原因了。一是:阈值threshol设错了,应该是50。另外是im_arr[i][j] = 255*im_arr[i][j]中应该是减号而不是乘号。