Simplicity
Simplicity

Reputation: 48916

Python/Tensorflow - I have trained the convolutional neural network, how to test it?

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

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

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

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 

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

EDIT-2

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

Thanks.

Upvotes: 3

Views: 1238

Answers (1)

Miriam Farber
Miriam Farber

Reputation: 19634

General discussion

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.

Evaluating accuracy

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})

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})

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.

Getting the labels that the network produced for test data

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))

You can do:

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

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}).

Applying the network on unlabeled data

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}).

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.

Upvotes: 7

Related Questions