注意力机制论文:Squeeze-and-ExcitationNetworks及其PyTorch实现

更新时间:2023-05-27 08:21:48 阅读: 评论:0

注意⼒机制论⽂:Squeeze-and-ExcitationNetworks及其PyTorch实现
Squeeze-and-Excitation Networks(SENet)是由⾃动驾驶公司Momenta在2017年公布的⼀种全新的图像识别结构,它通过对特征通道间的相关性进⾏建模,把重要的特征进⾏强化来提升准确率。这个结构是2017 ILSVR竞赛的冠军,top5的错误率达到了2.251%,⽐2016年的第⼀名还要低25%,可谓提升巨⼤。
1 概述
SENet通过学习channel之间的相关性,筛选出了针对通道的注意⼒,稍微增加了⼀点计算量,但是效果提升较明显
Squeeze-and-Excitation(SE) block是⼀个⼦结构,可以有效地嵌到其他分类或检测模型中。
SENet的核⼼思想在于通过⽹络根据loss去学习feature map的特征权重来使模型达到更好的结果
SE模块本质上是⼀种attention机制
2 Squeeze-and-Excitation模块
Squeeze 操作对 C x H x W 进⾏global average pooling,得到⼤⼩为 C x 1 x 1 的特征图
Excitation 操作 使⽤⼀个全连接神经⽹络,对Sequeeze之后的结果做⼀个⾮线性变换
Reweight 操作 使⽤Excitation 得到的结果作为权重,乘到输⼊特征上
3 SE模块应⽤举例
SE-Inception 和 SE-ResNet
class SE_Module(nn.Module):
def__init__(lf, channel,ratio =16):
super(SE_Module, lf).__init__()
lf.squeeze = nn.AdaptiveAvgPool2d(1)
nn.Linear(in_features=channel, out_features=channel // ratio),
nn.ReLU(inplace=True),
nn.Linear(in_features=channel // ratio, out_features=channel),
nn.Sigmoid()厦门土楼旅游攻略
感叹是什么意思)
def forward(lf, x):
b, c, _, _ = x.size()山根有横纹
y = lf.squeeze(x).view(b, c)
z = lf.excitation(y).view(b, c,1,1)
return x * z.expand_as(x)
4 SENet初中学生会
PyTorch代码:
import torch
as nn
import torchvision
def Conv1(in_planes, places, stride=2):
return nn.Sequential(
nn.Conv2d(in_channels=in_planes,out_channels=places,kernel_size=7,stride=stride,padding=3, bias=Fal),        nn.BatchNorm2d(places),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
)
class SE_Module(nn.Module):
def__init__(lf, channel,ratio =16):
super(SE_Module, lf).__init__()
lf.squeeze = nn.AdaptiveAvgPool2d(1)
nn.Linear(in_features=channel, out_features=channel // ratio),
nn.ReLU(inplace=True),
nn.Linear(in_features=channel // ratio, out_features=channel),
nn.Sigmoid()
)
def forward(lf, x):
b, c, _, _ = x.size()
y = lf.squeeze(x).view(b, c)
z = lf.excitation(y).view(b, c,1,1)
return x * z.expand_as(x)
class SE_ResNetBlock(nn.Module):
def__init__(lf,in_places,places, stride=1,downsampling=Fal, expansion =4):
super(SE_ResNetBlock,lf).__init__()
lf.downsampling = downsampling
lf.bottleneck = nn.Sequential(
nn.Conv2d(in_channels=in_places,out_channels=places,kernel_size=1,stride=1, bias=Fal),
nn.BatchNorm2d(places),
男的笔顺怎么写
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=places, out_channels=places, kernel_size=3, stride=stride, padding=1, bias=Fal),
nn.BatchNorm2d(places),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=places, out_channels=pansion, kernel_size=1, stride=1, bias=Fal),
nn.BatchNorm2d(pansion),
)
if lf.downsampling:
lf.downsample = nn.Sequential(
nn.Conv2d(in_channels=in_places, out_channels=pansion, kernel_size=1, stride=strid
e, bias=Fal),                nn.BatchNorm2d(pansion)
)
def forward(lf, x):
residual = x
out = lf.bottleneck(x)
if lf.downsampling:
residual = lf.downsample(x)
out += residual
各有所短
out = lf.relu(out)
return out
class SE_ResNet(nn.Module):
def__init__(lf,blocks, num_class=1000, expansion =4):
super(SE_ResNet,lf).__init__()
lf.layer1 = lf.make_layer(in_places =64, places=64, block=blocks[0], stride=1)
lf.layer2 = lf.make_layer(in_places =256,places=128, block=blocks[1], stride=2)
lf.layer3 = lf.make_layer(in_places=512,places=256, block=blocks[2], stride=2)
lf.layer4 = lf.make_layer(in_places=1024,places=512, block=blocks[3], stride=2)
lf.avgpool = nn.AvgPool2d(7, stride=1)
lf.fc = nn.Linear(2048,num_class)
for m dules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
elif isinstance(m, nn.BatchNorm2d):
stant_(m.weight,1)
stant_(m.bias,0)
def make_layer(lf, in_places, places, block, stride):
layers =[]
layers.append(SE_ResNetBlock(in_places, places,stride, downsampling =True)) for i in range(1, block):
layers.append(SE_ResNetBlock(pansion, places)) return nn.Sequential(*layers)
def forward(lf, x):
x = lf.conv1(x)
x = lf.layer1(x)
x = lf.layer2(x)
x = lf.layer3(x)经略使
x = lf.layer4(x)
x = lf.avgpool(x)
绿色痰
x = x.view(x.size(0),-1)
x = lf.fc(x)
return x
def SE_ResNet50():
return SE_ResNet([3,4,6,3])
if __name__=='__main__':
model = SE_ResNet50()
print(model)
input= torch.randn(1,3,224,224)
out = model(input)
print(out.shape)

本文发布于:2023-05-27 08:21:48,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/936430.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:提升   权重   模型
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图