Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to visualize learned filters on tensorflow

Similarly to the Caffe framework, where it is possible to watch the learned filters during CNNs training and it's resulting convolution with input images, I wonder if is it possible to do the same with TensorFlow?

A Caffe example can be viewed in this link:

http://nbviewer.jupyter.org/github/BVLC/caffe/blob/master/examples/00-classification.ipynb

Grateful for your help!

like image 249
Twimnox Avatar asked Mar 02 '16 22:03

Twimnox


People also ask

How do you visualize filters in keras?

Set up the end-to-end filter visualization loop Repeatedly apply the gradient ascent step function defined above. Convert the resulting input image back to a displayable form, by normalizing it, center-cropping it, and restricting it to the [0, 255] range.

How do I find learnable parameters on CNN?

CONV layer: This is where CNN learns, so certainly we'll have weight matrices. To calculate the learnable parameters here, all we have to do is just multiply the by the shape of width m, height n, previous layer's filters d and account for all such filters k in the current layer.


1 Answers

To see just a few conv1 filters in Tensorboard, you can use this code (it works for cifar10)

# this should be a part of the inference(images) function in cifar10.py file

# conv1
with tf.variable_scope('conv1') as scope:
  kernel = _variable_with_weight_decay('weights', shape=[5, 5, 3, 64],
                                       stddev=1e-4, wd=0.0)
  conv = tf.nn.conv2d(images, kernel, [1, 1, 1, 1], padding='SAME')
  biases = _variable_on_cpu('biases', [64], tf.constant_initializer(0.0))
  bias = tf.nn.bias_add(conv, biases)
  conv1 = tf.nn.relu(bias, name=scope.name)
  _activation_summary(conv1)

  with tf.variable_scope('visualization'):
    # scale weights to [0 1], type is still float
    x_min = tf.reduce_min(kernel)
    x_max = tf.reduce_max(kernel)
    kernel_0_to_1 = (kernel - x_min) / (x_max - x_min)

    # to tf.image_summary format [batch_size, height, width, channels]
    kernel_transposed = tf.transpose (kernel_0_to_1, [3, 0, 1, 2])

    # this will display random 3 filters from the 64 in conv1
    tf.image_summary('conv1/filters', kernel_transposed, max_images=3)

I also wrote a simple gist to display all 64 conv1 filters in a grid.

like image 110
etoropov Avatar answered Oct 27 '22 16:10

etoropov