昨天学习了CBAM模块和ACNet。
今天就想试一下CBAM模块的效果,所以编写了代码在MNIST数据集上做分类任务,但是看不出什么差别,而且没加CBAM模块的效果反而好一些。
我觉得原因可能是因为数据集太小了没法做到这一点,改天在VOC数据集上试一试效果看看如何。
今天先把实验结果报道一下,学习率,epoch次数和batch_size的大小完全保持不变
先上Pytorch代码
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.utils.model_zoo as model_zoo
import math
import torchvision
import torch.optim as optim # Pytoch常用的优化方法都封装在torch.optim里面
import matplotlib.pyplot as plt
from torch.utils.data import DataLoader
model_urls = {
'resnet18': 'https://download.pytorch.org/models/resnet18-5c106cde.pth',
}
# 定义卷积核3*3,padding=1,stride=1的卷积,这个卷积的特点是不改变特征图的size,但可能改变channel
def conv3x3(in_channels,out_channels,stride=1):
return nn.Conv2d(in_channels=in_channels,out_channels=out_channels,kernel_size=3,stride=stride,padding=1,bias=False)
# 定义CBAM中的channel attention模块
class ChannelAttention(nn.Module):
# 需要传入输入通道数和压缩倍数reduction,默认是16
def __init__(self,in_channels,reduction = 16):
super(ChannelAttention,self).__init__()
#定义需要的全局平均池化和全局最大池化,传入输出通道数output_size = 1
self.avg = nn.AdaptiveAvgPool2d(output_size=1)
self.max = nn.AdaptiveMaxPool2d(output_size=1)
## 定义共享感知机MLP
self.fc1 = nn.Conv2d(in_channels=in_channels,out_channels=in_channels//reduction,kernel_size=1,bias=False)
self.relu1 = nn.ReLU()
self.fc2 = nn.Conv2d(in_channels=in_channels//reduction,out_channels=in_channels,kernel_size=1,bias=False)
## 定义线性激活函数
self.sigmod = nn.Sigmoid()
def forward(self,x):
avg_out = self.fc2(self.relu1(self.fc1(self.avg(x))))
max_out = self.fc2(self.relu1(self.fc1(self.max(x))))
out = self.sigmod(avg_out + max_out)
return out
## 定义spatial attention模块
class SpatialAttention(nn.Module):
# 空间注意力模块有一个卷积层,需要传入kernel_size
def __init__(self,kernel_size = 7):
super(SpatialAttention,self).__init__()
padding = 3 if kernel_size == 7 else 1
self.conv1 = nn.Conv2d(in_channels = 2,out_channels = 1,kernel_size=kernel_size,padding = padding,bias=False)
self.sigmod = nn.Sigmoid()
def forward(self,x):
avg_out = torch.mean(x, dim = 1, keepdim = True)
max_out, _ = torch.max(x, dim =

最低0.47元/天 解锁文章
3447





