Python / Tensorflow - 我已经训练了卷积神经网络,如何测试它?

时间:2017-03-26 16:52:58

标签: python tensorflow neural-network conv-neural-network convolution

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

[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个epoh:

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,但是如何测试呢?

修改-1

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

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

当应用获取网络为测试数据生成的标签时,我得到以下内容:

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 

为什么我要么0还是1?这些值是否有意义(即没有分数)?

修改-2

对于获取网络为测试数据生成的标签,在打印出标签值和每个纪元的准确性时,我得到以下内容(标签总是0,尽管我我只期待02,准确度为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

感谢。

1 个答案:

答案 0 :(得分:7)

一般性讨论

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

如果您没有这样的新数据(也就是说,如果您使用了所有数据进行培训)并且无法生成新数据,我建议您将您的培训数据与培训和测试分开,并重新运行您从一开始就训练数据的训练程序。重要的是,测试数据将是未使用的数据,以便能够评估模型的性能。

评估准确性

现在,假设您正在讨论来自this问题的网络, 您可以执行类似的操作来衡量测试数据的准确性:

fathers_occupation

其中accuracy_test = sess.run(accuracy, feed_dict={x: test_data, y: test_onehot_vals}) test_data是您的测试图片(和相应的标签)。

回想一下,对于培训,您可以执行以下操作:

test_onehot_vals

请注意,我在评估_, accuracy_val = sess.run([train_op, accuracy], feed_dict={x: batch_data, y: batch_onehot_vals}) 时没有使用train_op。这是因为当您测试性能时,您不会优化权重或类似的任何内容(accuracy_test执行此操作)。您只需应用当前拥有的网络。

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

最后,如果您想要测试数据的实际标签,则需要获得train_op的值。因此,您可以将其设置为单独的变量,例如在行

的正上方
tf.argmax(model_op, 1)

你可以这样做:

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

在未标记数据上应用网络

完成网络测试后,假设您对结果感到满意,您可以继续并在新的和未标记的数据上应用网络。例如,通过

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

请注意,为了生成res_new = sess.run(res_model, feed_dict={x: new_data})(这基本上意味着只在输入上应用网络),您不需要任何标签,因此Feed_dict中不需要res_model个值。 y将是新标签。