pytorch 模型量化quantization

pytorch 模型量化quantization

  • 1.workflow
    • 1.1 PTQ
    • 1.2 QAT
  • 2. demo
    • 2.1 构建resnet101_quantization模型
    • 2.2 PTQ
    • 2.3 QAT
  • 参考文献

pytorch框架提供了三种量化方法,包括:

  • Dynamic Quantization
  • Post-Training Static Quantization(PTQ)
  • Quantization Aware Training(QAT)

此博客结合CIFAR100数据集分类任务,分别采用Post-Training Static QuantizationQuantization Aware Training对resnet101模型进行量化。

1.workflow

1.1 PTQ

在这里插入图片描述

图片来自Practical Quantization in PyTorch。

1.2 QAT

在这里插入图片描述

图片来自Practical Quantization in PyTorch。

从两张图片来看,PTQ和QAT的差别在于:PTQ量化前使用了calibration data,而QAT则有一个和训练阶段类似的训练过程。

2. demo

2.1 构建resnet101_quantization模型

import torch
import torch.nn as nn
from torch.ao.quantization import QuantStub, DeQuantStub

class ConvBNReLU(nn.Sequential):
    def __init__(self, in_planes, out_planes, kernel_size=3, stride=1, groups=1):
        padding = (kernel_size - 1) // 2
        super(ConvBNReLU, self).__init__(
            nn.Conv2d(in_planes, out_planes, kernel_size, stride,
                      padding, groups=groups, bias=False),
            nn.BatchNorm2d(out_planes, momentum=0.1),
            nn.ReLU(inplace=False)
        )


class ConvBN(nn.Sequential):
    def __init__(self, in_planes, out_planes, kernel_size=3, stride=1, groups=1):
        padding = (kernel_size - 1) // 2
        super(ConvBN, self).__init__(
            nn.Conv2d(in_planes, out_planes, kernel_size, stride,
                      padding, groups=groups, bias=False),
            nn.BatchNorm2d(out_planes, momentum=0.1),
        )


class BottleNeck(nn.Module):
    expansion = 4

    def __init__(self, in_channels, out_channels, stride=1):
        super().__init__()
        self.residual_function = nn.Sequential(
            ConvBNReLU(in_channels, out_channels, kernel_size=1),
            ConvBNReLU(out_channels, out_channels, kernel_size=3),
            ConvBN(out_channels, out_channels *
                   BottleNeck.expansion, kernel_size=1)
        )

        self.shortcut = nn.Sequential()

        if stride != 1 or in_channels != out_channels * BottleNeck.expansion:
            self.shortcut = ConvBN(in_channels, out_channels *
                                   BottleNeck.expansion, kernel_size=1)

        self.skip_add = nn.quantized.FloatFunctional()

    def forward(self, x):
        return nn.ReLU(inplace=True)(self.skip_add.add(self.residual_function(x), self.shortcut(x)))


class ResNet(nn.Module):

    def __init__(self, block, num_block, num_classes=100):
        super().__init__()

        self.in_channels = 64

        self.conv1 = nn.Sequential(
            ConvBNReLU(3, 64, kernel_size=3))

        self.conv2_x = self._make_layer(block, 64, num_block[0], 1)
        self.conv3_x = self._make_layer(block, 128, num_block[1], 2)
        self.conv4_x = self._make_layer(block, 256, num_block[2], 2)
        self.conv5_x = self._make_layer(block, 512, num_block[3], 2)
        self.avg_pool = nn.AdaptiveAvgPool2d((1, 1))

        self.fc = nn.Linear(512 * block.expansion, num_classes)

        self.quant = QuantStub()
        self.dequant = DeQuantStub()

    def _make_layer(self, block, out_channels, num_blocks, stride):

        strides = [stride] + [1] * (num_blocks - 1)
        layers = []
        for stride in strides:
            layers.append(block(self.in_channels, out_channels, stride))
            self.in_channels = out_channels * block.expansion

        return nn.Sequential(*layers)

    def forward(self, x):
        x = self.quant(x)
        output = self.conv1(x)
        output = self.conv2_x(output)
        output = self.conv3_x(output)
        output = self.conv4_x(output)
        output = self.conv5_x(output)
        output = self.avg_pool(output)
        output = output.view(output.size(0), -1)
        output = self.fc(output)
        x = self.dequant(x)
        return output
    # Fuse Conv+BN and Conv+BN+Relu modules prior to quantization
    # This operation does not change the numerics

    def fuse_model(self, is_qat=False):
        fuse_modules = torch.ao.quantization.fuse_modules_qat if is_qat else torch.ao.quantization.fuse_modules
        for m in self.modules():
            if type(m) == ConvBNReLU:
                fuse_modules(m, ['0', '1', '2'], inplace=True)

            if type(m) == ConvBN:
                fuse_modules(m, ['0', '1'], inplace=True)
