当前位置:网站首页>Pytorch quantitative perception training (qat) steps
Pytorch quantitative perception training (qat) steps
2022-06-30 21:47:00 【Breeze_】
# QAT follows the same steps as PTQ, with the exception of the training loop before you actually convert the model to its quantized version
# QAT Follow and PTQ The same procedure , In addition to the training cycle before actually converting the model to a quantitative version
''''''
''' Quantify perception training steps : step1. Build a model step2. The fusion ( An optional step ) step3. Insert stubs(1 and 3 Can be combined ) step4. Get ready ( The main thing is to choose the architecture ) step5. Training step6. Model transformation '''
import torch
from torch import nn
backend = "fbgemm" # running on a x86 CPU. Use "qnnpack" if running on ARM.
'''step1. Build a model build model'''
m = nn.Sequential(
nn.Conv2d(2,64,8),
nn.ReLU(),
nn.Conv2d(64, 128, 8),
nn.ReLU(),
)
"""step2. The fusion Fuse( An optional step )"""
torch.quantization.fuse_modules(m, ['0','1'], inplace=True) # fuse first Conv-ReLU pair
torch.quantization.fuse_modules(m, ['2','3'], inplace=True) # fuse second Conv-ReLU pair
"""step3. Insert stubs In the model ,Insert stubs"""
m = nn.Sequential(torch.quantization.QuantStub(),
*m,
torch.quantization.DeQuantStub())
"""step4. Get ready Prepare"""
m.train()
m.qconfig = torch.quantization.get_default_qconfig(backend)
torch.quantization.prepare_qat(m, inplace=True)
"""step5. Training Training Loop"""
n_epochs = 10
opt = torch.optim.SGD(m.parameters(), lr=0.1)
loss_fn = lambda out, tgt: torch.pow(tgt-out, 2).mean()
for epoch in range(n_epochs):
x = torch.rand(10,2,24,24)
out = m(x)
loss = loss_fn(out, torch.rand_like(out))
opt.zero_grad()
loss.backward()
opt.step()
print(loss)
"""step6. Model transformation Convert"""
m.eval()
torch.quantization.convert(m, inplace=True)
边栏推荐
猜你喜欢

SQL server extracts pure numbers from strings

A comprehensive understanding of gout: symptoms, risk factors, pathogenesis and management

Random talk about Clickhouse join

Rethink healthy diet based on intestinal microbiome

兴奋神经递质——谷氨酸与大脑健康

Markdown notes concise tutorial

介绍一款|用于多组学整合和网络可视化分析的在线平台

Reading notes of Clickhouse principle analysis and Application Practice (2)

模板方法模式介绍与示例

jupyter notebook/lab 切换conda环境
随机推荐
1-2 install and configure MySQL related software
1-19 利用CORS解决接口跨域问题
Dm8: generate DM AWR Report
Zaah Sultan looks at the old driver
1-7 path module
Deployment and use of Nacos
Introduce an online platform for multi omics integration and network visual analysis
Three techniques for reducing debugging time of embedded software
5g demand in smart medicine
开发属于自己的包
jupyter notebook/lab 切换conda环境
Excuse me, can I open an account for the company? Is it safe? All the answers you want are here
【回溯】全排列 leetcode46
微服务链路风险分析
侧睡哈哈哈哈
Pytorch quantitative practice (1)
To the Sultanate of Anderson
clickhouse原生監控項,系統錶描述
Open the jupyter notebook/lab and FAQ & settings on the remote server with the local browser
Installing jupyter notebook under Anaconda