1
$\begingroup$

My CNN model is trained on the training set and validated on the validation set, now I want to test it on test set, here is my code:

x_img = tf.placeholder(tf.float32, name='x_img')
y_label = tf.placeholder(tf.float32, name='y_label')
reshape = tf.reshape(x_img, shape=[-1, img_x, img_y, img_z, 1], name='reshape')

def CNN_Model(input):    
    conv1 = conv_layer(reshape, num_channels, n_f_conv1, name="conv1")    
    max_pool1 = maxpool_layer(conv1, name="max_pool1")    
    conv2 = conv_layer(max_pool1, n_f_conv1, n_f_conv2, name="conv2")  
    max_pool2 = maxpool_layer(conv2, name="max_pool2")
    shape = 4*4*4*64
    flattened = tf.reshape(max_pool2,shape=[-1, shape], name='flattened')
    fc = fc_layer(flattened, shape, n_node_fc, name="fc")
    dropout1 = dropout(fc, keep_rate, name="dropout1")
    output_layer = output(dropout1, n_node_fc, num_classes, name="output_layer") 
    return output_layer

def train_CNN(input):
    train_predict = CNN_Model(x_img)
    with tf.variable_scope("cross_entropy", reuse=tf.AUTO_REUSE):
        lose = tf.nn.softmax_cross_entropy_with_logits_v2(logits=train_predict, labels=y_label, name='cross_entropy')
        cost = tf.reduce_mean(lose, name='reduce_mean_cost')
        tf.summary.scalar("cost", cost)

    with tf.variable_scope("optimization", reuse=tf.AUTO_REUSE):
        optimizer = tf.train.AdamOptimizer(learning_rate, name='AdamOptimizer').minimize(cost)

    init = tf.global_variables_initializer()
    print("Starting session...")
    with tf.Session() as sess:
        sess.run(init)
        all_time = 0
        batch_size = 120
        batch = 0
        print("Starting training...")
        for epoch in range(num_epochs):
            train_batch = train_data[batch:batch_size]
            batch += batch_size
            batch_size += batch_size
            start_time = time.time()
            ep_loss = 0
            for data in train_batch:
                X = data[0]
                Y = data[1]
                _, c = sess.run([optimizer, cost], feed_dict={x_img: X, y_label: Y})
                ep_loss += c
            end_time = time.time()
            all_time += int(end_time-start_time)
            print('Epoch', epoch+1, 'completed out of',num_epochs,'loss:',ep_loss, 
                  'time usage: '+str(int(end_time-start_time))+' seconds')


        correct_predict = tf.equal(tf.argmax(train_predict, 1), tf.argmax(y_label, 1))
        accuracy = tf.reduce_mean(tf.cast(correct_predict, tf.float32), name='reduce_mean_acc')
        print("Validation accuracy:", accuracy.eval({x_img:[i[0] for i in validate_data], 
                                               y_label:[i[1] for i in validate_data]}))
        print("Test accuracy:", accuracy.eval({x_img:[i[0] for i in test_data], 
                                               y_label:[i[1] for i in test_data]}))

I have a test dataset stored as test_data like train_data in the code above, tried to do it in more than one way, but I did not succeed, can anyone share a testing code with me, ofcourse based on my code?

$\endgroup$
5
  • $\begingroup$ This code just shows the optimisation operations you have done. Do have any place holder in your CNN_Model? $\endgroup$ Commented Sep 8, 2018 at 9:37
  • $\begingroup$ @Media, added some other parts of the code, I can add more if needed. $\endgroup$
    – Hunar
    Commented Sep 8, 2018 at 11:33
  • $\begingroup$ What you need to do is feeding your test data in the feed_dict of the run function. $\endgroup$ Commented Sep 8, 2018 at 11:57
  • $\begingroup$ can you do it by code? as I said I tried more than one time without success. anything you need I can provide it. $\endgroup$
    – Hunar
    Commented Sep 8, 2018 at 12:09
  • $\begingroup$ Let me know if you can't figure it out. $\endgroup$ Commented Sep 8, 2018 at 12:16

1 Answer 1

1
$\begingroup$

I don't know exactly where you have the problem but according to comments, take a look at the following line.

_, c = sess.run([optimizer, cost], feed_dict={x_img: X, y_label: Y})

feed_dict is used for passing data to your network. As you can see, X is the training data. You can replace it with the test data. You should also change the y_labels to the labels of the test data.

$\endgroup$
11
  • $\begingroup$ see the code, you mean something like this? $\endgroup$
    – Hunar
    Commented Sep 8, 2018 at 12:36
  • $\begingroup$ Yes, exactly. Consider this kind of coding of NNs as a kind of machine that you have tuned its bolts and nuts and you just input something, placeholders, and you get your results. $\endgroup$ Commented Sep 8, 2018 at 12:41
  • $\begingroup$ ok but I have a problem with that, with only 600 sample of data (360 for training, 120 for each of validation and testing) I get 95% of accuracy for both validation and test, I think this is not real accuracy with this amount of data? $\endgroup$
    – Hunar
    Commented Sep 8, 2018 at 12:52
  • $\begingroup$ Yes, your set is small for training deep models. $\endgroup$ Commented Sep 8, 2018 at 12:56
  • $\begingroup$ I know it's very small, but why I get 95% of accuracy? have you any idea? $\endgroup$
    – Hunar
    Commented Sep 8, 2018 at 12:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.