python手机版有pytorch库吗_SENet 的一个PyTorch实现

SENet.pytorch

An implementation of SENet, proposed in Squeeze-and-Excitation Networks by Jie Hu, Li Shen and Gang Sun, who are the winners of ILSVRC 2017 classification competition.

Now SE-ResNet (18, 34, 50, 101, 152/20, 32) and SE-Inception-v3 are implemented.

python cifar.py runs SE-ResNet20 with Cifar10 dataset.

python imagenet.py IMAGENET_ROOT runs SE-ResNet50 with ImageNet(2012) dataset.

You need to prepare dataset by yourself

First download files and then follow the instruction.

The number of workers and some hyper parameters are fixed so check and change them if you need.

This script uses all GPUs available. To specify GPUs, use CUDA_VISIBLE_DEVICES variable. (e.g. CUDA_VISIBLE_DEVICES=1,2 to use GPU 1 and 2)

For SE-Inception-v3, the input size is required to be 299x299 as the original Inception.

Pre-requirements

Python>=3.6

PyTorch>=1.0

torchvision>=0.3

For training

To run cifar.py or imagenet.py, you need

pip install git+https://github.com/moskomule/homura

pip install git+https://github.com/moskomule/miniargs

hub

You can use some SE-ResNet (se_resnet{20, 56, 50, 101}) via torch.hub.

import torch.hub

hub_model = torch.hub.load(

'moskomule/senet.pytorch',

'se_resnet20',

num_classes=10)

Also, a pretrained SE-ResNet50 model is available.

import torch.hub

hub_model = torch.hub.load(

'moskomule/senet.pytorch',

'se_resnet50',

pretrained=True,)

Result

SE-ResNet20/Cifar10

python cifar.py [--baseline]

ResNet20

SE-ResNet20 (reduction 4 or 8)

max. test accuracy

92%

93%

SE-ResNet50/ImageNet

The initial learning rate and mini-batch size are different from the original version because of my computational resource .

ResNet

SE-ResNet

max. test accuracy(top1)

76.15 %(*)

77.06% (**)

(**): When using imagenet.py with the --distributed setting on 8 GPUs. The weight is available.

# !wget https://github.com/moskomule/senet.pytorch/releases/download/archive/seresnet50-60a8950a85b2b.pkl

senet = se_resnet50(num_classes=1000)

senet.load_state_dict(torch.load("seresnet50-60a8950a85b2b.pkl"))

References

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SENet(Squeeze-and-Excitation Network)是一种用于卷积神经网络(CNN)中的注意力机制,它通过学习通道间的关系来增强特征表示的重要性。在SENet中,网络会自动学习特征通道之间的相关性,并针对每个通道进行加权,以提高整体特征表示的性能。 在PyTorch中,可以使用以下代码实现SENet注意力机制: ```python import torch.nn as nn class 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) ``` 在上面的代码中,我们定义了一个SELayer类,它包含两个部分:全局平均池化和全连接层。首先,我们使用全局平均池化对每个通道进行平均池化,然后将结果输入到全连接层中。全连接层通过两个线性层和ReLU激活函数来学习特征通道之间的关系,最后使用Sigmoid函数将输出值压缩到0到1之间。最后,我们将SELayer应用于输入特征图上,以增强特征表示的重要性。 SENet是一种非常有效的注意力机制,可以提高卷积神经网络的性能。在实际应用中,可以将SELayer添加到现有的CNN架构中,以提高模型的性能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值