Reputation: 399
I am preparing my dataset using new tensoflow input pipeline, here is my code:
train_data = tf.data.Dataset.from_tensor_slices(train_images)
train_labels = tf.data.Dataset.from_tensor_slices(train_labels)
train_set = tf.data.Dataset.zip((train_data,train_labels)).shuffle(500).batch(30)
valid_data = tf.data.Dataset.from_tensor_slices(valid_images)
valid_labels = tf.data.Dataset.from_tensor_slices(valid_labels)
valid_set = tf.data.Dataset.zip((valid_data,valid_labels)).shuffle(200).batch(20)
test_data = tf.data.Dataset.from_tensor_slices(test_images)
test_labels = tf.data.Dataset.from_tensor_slices(test_labels)
test_set = tf.data.Dataset.zip((test_data, test_labels)).shuffle(200).batch(20)
# create general iterator
iterator = tf.data.Iterator.from_structure(train_set.output_types, train_set.output_shapes)
next_element = iterator.get_next()
train_init_op = iterator.make_initializer(train_set)
valid_init_op = iterator.make_initializer(valid_set)
test_init_op = iterator.make_initializer(test_set)
Now I wanted to create a confusion matrix for validation set of my CNN model after training, here is what I try to do:
sess.run(valid_init_op)
valid_img, valid_label = next_element
finalprediction = tf.argmax(train_predict, 1)
actualprediction = tf.argmax(valid_label, 1)
confusion_matrix = tf.confusion_matrix(labels=actualprediction,predictions=finalprediction,
num_classes=num_classes,dtype=tf.int32,name=None, weights=None)
print(sess.run(confusion_matrix, feed_dict={keep_prob: 1.0}))
In this way it creates confusion matrix but only for one batch of validation set. for that I tried to collect all validation set batches in list and then use the list for creating confusion matrix:
val_label_list = []
sess.run(valid_init_op)
for i in range(valid_iters):
while True:
try:
elem = sess.run(next_element[1])
val_label_list.append(elem)
except tf.errors.OutOfRangeError:
print("End of append.")
break
val_label_list = np.array(val_label_list)
val_label_list = val_label_list.reshape(40,2)
and now the val_label_list
contain the labels for all batches of my validation set and I can use it to create confusion matrix:
finalprediction = tf.argmax(train_predict, 1)
actualprediction = tf.argmax(val_label_list, 1)
confusion = tf.confusion_matrix(labels=actualprediction,predictions=finalprediction,
num_classes=num_classes, dtype=tf.int32,name="Confusion_Matrix")
But now when I want to run the confusion matrix and print it:
print(sess.run(confusion, feed_dict={keep_prob: 1.0}))
it gives me an error:
OutOfRangeError: End of sequence
[[Node: IteratorGetNext_5 = IteratorGetNext[output_shapes=[[?,10,32,32], [?,2]], output_types=[DT_FLOAT, DT_FLOAT], _device="/job:localhost/replica:0/task:0/device:CPU:0"](Iterator_5)]]
anyone can tell me how to deal with this error? or any other solution that solve my original problem?
Upvotes: 0
Views: 204
Reputation: 1751
The problem is related with the graph flow execution. Look at this line:
print(sess.run(confusion, feed_dict={keep_prob: 1.0}))
You are running the graph for getting the 'confusion' value. So all dependent nodes will be also executed. Then:
finalprediction = tf.argmax(train_predict, 1)
actualprediction = tf.argmax(val_label_list, 1)
confusion = tf.confusion_matrix(...)
I guess that your call to train_predict will try to obtain a new element from the training iterator which has been already completely iterated and after this the error is triggered.
You should compute the confusion matrix directly in the loop and accumulate the results in a variable.
sess.run(valid_init_op)
confusion_matrix = np.zeros(n_labels,n_labels)
while True:
try:
conf_matrix = sess.run(confusion)
confusion_matrix += conf_matrix
except tf.errors.OutOfRangeError:
print("End of append.")
break
Upvotes: 1