Change specific named variables in all Modules

11179: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/6/5689533a10d026b5a50affdca3619a9cc56700a0.png" data-download-href="https://discuss.pytorch.org/uploads/default/5689533a10d026b5a50affdca3619a9cc56700a0" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/5/6/5689533a10d026b5a50affdca3619a9cc56700a0.png" alt="image" data-base62-sha1="clxhmQSS1EiRXR5TStjgRrfqQs8" width="690" height="346" data-dominant-color="F2F2E2"><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">875×440 6.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>
<p>Hello, I have some problems changing pretrained models (in my case Yolov5).<br>
Model is built by selections of modules and numbers of repetition.<br>
I want to change modules like above, trying to mask specific spatial regions of input features per image inference.<br>
However, I don’t know how to change all module variables named mask per infernece.</p>
<p>How can I check and replace mask values in all modules?<br>
This is the logic flow I want:</p>
<pre><code class="lang-auto">&gt; for module in all_Modules:
&gt;    if mask in module variables:
&gt;        mask = specific_values_I_want</code></pre>

ptrblck: <p>You could iterate the <code>modules</code> of the parent <code>nn.Module</code>, check for the <code>mask</code> attribute via <code>hasattr</code> and assign e.g. a new <code>nn.Parameter</code> to it as seen here:</p>
<pre><code class="lang-python">class MyModule(nn.Module):
    def __init__(self):
        super().__init__()
        self.mask = None
        

class MyModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.lin1 = nn.Linear(1, 1)
        self.mod1 = MyModule()
        self.lin2 = nn.Linear(1, 1)
        self.mod2 = MyModule()


model = MyModel()
print(dict(model.named_parameters()))
&gt; {'lin1.weight': Parameter containing:
  tensor([[-0.3372]], requires_grad=True), 'lin1.bias': Parameter containing:
  tensor([0.7264], requires_grad=True), 'lin2.weight': Parameter containing:
  tensor([[0.0947]], requires_grad=True), 'lin2.bias': Parameter containing:
  tensor([0.2308], requires_grad=True)}

for name, module in model.named_modules():
    if hasattr(module, 'mask'):
        print('{} has mask attribute'.format(name))
        module.mask = nn.Parameter(torch.randn(1))
&gt; mod1 has mask attribute
  mod2 has mask attribute

print(dict(model.named_parameters()))
&gt; {'lin1.weight': Parameter containing:
  tensor([[-0.3372]], requires_grad=True), 'lin1.bias': Parameter containing:
  tensor([0.7264], requires_grad=True), 'mod1.mask': Parameter containing:
  tensor([-0.6128], requires_grad=True), 'lin2.weight': Parameter containing:
  tensor([[0.0947]], requires_grad=True), 'lin2.bias': Parameter containing:
  tensor([0.2308], requires_grad=True), 'mod2.mask': Parameter containing:
  tensor([-0.1601], requires_grad=True)}
</code></pre>