Understanding deep network: visualize weights

johnny5550822: <p>Are there any exiting implementations of understanding what it is learning in the networks. For example, weight visualization, “deconvolution” for visualization, gradient ascent, etc?</p>
<p>Thanks!</p>

bodokaiser: <p>I dont think there exists a reliable all-in-one solution. For me I found <a href="https://github.com/facebookresearch/visdom" rel="nofollow noopener">visdom</a> to be a good building block for visualization.</p>
<p>You can access model weights via:</p>
<pre><code>for m in model.modules():
    if isinstance(m, nn.Conv2d):
        print(m.weights.data)
</code></pre>
<p>However you still need to convert <code>m.weights.data</code> to numpy and maybe even do some type casting so that you can pass it to <code>vis.image</code>.</p>

johnny5550822: <p>Great, I have heard about visdom too.</p>
<p>I guess it will be a good further pull request of pytorch. <img src="//discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=5" title=":slight_smile:" class="emoji" alt=":slight_smile:"> <a class="mention" href="/u/soumith_chintala">@Soumith_Chintala</a></p>

lakehanne: <p>Did you implement this already in visdom?</p>

johnny5550822: <p>Not yet, planning too. Did you have any implementation?</p>

lakehanne: <p><a class="mention" href="/u/bodokaiser">@bodokaiser</a>, are you sure the <code>model.modules()</code>  <code>nn.Conv2d</code> instance has weights attribute?<br>
Cause I tried to access the weights using your example and it returns</p>
<pre><code class="lang-auto">  self.weights = m.weights#.data
  File "/home/user/anaconda2/envs/py27/lib/python2.7/site-packages/torch/nn/modules/module.py", line 237, in __getattr__
    return object.__getattr__(self, name)
AttributeError: type object 'object' has no attribute '__getattr__'
</code></pre>

lakehanne: <p>The weights can be found via <code>model.state_dict()</code> and the values for layer weights can be extracted from the dictionary using <code>model.state_dict()['name of key']</code></p>

Fchaubard: <p>Not using visdom, but something I hacked together for this purpose (with an example for VGG):</p>
<pre><code>import torch
import torchvision.models as models
from matplotlib import pyplot as plt

def plot_kernels(tensor, num_cols=6):
    if not tensor.ndim==4:
        raise Exception("assumes a 4D tensor")
    if not tensor.shape[-1]==3:
        raise Exception("last dim needs to be 3 to plot")
    num_kernels = tensor.shape[0]
    num_rows = 1+ num_kernels // num_cols
    fig = plt.figure(figsize=(num_cols,num_rows))
    for i in range(tensor.shape[0]):
        ax1 = fig.add_subplot(num_rows,num_cols,i+1)
        ax1.imshow(tensor[i])
        ax1.axis('off')
        ax1.set_xticklabels([])
        ax1.set_yticklabels([])

    plt.subplots_adjust(wspace=0.1, hspace=0.1)
    plt.show()
    
  
vgg = models.vgg16(pretrained=True)
mm = vgg.double()
filters = mm.modules
body_model = [i for i in mm.children()][0]
layer1 = body_model[0]
tensor = layer1.weight.data.numpy()
plot_kernels(tensor)</code></pre>

johnny5550822: <p>Do you have the output of it?</p>

Fchaubard: <p>Here ya go!</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/1X/34dbfb7174515502893b6963e21c20e08514eae8.png" data-download-href="https://discuss.pytorch.org/uploads/default/34dbfb7174515502893b6963e21c20e08514eae8" title="Screen Shot 2017-05-03 at 10.24.32 PM.png"><img src="https://discuss.pytorch.org/uploads/default/optimized/1X/34dbfb7174515502893b6963e21c20e08514eae8_2_285x500.png" width="285" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/1X/34dbfb7174515502893b6963e21c20e08514eae8_2_285x500.png, https://discuss.pytorch.org/uploads/default/optimized/1X/34dbfb7174515502893b6963e21c20e08514eae8_2_427x750.png 1.5x, https://discuss.pytorch.org/uploads/default/original/1X/34dbfb7174515502893b6963e21c20e08514eae8.png 2x" data-dominant-color="A2A4A1"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screen Shot 2017-05-03 at 10.24.32 PM.png</span><span class="informations">518×906 87.8 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

johnny5550822: <p><a class="mention" href="/u/fchaubard">@Fchaubard</a>  Interesting, so they are 3x3 filters?</p>

smth: <p>yes vgg has 3x3 filters all over.</p>

zym1010: <p>How can I implement visualization techniques like deconvolution, guided backprop?</p>

johnny5550822: <p>I am working on it. May release some gist snippets in sometime. Would this be useful?</p>

zym1010: <p>Yes. How are you going to do it? I’m planning to implement it using backward hooks. However, seems that, backward hook only takes the gradient as input, and for many visualization techniques, the original input and output are also needed.</p>
<p>One thing I can think of, is using both forward and backward hooks, and keeping all the input/output in some external dictionary.</p>

johnny5550822: <p><a class="mention" href="/u/zym1010">@zym1010</a> Sorry for late reply. Oh, for salency map and class model optimization. It should be pretty easy. Guided backprop I have not invested into it yet. Backprop is just taking the gradient out, so I guess it’s fine.</p>
<p>I am going to create a repo (probably) to have some examples on saliency map and other. You want to work on it together?</p>

zym1010: <p>Yeah. sure. Let’s work on this together.</p>
<p>My goal is to have something in PyTorch that is similar to <a href="https://github.com/yosinski/deep-visualization-toolbox" rel="nofollow noopener">https://github.com/yosinski/deep-visualization-toolbox</a>, yet more flexible and powerful. The visualization code for custom networks, in the deep visualization toolbox is painful.</p>
<p>One reference I can think of, on saliency map stuffs, is <a href="https://github.com/Lasagne/Recipes/blob/master/examples/Saliency%20Maps%20and%20Guided%20Backpropagation.ipynb" rel="nofollow noopener">https://github.com/Lasagne/Recipes/blob/master/examples/Saliency%20Maps%20and%20Guided%20Backpropagation.ipynb</a></p>

johnny5550822: <p><a class="mention" href="/u/zym1010">@zym1010</a> Cool. I will open up a github repo in a new couple of weeks (probably). My goal at the beginning is to first show examples of how to implement several visualization algorithm such as saliency map, and eventually may develop a tool like the deep-visualization-toolbox (if there are enough time).</p>

zym1010: <p>I’ve finished a rough re-implementation of that Lasagne example I’m talking about.</p>
<p>Check <a href="https://github.com/leelabcnbc/cnnvis-pytorch/blob/master/test.ipynb" rel="nofollow noopener">https://github.com/leelabcnbc/cnnvis-pytorch/blob/master/test.ipynb</a></p>
<p>I’ve double checked it against the Lasagne example, and they are exactly the same, except for some bug on the Lasagne/Theano side on my system, for deconv.</p>

herleeyandi: <p><a class="mention" href="/u/johnny5550822">@johnny5550822</a> <a class="mention" href="/u/zym1010">@zym1010</a> How to visualize of the image as the result of every convolution?, are you able to make like <a href="https://github.com/yosinski/deep-visualization-toolbox" rel="nofollow noopener">this</a> . Or can we copy the Pytorch weight to caffe and produce the result?, do you have any suggestion?<br>
-Thank you-</p>