import torch
import torch.nn as nn
import torch.nn.functional as F
from models.layer import MultiSpectralAttentionLayer
from models.SEAM import SEAM
# SENet在2017年提出
class SENet(nn.Module):
def __init__(self, channel, reduction=16):
super(SENet, self).__init__()
self.avg_pool = nn.AdaptiveAvgPool2d(1)
self.fc = nn.Sequential(
nn.Linear(channel, channel//reduction, bias=False),
nn.ReLU(inplace=True),
nn.Linear(channel // reduction, channel, bias=True),
nn.Sigmoid())
def forward(self, x):
b, c, _, _ = x.size()
y = self.avg_pool(x).view(b, c)
y = self.fc(y).view(b, c, 1, 1)
return x*y.expand_as(x)
class BasicConv(nn.Module):
def __init__(self, in_planes, out_planes, kernel_size, stride=1, padding=0, dilation=1, groups=1, relu=True, bn=True, bias=False):
super(BasicConv, self).__init__()
self.out_channels = out_planes
self.conv = nn.Conv2d(in_planes, out_planes, kernel_size=kernel_size, stride=stride, padding=padding, dilation=dilation, groups=groups, bias=bias)
self.bn = nn.BatchNorm2d(out_planes, eps=1e-5, momentum=0.01, affine=True)if bn else None
self.relu = nn.ReLU()if relu else None
def forward(self, x):
x = self.conv(x)
if self.bn is not None:
x = self.bn(x)
if self.relu is not None:
x = self.relu(x)
return x
class Flatten(nn.Module):
def forward(self, x):
return x.view(x.size(0), -1)
def logsumexp_2d(tensor):
tensor_flatten = tensor.view(tensor.size(0), tensor.size(1), -1)
s, _ = torch.max(tensor_flatten, dim=2, keepdim=True)
outputs = s + (tensor_flatten - s).exp().sum(dim=2, keepdim=True).log()
return outputs
class ChannelGate(nn.Module):
def __init__(self, gate_channels, reduction_ratio=16, pool_types=['avg','max']):
super(ChannelGate, self).__init__()
self.pool_types = pool_types
self.gate_channels = gate_channels
self.mlp = nn.Sequential(
Flatten(),
nn.Linear(gate_channels, gate_channels//reduction_ratio),
nn.ReLU(),
nn.Linear(gate_channels//reduction_ratio, gate_channels)
)
def forward(self, x):
channel_att_sum = None
for pool_type in self.pool_types:
if pool_type == 'avg':
avg_pool = F.avg_pool2d(x, (x.size(2), x.size(3)), stride=(x.size(2), x.size(3)))
channel_att_raw = self.mlp(avg_pool)
elif pool_type == 'max':
max_pool = F.max_pool2d(x, (x.size(2), x.size(3)), stride=(x.size(2), x.size(3)))
channel_att_raw = self.mlp(max_pool)
elif pool_type == 'lp':
lp_pool = F.lp_pool2d(x, 2, (x.size(2), x.size(3)), stride=(x.size(2), x.size(3)))
channel_att_raw = self.mlp(lp_pool)
elif pool_type == 'lse':
lse_pool = logsumexp_2d(x)
channel_att_raw = self.mlp(lse_pool)
if channel_att_sum is None:
channel_att_sum = channel_att_raw
else:
channel_att_sum = channel_att_sum + channel_att_raw
scale = F.sigmoid(channel_att_sum).unsqueeze(2).unsqueeze(3).expand_as(x)
return x * scale
class ChannelPool(nn.Module):
def forward(self, x):
return torch.cat((torch.max(x, 1)[0].unsqueeze(1), torch.mean(x, 1).unsqueeze(1)), dim=1)
class SpatialGate(nn.Module):
def __init__(self):
super(SpatialGate, self).__init__()
kernel_size = 7
self.compress = ChannelPool()
self.spatial = BasicConv(2, 1, kernel_size, stride=1, padding=(kernel_size-1) // 2, relu=False)
def forward(self, x):
x_compress = self.compress(x)
x_out = self.spatial(x_compress)
scale = F.sigmoid(x_out)
return x * scale
# CABM2018年提出
class CBAM(nn.Module):
def __init__(self, gate_channels, reduction_ratio=16, pool_types=['avg', 'max'], no_spatial=True):
super(CBAM, self).__init__()
self.ChannelGate = ChannelGate(gate_channels, reduction_ratio, pool_types)
self.no_spatial = no_spatial
if not no_spatial:
self.SpatialGate = SpatialGate()
def forward(self, x):
x_out = self.ChannelGate(x)
if not self.no_spatial:
x_out = self.SpatialGate(x_out)
return x_out
# ECANet2020年提出
class ECANet(nn.Module):
def __init__(self, k_size):
super(ECANet, self).__init__()
self.avg_pool = nn.AdaptiveAvgPool2d(1)
self.conv = nn.Conv1d(1, 1, kernel_size=k_size, padding=0, bias=False)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
y = self.avg_pool(x)
y = self.conv(y.squeeze(-1).transpose(-1, -2)).unsqueeze(-1)
y = self.sigmoid(y)
return x*y.expand_as(x)
# FCANet2021年提出
class FCANet(nn.Module):
def __init__(self, channel, dct_h, dct_w, reduction=16, freq_sel_method='top16'):
super(FCANet, self).__init__()
self.att = MultiSpectralAttentionLayer(channel, dct_h=dct_h, dct_w=dct_w, reduction=reduction, freq_sel_method=freq_sel_method)
def forward(self, x):
x1 = self.att(x)
return x1
# 线性变换激活函数
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 CA(nn.Module):
def __init__(self, in_channel, out_channel, reduction=32):
super(CA, self).__init__()
self.h_pool = nn.AdaptiveAvgPool2d((None, 1))
self.w_pool = nn.AdaptiveAvgPool2d((1, None))
mip = max(8, in_channel//reduction)
self.conv1 = nn.Conv2d(in_channel, mip, kernel_size=1, stride=1, padding=0)
self.bn1 = nn.BatchNorm2d(mip)
self.act = h_swish()
self.h_conv = nn.Conv2d(mip, out_channel, kernel_size=1, stride=1, padding=0)
self.w_conv = nn.Conv2d(mip, out_channel, kernel_size=1, stride=1, padding=0)
def forward(self, x):
identity = x
n, c, h, w = x.size()
x_h = self.h_pool(x)
x_w = self.w_pool(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.h_conv(x_h).sigmoid()
a_w = self.w_conv(x_w).sigmoid()
out = identity * a_w * a_h
return out
class EMA(nn.Module):
def __init__(self, channels, factor=4):
super(EMA, self).__init__()
self.groups = factor
assert channels//self.groups > 0
self.softmax = nn.Softmax(-1)
self.avg_pool = nn.AdaptiveAvgPool2d((1, 1))
self.h_pool = nn.AdaptiveAvgPool2d((None, 1))
self.w_pool = nn.AdaptiveAvgPool2d((1, None))
self.gn = nn.GroupNorm(channels//self.groups, channels//self.groups)
self.conv1x1 = nn.Conv2d(channels//self.groups, channels//self.groups, kernel_size=1, stride=1, padding=0)
self.conv3x3 = nn.Conv2d(channels//self.groups, channels//self.groups, kernel_size=3, stride=1, padding=1)
def forward(self, x):
b, c, h, w = x.size()
group_x = x.reshape(b*self.groups, -1, h, w)
x_h = self.h_pool(group_x)
x_w = self.w_pool(group_x).permute(0, 1, 3, 2)
hw = self.conv1x1(torch.cat([x_h, x_w], dim=2))
x_h, x_w = torch.split(hw, [h, w], dim=2)
x1 = self.gn(group_x*x_h.sigmoid()*x_w.permute(0, 1, 3, 2).sigmoid())
print(x1.size())
x2 = self.conv3x3(group_x)
print(x2.size())
x11 = self.softmax(self.avg_pool(x1).reshape(b*self.groups, -1, 1).permute(0, 2, 1))
print(x11.size())
x12 = x2.reshape(b*self.groups, c//self.groups, -1)
print(x12.size())
x21 = self.softmax(self.avg_pool(x2).reshape(b*self.groups, -1, 1).permute(0, 2, 1))
print(x21.size())
x22 = x1.reshape(b*self.groups, c//self.groups, -1)
print(x22.size())
weights = torch.matmul(x11, x12) + torch.matmul(x21, x22).reshape(b*self.groups, 1, h, w)
print(weights.size())
return (group_x*weights.sigmoid()).reshape(b, c, h, w)
class seam(nn.Module):
def __init__(self, in_channel, out_channel, reduction=16):
super(seam, self).__init__()
self.seam = SEAM(in_channel, out_channel, 1, reduction=reduction)
def forward(self, x):
x1 = self.seam(x)
return x1
class Channel_Gate(nn.Module):
def __init__(self, gate_channel, reduction_ratio=16, num_layers=1):
super(Channel_Gate, self).__init__()
self.gate_c = nn.Sequential()
self.gate_c.add_module('flatten', Flatten() )
gate_channels = [gate_channel]
gate_channels += [gate_channel // reduction_ratio] * num_layers
gate_channels += [gate_channel]
for i in range( len(gate_channels) - 2 ):
self.gate_c.add_module('gate_c_fc_%d' % i, nn.Linear(gate_channels[i], gate_channels[i+1]))
self.gate_c.add_module('gate_c_bn_%d' % (i+1), nn.BatchNorm1d(gate_channels[i+1]))
self.gate_c.add_module('gate_c_relu_%d' % (i+1), nn.ReLU() )
self.gate_c.add_module('gate_c_fc_final', nn.Linear(gate_channels[-2], gate_channels[-1]))
def forward(self, in_tensor):
avg_pool = F.avg_pool2d(in_tensor, in_tensor.size(2), stride=in_tensor.size(2) )
return self.gate_c(avg_pool).unsqueeze(2).unsqueeze(3).expand_as(in_tensor)
class Spatial_Gate(nn.Module):
def __init__(self, gate_channel, reduction_ratio=16, dilation_conv_num=2, dilation_val=4):
super(Spatial_Gate, self).__init__()
self.gate_s = nn.Sequential()
self.gate_s.add_module('gate_s_conv_reduce0', nn.Conv2d(gate_channel, gate_channel//reduction_ratio, kernel_size=1))
self.gate_s.add_module('gate_s_bn_reduce0', nn.BatchNorm2d(gate_channel//reduction_ratio))
self.gate_s.add_module('gate_s_relu_reduce0', nn.ReLU())
for i in range( dilation_conv_num ):
self.gate_s.add_module('gate_s_conv_di_%d' % i, nn.Conv2d(gate_channel//reduction_ratio, gate_channel//reduction_ratio, kernel_size=3, padding=dilation_val, dilation=dilation_val) )
self.gate_s.add_module('gate_s_bn_di_%d' % i, nn.BatchNorm2d(gate_channel//reduction_ratio))
self.gate_s.add_module('gate_s_relu_di_%d' % i, nn.ReLU())
self.gate_s.add_module('gate_s_conv_final', nn.Conv2d(gate_channel//reduction_ratio, 1, kernel_size=1))
def forward(self, in_tensor):
return self.gate_s(in_tensor).expand_as(in_tensor)
class BAM(nn.Module):
def __init__(self, gate_channel):
super(BAM, self).__init__()
self.channel_att = Channel_Gate(gate_channel)
self.spatial_att = Spatial_Gate(gate_channel)
def forward(self, in_tensor):
att = 1 + F.sigmoid(self.channel_att(in_tensor) * self.spatial_att(in_tensor))
return att * in_tensor
class Channel_Att(nn.Module):
def __init__(self, channels, t=16):
super(Channel_Att, self).__init__()
self.channels = channels
self.bn2 = nn.BatchNorm2d(self.channels, affine=True)
def forward(self, x):
residual = x
x = self.bn2(x)
weight_bn = self.bn2.weight.data.abs() / torch.sum(self.bn2.weight.data.abs())
x = x.permute(0, 2, 3, 1).contiguous()
x = torch.mul(weight_bn, x)
x = x.permute(0, 3, 1, 2).contiguous()
x = torch.sigmoid(x) * residual
return x
class NAMAtt(nn.Module):
def __init__(self, channels):
super(NAMAtt, self).__init__()
self.Channel_Att = Channel_Att(channels)
def forward(self, x):
x_out1 = self.Channel_Att(x)
return x_out1
SENet、CBAM、ECANet、FCANet、CA、EMA、SEMA、BAM、NAMAtt代码整理合集,需要的朋友拿走吧
最新推荐文章于 2024-07-13 22:44:58 发布