在训练和评估了TensorFlow 教程中的CIFAR10数据集后,如何用样本图像来测试它呢?
我可以训练和评估来自Caffe机器学习框架的Imagenet教程,并且使用Python API在自定义应用中使用训练好的模型是相对容易的。
回答:
这并不是问题的100%答案,但它是一种类似的解决方法,基于在问题评论中建议的MNIST神经网络训练示例。
基于TensorFlow初学者MNIST教程,并感谢这个教程,这是一种使用自定义数据训练和使用您的神经网络的方法。
请注意,类似的事情也应该对CIFAR10等教程进行,如@在评论中提到的。
import input_dataimport datetimeimport numpy as npimport tensorflow as tfimport cv2from matplotlib import pyplot as pltimport matplotlib.image as mpimgfrom random import randintmnist = input_data.read_data_sets("MNIST_data/", one_hot=True)x = tf.placeholder("float", [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("float", [None,10])cross_entropy = -tf.reduce_sum(y_*tf.log(y))train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)init = tf.initialize_all_variables()sess = tf.Session()sess.run(init)#Train our modeliter = 1000for i in range(iter): batch_xs, batch_ys = mnist.train.next_batch(100) sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})#Evaluationg our model:correct_prediction=tf.equal(tf.argmax(y,1), tf.argmax(y_,1))accuracy=tf.reduce_mean(tf.cast(correct_prediction,"float"))print "Accuracy: ", sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels})#1: Using our model to classify a random MNIST image from the original test set:num = randint(0, mnist.test.images.shape[0])img = mnist.test.images[num]classification = sess.run(tf.argmax(y, 1), feed_dict={x: [img]})'''#Uncomment this part if you want to plot the classified image.plt.imshow(img.reshape(28, 28), cmap=plt.cm.binary)plt.show()'''print 'Neural Network predicted', classification[0]print 'Real label is:', np.argmax(mnist.test.labels[num])#2: Using our model to classify MNIST digit from a custom image:# create an an array where we can store 1 pictureimages = np.zeros((1,784))# and the correct valuescorrect_vals = np.zeros((1,10))# read the imagegray = cv2.imread("my_digit.png", 0 ) #0=cv2.CV_LOAD_IMAGE_GRAYSCALE #must be .png!# rescale itgray = cv2.resize(255-gray, (28, 28))# save the processed imagescv2.imwrite("my_grayscale_digit.png", gray)"""all images in the training set have an range from 0-1and not from 0-255 so we divide our flatten images(a one dimensional vector with our 784 pixels)to use the same 0-1 based range"""flatten = gray.flatten() / 255.0"""we need to store the flatten image and generatethe correct_vals arraycorrect_val for a digit (9) would be[0,0,0,0,0,0,0,0,0,1]"""images[0] = flattenmy_classification = sess.run(tf.argmax(y, 1), feed_dict={x: [images[0]]})"""we want to run the prediction and the accuracy functionusing our generated arrays (images and correct_vals)"""print 'Neural Network predicted', my_classification[0], "for your digit"
为了进一步的图像处理(数字应在白色背景上完全变黑)和更好的神经网络训练(准确率>91%),请查看TensorFlow的高级MNIST教程或我提到的第二个教程。