mobilenet_prune_pytorch图像分类算法模型

mobilenet_prune

论文

模型结构

mobilenetv2:

image

详细模型结构见官方论文:

算法原理

mobilenetv2:

具有线性瓶颈的反转残差(inverted residual ):该模块将低维压缩表示作为输入,首先将其扩展为高维度并使用轻量级沿深度卷积(depthwiseconvolution)进行滤波。随后通过线性卷积将特征投射回低维表示。中间层使用轻量级的沿深度卷积来对特征进行滤波作为非线性的来源。

image

数据集

CIFAR-10

训练数据目录结构如下,用于正常训练的完整数据集请按此目录结构进行制备:

├── cifar-10-batches-py
│   ├── batches.meta
│   ├── data_batch_1
│   ├── data_batch_2
│   ├── data_batch_3
│   ├── data_batch_4
│   ├── data_batch_5
│   ├── readme.html
│   └── test_batch

**tips:本模型也会自动下载数据集

环境配置

光源可拉取剪枝及微调的docker镜像,在光合开发者社区可下载torch安装包。mobilenet_prune推荐的镜像如下:

docker pull image.sourcefind.cn:5000/dcu/admin/base/pytorch:1.13.1-centos7.6-dtk-23.04.1-py37-latest
docker run -d -t --privileged --device=/dev/kfd --device=/dev/dri/ --network=host --group-add video --name prune-test image.sourcefind.cn:5000/dcu/admin/base/pytorch:1.13.1-centos7.6-dtk-23.04.1-py37-latest
docker exec -it prune-test bash

安装依赖

cd mobilenet_prune
pip3 install -r requirements.txt

训练

剪枝

efficientnet

python3 main.py --mode prune --model efficientnet --batch-size 128 --restore ./checkpoints/efficientnet.pt --dataset cifar10  --method l1 --speed-up 2.11 --global-pruning

mobilenetv2

python3 main.py --mode prune --model mobilenetv2 --batch-size 128 --restore ./checkpoints/mobilenetv2.pt --dataset cifar10  --method l1 --speed-up 2.11 --global-pruning

mobilenetv3

python3 main.py --mode prune --model mobilenetv3 --batch-size 128 --restore ./checkpoints/mobilenetv3.pt --dataset cifar10  --method l1 --speed-up 2.11 --global-pruning

测试

efficientnet

python3 main.py --mode test --model efficientnet --batch-size 128 --restore ./run/cifar10/prune/cifar10-global-l1-efficientnet/cifar10_efficientnet_l1.pt --dataset cifar10  --method l1 --speed-up 2.11 --global-pruning

mobilenetv2

python3 main.py --mode test --model mobilenetv2 --batch-size 128 --restore ./run/cifar10/prune/cifar10-global-l1-mobilenetv2/cifar10_mobilenetv2_l1.pt --dataset cifar10  --method l1 --speed-up 2.11 --global-pruning

mobilenetv3

python3 main.py --mode test --model mobilenetv3 --batch-size 128 --restore ./run/cifar10/prune/cifar10-global-l1-mobilenetv3/cifar10_mobilenetv3_l1.pt --dataset cifar10  --method l1 --speed-up 2.11 --global-pruning

result

image

精度

method l1

modelBase AccPruned Acc
efficientnet0.93000.9248
mobilenetv20.92220.9205
mobilenetv30.90340.8963

method lamp

modelBase AccPruned Acc
efficientnet0.93000.9256
mobilenetv20.92220.9225
mobilenetv30.90340.9044

method group_norm

modelBase AccPruned Acc
efficientnet0.93000.8977
mobilenetv20.92220.8953
mobilenetv30.90340.8852

应用场景

算法类别

图像分类

热点应用行业

交通,金融,医疗,教育,家居

源码仓库及问题反馈

ModelZoo / mobilenet_prune_pytorch · GitLab

参考资料

  • 10
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是PyTorch版本的`pruning_filters`源码: ```python import torch import torch.nn as nn import torch.nn.utils.prune as prune class PrunedConv2d(nn.Module): def __init__(self, conv): super(PrunedConv2d, self).__init__() self.conv = conv def forward(self, x): return self.conv(x) def pruning_filters(model, layer_name, amount): """ 对指定层进行卷积核剪枝 :param model: 待剪枝的模型 :param layer_name: 指定层的名称 :param amount: 剪枝比例,即需要删除的卷积核所占的比例 :return: """ layer = dict(model.named_modules())[layer_name] if isinstance(layer, PrunedConv2d): layer = layer.conv if not isinstance(layer, nn.Conv2d): raise TypeError("Only 'nn.Conv2d' layer type can be pruned") # 获取卷积核数量 num_filters = layer.weight.shape[0] # 计算需要删除的卷积核数量 num_pruned_filters = int(num_filters * amount) # 构建剪枝对象 prune_params = ( prune.get_parameter(module=layer, name="weight"), prune.L1Unstructured, prune.Criterion.L1, (num_pruned_filters,) ) # 执行剪枝操作 prune.remove(*prune_params) # 创建新的PrunedConv2d层替换原有的层 new_layer = PrunedConv2d(layer) setattr(model, layer_name, new_layer) ``` 此代码中定义了一个`PrunedConv2d`类,用于将剪枝后的卷积层替换原有的卷积层。`pruning_filters`函数接收一个待剪枝的模型、需要剪枝的层的名称以及剪枝比例,然后使用PyTorch内置的剪枝函数`prune.remove`对指定层进行卷积核剪枝,并使用`PrunedConv2d`类创建一个新的剪枝后的卷积层替换原有的层。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

技术瘾君子1573

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值