How To Use Image_summary To View Images From Different Batches In Tensorflow?
Solution 1:
To view one image from each batch, you need to fetch the result of the tf.image_summary()
op every time you run a step. For example, it you have the following setup:
images = ...
loss = ...
optimizer = ...
train_op = optimizer.minimize(loss)
init_op = tf.initialize_all_variables()
image_summary_t = tf.image_summary(images.name, images, max_images=1)
sess = tf.Session()
summary_writer = tf.train.SummaryWriter(...)
sess.run(init_op)
...you could set up your training loop to capture one image per iteration as follows:
for _ in range(10000):
_, image_summary = sess.run([train_op, image_summary_t])
summary_writer.add_summary(image_summary)
Note that capturing summaries on each batch might be inefficient, and you should probably only capture the summary periodically for faster training.
EDIT: The above code writes a separate summary for each image, so your log will contain all of the images, but they will not all be visualized in TensorBoard. If you want to combine your summaries to visualize images from multiple batches, you could do the following:
combined_summary = tf.Summary()
for i in range(10000):
_, image_summary = sess.run([train_op, image_summary_t])
combined_summary.MergeFromString(image_summary)
if i % 10 == 0:
summary_writer.add_summary(combined_summary)
combined_summary = tf.Summary()
Solution 2:
I was able to solve this by creating a new image_summary
op for each batch. i.e. I went from something that looked like:
train_writer = tf.train.SummaryWriter('summary_dir')
img = tf.image_summary("fooImage", img_data)
for i inrange(N_BATCHES):
summary, _ = sess.run([img, train_step])
train_writer.add_summary(summary, i)
(Which, frustratingly, was not doing what I expected.) To...
train_writer = tf.train.SummaryWriter('summary_dir')
for i inrange(N_BATCHES):
# Images are sorted in lexicographic order, so zero-pad the name
img = tf.image_summary("fooImage{:06d}".format(i), img_data)
summary, _ = sess.run([img, train_step])
train_writer.add_summary(summary)
Post a Comment for "How To Use Image_summary To View Images From Different Batches In Tensorflow?"