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

**URL:** <https://discuss.pytorch.org/t/changing-dilation-rate-stride-in-conv-layer-without-changing-its-weights/127689>\
**Category:** vision\
**Created:** [July 25, 2021, 11:39am UTC](https://discuss.pytorch.org/t/changing-dilation-rate-stride-in-conv-layer-without-changing-its-weights/127689 "2021-07-25T11:39:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lavish619](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/lavish619/32/40629_2.png) [@lavish619](https://discuss.pytorch.org/u/lavish619)\
**Post date:** [July 25, 2021, 11:39am UTC](https://discuss.pytorch.org/t/changing-dilation-rate-stride-in-conv-layer-without-changing-its-weights/127689/1 "2021-07-25T11:39:44Z")

</div>

I am using a pretrained resnet101 and I want to change the dilation rates and stride of some conv layers.

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.

So how can I change the layer configuration without changing the weights keeping the kernel size same.

Currently, This is what I am doing, but it changes the weights.

```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)

```

---

<div class="post-metadata">

**Author:** ![KFrank](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/k/ecb155/32.png) [@KFrank](https://discuss.pytorch.org/u/KFrank)\
**Post date:** [July 25, 2021, 6:18pm UTC](https://discuss.pytorch.org/t/changing-dilation-rate-stride-in-conv-layer-without-changing-its-weights/127689/2 "2021-07-25T18:18:44Z")

</div>

Hi Lavish!

> [@lavish619](#):
>
> So how can I change the layer configuration without changing the weights keeping the kernel size same.

If it were me, I would probably do:

```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)

```

I _think_ that the following would also work (but I haven’t tried it):

```python
for i in range(23):
    self.resnet.layer3[i].conv2.dilation = (2, 2)

```

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

Best.

K. Frank

---

<div class="post-metadata">

**Author:** ![lavish619](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/lavish619/32/40629_2.png) [@lavish619](https://discuss.pytorch.org/u/lavish619)\
**Post date:** [July 26, 2021, 4:54am UTC](https://discuss.pytorch.org/t/changing-dilation-rate-stride-in-conv-layer-without-changing-its-weights/127689/3 "2021-07-26T04:54:13Z")

</div>

Hi, Thanks for the solution.

But I want to ask why have you used torch.no\_grad() for copying the weights.?

---

<div class="post-metadata">

**Author:** ![KFrank](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/k/ecb155/32.png) [@KFrank](https://discuss.pytorch.org/u/KFrank)\
**Post date:** [July 26, 2021, 1:55pm UTC](https://discuss.pytorch.org/t/changing-dilation-rate-stride-in-conv-layer-without-changing-its-weights/127689/4 "2021-07-26T13:55:34Z")

</div>

Hi Lavish!

> [@lavish619](#):
>
> why have you used torch.no\_grad() for copying the weights.?

Try performing the weight-copy without `torch.no_grad()` and see.

Best.

K. Frank
