本文介绍了Python/Tensorflow-我已经训练了卷积神经网络,如何对其进行测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用二进制文件中的以下数据(标签,文件名,数据(像素))训练了卷积神经网络(CNN):

I have trained a convolutional neural network (CNN) with the following data that I had in a binary file (label, filename, data (pixels)):

[array([2, 1, 0, 2, 1, 0, 2, 1, 0, 2, 1, 0, 2, 1, 0, 2, 1, 0, 2, 1, 0, 2, 1,
           0, 2, 1, 0, 2, 1, 0]), array(['10_c.jpg', '10_m.jpg', '10_n.jpg', '1_c.jpg',
           '1_m.jpg', '1_n.jpg', '2_c.jpg', '2_m.jpg',
           '2_n.jpg', '3_c.jpg', '3_m.jpg', '3_n.jpg',
           '4_c.jpg', '4_m.jpg', '4_n.jpg', '5_c.jpg',
           '5_m.jpg', '5_n.jpg', '6_c.jpg', '6_m.jpg',
           '6_n.jpg', '7_c.jpg', '7_m.jpg', '7_n.jpg',
           '8_c.jpg', '8_m.jpg', '8_n.jpg', '9_c.jpg',
           '9_m.jpg', '9_n.jpg'], 
          dtype='<U15'), array([[255, 252, 255, ..., 255, 255, 255],
           [136, 137, 138, ..., 114, 110, 111],
           [200, 200, 199, ..., 179, 178, 177],
           ..., 
           [146, 157, 165, ..., 202, 202, 201],
           [228, 225, 222, ..., 219, 221, 223],
           [128, 127, 127, ..., 133, 129, 127]])]

每个批次都包含所有图像,并运行了30个epohs:

Each batch contained all the images, and 30 epohs were run:

EPOCH 0
0 0.476923
DONE WITH EPOCH
EPOCH 1
0 0.615385
DONE WITH EPOCH
EPOCH 2
0 0.615385
DONE WITH EPOCH
EPOCH 3
0 0.538462
DONE WITH EPOCH
EPOCH 4
0 0.384615
DONE WITH EPOCH
...
...
EPOCH 28
0 0.615385
DONE WITH EPOCH
EPOCH 29
0 0.692308
DONE WITH EPOCH

我的问题是我想尝试新图像(测试),并且想知道返回的类(0,1,2).在这种情况下我该怎么办?换句话说,我训练了CNN,但是如何测试呢?

My question is that I would like to try new images (test), and would like to know the class returned (0,1,2). What should I do in this case? In other words, I trained the CNN, but how to test it?

EDIT-1

对于评估准确性点,在测试20张图像时,我得到了以下结果:

For the Evaluating accuracy point, I got the following results when testing 20 images:

EPOCH 0
0 1.0
DONE WITH EPOCH
EPOCH 1
0 1.0
DONE WITH EPOCH
EPOCH 2
0 1.0
DONE WITH EPOCH
EPOCH 3
0 1.0
DONE WITH EPOCH
EPOCH 4
0 1.0
DONE WITH EPOCH
EPOCH 5
0 1.0
DONE WITH EPOCH
EPOCH 6
0 1.0
DONE WITH EPOCH
EPOCH 7
0 1.0
DONE WITH EPOCH
EPOCH 8
0 1.0
DONE WITH EPOCH
EPOCH 9
0 1.0
DONE WITH EPOCH
EPOCH 10
0 1.0
DONE WITH EPOCH
EPOCH 11
0 1.0
DONE WITH EPOCH
EPOCH 12
0 1.0
DONE WITH EPOCH
EPOCH 13
0 1.0
DONE WITH EPOCH
EPOCH 14
0 1.0
DONE WITH EPOCH
EPOCH 15
0 1.0
DONE WITH EPOCH
EPOCH 16
0 1.0
DONE WITH EPOCH
EPOCH 17
0 1.0
DONE WITH EPOCH
EPOCH 18
0 1.0
DONE WITH EPOCH
EPOCH 19
0 1.0
DONE WITH EPOCH
EPOCH 20
0 1.0
DONE WITH EPOCH
EPOCH 21
0 1.0
DONE WITH EPOCH
EPOCH 22
0 1.0
DONE WITH EPOCH
EPOCH 23
0 1.0
DONE WITH EPOCH
EPOCH 24
0 1.0
DONE WITH EPOCH
EPOCH 25
0 1.0
DONE WITH EPOCH
EPOCH 26
0 1.0
DONE WITH EPOCH
EPOCH 27
0 1.0
DONE WITH EPOCH
EPOCH 28
0 1.0
DONE WITH EPOCH
EPOCH 29
0 1.0
DONE WITH EPOCH

应用获取网络为测试数据生成的标签点时,我得到了以下信息:

When applying the Getting the labels that the network produced for test data point, I got the following:

