Can I change the channel for a specific Conv2d inside a network?

username: ooodragon
cooked: <p>Hello world!</p>
<p>Just wondering if there is any way to change the channel of given network’s specific convolution layer.</p>
<p>I tried</p>
<pre><code class="lang-auto">for index, (name, module) in enumerate(model.named_modules()):
    if 'conv' in name:
        print(module) # nn.Conv2d(3, 32, 3 , 3)... something like this
        getattr(model, name) = nn.Conv2d(3,3,3,3)
</code></pre>
<p>but it won’t work<br>
any suggestions??</p>

username: Nikronic
cooked: <p>Hi,</p>
<p>In this <a href="https://discuss.pytorch.org/t/renaming-model-object/85111/2">post</a> I wrote a function that renames a name of module without changing the order of it. You can use it in reverse mode, instead of changing name of a module, change the module of a name!</p>
<pre><code class="lang-auto">
from collections import OrderedDict
model = # your model
new_layer = # your desired layer
model.__dict__['_modules'] = OrderedDict([(k, new_layer) if k == 'name_of_layer_to_change' else (k, v) for k, v in model.__dict__['_modules'].items()])
</code></pre>
<p>Bests</p>

username: ooodragon
cooked: <p>hello!<br>
thanks for your answer!<br>
I greatly appreciate it</p>
<p>However I’m still unsure how I should go ahead and code this<br>
below is the network I would like to change.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/9/3/93e4c11c078232b809bcc199fc7cc2ea823014d5.png" data-download-href="https://discuss.pytorch.org/uploads/default/93e4c11c078232b809bcc199fc7cc2ea823014d5" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/9/3/93e4c11c078232b809bcc199fc7cc2ea823014d5.png" alt="image" data-base62-sha1="l6ki6F9NSRsBXO6yuGwqw1rD1mB" width="690" height="363" data-dominant-color="252525"><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">742×391 19.3 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>With your code, if I understood correctly, I must make a model that contains information on changed input and output channels but with a same model architecture that I want to change with…?</p>
<p>Could you give me an example code on it?</p>
<p>thanks a ton!</p>

username: Nikronic
cooked: <p>Let’s say I want to change <code>_bn0</code> to something else.</p>
<pre><code class="lang-auto">from collections import OrderedDict
new_layer = nn.BatchNorm2d(32, eps=0.5)
model.__dict__['_modules'] = OrderedDict([(k, new_layer) if k == '_bn0' else (k, v) for k, v in model.__dict__['_modules'].items()])
</code></pre>
<p>But this code need to be changed to accept recursion to change hierarchical layers. In that case, I prefer manual change rather than coding it:<br>
For instance, let’s say we want to change <code>static_padding</code> in first <code>MBConvBlock</code>:</p>
<pre><code class="lang-auto">model._blocks[0]._depthwise_conv.static_padding = nn.ZeroPad2d(padding=(2, 2, 2, 2), value=1.1313)

</code></pre>
<p>I think there should be better ways as there are different methods to read modules as you have mentioned. But I do not think there is any builtin method for such a case, so you need to program it. If you came up with a code that can generalize this behavior, would be great if you could share.</p>
<p>Bests</p>

username: ooodragon
cooked: <p>I will!<br>
Thanks a lot!</p>