Unique module identifier

blackbirdbarber: <p>Is there any way to get id of module when I need that.</p>
<p>I can currently have a name but names may be duplicates:</p>
<pre><code class="lang-auto">for c in model.children():
    name = c._get_name()
</code></pre>

ptrblck: <pre><code class="lang-python">for name, moduke in model.named_modules():  # or model.named_children():
    print(name)
</code></pre>
<p>should return the module names, which should be unique.</p>
<p>Would this work for you?</p>

blackbirdbarber: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> : Does the inner module knows his name inside main module?<br>
Are we destined to use this code:</p>
<pre><code class="lang-auto">for n, m in model.named_modules():
        if (m==module): 
</code></pre>
<p>in order to grab the <code>n</code> (module name)<br>
Here is the detail of using <code>named_modules()</code></p>
<pre><code class="lang-auto">%matplotlib inline

import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader, Dataset, TensorDataset
from torch.optim import *
import torchvision

model = torchvision.models.segmentation.deeplabv3_resnet50(weights='DEFAULT')

## dict that will have all named modules and thair values 
## in, out, w, b (std dev and mean)
from collections import defaultdict
statDict= defaultdict(list)

''' 
has input, output and model during forward
'''
def stata(module, inp, outp):        
    for n, m in model.named_modules():
        if (m==module):           
            statDict[n+'_mean'].append(outp.mean().item())

hooks=[]
for name, m in model.named_modules(): 
    if(isinstance(m, (nn.BatchNorm2d, nn.Conv2d))):
        hooks.append(m.register_forward_hook(stata))        
        

# simplified batches

for i in range(0,3):
    x = torch.randn(2,3,80,80)
    output = model(x)

for h in hooks:
    h.remove()
</code></pre>

ptrblck: <p>Sorry, I don’t fully understand your code.<br>
It seems you are trying to register a forward hook on <code>m</code> and are internally checking again for this module to manipulate a <code>statDict</code> object? What’s your use case as it seems strange to manipulate a <code>state_dict</code> inside a forward hook?</p>

blackbirdbarber: <p>No this is <code>statDict</code> (statistical dictionary).<br>
The idea is to save calculated values for each batch. Something like how the specific layer inside the model behaves when training.</p>

blackbirdbarber: <p>Or to get plots like this:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/4/e/4e5d03a5efa9959947d17104e60a95c995aef3a4.png" data-download-href="https://discuss.pytorch.org/uploads/default/4e5d03a5efa9959947d17104e60a95c995aef3a4" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/4/e/4e5d03a5efa9959947d17104e60a95c995aef3a4_2_427x500.png" alt="image" data-base62-sha1="bbewxmtvZq7YO2YWQAvhybTL8c4" width="427" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/4/e/4e5d03a5efa9959947d17104e60a95c995aef3a4_2_427x500.png, https://discuss.pytorch.org/uploads/default/optimized/3X/4/e/4e5d03a5efa9959947d17104e60a95c995aef3a4_2_640x750.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/4/e/4e5d03a5efa9959947d17104e60a95c995aef3a4.png 2x" data-dominant-color="F9F9FA"><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">image</span><span class="informations">666×779 39.7 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>

blackbirdbarber: <p>I found quite the opposite can be done with this code:</p>
<pre><code class="lang-auto">from functools import reduce
model = torchvision.models.segmentation.deeplabv3_resnet50(weights='DEFAULT')

def get_module_by_name(module, access_string):
     names = access_string.split(sep='.')
     print(names)
     return reduce(getattr, names, module)

submodule = get_module_by_name(model, 'backbone.layer4.2.conv1')
print(submodule)
</code></pre>
<p>but this is getting the module based on his <code>named_modules</code> name. Can we get the <code>named_module</code> name easy when we have parent module and inside sub-module?</p>

ptrblck: <p>Ah OK, in that case your code might work, but I would probably just pass a unique identifier to the hook and store the stats using this identifier as the <code>key</code> to the <code>dict</code> as seen <a href="https://discuss.pytorch.org/t/how-can-l-load-my-best-model-as-a-feature-extractor-evaluator/17254/6">here</a>.</p>