bug记录:Yolov5使用注意力机制CBAM报错untimeerror: adaptive_avg_pool2d_backward_cuda does not have a deterministi

用colab跑CBAM注意力机制的时候一直报错

runtimeerror: adaptive_avg_pool2d_backward_cuda does not have a deterministic implementation, but you set 'torch.use_deterministic_algorithms(true)'. you can turn off determinism just for this operation, or you can use the 'warn_only=true' option, if that's acceptable for your application. you can also file an issue at https://github.com/pytorch/pytorch/issues to help us prioritize adding deterministic support for this operation.

改了几天,我发现用pycharm就不报错,可能是池化层的反向传播出现了问题。所以我尝试把torch.use_deterministic_algorithms(False)加到train.py里面,结果就成功了。

加到train.py 的335行scaler.scale(loss).backward()前面

 

 

 

  • 36
    点赞
  • 60
    收藏
    觉得还不错? 一键收藏
  • 26
    评论
您好!对于在YOLOv8中添加注意力机制CBAM,可以按照以下步骤进行: 1. 导入必要的模块和库: ```python import torch import torch.nn as nn ``` 2. 定义CBAM模块: ```python class CBAM(nn.Module): def __init__(self, channels, reduction=16): super(CBAM, self).__init__() self.channels = channels self.avg_pool = nn.AdaptiveAvgPool2d(1) self.max_pool = nn.AdaptiveMaxPool2d(1) self.fc1 = nn.Conv2d(channels, channels // reduction, kernel_size=1, padding=0) self.relu = nn.ReLU(inplace=True) self.fc2 = nn.Conv2d(channels // reduction, channels, kernel_size=1, padding=0) self.sigmoid_channel = nn.Sigmoid() self.conv_after_concat = nn.Conv2d(2, 1, kernel_size=3, stride=1, padding=1) self.sigmoid_spatial = nn.Sigmoid() def forward(self, x): avg_out = self.fc2(self.relu(self.fc1(self.avg_pool(x)))) max_out = self.fc2(self.relu(self.fc1(self.max_pool(x)))) channel_out = self.sigmoid_channel(avg_out + max_out) spatial_out = torch.cat([avg_out, max_out], dim=1) spatial_out = self.conv_after_concat(spatial_out) spatial_out = self.sigmoid_spatial(spatial_out) return x * channel_out * spatial_out ``` 3. 在YOLOv8的特征提取部分使用CBAM模块: ```python class YOLOv8(nn.Module): def __init__(self): super(YOLOv8, self).__init__() # ... self.cbam = CBAM(channels=XX) # ... def forward(self, x): # ... x = self.cbam(x) # ... return x ``` 请注意,上述代码中的`XX`需要替换为适合您的网络的通道数。这是一个简单的示例,您还可以根据需要进行更改和调整。 希望能对您有所帮助!如果您有任何疑问,请随时提问。
评论 26
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值