Tensorflow - 使用我自己的图像测试mnist神经网络

时间:2016-10-21 14:26:40

标签: python numpy tensorflow mnist

我正在尝试编写一个脚本,允许我绘制一个数字图像,然后用MNIST训练的模型确定它是什么数字。

这是我的代码:

import random
import image
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
import numpy as np
import scipy.ndimage

mnist = input_data.read_data_sets( "MNIST_data/", one_hot=True )

x = tf.placeholder(tf.float32, [None, 784])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

y = tf.nn.softmax(tf.matmul(x, W) + b)
y_ = tf.placeholder(tf.float32, [None, 10])

cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))

train_step = tf.train.GradientDescentOptimizer(0.5).minimize (cross_entropy)

init = tf.initialize_all_variables()

sess = tf.Session()
sess.run(init)

for i in range( 1000 ):
    batch_xs, batch_ys = mnist.train.next_batch( 1000 )
    sess.run(train_step, feed_dict= {x: batch_xs, y_: batch_ys})

print ("done with training")

data = np.ndarray.flatten(scipy.ndimage.imread("im_01.jpg", flatten=True))
result = sess.run(tf.argmax(y,1), feed_dict={x: [data]})
print (' '.join(map(str, result)))

由于某种原因,结果总是错误的,但当我使用标准测试方法时,准确率为92%。

我认为问题可能是我对图像进行编码的方式:

data = np.ndarray.flatten(scipy.ndimage.imread("im_01.jpg", flatten=True))

我尝试查看the next_batch() function的tensorflow代码,看看他们是如何做到的,但我不知道如何与我的方法进行比较。

问题也可能出在其他地方。

任何有助于提高80 +%准确度的帮助都将非常感激。

1 个答案:

答案 0 :(得分:7)

我发现了我的错误:它反过来编码,黑色是255而不是0。

 data = np.vectorize(lambda x: 255 - x)(np.ndarray.flatten(scipy.ndimage.imread("im_01.jpg", flatten=True)))

修正了它。

相关问题