I am following google codelab:

Tensorflow for poet to train my custom model. This google codelab use the Inception-V3 model for training.

The inception-V3 model have 48 layer. My question is that how can i visualize image features at the hidden layers?


1 Answer 1



Here are two adapted functions from the first of the links below that should get some weights out for a given input sample for you and plot the activations for some filters of that layer:

def getActivations(layer,sample):
    units = sess.run(layer,feed_dict={x:np.reshape(sample, [1,784], order='F'), keep_prob:1.0})

def plotNNFilter(units):
    filters = units.shape[3]
    plt.figure(1, figsize=(20,20))
    n_columns = 6
    n_rows = math.ceil(filters / n_columns) + 1
    for i in range(filters):
        plt.subplot(n_rows, n_columns, i+1)
        plt.title('Filter ' + str(i))
        plt.imshow(units[0,:,:,i], interpolation="nearest", cmap="gray")

This is the result, showing the second layer of activations from their model used for MNIST classification - the sample used was clearly a 7:

example activations from layer 2

Rather than paste the entire code here, I suggest you read their instructions/explanations in context. The first link contains the function above.

Some background

You can do this a couple of ways:

  1. extract the activations for a given sample and plot them - you will get plots of varying sizes as you move through the network, correspoding to the dimensions of the weight matrix.

  2. select your target layer, freeze all layers before that layer, then perform backbrop all the way to the beginning. This essentially extrapolates the weights back to the input, allowing you to display them over an input sample - giving insight as to what a particular layer is focusing on.

Here is a lecture presented by Matt Zeiler, who co-authored the original paper that showed the second method above. Their approach gives some results like this:

Layer three of the network used in the linked paper

They use a "deconvolutional model" - which is also named the transpose convolution, which describes it more accurately. A deconvolution is actually a mathematical operator, which has a different meaning, so many people choose not to use the term.

Another approach to this, which goes a bit beyond what you asked, was proposed a few months later, by Simonyan et al., which looks at the saliency maps of weights/images - this is "the spatial support of a given class in a given image". IT can help localise objects - read the paper if that sounds good. In Section 4 of their paper, they claim their method to be a generalisation of the approach from Zeiler and Fergus.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.