2015-11-18 27 views
9

我对机器学习比较陌生,目前在开发它时几乎没有任何经验。如何测试tensorflow cifar10 cnn教程模型

所以我问题是:培训,并从tensorflow tutorial我想知道评估cifar10数据集后,怎么可能用一个样本图像测试吗?

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

任何帮助将不胜感激!

回答

10

这是不是100%的回答到这个问题,但它是一个类似的解决方法,基于对该问题的评论中提出的MNIST NN培训示例。

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

请注意,对于像CIFAR10这样的教程,应该类似于@Yaroslav Bulatov在评论中提到的那样。

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" 

对于进一步的图像调节(数字应该是一个白色的背景完全黑暗)和更好的NN训练(准确度> 91%),请从TensorFlow高级教程MNIST或第二教程中,我已经提到过。

+0

请同时添加“如何保存和加载已经过训练的模型”的代码行 –

3

我建议看看TensorFlow网站上的basic MNIST tutorial。它看起来像你定义了一些函数,生成你想要的输出类型,然后运行你的会话,并传递这个评估函数(下面的correct_prediction)和一个包含你需要的任何参数的字典(下面的xy_)。

如果您定义和训练的一些网络,其采用输入x,基于你输入的响应y,并且你知道你期望的响应为您的测试设置y_,你可以打印出每一个响应你测试设置喜欢的东西:

correct_prediction = tf.equal(y, y_) % Check whether your prediction is correct 
print(sess.run(correct_prediction, feed_dict={x: test_images, y_: test_labels})) 

这仅仅是一个什么样的教程,在那里,而不是试图打印每个响应,他们判断的正确反应的百分比进行修改。还要注意,本教程使用单热矢量进行预测y和实际值y_,因此为了返回相关数字,他们必须找到这些矢量的哪个索引与tf.argmax(y, 1)中的哪一个索引相等。

编辑

一般来说,如果你定义在图形中的东西,你可以将它输出后,当您运行图。假设你定义的东西,决定了你的输出logits的SOFTMAX函数的结果为:

graph = tf.Graph() 
with graph.as_default(): 
    ... 
    prediction = tf.nn.softmax(logits) 
    ... 

那么你就可以在运行时的输出:

with tf.Session(graph=graph) as sess: 
    ... 
    feed_dict = { ... } # define your feed dictionary 
    pred = sess.run([prediction], feed_dict=feed_dict) 
    # do stuff with your prediction vector 
+0

谢谢您的建议。我可以理解他们的操作,以“确定对mnist测试数据和标签的正确响应的百分比”。 我现在怀疑的是如何评估一个数字并得到它的分类。我猜其中一个程序也会将我的数据转换为与tensorflow兼容的格式? 谢谢。 – Twimnox

+0

您需要将图像转换为[高度,宽度,深度]值为0..255的numpy数组,并将该数组输入。请参阅此处的格式:https://tensorflow.googlesource.com/tensorflow/+/master/ tensorflow/models/image/cifar10/cifar10_input.py –

+0

你能包括如何输出y_吗?现在您输入测试标签并基本上返回它们正确的频率。如果要输入测试图像并直接输出关联的softmax y_矢量,该怎么办? – BigBoy1337

3

以下示例不适用于mnist教程,而是一个简单的XOR示例。请注意0​​和test()方法。我们所宣布的所有&保持全球是权重,偏见和会议。在测试方法中,我们重新定义了输入的形状,并重用了我们在训练中提炼的偏差(和会话)相同的权重。

import tensorflow as tf 

#parameters for the net 
w1 = tf.Variable(tf.random_uniform(shape=[2,2], minval=-1, maxval=1, name='weights1')) 
w2 = tf.Variable(tf.random_uniform(shape=[2,1], minval=-1, maxval=1, name='weights2')) 

#biases 
b1 = tf.Variable(tf.zeros([2]), name='bias1') 
b2 = tf.Variable(tf.zeros([1]), name='bias2') 

#tensorflow session 
sess = tf.Session() 


def train(): 

    #placeholders for the traning inputs (4 inputs with 2 features each) and outputs (4 outputs which have a value of 0 or 1) 
    x = tf.placeholder(tf.float32, [4, 2], name='x-inputs') 
    y = tf.placeholder(tf.float32, [4, 1], name='y-inputs') 

    #set up the model calculations 
    temp = tf.sigmoid(tf.matmul(x, w1) + b1) 
    output = tf.sigmoid(tf.matmul(temp, w2) + b2) 

    #cost function is avg error over training samples 
    cost = tf.reduce_mean(((y * tf.log(output)) + ((1 - y) * tf.log(1.0 - output))) * -1) 

    #training step is gradient descent 
    train_step = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(cost) 

    #declare training data 
    training_x = [[0,1], [0,0], [1,0], [1,1]] 
    training_y = [[1], [0], [1], [0]] 

    #init session 
    init = tf.initialize_all_variables() 
    sess.run(init) 

    #training 
    for i in range(100000): 
     sess.run(train_step, feed_dict={x:training_x, y:training_y}) 

     if i % 1000 == 0: 
      print (i, sess.run(cost, feed_dict={x:training_x, y:training_y})) 

    print '\ntraining done\n' 


def test(inputs): 
    #redefine the shape of the input to a single unit with 2 features 
    xtest = tf.placeholder(tf.float32, [1, 2], name='x-inputs') 

    #redefine the model in terms of that new input shape 
    temp = tf.sigmoid(tf.matmul(xtest, w1) + b1) 
    output = tf.sigmoid(tf.matmul(temp, w2) + b2) 

    print (inputs, sess.run(output, feed_dict={xtest:[inputs]})[0, 0] >= 0.5) 


train() 

test([0,1]) 
test([0,0]) 
test([1,1]) 
test([1,0])