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?
CNN_Model
? $\endgroup$