def resnet101():
    """ return a ResNet 101 object
    """
    return ResNet(BottleNeck, [3, 4, 23, 3])

代码改编自https://github.com/weiaicunzai/pytorch-cifar100。

如果要使用quantization,构建的模型和常规模型差别主要在以下内容:


class BottleNeck(nn.Module):
    expansion = 4

    def __init__(self, in_channels, out_channels, stride=1):
        super().__init__()
        ...
        ...
        self.skip_add = nn.quantized.FloatFunctional()

    def forward(self, x):
        return nn.ReLU(inplace=True)(self.skip_add.add(self.residual_function(x), self.shortcut(x)))

这是因为没有直接用于相加的算子。

如果没有这一操作,可能会报如下错误:

NotImplementedError: Could not run 'aten::add.out' with arguments from the 'QuantizedCPU' backend. This could be because the operator doesn't exist for this backend, or was omitted during the selective/custom build process (if using custom build). 

另外就是:


class ResNet(nn.Module):
    def __init__(self, block, num_block, num_classes=100):
        super().__init__()
        ...
        ...
        self.quant = QuantStub() #observer
        self.dequant = DeQuantStub()

    def _make_layer(self, block, out_channels, num_blocks, stride):
        ...
        ...

    def forward(self, x):
        x = self.quant(x)
        ...
        ...
        x = self.dequant(x)
        return output
    # Fuse Conv+BN and Conv+BN+Relu modules prior to quantization
    # This operation does not change the numerics

    def fuse_model(self, is_qat=False):
        ...
        ...

即添加observer,以及将Conv+BN 和Conv+BN+Relu 模块融合到一起。

2.2 PTQ

def print_size_of_model(model):
    torch.save(model.state_dict(), "temp.p")
    print('Size (MB):', os.path.getsize("temp.p")/1e6)
    os.remove('temp.p')

qmodel = resnet101()
# qmodel.load_state_dict(torch.load(args.weights))

qmodel = qmodel.to(device)
# # print(qmodel)
qmodel.eval()

print("Size of model befor quantization")
print_size_of_model(qmodel)

num_calibration_batches = 32
qmodel.eval()
# Fuse Conv, bn and relu
qmodel.fuse_model()

# Specify quantization configuration
# Start with simple min/max range estimation and per-tensor quantization of weights
qmodel.qconfig = torch.ao.quantization.default_qconfig
print(qmodel.qconfig)
torch.ao.quantization.prepare(qmodel, inplace=True)

# Calibrate first
print('Post Training Quantization Prepare: Inserting Observers')


# Calibrate with the training set
criterion = nn.CrossEntropyLoss()
evaluate(qmodel, criterion, cifar100_test_loader,
             neval_batches=10)
print('Post Training Quantization: Calibration done')

# Convert to quantized model
torch.ao.quantization.convert(qmodel, inplace=True)
print('Post Training Quantization: Convert done')
# print('\n Inverted Residual Block: After fusion and quantization, note fused modules: \n\n',
#       qmodel.features[1].conv)

print("Size of model after quantization")
print_size_of_model(qmodel)
Size of model befor quantization
Size (MB): 171.40158

Size of model after quantization
Size (MB): 42.970334

size大致缩小了四倍。

经过测试,在本地cpu上推断时间也缩小了3~4倍。

2.3 QAT

#%% QAT
float_model_file="resnet101.pt"
qat_model=resnet101()
qat_model.load_state_dict(torch.load(float_model_file))
qat_model.fuse_model(is_qat=True)

