如何从Pytorch中的FasternRCNN中删除某些层?

2024-06-02 06:36:35 发布

您现在位置:Python中文网/ 问答频道 /正文

目标:我想使用预训练的更快的RCNN模型从图像中提取特征

我尝试的内容:我使用以下代码构建模型:

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)

错误:但我得到了以下错误:TypeError: conv2d(): argument 'input' (position 1) must be Tensor, not tuple

请帮忙!谢谢大家!


Tags: 模型imageimportmodulesimgmodelmodelsas