注意力机制大锅饭
2022/1/4 23:15:50
本文主要是介绍注意力机制大锅饭,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
最近做yolo添加注意力机制,将找到的关于注意力机制的资料集合到一起。会给出使用原文的链接,感谢各位乐于分享的博主,侵删!
CBAM
import torch from torch import nn class ChannelAttention(nn.Module): def __init__(self, in_planes, ratio=16): super(ChannelAttention, self).__init__() self.avg_pool = nn.AdaptiveAvgPool2d(1) self.max_pool = nn.AdaptiveMaxPool2d(1) self.fc1 = nn.Conv2d(in_planes, in_planes // ratio, 1, bias=False) self.relu1 = nn.ReLU() self.fc2 = nn.Conv2d(in_planes // ratio, in_planes, 1, bias=False) self.sigmoid = nn.Sigmoid() def forward(self, x): avg_out = self.fc2(self.relu1(self.fc1(self.avg_pool(x)))) max_out = self.fc2(self.relu1(self.fc1(self.max_pool(x)))) out = avg_out + max_out return self.sigmoid(out) class SpatialAttention(nn.Module): def __init__(self, kernel_size=7): super(SpatialAttention, self).__init__() assert kernel_size in (3, 7), 'kernel size must be 3 or 7' padding = 3 if kernel_size == 7 else 1 self.conv1 = nn.Conv2d(2, 1, kernel_size, padding=padding, bias=False) # 7,3 3,1 self.sigmoid = nn.Sigmoid() def forward(self, x): avg_out = torch.mean(x, dim=1, keepdim=True) max_out, _ = torch.max(x, dim=1, keepdim=True) x = torch.cat([avg_out, max_out], dim=1) x = self.conv1(x) return self.sigmoid(x) class CBAM(nn.Module): def __init__(self, in_planes, ratio=16, kernel_size=7): super(CBAM, self).__init__() self.ca = ChannelAttention(in_planes, ratio) self.sa = SpatialAttention(kernel_size) def forward(self, x): out = x * self.ca(x) result = out * self.sa(out) return result ———————————————— 版权声明:本文为CSDN博主「敲代码的小风」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/m0_46653437/article/details/114829275
原博主还介绍了通道、空间、SE注意力。值得一看。
SE
再写一个SE主要是这位博主给出了添加位置和条件,跟一般增加模块的位置不一样,为了直接cv。
for i, (f, n, m, args) in enumerate(d['backbone'] + d['head']): m = eval(m) if isinstance(m, str) else m # eval strings for j, a in enumerate(args): try: args[j] = eval(a) if isinstance(a, str) else a # eval strings except: pass n = max(round(n * gd), 1) if n > 1 else n # depth gain if m in [Conv, GhostConv, Bottleneck, GhostBottleneck, SPP, DWConv, MixConv2d, Focus, CrossConv, BottleneckCSP, C3]: c1, c2 = ch[f], args[0] if c2 != no: # if not output c2 = make_divisible(c2 * gw, 8) args = [c1, c2, *args[1:]] if m in [BottleneckCSP, C3]: args.insert(2, n) # number of repeats n = 1 elif m is nn.BatchNorm2d: args = [ch[f]] elif m is Concat: c2 = sum([ch[x] for x in f]) elif m is Detect: args.append([ch[x] for x in f]) if isinstance(args[1], int): # number of anchors args[1] = [list(range(args[1] * 2))] * len(f) elif m is Contract: c2 = ch[f] * args[0] ** 2 elif m is Expand: c2 = ch[f] // args[0] ** 2 #SE添加位置 elif m is SELayer: # 这里是修改的部分 channel, re = args[0], args[1] channel = make_divisible(channel * gw, 8) if channel != no else channel args = [channel, re] else: c2 = ch[f]
版权声明:本文为CSDN博主「pprp」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/DD_PP_JJ/article/details/114098169
CoorAttention
原博主介绍了很多注意力机制,还有可以直接cv的,这里只放不重叠的CoorAttention。我也直接cv了。
# 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
版权声明:本文为CSDN博主「调参者」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/zqt321/article/details/121772854
这篇关于注意力机制大锅饭的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23Springboot应用的多环境打包入门
- 2024-11-23Springboot应用的生产发布入门教程
- 2024-11-23Python编程入门指南
- 2024-11-23Java创业入门:从零开始的编程之旅
- 2024-11-23Java创业入门:新手必读的Java编程与创业指南
- 2024-11-23Java对接阿里云智能语音服务入门详解
- 2024-11-23Java对接阿里云智能语音服务入门教程
- 2024-11-23JAVA对接阿里云智能语音服务入门教程
- 2024-11-23Java副业入门:初学者的简单教程
- 2024-11-23JAVA副业入门:初学者的实战指南