optimizer = torch.optim.SGD(qat_model.parameters(), lr = 0.0001)
# The old 'fbgemm' is still available but 'x86' is the recommended default.
qat_model.qconfig = torch.ao.quantization.get_default_qat_qconfig('x86')
torch.ao.quantization.prepare_qat(qat_model, inplace=True)
# print('Inverted Residual Block: After preparation for QAT, note fake-quantization modules \n',qat_model.features[1].conv)

num_train_batches,num_eval_batches = 2,2
eval_batch_size=32
criterion = nn.CrossEntropyLoss()
nepochs=10

# QAT takes time and one needs to train over a few epochs.
# Train and check accuracy after each epoch
for nepoch in range(nepochs):
    train_one_epoch(qat_model, criterion, optimizer, cifar100_test_loader, torch.device('cpu'),  num_train_batches)
    if nepoch > 3:
        # Freeze quantizer parameters
        qat_model.apply(torch.ao.quantization.disable_observer)
    if nepoch > 2:
        # Freeze batch norm mean and variance estimates
        qat_model.apply(torch.nn.intrinsic.qat.freeze_bn_stats)

    # Check the accuracy after each epoch
    quantized_model = torch.ao.quantization.convert(qat_model.eval(), inplace=False)
    quantized_model.eval()
    top1, top5 = evaluate(quantized_model,criterion, cifar100_test_loader, neval_batches=2)
    print('Epoch %d :Evaluation accuracy on %d images, %2.2f'%(nepoch, num_eval_batches * eval_batch_size, top1.avg))

完整代码后续将分享在github或csdn资源中。

参考文献

[1] Introduction to Quantization on PyTorch
[2] https://github.com/pytorch/pytorch/wiki/Introducing-Quantized-Tensor
[3] tensorflow 训练后量化
[4] pytorch dynamic and static quantization 适用的算子
[5] ★★★pytorch_static quantization tutorial
[6] PyTorch Static Quantization
[7] Practical Quantization in PyTorch
[8] ★★★https://github.com/weiaicunzai/pytorch-cifar100

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/213313.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

wordpress安装之Linux解压缩安装

本次教程是为了让大家少走弯路,可以更直观的去认识我们不懂的知识面。 首先我们安装解压缩的软件 命令如下: yum install -y unzip 上一篇我们讲到传输文件了 这篇我们把传输过来的压缩包解压并进行安装。follow me! 我们输入命令 unzi…

【数值计算方法(黄明游)】矩阵特征值与特征向量的计算(五):Householder方法【理论到程序】

文章目录 一、Jacobi 旋转法二、Jacobi 过关法三、Householder 方法1. 旋转变换a. 旋转变换的选择b. 旋转变换的顺序 2. Householder矩阵(Householder Matrix)a. H矩阵的定义b. H变换的几何解释c. H变换的应用场景 3. H变换过程详解a. 过程介绍b. 细节解…

全面的.NET微信网页开发之JS-SDK使用步骤、配置信息和接口请求签名生成详解

JSSDK使用步骤 步骤一:绑定安全域名: 先登录微信公众平台进入“公众号设置”的“功能设置”里填写“JS接口安全域名”。 步骤二:引入JS文件: 在需要调用JS接口的页面引入如下JS文件,(支持https):http://…

业务数据治理体系化实施流程学习总结

目录 一、业务数据治理实施流程 步骤 1:发现问题和制定目标 步骤 2:针对问题进行拆解,设计可衡量的指标 步骤 3:制定解决SOP和检查研发标准规范 步骤 4:推广运营,以拿结果为核心目标 步骤 5&#xff…

matlab科学计算

欢迎关注博主 Mindtechnist 或加入【智能科技社区】一起学习和分享Linux、C、C、Python、Matlab,机器人运动控制、多机器人协作,智能优化算法,滤波估计、多传感器信息融合,机器学习,人工智能等相关领域的知识和技术。关…

ARM架构安装RabbitMQ

1.查看centos内核版本 uname -a uname -r2.安装之前的准备工作 安装RabbitMQ必装Erlang(RabbitMQ官网添加链接描述) 2.1.Erlang简介 Erlang是一种通用的面向并发的编程语言,它由瑞典电信设备制造商爱立信所辖的CS-Lab开发,目的是创造一种可以应对…

【Java基础篇 | 面向对象】—— 聊聊什么是多态(下篇)

