修改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
相关产品推荐
相关产品推荐

