我正在进行对象检测,我有一个包含图像及其相应边界框(地面实况值(的数据集。
实际上,我已经建立了自己的特征提取器,它以图像为输入并输出特征图(基本上是一个编码器-解码器系统,解码器的最终输出与图像大小相同,并且有3个通道(。现在,我想把这个特征图作为FasterRCNN模型的输入,用于检测,而不是原始图像。我正在使用以下代码在FRCNN检测模块的顶部添加特征图(使用RTFNet生成特征图-此链接处的代码(
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)
没有错误,它工作良好
有人能告诉我哪里出了问题吗?提前感谢
这是因为只有图像输入才应该传递到模型中,而不是图像和地面实况目标。所以你可以做output = model(images)
,而不是做output = model(images, targets)
。
至于为什么错误消息说要给定3个位置参数,这是因为forward是用默认的self
关键字启动的,该关键字表示类实例。因此,除了self
之外,您只应该再给出1个参数,即输入图像。