Remove CNN Layers in Resnet18 for Transfer Learning

I am looking to do some experiments in transfer learning using Resnet18. Currently I replace the fully connected layer as follows :

fc_inputs = model.fc.in_features
model.fc = nn.Sequential(nn.Linear(fc_inputs, num_classes),)

I am trying to figure out how to remove conv5_x and connect the fully connected layer to the conv4_x layer. ( Or connect it to conv3_x )

I want to compare the results tapping into the network at different places.

If we import the ResNet model conv5_x will be layer4. We could do the following:

model = torchvision.models.resnet18(pretrained=True)
model.layer4 = nn.Identity()
model.fc = nn.Linear(256, num_classes)

And you could use on the model.fc = nn.Sequential(nn.Linear(), ...) if you wish to write several linear layers.

Hi Aladdin,
Thanks for your reply and and for the transfer learning it looks like it would work great. I will test it.

Is there a way to completely remove the conv4_x layer from the model?