Hunar
Hunar

Reputation: 399

how to create iterator.get_next() for validation set

I am working on project to classify medical images using the CNN model, for my project I use tensorflow, after doing some search, at last, I could use new tensorflow input pipeline to prepare the train, validation and test set, here is the 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)

I can use next_element to iterate over the train set (next_element[0] for images and next_element[1] for labels), now I want to do is doing same thing for the validation set(creating an iterator for validation set), anyone can give me an idea how to do it?

Upvotes: 0

Views: 169

Answers (1)

Fariborz Ghavamian
Fariborz Ghavamian

Reputation: 888

You should be able to use the same next_element to get validation and test set.

For example, initialize the dataset by sess.run(valid_init_op) and then next_element generates data in the validation set.

with tf.Session as sess:
  sess.run(train_init_op)
  image_train, label_train = next_element

  sess.run(valid_init_op)
  image_val, label_val = next_element

  sess.run(test_init_op)
  image_test, label_test = next_element

Upvotes: 1

Related Questions