Extract intermediate layer in resent 18

Hi;

I am trying the truncate the resnet 18 model. I would like to truncate the model after the 2nd convolution

import torch
import torchvision

model= torchvision.model.resnet18(pretrained=True)

*trun_ model = nn.Sequential(list(model.children()[:1])

I get the first conv

continuing in this fashion

*trun_ model = nn.Sequential(list(model.children()[:5])

Sequential (
** (0): Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)**
** (1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True)**
** (2): ReLU (inplace)**
** (3): MaxPool2d (size=(3, 3), stride=(2, 2), padding=(1, 1), dilation=(1, 1))**
** (4): Sequential (**
** (0): BasicBlock (**
** (conv1): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)**
** (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True)**
** (relu): ReLU (inplace)**
** (conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)**
** (bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True)**
** )**
** (1): BasicBlock (**
** (conv1): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)**
** (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True)**
** (relu): ReLU (inplace)**
** (conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)**
** (bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True)**
** )**
** )**
)

However I would like model to truncate after only the First Conv in Basic Block 0

Any ideas ?

This (untested) code

import torch
import torchvision
from collections import OrderedDict

model = torchvision.models.resnet18(pretrained=False)
children = [_ for _ in model.named_children()]
layers = children[:4]
layers.append(('BasicBlock0.conv1', [v for k, v in children[5][1].named_modules(prefix='conv1')][2]))
new_model = torch.nn.Sequential(OrderedDict(layers))

will give you this network:

Sequential (
  (conv1): Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
  (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True)
  (relu): ReLU (inplace)
  (maxpool): MaxPool2d (size=(3, 3), stride=(2, 2), padding=(1, 1), dilation=(1, 1))
  (BasicBlock0.conv1): Conv2d(64, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
)
1 Like