PyTorch FastErrorCNN TypeError:forward()接受2个位置参数,但给出了3个

2024-03-28 19:38:02 发布

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

我正在研究对象检测,我有一个包含图像及其相应边界框(地面真值)的数据集

实际上,我已经建立了自己的特征提取器,它将图像作为输入并输出特征映射(基本上是一个编码器-解码器系统,其中解码器的最终输出与图像大小相同,并且有3个通道)。现在,我想把这个特征图作为输入输入输入到FasterRCNN模型中进行检测,而不是原始图像。我正在使用以下代码在FRCNN检测模块的顶部添加特征映射(使用RTFNet生成特征映射-此link处的代码)

frcnn_model = fasterrcnn_resnet50_fpn(pretrained=True)
in_features = frcnn_model.roi_heads.box_predictor.cls_score.in_features
frcnn_model.roi_heads.box_predictor = FastRCNNPredictor(in_features, num_classes)
fpn_block = frcnn_model.backbone.fpn
rpn_block = frcnn_model.rpn

backbone = RTFNet(num_classes) RTFNet is a feature extractor taking as input, an image having 4 channels(fused RGB and thermal image) , 
model = nn.Sequential(backbone, nn.ReLU(inplace=True))
model = nn.Sequential(model,fpn_block)
model = nn.Sequential(model,rpn_block)
model = nn.Sequential(model,FastRCNNPredictor(in_features, num_classes))

我只是想通过使用下面生成随机图像和边界框的代码来测试它是否正常工作

images, boxes = torch.rand(1, 4, 512, 640), torch.rand(4, 11, 4)
labels = torch.randint(1, num_classes, (4, 11))
images = list(image for image in images)
targets = []
for i in range(len(images)):
  d = {}
  d['boxes'] = boxes[i]
  d['labels'] = labels[i]
  targets.append(d)
output = model(images, targets)

运行此命令会导致以下错误

TypeError                                 Traceback (most recent call last)
<ipython-input-22-2637b8c27ad2> in <module>()
----> 1 output = model(images, targets)

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    530             result = self._slow_forward(*input, **kwargs)
    531         else:
--> 532             result = self.forward(*input, **kwargs)
    533         for hook in self._forward_hooks.values():
    534             hook_result = hook(self, input, result)

TypeError: forward() takes 2 positional arguments but 3 were given

但是,当我用一个普通的FasterRCNN模型替换我的模型时

model = fasterrcnn_resnet50_fpn(pretrained=True)
in_features = model.roi_heads.box_predictor.cls_score.in_features
model.roi_heads.box_predictor = FastRCNNPredictor(in_features, num_classes)

没有错误,工作正常

有人能告诉我哪里出了问题吗?提前谢谢


Tags: in图像selfinputmodelnn特征num
1条回答
网友
1楼 · 发布于 2024-03-28 19:38:02

这是因为只有图像输入应该传递到模型中,而不是图像和地面真实目标。因此,您可以执行output = model(images),而不是执行output = model(images, targets)

至于为什么错误消息说要给3个位置参数,这是因为forward是用一个默认的self关键字启动的,它表示类实例。因此,除了self之外,您只需再提供1个参数,即输入图像

相关问题 更多 >