You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

修改YOLOv5替换Focus层为ENet后遇张量类型不匹配RuntimeError求助

YOLOv5替换Focus层为ENet后的RuntimeError问题

问题详情

修改YOLOv5网络结构,将原有Focus层替换为ENet网络后,运行时触发如下错误:

RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.cuda.HalfTensor) should be the same

待解决疑问

  • 不清楚错误产生的根源,网上查询到的解决方案是将输入数据转换为对应设备类型,示例代码如下:

    device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
    inputs = inputs.to(device)
    

    但项目文件数量较多,不知道该将这段代码添加到何处。

  • 错误起始于Bottleneck类forward方法中main=x的赋值操作,想了解Python中这种赋值操作是否属于不允许的行为?相关Bottleneck类代码如下:

    class Bottleneck(nn.Module):
        def __init__(self, channels, internal_ratio=4, kernel_size=3, padding=0, dilation=1,
                     asymmetric=False, dropout_prob=0, bias=False, relu=True):
            super(Bottleneck, self).__init__()
            internal_channels=channels//internal_ratio
    
            self.ext_conv1=nn.Sequential(
                    nn.Conv2d(channels,internal_channels,kernel_size=1,stride=1,bias=bias),
                    nn.BatchNorm2d(internal_channels),
                    activation())
            self.ext_conv2=nn.Sequential(
                        nn.Conv2d(internal_channels, internal_channels, kernel_size=kernel_size,
                                stride=1, padding=padding, dilation=dilation, bias=bias),
                        nn.BatchNorm2d(internal_channels),
                        activation())
            self.ext_conv3=nn.Sequential(
                    nn.Conv2d(internal_channels,channels,kernel_size=1,stride=1,bias=bias),
                    nn.BatchNorm2d(channels),
                    activation())
            self.ext_regul=nn.Dropout2d(p=dropout_prob)
            self.out_activation=activation()
                    
        def forward(self,x):
            main=x
            #print(type(x))
            #print("==========")
            ext=self.ext_conv1(x)
            ext=self.ext_conv2(ext)
            ext=self.ext_conv3(ext)
            ext=self.ext_regul(ext)
            out=main+ext
            return self.out_activation(out)
    

内容的提问来源于stack exchange,提问作者qianqiu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 22:57:43