Alex McCraw
Alex McCraw

Reputation: 67

Outputting an image summary to Tensorboard

I'm trying to visualize the filters of a convolutional layer using this code, but cannot get the image to write to my summary file. I have scalars that I am outputting with no issue, and I attempted to modify the code to add the image as follows

summary = tf.Summary()
summary.value.add(tag='Perf/Reward', simple_value=float(mean_reward))
summary.value.add(tag='Perf/Length', simple_value=float(mean_length))
with tf.variable_scope(self.name + "/conv1", reuse=True):
    weights = tf.get_variable("weights")
    grid = put_kernels_on_grid(weights)
    image = tf.summary.image('conv1/weights', grid, max_outputs=1)
    summary.value.add(tag='conv1/weights', image=image)
self.summary_writer.add_summary(summary, episode_count)

With only the scalars, this works fine, but adding the image gives the error

TypeError: Parameter to MergeFrom() must be instance of same class: expected Image got Tensor. for field Value.image

I also tried adding the image summary directly by changing the code to

summary = tf.Summary()
summary.value.add(tag='Perf/Reward', simple_value=float(mean_reward))
summary.value.add(tag='Perf/Length', simple_value=float(mean_length))
with tf.variable_scope(self.name + "/conv1", reuse=True):
    weights = tf.get_variable("weights")
    grid = put_kernels_on_grid(weights)
    image = tf.summary.image('conv1/weights', grid, max_outputs=1)
    self.summary_writer.add_summary(image, episode_count)
self.summary_writer.add_summary(summary, episode_count)

But got the error

AttributeError: 'Tensor' object has no attribute 'value'

What is the correct way to output my image to my summary file?

Upvotes: 1

Views: 914

Answers (1)

Stephen
Stephen

Reputation: 824

put_kernels_on_grid is returning a tensor; by 'image' I think the author just means that you can print it out to see what the kernel looks like. Try using tf.summary.tensor_summary.

Whoops, sorry. I have been able to save a tensor as an image using the following:

import tensorflow as tf
import numpy as np

batch_xs = np.ones((100, 100, 1)) * 200
init = tf.constant(batch_xs, dtype=tf.uint8)
grid = tf.get_variable('var_name', dtype=tf.uint8, initializer=init)

encoded_image = tf.image.encode_jpeg(grid)

fwrite = tf.write_file("junk.jpeg", encoded_image)
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    result = sess.run(fwrite)

However tf.image.encode_jpeg still returns a tensor with DataType string, so tf.summary.image won't accept it. The code you're working from predates TensorFlow 1.0, so it's definitely not going to work as written.

Hopefully this helps somewhat.

Upvotes: 1

Related Questions