How can I replace an intermediate layer in a pre-trained network?

username: erogol
cooked: <p>I try to change the filter size of the layer with new weights values and then replace the existing one with this new one. I can access the layer by <code>model._modules</code> but cannot replace it. Ant help pls?</p>

username: deepcode
cooked: <p>I would love to know how to do the same thing.</p>

username: erogol
cooked: <p>My way to do is happens to be</p>
<p><code>model._modules['layer_name'] = th.nn.some_layer(...)</code></p>

username: Dor_Rosenblum
cooked: <p>thanks for sharing! is there any other way?<br>
might the set attribute “setattr” aid?</p>

username: chauhan-utk
cooked: <p><a class="mention" href="/u/erogol">@erogol</a> Did you find any solution to this question?</p>

username: brijraj
cooked: <p>Following the responses.</p>

username: Mostafa_Elhoushi
cooked: <p>I have managed to develop a code that can convert all the instances of a certain layer (up to a certain <code>num_to_convert</code>) in a generic model to a different layer. The code handles nested modules or blocks. I have modified it here for generality:</p>
<pre><code class="lang-auto">def convert_layers(model, num_to_convert, layer_type_old, layer_type_new, convert_weights=False):
    conversion_count = 0
    for name, module in reversed(model._modules.items()):
        if len(list(module.children())) &gt; 0:
            # recurse
            model._modules[name], num_converted = convert_layers(model=module, num_to_convert=num_to_convert-conversion_count, layer_type_old, layer_type_new, convert_weights)
            conversion_count += num_converted

        if type(module) == layer_type_old and conversion_count &lt; num_to_convert:
            layer_old = module
            layer_new = layer_type_new(module.in_channels, module.out_channels, module.kernel_size, module.stride,
                                             module.padding, module.dilation, module.groups,
                                             module.bias is not None, module.padding_mode) 

            if convert_weights == True:
                layer_new.weight = layer_old.weight
                layer_new.bias = layer_old.bias

            model._modules[name] = layer_new
            conversion_count += 1

    return model, conversion_count
</code></pre>
<p>To call the above function:</p>
<pre><code class="lang-auto">new_model = convert_layers(model, num_to_convert, layer_type_old=nn.Conv2d, layer_type_new=MyConv2d, convert_weights=False)
</code></pre>

username: Mostafa_Elhoushi
cooked: <p>PS: I noticed after posting the above that this line:</p>
<pre><code class="lang-auto">            layer_new = layer_type_new(module.in_channels, module.out_channels, module.kernel_size, module.stride,
                                             module.padding, module.dilation, module.groups,
                                             module.bias is not None, module.padding_mode) c
</code></pre>
<p>will only work if the layer to replace is <code>nn.Conv2d</code> and the new layer has similar parameters to pass to its constructor. If someone has a more generic way to handle this that will be great to share.</p>

username: VCasecnikovs
cooked: <p>This method does not delete the parameters of the old modules.<br>
Is there any solution with deletion of the old modules’ parameters?</p>