# How to save and test CNN model on test set after training

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

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?

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

_, 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.