How to remove certain layers from Fastern-RCNN in Pytorch?
Target: I want to use the pretrained Faster-RCNN model to extract features from image.

What I have tried: I use below code to build the model:

import torchvision.models as models
from PIL import Image
import torchvision.transforms as T
import torch

# download the pretrained fasterrcnn model
model = models.detection.fasterrcnn_resnet50_fpn(pretrained=True)
model.eval()
model.cuda()

# remove [2:] layers
modules = list(model.children())[:2]
model_t=torch.nn.Sequential(*modules)

# load image and extract features
img = Image.open('data/person.jpg')
transform = T.Compose([T.ToTensor()])
img_t = transform(img)
batch_t = torch.unsqueeze(img_t, 0).cuda()
ft = model_t(batch_t)


Error: But I got the following error:TypeError: conv2d(): argument 'input' (position 1) must be Tensor, not tuple

Please help! Thank you!

print(model.modules) to get the layer names. Then delete a layer with:
del model.my_layer_name

