图像解析:
从整体上可以看出,GAM和CBAM注意力机制还是比较相似的,同样是使用了通道注意力机制和空间注意力机制。但是不同的是对通道注意力和空间注意力的处理。
了解了CBAM,我们来看GAM是怎么处理CAM 和SAM的,同样是先通道后空间。
这里给出GAM的pytorch实现代码:代码可能跟官方有些差异,是看图复现的
"""
GAM 注意力机制:对CBAM注意力进行改进
先通道注意力,再空间注意力
"""
import torch
import torch.nn as nn
# 通道注意力
class Channel_Attention(nn.Module):
def __init__(self, in_channel, out_channel, ratio=4):
super(Channel_Attention, self).__init__()
self.fc1 = nn.Linear(in_channel, in_channel // ratio)
self.relu = nn.ReLU()
self.fc2 = nn.Linear(in_channel // ratio, in_channel)
self.sig = nn.Sigmoid()
def forward(self, x):
# b, c, h, w = x.size()
input = x.permute(0, 3, 2, 1)
output = self.fc2(self.relu(self.fc1(input)))
output = output.permute(0, 3, 2, 1)
return output * x
# 空间注意力
class Spatial(nn.Module):
def __init__(self, in_channel, out_channel, ratio, kernel_size=7):
super(Spatial, self).__init__()
padding = kernel_size // 2
self.conv1 = nn.Conv2d(
in_channel, in_channel // ratio, kernel_size=7, padding=padding
)
self.bn = nn.BatchNorm2d(in_channel // ratio)
self.act = nn.ReLU()
self.conv2 = nn.Conv2d(
in_channel // ratio, in_channel, kernel_size=kernel_size, padding=padding
)
self.bn1 = nn.BatchNorm2d(in_channel)
self.sig = nn.Sigmoid()
def forward(self, x):
conv1 = self.act(self.bn(self.conv1(x)))
conv2 = self.bn1(self.conv2(conv1))
output = self.sig(conv2)
return x * output
class GAM(nn.Module):
def __init__(self,in_channel, out_channel, ratio = 4, kernel_size = 7):
super(GAM, self).__init__()
self.channel_attention = Channel_Attention(in_channel,out_channel,ratio)
self.spatial_attention = Spatial(in_channel,out_channel,ratio,kernel_size)
def forward(self, x):
input = self.channel_attention(x)
output= self.spatial_attention(input)
return output
input = torch.randn(1, 4, 24, 24).cuda()
model = GAM(4, 4).cuda()
output = model(input)
print(output)
print(output.size())
# 20220928
YOLOv5使用GAM