RuntimeError: Sizes of tensors must match except in dimension 3. Got 21 and 22 (The offending index

训练yolo的时候报错,debug发现,在yolo.py文件夹里面,具体在这个位置:

x = m(x),观察哪一行的x进行的错误,找到报错模块。

本问题具体体现在步长为2的Conv,卷积后得到的尺寸,经过upsampling后无法回到原来的尺寸。

所以更改Concat就可以了,找到common.py

class Concat(nn.Module):
    # Concatenate a list of tensors along dimension
    def __init__(self, dimension=1):
        super(Concat, self).__init__()
        self.d = dimension

    def forward(self, x):
        # 确保x是一个列表
        assert isinstance(x, list), 'Concat expects a list of tensors'
        
        # 找出最大的空间维度
        max_h = max(tensor.size(-2) for tensor in x)
        max_w = max(tensor.size(-1) for tensor in x)

        # 调整每个张量的大小
        resized_tensors = []
        for tensor in x:
            if tensor.size(-2) != max_h or tensor.size(-1) != max_w:
                resized = F.interpolate(tensor, size=(max_h, max_w), mode='bilinear', align_corners=False)
                resized_tensors.append(resized)
            else:
                resized_tensors.append(tensor)

        # 连接调整大小后的张量
        return torch.cat(resized_tensors, self.d)

然后就ok了

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值