个人主页:兜里有颗棉花糖 欢迎 点赞👍 收藏✨ 留言✉ 加关注💓本文由 兜里有颗棉花糖 原创 收录于专栏【JavaSE_primary】 本专栏旨在分享学习JavaSE的一点学习心得,欢迎大家在评论区讨论💌 目录 一、动态绑定和静态绑…

C++ string类(2)—成员访问、插入、删除、替换、查找和交换操作

目录 一、成员访问 1、[ ]&at 2、front( )&back( ) 二、插入元素 三、删除元素 四、替换元素 五、查找元素 1、查找第一次出现位置 2 、在指定范围内查找 六、交换字符串 七、c_str 八、rfind&substr 一、成员访问 1、[ ]&at 虽然二者功能一样&…

【微信小程序】上传头像 微信小程序内接小程序客服

这里写目录标题 微信小程序上传头像使用button按钮包裹img 微信小程序内接小程序客服使用button按钮跳转客服 微信小程序上传头像 使用button按钮包裹img 原本思路是只使用image标签再加上chooseImg,但发现使用button标签上传头像这种方法更实用。微信小程序文档上…

栈实现队列,力扣

题目地址: 232. 用栈实现队列 - 力扣(LeetCode) 难度:简单 今天刷栈实现队列,大家有兴趣可以点上看看题目要求,试着做一下。 题目: 请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支…

简单句子成分、阅读技巧

四、段落的主旨题:问这一段讲了什么(一般都在段落的第一句话或最后一句话) 词汇题的答案一般都在生词的上一句或者下一句 做题步骤: 1、先标段落 2、看题,划出关键词 3、去原文定位,标注中文意思 4、第一遍…

类和对象——(5)定义对象数组

归纳编程学习的感悟, 记录奋斗路上的点滴, 希望能帮到一样刻苦的你! 如有不足欢迎指正! 共同学习交流! 🌎欢迎各位→点赞 👍 收藏⭐ 留言​📝 芳华没有草稿纸,我们永久不…

异常(C++)

异常 前言一、程序的错误分类二、异常1. 概念2. 捕获异常的关键字和格式3. 异常的使用异常的原则异常再抛出异常说明注意事项 4. 自定义异常体系5. C标准库的异常体系 三、总结 前言 在程序运行时经常碰到一些错误,例如年龄、身高不能为负,除数为0等&…

阿里微服务质量保障系列:性能监控最佳实践

建设一体化性能监控平台 随着互联网技术的不断发展,企业的业务规模和复杂度也在不断增加。为了保证业务的稳定性和可靠性,企业需要对其系统进行全面的性能监控。而一体化性能监控就是一种集成了多种监控工具和技术的综合性监控方案,可以帮助…

PPT设置背景颜色

问题描述:PPT如何设置背景颜色? 问题解决:设计→设置背景格式→颜色→蓝色(最好选择看着比较舒服的颜色)

软件设计模式原则(三)单一职责原则

单一职责原则(SRP)又称单一功能原则。它规定一个类应该只有一个发生变化的原因。所谓职责是指类变化的原因。如果一个类有多于一个的动机被改变,那么这个类就具有多于一个的职责。而单一职责原则就是指一个类或者模块应该有且只有一个改变的原…

linux下安装nginx

第一步:压缩包 准备压缩包,最好准备一个稳定的版本:下载地址 我这边选用的是1.24.0双版本号 第二步:解压 在相对应的目录下,执行命令:tar -zxvf nginx-1.18.0.tar.gz 第三步:配置\编译 推荐…

【vue】尚硅谷vue3学习笔记

Vue3快速上手 1.Vue3简介 2020年9月18日,Vue.js发布3.0版本,代号:One Piece(海贼王)耗时2年多、2600次提交、30个RFC、600次PR、99位贡献者github上的tags地址:https://github.com/vuejs/vue-next/release…

图论|并查集理论基础 1971. 寻找图中是否存在路径

什么是并查集 并查集是一种数据结构,用于处理一些不交集的合并及查询问题。它支持两种操作: 查找(Find):确定某个元素属于哪个子集。它可以用来判断两个元素是否属于同一个子集。 合并(Union)&…

Button初了解

Button 由TextView派生而来,二者的区别有: Button有默认的按钮背景,TextView默认无背景Button的内部文本默认居中对齐,而TextView的默认靠左对齐2023年以前,Button默认将英文换为大写,而TextView保持原始的…