EPOCH 0
0 0.0
DONE WITH EPOCH
EPOCH 1
0 0.0
DONE WITH EPOCH
EPOCH 2
0 0.0
DONE WITH EPOCH
EPOCH 3
0 0.0
DONE WITH EPOCH
EPOCH 4
0 0.0
DONE WITH EPOCH
EPOCH 5
0 0.0
DONE WITH EPOCH
EPOCH 6
0 0.0
DONE WITH EPOCH
EPOCH 7
0 0.0
DONE WITH EPOCH
EPOCH 8
0 0.0
DONE WITH EPOCH
EPOCH 9
0 0.0
DONE WITH EPOCH
EPOCH 10
0 0.0
DONE WITH EPOCH
EPOCH 11
0 0.0
DONE WITH EPOCH
EPOCH 12
0 0.0
DONE WITH EPOCH
EPOCH 13
0 0.0
DONE WITH EPOCH
EPOCH 14
0 0.0
DONE WITH EPOCH
EPOCH 15
0 0.0
DONE WITH EPOCH
EPOCH 16
0 0.0
DONE WITH EPOCH
EPOCH 17
0 0.0
DONE WITH EPOCH
EPOCH 18
0 0.0
DONE WITH EPOCH
EPOCH 19
0 0.0
DONE WITH EPOCH
EPOCH 20
0 0.0
DONE WITH EPOCH
EPOCH 21
0 0.0
DONE WITH EPOCH
EPOCH 22
0 0.0
DONE WITH EPOCH
EPOCH 23
0 0.0
DONE WITH EPOCH
EPOCH 24
0 0.0
DONE WITH EPOCH
EPOCH 25
0 0.0
DONE WITH EPOCH
EPOCH 26
0 0.0
DONE WITH EPOCH
EPOCH 27
0 0.0
DONE WITH EPOCH
EPOCH 28
0 0.0
DONE WITH EPOCH
EPOCH 29
0 0.0
DONE WITH EPOCH 

为什么我会得到01?拥有这些值是否有意义(即没有分数)?

Why am I either getting 0 or 1? Does having those values make sense (i.e. no fractions)?

EDIT-2

对于获取网络为测试数据生成的标签,在打印出标签值和每个时期的准确性时,我得到以下信息(标签始终为0,尽管我期望只能是02,并且精度为1):

For Getting the labels that the network produced for test data, when printing out the label value and the accuracy of each epoch, I got the following (labels always 0, although I was expecting either 0 or 2 only, and accuracy given as 1):

EPOCH 0
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] 1.0
DONE WITH EPOCH
EPOCH 1
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] 1.0
DONE WITH EPOCH
EPOCH 2
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] 1.0
DONE WITH EPOCH
EPOCH 3
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] 1.0
DONE WITH EPOCH
EPOCH 4
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] 1.0
DONE WITH EPOCH
EPOCH 5
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] 1.0
DONE WITH EPOCH
.....
.....
EPOCH 28
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] 1.0
DONE WITH EPOCH
EPOCH 29
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] 1.0
DONE WITH EPOCH

谢谢.

推荐答案

一般性讨论

通常,为了测试神经网络,您需要获取未用于训练的新标签数据,将网络应用于此数据(即,应用前馈过程),并评估结果(与您知道是真实的标签相比).

In general, in order to test neural network, you need to take new labeled data that you did not use for training, apply the network on this data (that is, apply the feed forward process), and evaluate the accuracy of the result (in comparison to the labels that you know to be true).

如果您没有这样的新数据(也就是说,如果您将所有数据都用于培训)并且您无法生成新数据,那么我建议您将您的培训数据拿出来,分开进行培训和测试,然后重新运行从一开始就对培训数据进行培训.重要的是,测试数据将是未使用的数据,以便能够评估模型的性能.

If you don't have such new data (that is, if you used all your data for training) and you cannot produce new data, I would suggest to take your training data, separate it to training and testing, and rerun your training procedure on the training data from the beginning. It is important that the test data would be an unused data in order to be able to evaluate the performance of your model.

评估准确性

现在,假设您是从问题中的"1/值",您可以执行类似的操作来测量测试数据的准确性:

Now, assuming you are talking about the network from this question, You can do something like that to measure the accuracy of your test data:

accuracy_test = sess.run(accuracy, feed_dict={x: test_data, y: test_onehot_vals})

其中test_datatest_onehot_vals是您的测试图片(和相应的标签).

where test_data and test_onehot_vals are your test pictures (and corresponding labels).

回想一下,为了进行培训,您可以运行以下命令:

Recall that for training you run the following:

_, accuracy_val = sess.run([train_op, accuracy], feed_dict={x: batch_data, y: batch_onehot_vals})

请注意,在评估accuracy_test时未使用train_op.这是由于以下事实:测试性能时,您并未优化权重或类似的东西(train_op会优化).您只需应用当前拥有的网络即可.

Note that I did not use train_op in the evaluation of accuracy_test. This is due to the fact that when you test your performance, you do not optimize weights or anything like that (which train_op does). You just apply the network that you currently have.

获取网络为测试数据生成的标签

最后,如果需要测试数据的实际标签,则需要获取tf.argmax(model_op, 1)的值.因此,您可以将其设置为单独的变量,例如

Finally, if you want the actual labels of your test data, you need to get the value of tf.argmax(model_op, 1). So you can set it into a separate variable, for example right above the line

correct_pred = tf.equal(tf.argmax(model_op, 1), tf.argmax(y,1))

您可以这样做:

res_model=tf.argmax(model_op, 1)
correct_pred = tf.equal(res_model, tf.argmax(y,1))

,然后将其与accuracy_test一起评估,如下所示:

and then evaluate it together with accuracy_test as follows:

res, accuracy_test = sess.run([res_model,accuracy], feed_dict={x: test_data, y: test_onehot_vals}).

在未标记的数据上应用网络

在完成对网络的测试之后,并假设您对结果满意,则可以继续使用网络并将其应用于新的和未标记的数据.例如,通过

After you finished testing the network, and assuming you are satisfied with the results, you can move on and apply the network on new and unlabeled data. For example by doing

res_new = sess.run(res_model, feed_dict={x: new_data}).

请注意,为了生成res_model(基本上意味着仅在输入上应用网络),您不需要任何标签,因此在feed_dict中不需要y值. res_new将是新标签.

Note that in order to produce res_model (which basically means just applying the network on the input) you don't need any labels, so you don't need y values in your feed_dict. res_new will be the new labels.

这篇关于Python/Tensorflow-我已经训练了卷积神经网络,如何对其进行测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!