模型轻量化:MobileNet、量化、剪枝

📂 所属阶段:第五阶段 — 工业落地与部署(实战篇)
🔗 相关章节:3D 视觉基础 · 推理加速框架


1. 轻量化网络

import torch
import torchvision.models as models

# MobileNetV3
mobilenet = models.mobilenet_v3_small(pretrained=True)
print(f"参数量: {sum(p.numel() for p in mobilenet.parameters()) / 1e6:.2f}M")

# ShuffleNet
shufflenet = models.shufflenet_v2_x1_0(pretrained=True)
print(f"参数量: {sum(p.numel() for p in shufflenet.parameters()) / 1e6:.2f}M")

# 对比 ResNet50
resnet50 = models.resnet50(pretrained=True)
print(f"参数量: {sum(p.numel() for p in resnet50.parameters()) / 1e6:.2f}M")

2. 模型量化

import torch
import torch.quantization as quantization

# 动态量化
quantized_model = quantization.quantize_dynamic(
    model, {torch.nn.Linear}, dtype=torch.qint8
)

# 静态量化
model.qconfig = quantization.get_default_qconfig('fbgemm')
quantization.prepare(model, inplace=True)
# 校准...
quantization.convert(model, inplace=True)

3. 模型剪枝

import torch.nn.utils.prune as prune

# 结构化剪枝
prune.ln_structured(model.layer1[0].conv1, name="weight", amount=0.3, dim=0)

# 非结构化剪枝
prune.l1_unstructured(model.layer1[0].conv1, name="weight", amount=0.3)

# 移除剪枝掩码
prune.remove(model.layer1[0].conv1, 'weight')

4. 小结

轻量化三大方法:

1. 轻量化网络:MobileNet、ShuffleNet
2. 量化:8 位整数替代 32 位浮点
3. 剪枝:移除不重要的权重

效果:
- 模型大小减少 4-10 倍
- 推理速度提升 2-5 倍
- 准确率下降 1-3%

💡 记住:轻量化是部署的必要条件。没有轻量化,模型无法跑在手机上。


🔗 扩展阅读