Changing dilation rate, stride in Conv Layer without changing its weights

username: lavish619
cooked: <p>I am using a pretrained resnet101 and I want to change the dilation rates and stride of some conv layers.</p>
<p>If I initialize the layers again, that will change the weights of that layer, but incase of stride or dilation rate change only, the weights should not get changed because the kernel size is same.</p>
<p>So how can I change the layer configuration without changing the weights keeping the kernel size same.</p>
<p>Currently, This is what I am doing, but it changes the weights.</p>
<pre><code class="lang-python">self.resnet = torchvision.models.resnet101(pretrained=True)
for i in range(23):
    self.resnet.layer3[i].conv2 = nn.Conv2d(256, 256,
                                            kernel_size=3,
                                            stride=1, 
                                            padding=2,
                                            dilation=2, 
                                            bias=False)
</code></pre>

username: KFrank
cooked: <p>Hi Lavish!</p>
<aside class="quote no-group" data-username="lavish619" data-post="1" data-topic="127689" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/lavish619/48/40629_2.png" class="avatar"> lavish619:</div>
<blockquote>
<p>So how can I change the layer configuration without changing the weights keeping the kernel size same.</p>
</blockquote>
</aside>
<p>If it were me, I would probably do:</p>
<pre data-code-wrap="python"><code class="lang-python">for i in range(23):
    saveWeight = self.resnet.layer3[i].conv2
    self.resnet.layer3[i].conv2 = nn.Conv2d (256, 256,
                                            kernel_size=3,
                                            stride=1, 
                                            padding=2,
                                            dilation=2, 
                                            bias=False)
    with torch.no_grad():
        self.resnet.layer3[i].conv2.weight.copy_ (saveWeight)

</code></pre>
<p>I <em>think</em> that the following would also work (but I haven’t tried it):</p>
<pre data-code-wrap="python"><code class="lang-python">for i in range(23):
    self.resnet.layer3[i].conv2.dilation = (2, 2)

</code></pre>
<p>The issue is that I don’t know whether <code>Conv2d</code> does something<br>
non-trivial with <code>dilation</code> when its constructor is called (other than<br>
just <code>self.dilation = dilation</code>), and even if this scheme works<br>
now, I don’t know whether it would be guaranteed to work in the future.</p>
<p>Best.</p>
<p>K. Frank</p>

username: lavish619
cooked: <p>Hi, Thanks for the solution.</p>
<p>But I want to ask why have you used torch.no_grad() for copying the weights.?</p>

username: KFrank
cooked: <p>Hi Lavish!</p>
<aside class="quote no-group" data-username="lavish619" data-post="3" data-topic="127689" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/lavish619/48/40629_2.png" class="avatar"> lavish619:</div>
<blockquote>
<p>why have you used torch.no_grad() for copying the weights.?</p>
</blockquote>
</aside>
<p>Try performing the weight-copy without <code>torch.no_grad()</code> and see.</p>
<p>Best.</p>
<p>K. Frank</p>