Accessing layers of classifier from DeepLabv3 (torch vision model)

Hi, I wanted to access certain layers of DeepLab V3. Like this I got the model

Blockquote model = tv.segmentation.deeplabv3_resnet101(pretrained=False, progress=True, num_classes=1, aux_loss=None)

After that I accessed the backbone layers with success:

Blockquote self.bn1 = model.backbone.bn1
self.relu = model.backbone.relu
self.maxpool = model.backbone.maxpool
self.layer1 = model.backbone.layer1
self.layer2 = model.backbone.layer2

However, don’t succeed to get the layers from the Classifier part, because they are defined with an integer:

Blockquote DeepLabHead(
(0): ASPP(
(convs): ModuleList(
(0): Sequential(
(0): Conv2d(2048, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
(1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(2): ReLU()
)
(1): ASPPConv(
(0): Conv2d(2048, 256, kernel_size=(3, 3), stride=(1, 1), padding=(12, 12), dilation=(12, 12), bias=False)
(1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(2): ReLU()
)
(2): ASPPConv(
(0): Conv2d(2048, 256, kernel_size=(3, 3), stride=(1, 1), padding=(24, 24), dilation=(24, 24), bias=False)
(1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(2): ReLU()
)
(3): ASPPConv(
(0): Conv2d(2048, 256, kernel_size=(3, 3), stride=(1, 1), padding=(36, 36), dilation=(36, 36), bias=False)
(1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(2): ReLU()

Has somebody a solution for this? Thanks.

Hi,

In cases that names of layers are defined using integers, you can access them by using indexing like lists in python.
For instance, let’s say model.deeplabhead gives the quoted code. You can get ReLU in first sequential layer using model.deeplabhead[0].convs[0][2].

PS: You can format codes by wrapping them using a pair of ```

```
code
```

Bests