我在centernet模型里加了注意力机制,分别加了好几种,但所有的结果变化不到1%,请问这是正常现象吗,还是添加的注意力没有起到效果?
我在主干网络resnet50中添加了4次注意力机制
class ResNet(nn.Module):
def __init__(self, block, layers, num_classes=1000,height=256,width=128,spa_on=True,
cha_on=True,s_ratio=8,c_ratio=8,d_ratio=8):
self.inplanes = 64
super(ResNet, self).__init__()
# 512,512,3 -> 256,256,64
self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3,bias=False)
self.bn1 = nn.BatchNorm2d(64)
self.relu = nn.ReLU(inplace=True)
# 256x256x64 -> 128x128x64
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=0, ceil_mode=True) # change
# 128x128x64 -> 128x128x256
self.layer1 = self._make_layer(block, 64, layers[0])
# 128x128x256 -> 64x64x512
self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
# 64x64x512 -> 32x32x1024
self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
# 32x32x1024 -> 16x16x2048
self.layer4 = self._make_layer(block, 512, layers[3],stride=2)
# 通道和空间注意力
self.ca1 = ChannelAttention(self.inplanes)
self.sa1 = SpatialAttention()
self.avgpool = nn.AvgPool2d(7)
self.fc = nn.Linear(512 * block.expansion, num_classes)
# 添加一个SE注意力方法#
# self.se256 = SEAttention(channel=256)
# self.se512 = SEAttention(channel=512)
# self.se1024 = SEAttention(channel=1024)
#COORD attention注意力
# self.ca256 = CA_Block(channel=256)
# self.ca512 = CA_Block(channel=512)
# self.ca1024 = CA_Block(channel=1024)
#CBAM attention注意力
# self.cbam256 = CA_Block(channel=256)
# self.cbam512 = CA_Block(channel=512)
# self.cbam1024 = CA_Block(channel=1024)
#ECA attention注意力
# self.ECA256 = ECAAttention(channel=256)
# self.ECA512 = ECAAttention(channel=512)
# self.ECA1024 =ECAAttention(channel=1024)
#rga注意力
self.rga1 = RGA_Module(256, (height // 4) * (width // 4), use_spatial=spa_on, use_channel=cha_on,
cha_ratio=c_ratio, spa_ratio=s_ratio, down_ratio=d_ratio)
self.rga2 = RGA_Module(512, (height // 8) * (width // 8), use_spatial=spa_on, use_channel=cha_on,
cha_ratio=c_ratio, spa_ratio=s_ratio, down_ratio=d_ratio)
self.rga3 = RGA_Module(1024, (height // 16) * (width // 16), use_spatial=spa_on, use_channel=cha_on,
cha_ratio=c_ratio, spa_ratio=s_ratio, down_ratio=d_ratio)
self.rga4 = RGA_Module(2048, (height // 16) * (width // 16), use_spatial=spa_on, use_channel=cha_on,
cha_ratio=c_ratio, spa_ratio=s_ratio, down_ratio=d_ratio)
# 权重初始化
for m in self.modules():
if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.data.normal_(0, math.sqrt(2. / n))
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
def _make_layer(self, block, planes, blocks,stride=1):
downsample = None
if stride != 1 or self.inplanes != planes * block.expansion:
downsample = nn.Sequential(
nn.Conv2d(self.inplanes, planes * block.expansion,
kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(planes * block.expansion),
)
layers = []
layers.append(block(self.inplanes, planes, stride, downsample))
self.inplanes = planes * block.expansion
for i in range(1, blocks):
layers.append(block(self.inplanes, planes))
return nn.Sequential(*layers)
**注意力添加在这里 一共四次 以rga为例
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.maxpool(x)
x = self.layer1(x) # 128x128x64 -> 128x128x256
x = self.rga1(x)
x = self.layer2(x) # 128x128x256 -> 64x64x512
x = self.rga2(x)
x = self.layer3(x) # 64x64x512 -> 32x32x1024
x = self.rga3(x)
x = self.layer4(x)
x = self.rga4(x)