pytorch实现的通道注意力机制SENet的代码

编程入门 行业动态 更新时间:2024-10-25 07:32:26

pytorch实现的通道<a href=https://www.elefans.com/category/jswz/34/1769627.html style=注意力机制SENet的代码"/>

pytorch实现的通道注意力机制SENet的代码

通道注意力机制SENet

Diagram of a Squeeze-and-Excitation building block

实现代码如下

import torch
import torch.nn as nnclass SELayer(nn.Module):def __init__(self, channel, reduction=16):super(SELayer, 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=False),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)if __name__ == "__main__":t = torch.ones((32, 128, 26, 26))se = SELayer(channel=128, reduction=16)out = se(t)print(out.shape)

更多推荐

pytorch实现的通道注意力机制SENet的代码

本文发布于:2024-02-08 20:40:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1674813.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:注意力   通道   机制   代码   pytorch

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!