【问题标题】:How to remove certain layers from Fastern-RCNN in Pytorch?如何从 Pytorch 中的 Faster-R CNN 中删除某些层?
【发布时间】:2020-05-06 01:11:12
【问题描述】:

目标:我想使用预训练的 Faster-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

请帮忙!谢谢!

【问题讨论】:

  • 试试 model.modules() 而不是 model.children()

标签: python deep-learning pytorch object-detection


【解决方案1】:

print(model.modules) 获取图层名称。然后删除一个图层:

del model.my_layer_name

【讨论】:

  • 这会在forward() 定义/使用已删除的模块时产生问题。
猜你喜欢
  • 2020-08-19
  • 2018-01-03
  • 2018-05-25
  • 1970-01-01
  • 1970-01-01
  • 2022-08-05
  • 2020-02-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多