python - 如何测试tensorflow cifar10 cnn教程模型

标签 python testing machine-learning tensorflow

我对机器学习比较陌生,目前几乎没有开发它的经验。

所以我的问题是:在训练和评估来自tensorflow tutorial的cifar10数据集之后我想知道如何使用示例图像对其进行测试?

我可以训练和评估 Imagenet tutorial from the caffe machine-learning framework并且使用 python API 在自定义应用程序上使用经过训练的模型相对容易。

如有任何帮助,我们将不胜感激!

最佳答案

这不是问题的 100% 答案,但它是一种类似的解决方法,基于问题评论中建议的 MNIST NN 训练示例。

基于 TensorFlow begginer MNIST 教程,感谢 this tutorial ,这是一种使用自定义数据训练和使用神经网络的方法。

请注意,正如@Yaroslav Bulatov 在评论中提到的那样,对于诸如 CIFAR10 之类的教程也应该做类似的事情。

import input_data
import datetime
import numpy as np
import tensorflow as tf
import cv2
from matplotlib import pyplot as plt
import matplotlib.image as mpimg
from random import randint


mnist = 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 model
iter = 1000
for 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 picture
images = np.zeros((1,784))
# and the correct values
correct_vals = np.zeros((1,10))

# read the image
gray = cv2.imread("my_digit.png", 0 ) #0=cv2.CV_LOAD_IMAGE_GRAYSCALE #must be .png!

# rescale it
gray = cv2.resize(255-gray, (28, 28))

# save the processed images
cv2.imwrite("my_grayscale_digit.png", gray)
"""
all images in the training set have an range from 0-1
and 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 generate
the correct_vals array
correct_val for a digit (9) would be
[0,0,0,0,0,0,0,0,0,1]
"""
images[0] = flatten


my_classification = sess.run(tf.argmax(y, 1), feed_dict={x: [images[0]]})

"""
we want to run the prediction and the accuracy function
using our generated arrays (images and correct_vals)
"""
print 'Neural Network predicted', my_classification[0], "for your digit"

如需进一步的图像调节(白色背景中的数字应完全变暗)和更好的神经网络训练(准确度>91%),请查看 TensorFlow 的高级 MNIST 教程或我提到的第二个教程。

关于python - 如何测试tensorflow cifar10 cnn教程模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33784214/

相关文章:

python - 保留 TFIDF 结果以使用 Scikit for Python 预测新内容

python - 使用 Python 脚本仅返回特定值

python - Python 新手,一直停留在这个问题上

python - python 3.6 与旧版本中的字典顺序

ruby-on-rails - 为什么在我运行 'rake test:models' 时此模型测试中断(未失败)?

javascript - 可下载的 HTML 测试语料库

python - 在 Scikit-Learn 中设置多个算法试验时遇到问题

python - 使用 pandas to_datetime 时如何定义格式?

wordpress - 在不创建多个页面/url 的情况下对 Wordpress 进行 AB 测试

algorithm - Google 新闻如何自动将文章分类为科技/科学/健康/娱乐/等?