基于MK-MMD度量迁移学习的轴承故障诊断方法研究-CSDN博客
注意力机制
一、注意力模块
1.1、SEnet模块
nn.AdaptiveAvgPool2d(1)
Pytorch学习笔记(五):nn.AdaptiveAvgPool2d()函数详解_nn.adaptiveavgpool2d((1, 1))-CSDN博客
含义:
注意力机制在CNN中使用总结_注意力机制加在cnn的什么位置_AI浩的博客-CSDN博客
import torch
import torch.nn as nn
m = nn.AdaptiveAvgPool2d((5,1))
m1 = nn.AdaptiveAvgPool2d((None,5))
m2 = nn.AdaptiveAvgPool2d(1)
input = torch.randn(2, 64, 8, 9)
output = m(input)
output1 = m1(input)
output2 = m2(input)
print('nn.AdaptiveAvgPool2d((5,1)):',output.shape)
print('nn.AdaptiveAvgPool2d((None,5)):',output1.shape)
print('nn.AdaptiveAvgPool2d(1):',output2.shape)
class senet(nn. Module):
def _init__(self,channel,ratio = 16):
super(senet,self)._init__()
self.avg.pool= nn. AdaptiveAvgPool2d(1)
self.fc= nn.Sequential(
nn.Linear(channel,channel//ratio,False),
nn.ReLU(),
nn.Linear(channel//ratio,channel,False),
nn.Sigmoid(),
)
def forward(self,x):
b, c, h, w = x.size()
# b, c, h, w ->b, c,1,1
avg = self.avg_pool(x).view([b,c])
# b,c ->b,c l/ ratio ->b, c ->b,c,1,1
fc = self.fc(avg).view([b,c,1,1])
return x*fc
model = senet(512)
print(model)
inputs = torch.ones ([2,512,26,26])
outputs = model(inputs)
print(outputs.shape)
1.2、CBAM模块实现
CBAM 通道注意力 & 空间注意力机制_空间注意力机制和通道注意力机制-CSDN博客
通道和空间注意力机制的结合
import torch
import torch.nn as nn
import torch.nn.functional as F
class CBAM(nn.Module):
def __init__(self, channels, reduction):
super(CBAM, self).__init__()
self.avg_pool = nn.AdaptiveAvgPool2d(1)
self.max_pool = nn.AdaptiveMaxPool2d(1)
self.fc = nn.Sequential(
nn.Linear(channels, channels // reduction, bias=False),
nn.ReLU(inplace=True),
nn.Linear(channels // reduction, channels, bias=False),
nn.Sigmoid()
)
def forward(self, x):
b, c, _, _ = x.size()
y = self.avg_pool(x).view(b, c)
z = self.max_pool(x).view(b, c)
y = self.fc(y)
z = self.fc(z)
x = x * y * z.expand_as(x)
return x
应用:如何应用注意力机制
class MyModel(nn.Module):
def __init__(self):
super(MyModel, self).__init__()
self.conv1 = nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1)
self.cbam = CBAM(64, 16) # CBAM模块的输入通道数是64,reduction参数是16
self.conv2 = nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1)
def forward(self, x):
x = F.relu(self.conv1(x))
x = self.cbam(x) # 在卷积后插入CBAM模块
x = F.relu(self.conv2(x))
return x
class ChannelAttention(nn.Module):
def __init__(self, in_planes, ratio=8):
super(ChannelAttention, self).__init__()
self.avg_pool = nn.AdaptiveAvgPool2d(1)
self.max_pool = nn.AdaptiveMaxPool2d(1)
# 利用1x1卷积代替全连接
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)
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_block(nn.Module):
def __init__(self, channel, ratio=8, kernel_size=7):
super(cbam_block, self).__init__()
self.channelattention = ChannelAttention(channel, ratio=ratio)
self.spatialattention = SpatialAttention(kernel_size=kernel_size)
def forward(self, x):
x = x * self.channelattention(x)
x = x * self.spatialattention(x)
return x
由于在大部分情况下我们感兴趣的区域只是图像中的一小部分,因此空间注意力的本质就是定位目标并进行一些变换或者获取权重
1.3、ECAnet注意力机制实现
1.4、Triplet注意力机制
1.5、EMCA:高效的多尺度通道注意力模块
文献:EMCA: Efficient Multiscale Channel Attention Module,2022.
1.6、应用:
引用:EPSANet: 一种高效的多尺度通道注意力机制,主要提出了金字塔注意力模块,即插即用,效果显著,已开源! - 知乎 (zhihu.com)
深大、西交合作多尺度通道注意力机制EPSANet - 知乎 (zhihu.com)
参考如何嵌入神经网络
二、空间注意力机制
1.1、
三、通道注意力机制
1.1、文献1
基于VMD和改进DenseNet的滚动轴承故障诊断,2023.,中文核心。
当滚动轴承出现故障时,局部故障源和健康部位冲击产生振动,不同部位发生故障产生的振动信号不完全相同,为了增大不同故障的区分度,引入通道注意力机制。
加入通道注意力机制可以对不同特征赋予不同的权重,故障特征的权重更大,使得模型更加关注故障特征,从而增强了模型的诊断性能。模型如图2所示。
图片分类:支持向量机:
引用: