1
$\begingroup$

I have a CNN model for classifying lung CT images, the code is written in tensorflow, I added some tensorflow summaries to my code to show graph, scalar, histogram, ... of my tensorflow model in tensorboard, in last step when I want to add_summary to the file writer it gives me an error, here is my code:

def train_CNN(input):
    train_predict = CNN_Model(x_img)
    with tf.name_scope("cross_entropy"):
        cost = tf.nn.softmax_cross_entropy_with_logits_v2(logits=train_predict, labels=y_label, name='cross_entropy')
        cost = tf.reduce_mean(cost, name='reduce_mean')
        tf.summary.scalar("cost", cost)
    with tf.name_scope("optimization"):
        optimizer = tf.train.AdamOptimizer(learning_rate, name='AdamOptimizer').minimize(cost)

    with tf.name_scope("accuracy"):
        correct_predict = tf.equal(tf.argmax(train_predict, 1), tf.argmax(y_label, 1))
        accuracy = tf.reduce_mean(tf.cast(correct_predict, tf.float32))
        tf.summary.scalar("accuracy", accuracy)

    #tf.summary.image("input", x_img, 5)

    sess.run(tf.global_variables_initializer())
    log_train_path = 'C:/temp/tensorflow_logs' + '/train_{}'.format(datetime.now().strftime("%Y-%m-%d-%H%M%S"))   
    summary_writer = tf.summary.FileWriter(log_train_path)
    summary_writer.add_graph(sess.graph)
    merged_summary = tf.summary.merge_all()

    all_time = 0

    for epoch in range(num_epochs):
        start_time = time.time()
        ep_loss = 0
        for data in train_data:
            X = data[0]
            Y = data[1]
            summary, _, c = sess.run([merged_summary, optimizer, cost], feed_dict={x_img: X, y_label: Y})
            ep_loss += c
            summary_writer.add_summary(summary, epoch)
        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')

        print('Accuracy of this epoch:',accuracy.eval({x_img:[i[0] for i in val_data], y_label:[i[1] for i in val_data]}))

    print('Finall Accuracy:',accuracy.eval({x_img:[i[0] for i in val_data], y_label:[i[1] for i in val_data]}), 'time usage: '+str(all_time)+' seconds')

after running the model it give me error, can anyone tell me how to solve it?

here is the error:

InvalidArgumentError: Expected dimension in the range [-1, 1), but got 1
     [[Node: accuracy/ArgMax_1 = ArgMax[T=DT_FLOAT, Tidx=DT_INT32, output_type=DT_INT64, _device="/job:localhost/replica:0/task:0/device:CPU:0"](_arg_y_label_0_1, accuracy/ArgMax_1/dimension)]]
$\endgroup$
  • $\begingroup$ Which line does it refer to? $\endgroup$ – Media Aug 25 '18 at 12:16
  • $\begingroup$ @Media, these two lines causes the error: summary, _, c = sess.run([merged_summary, optimizer, cost], feed_dict={x_img: X, y_label: Y}), summary_writer.add_summary(summary, epoch) $\endgroup$ – Hunar Aug 25 '18 at 12:53
  • $\begingroup$ Ostensibly our input dimensions do not match. $\endgroup$ – Media Aug 25 '18 at 17:21
  • $\begingroup$ the problem is occurred because of the summary if I make the first line like this _, c = sess.run([optimizer, cost], feed_dict={x_img: X, y_label: Y}) and remove the second line it works without any problem. because that I don't think the problem in the input dimension. $\endgroup$ – Hunar Aug 26 '18 at 6:32
1
$\begingroup$

Check out dimension

 X = data[0]
 Y = data[1]

Its likely that have dimensions like (1, ) what means that's 1 dim vector.

That should be work

import numpy as np
X = np.array(X).reshape(-1,1)
Y = np.array(Y).reshape(-1,1)
| improve this answer | |
$\endgroup$
  • $\begingroup$ X is a 3d array like this [ [ [] ] ] and Y is a 1d array like this [], the problem is occurred when I add summary to this line summary, _, c = sess.run([merged_summary, optimizer, cost], feed_dict={x_img: X, y_label: Y}) if I use it like this: _, c = sess.run([optimizer, cost], feed_dict={x_img: X, y_label: Y}). there is no error. $\endgroup$ – Hunar Aug 26 '18 at 8:53
  • $\begingroup$ Check this X = np.squeeze(X, axis=1) $\endgroup$ – fuwiak Aug 26 '18 at 9:02
  • $\begingroup$ thank you dear, I am sure this does not solve my problem. $\endgroup$ – Hunar Aug 26 '18 at 11:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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