Skip to content

Instantly share code, notes, and snippets.

@panmari
Last active January 15, 2019 14:54
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save panmari/4622b78ce21e44e2d69c to your computer and use it in GitHub Desktop.
Save panmari/4622b78ce21e44e2d69c to your computer and use it in GitHub Desktop.
Tensorflow visualize convolutions
channels = 32
img_size = 128
W_conv1 = weight_variable([5, 5, 1, channels])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1))
# Produces a tensor of size [-1, img_size, img_size, channels]
## Prepare for visualization
# Take only convolutions of first image, discard convolutions for other images.
V = tf.slice(h_conv1, (0, 0, 0, 0), (1, -1, -1, -1), name='slice_first_input')
V = tf.reshape(V, (img_size, img_size, channels))
# Reorder so the channels are in the first dimension, x and y follow.
V = tf.transpose(V, (2, 0, 1))
# Bring into shape expected by image_summary
V = tf.reshape(V, (-1, img_size, img_size, 1))
tf.image_summary("first_conv", V)
@monjoybme
Copy link

Just an update: use tf.summary.image("first_conv", V) instead of tf.image_summary("first_conv", V). The code will be

channels = 32
img_size = 128
W_conv1 = weight_variable([5, 5, 1, channels])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1))
V = tf.slice(h_conv1, (0, 0, 0, 0), (1, -1, -1, -1), name='slice_first_input')
V = tf.reshape(V, (img_size, img_size, channels))
V = tf.transpose(V, (2, 0, 1))
V = tf.reshape(V, (-1, img_size, img_size, 1))
tf.summary.image("first_conv", V)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment