Repository: pytorch/vision

Issue:
The ASPP layer has a hard-coded value that only works if the atrous_rates param is of length 3
## 🐛 Bug

The ASPP layer has a hard-coded value that only works if the list of atrous rates provided is of length 3. The hard-coded value in question is the `5` on line 83 here:
https://github.com/pytorch/vision/blob/master/torchvision/models/segmentation/deeplabv3.py#L82-L86

## To Reproduce

```python3
aspp = tv.models.segmentation.deeplabv3.ASPP(1, [12, 24]) # DeepLab uses [12, 24, 36], which works

aspp.eval()
with torch.no_grad():
    out = aspp(torch.empty(1, 1, 256, 256))
```
Results in
```
RuntimeError: Given groups=1, weight of size [256, 1280, 1, 1], expected input[1, 1024, 256, 256] to have 1280 channels, but got 1024 channels instead
```

## Expected behavior

It should work with different number of atrous rates. This can be fixed simply by replacing
```python3
nn.Conv2d(5 * out_channels, out_channels, 1, bias=False)
```
with

```python3
nn.Conv2d(len(self.convs) * out_channels, out_channels, 1, bias=False)
```

## Environment

```
PyTorch version: 1.6.0
Is debug build: False
CUDA used to build PyTorch: 10.2
ROCM used to build PyTorch: N/A

OS: Ubuntu 20.04.1 LTS (x86_64)
GCC version: (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Clang version: Could not collect
CMake version: version 3.18.2

Python version: 3.7 (64-bit runtime)
Is CUDA available: True
CUDA runtime version: Could not collect
GPU models and configuration: GPU 0: GeForce GTX 1060 with Max-Q Design
Nvidia driver version: 450.66
cuDNN version: /usr/lib/x86_64-linux-gnu/libcudnn.so.7.6.1
HIP runtime version: N/A
MIOpen runtime version: N/A

Versions of relevant libraries:
[pip3] numpy==1.19.1
[pip3] torch==1.6.0
[pip3] torchvision==0.7.0
[conda] blas                      1.0                         mkl  
[conda] cudatoolkit               10.2.89              hfd86e86_1  
[conda] mkl                       2020.2                      256  
[conda] mkl-service               2.3.0            py37he904b0f_0  
[conda] mkl_fft                   1.1.0            py37h23d657b_0  
[conda] mkl_random                1.1.1            py37h0573a6f_0  
[conda] numpy                     1.19.1           py37hbc911f0_0  
[conda] numpy-base                1.19.1           py37hfa32c7d_0  
[conda] torch                     1.6.0                    pypi_0    pypi
[conda] torchvision               0.7.0                    pypi_0    pypi
```


cc @vfdev-5


Discussion:
@AdeelH thanks for the report! As you correctly pointed out that 5 is due to the number of submodules `len(self.convs)` = 1 conv + len(atrous_rates) + 1 ASPPPooling. I agree that `ASPP` being a public class and publicly exposing `atrous_rates` should not hard-code the number of modules.

If you would like to fix that, feel free to send a PR.
Yes please ! A PR fixing this would be great! We missed this in https://github.com/pytorch/vision/pull/2174

Resolving patch:
diff --git a/torchvision/models/segmentation/deeplabv3.py b/torchvision/models/segmentation/deeplabv3.py
--- a/torchvision/models/segmentation/deeplabv3.py
+++ b/torchvision/models/segmentation/deeplabv3.py
@@ -80,7 +80,7 @@ def __init__(self, in_channels, atrous_rates, out_channels=256):
         self.convs = nn.ModuleList(modules)
 
         self.project = nn.Sequential(
-            nn.Conv2d(5 * out_channels, out_channels, 1, bias=False),
+            nn.Conv2d(len(self.convs) * out_channels, out_channels, 1, bias=False),
             nn.BatchNorm2d(out_channels),
             nn.ReLU(),
             nn.Dropout(0.5))


Test patch: