Python / Tensorflow - I've trained a convolutional neural network, how do I test it?

I prepared 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]])]

      

Each batch contained all the images and performed 30 periods:

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

      

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

EDIT-1

For the Accuracy Evaluation 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 Retrieving Shortcuts Generated by Network for Test Data I got this:

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 

      

Why am I either getting 0

or 1

? Do these values ​​make sense (i.e. no fractions)?

EDIT-2

For Getting the labels generated by the network for the test data , when printing the label value and precision of each epoch, I got the following (labels are always 0

, although I expected either 0

or 2

, and precision is 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

      

Thank.

+3


source to share


1 answer


General discussion

In general, to test a neural network, you need to take new tainted data that you did not use for training, apply the network to that data (i.e. apply a forward feed process), and evaluate the accuracy of the result (versus the labels you know to be true).

If you do not have such new data (that is, if you have used all your data for training) and you cannot create new data, I would suggest taking your training data, split it into training and testing, and repeating your training procedure for training data from the very beginning. It is important that the test data is unused data in order to be able to evaluate the performance of your model.

Accuracy assessment

Now if you are talking about networking from this question, you can do something like this to measure the accuracy of your test data:

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

      

where test_data

and test_onehot_vals

are your test images (and corresponding labels).

To recap, for training, you run the following:

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

      

Please note that I have not used train_op

in the evaluation accuracy_test

. This is because when you test for performance, you are not optimizing weights or anything like that (which does train_op

). You are simply using the network that you currently have.

Retrieving Network Generated Shortcuts for Test Data



Finally, if you want the actual labels of your test data, you need to get the value tf.argmax(model_op, 1)

. This way you can set it to a separate variable, for example, right above the line

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

      

You can do:

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

      

and then evaluate it along with the accuracy_test

following:

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

      

Application of the network over untagged data

After you are done testing the network and feel satisfied with the results, you can go ahead and apply the network to new and unlabeled data. For example, by doing

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

...

Note that res_model

you don't need labels to create (which basically means just applying the feed to the input), so you don't need the values y

in your feed_dict. res_new

there will be new labels.

+7


source







All Articles