How to visualize learned filters on tensorflow

ghz 7months ago ⋅ 147 views

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!

Answers

Yes, it is possible to visualize learned filters and their resulting convolutions with input images in TensorFlow. Here's a general approach to achieve this:

  1. Accessing Filter Weights: You can access the weights of convolutional layers in TensorFlow using tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES). This will give you a list of trainable variables, which you can filter to extract the weights of convolutional layers.

  2. Visualizing Filters: Once you have the filter weights, you can visualize them using various methods. One common approach is to plot the weights as images.

  3. Visualizing Convolution Results: You can also visualize the convolution results by applying the learned filters to input images. This involves performing convolution operations using TensorFlow's tf.nn.conv2d function and then visualizing the resulting feature maps.

Here's a simplified example demonstrating how to visualize filter weights and convolution results in TensorFlow:

import tensorflow as tf
import matplotlib.pyplot as plt

# Assuming 'model' is your TensorFlow model
# Access the trainable variables
trainable_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)

# Filter convolutional layer weights
conv_weights = [var for var in trainable_vars if 'conv' in var.name]

# Assuming 'input_image' is your input image tensor
input_image = ...

# Visualize filter weights
with tf.Session() as sess:
    # Initialize variables
    sess.run(tf.global_variables_initializer())
    
    # Retrieve filter weights
    weights = sess.run(conv_weights)
    
    # Plot filter weights
    for i, weight in enumerate(weights):
        plt.subplot(1, len(weights), i+1)
        plt.imshow(weight, cmap='gray')
        plt.title('Filter {}'.format(i+1))
        plt.axis('off')
    plt.show()

    # Visualize convolution results
    for i, weight in enumerate(weights):
        # Perform convolution with input image
        conv_result = tf.nn.conv2d(input_image, weight, strides=[1, 1, 1, 1], padding='SAME')
        conv_result = sess.run(conv_result, feed_dict={input_image: input_image_data})
        
        # Plot convolution result
        plt.subplot(1, len(weights), i+1)
        plt.imshow(conv_result[0, :, :, 0], cmap='gray')  # Assuming single-channel output
        plt.title('Convolution {}'.format(i+1))
        plt.axis('off')
    plt.show()

This code demonstrates how to visualize filter weights and the resulting convolutions for each filter. You can modify it based on your specific model architecture and visualization preferences.