Ultralytics中的RT-DETR模块的RepC3 bug

在跑U版本的RT-DETR,我修改了RepC3的e的默然参数,e 不等于1的时候,程序就会报错,在检查代码的时候,发现官方的代码有问题。

原来的代码:

python 复制代码
class RepC3(nn.Module):
    """Rep C3."""

    def __init__(self, c1, c2, n=3, e=1.0):
        """Initialize CSP Bottleneck with a single convolution using input channels, output channels, and number."""
        super().__init__()
        c_ = int(c2 * e)  # hidden channels
        self.cv1 = Conv(c1, c2, 1, 1)
        self.cv2 = Conv(c1, c2, 1, 1)
        self.m = nn.Sequential(*[RepConv(c_, c_) for _ in range(n)])
        self.cv3 = Conv(c_, c2, 1, 1) if c_ != c2 else nn.Identity()

    def forward(self, x):
        """Forward pass of RT-DETR neck layer."""
        return self.cv3(self.m(self.cv1(x)) + self.cv2(x))

在当前的代码实现中,self.cv1self.cv2的输出通道数应为隐藏层通道数c_而非最终输出通道数c2,否则会导致维度不匹配的问题。

问题分析:

  1. 维度不匹配: 当扩展系数 e ≠ 1 时,c_ = c2 * e 会改变通道数。若 self.cv1 的输出通道为 c2,而后续模块 self.m 中的 RepConv 层要求输入通道为 c_,将引发维度错误。

  2. 设计逻辑: RepC3 的结构预期是先将输入通道通过 cv1/cv2 调整到隐藏层 c_,经过处理后再通过 cv3 调整到目标输出 c2

修正后的代码:

python 复制代码
class RepC3(nn.Module):
    """Rep C3."""

    def __init__(self, c1, c2, n=3, e=1.0):
        super().__init__()
        c_ = int(c2 * e)  # 隐藏通道数
        self.cv1 = Conv(c1, c_, 1, 1)  # 修正为c_
        self.cv2 = Conv(c1, c_, 1, 1)  # 修正为c_
        self.m = nn.Sequential(*[RepConv(c_, c_) for _ in range(n)])
        self.cv3 = Conv(c_, c2, 1, 1) if c_ != c2 else nn.Identity()

    def forward(self, x):
        return self.cv3(self.m(self.cv1(x)) + self.cv2(x))

修改说明:

  • self.cv1self.cv2 的输出通道从 c2 改为 c_,确保与后续 RepConv 层的输入通道一致。

  • self.cv3 负责将隐藏通道 c_ 映射到目标输出通道 c2,保证最终输出尺寸正确。

此修正确保了在任意扩展系数 e 下,网络层的维度计算均正确,避免了潜在的错误。已将bug报告给了官方。

相关推荐
黎猫大侠17 小时前
一次Android Fragment内存泄露的bug解决记录|Fragment not attach to an Activity
android·bug
七七小报19 小时前
uniapp-商城-48-后台 分类数据添加修改弹窗bug
uni-app·bug
windwind20001 天前
发行基础:本地化BUG导致审核失败
游戏·青少年编程·编辑器·bug·创业创新·玩游戏
Htht1111 天前
【Qt】之【Bug】点击按钮(ui->pushButton)触发非本类设置的槽函数
qt·ui·bug
gxn_mmf2 天前
典籍知识问答模块AI问答功能feedbackBug修改+添加对话名称修改功能
前端·后端·bug
marvindev2 天前
提bug测试专用
开发语言·javascript·bug
虎头金猫2 天前
如何解决 403 错误:请求被拒绝,无法连接到服务器
运维·服务器·python·ubuntu·chatgpt·centos·bug
L_593 天前
火影bug,未保证短时间数据一致性,拿这个例子讲一下Redis
redis·bug·springcloud
远瞻。3 天前
【bug】fused_bias_act_kernel.cu卡住没反应
bug
编程武士3 天前
python 闭包获取循环数据经典 bug
开发语言·python·bug