1、找到models/common.py 添加新增的网络结构,代码如下:
import torch
import torch.nn as nn
import math
import torch.nn.functional as F
class h_sigmoid(nn.Module):
def __init__(self, inplace=True):
super(h_sigmoid, self).__init__()
self.relu = nn.ReLU6(inplace=inplace)
def forward(self, x):
return self.relu(x + 3) / 6
class h_swish(nn.Module):
def __init__(self, inplace=True):
super(h_swish, self).__init__()
self.sigmoid = h_sigmoid(inplace=inplace)
def forward(self, x):
return x * self.sigmoid(x)
class CoordAtt(nn.Module):
def __init__(self, inp, oup, reduction=32):
super(CoordAtt, self).__init__()
self.pool_h = nn.AdaptiveAvgPool2d((None, 1))
self.pool_w = nn.AdaptiveAvgPool2d((1, None))
mip = max(8, inp // reduction)
self.conv1 = nn.Conv2d(inp, mip, kernel_size=1, stride=1, padding=0)
self.bn1 = nn.BatchNorm2d(mip)
self.act = h_swish()
self.conv_h = nn.Conv2d(mip, oup, kernel_size=1, stride=1, padding=0)
self.conv_w = nn.Conv2d(mip, oup, kernel_size=1, stride=1, padding=0)
def forward(self, x):
identity = x
n,c,h,w = x.size()
x_h = self.pool_h(x)
x_w = self.pool_w(x).permute(0, 1, 3, 2)
y = torch.cat([x_h, x_w], dim=2)
y = self.conv1(y)
y = self.bn1(y)
y = self.act(y)
x_h, x_w = torch.split(y, [h, w], dim=2)
x_w = x_w.permute(0, 1, 3, 2)
a_h = self.conv_h(x_h).sigmoid()
a_w = self.conv_w(x_w).sigmoid()
out = identity * a_w * a_h
return out
代码链接:https://github.com/houqb/CoordAttention/blob/main/coordatt.py
2、在yolo.py文件里的parse_model函数加入类名:
首先找到yolo.py里面parse_model函数的这一部分,在此处添加红框里面的内容
3、创建一个新的yaml文件
首先复制一个yolov5s.yaml,改名为yolov5s_ca.yaml
可以在backbone部分添加CA注意力机制,例如:
不要忘记修改nc,nc是类别数量
在此处添加CA模块后,网络结构发生了改变,原本的第9层变成了第10层,第10层变成了第11层,以此类推,所以后面的连接层,从第9层开始都要加1,例如:
4、验证是否添加成功,运行yolo.py文件,将cfg的参数改为刚刚创建的yolov5s_ca.yaml
python models\yolo.py --cfg yolov5s_ca.yaml
结果如下,表示添加CA注意力机制成功
5、运行train.py文件开始训练
6、其它问题
如果出现错误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.
定位错误位置,在该函数前添加
torch.use_deterministic_algorithms(False)
如图位置