当前位置:网站首页>General code for pytorch model to libtorch and onnx format
General code for pytorch model to libtorch and onnx format
2022-08-02 15:26:00 【Hongyao】
依赖
- torch
- onnx
- onnx simplifer
Important parameters that need to be set by yourself
- model_path 模型权重路径
- model 网络实例
- inp 样例输入,就是一个shape合法的tensor,batchsize(第一维)设置为1就行
下面以torchvision自带的resnet101模型为例.The weights are using the official pretrained model,调用resnet101(pretrained=True)will be downloaded automatically%USERPROFILE%/.cache/torch/hub下面
import onnx
import torch
from torch.utils.mobile_optimizer import optimize_for_mobile
from torchvision.models.resnet import resnet101
from utils.func import file_size, colorstr
model_path = './weights/resnet101.pth' # 模型权重路径
model = resnet101() # 模型对象
height, width = 640, 640
inp = torch.zeros([1, 3, height, width]) # 样例输入,用于trace
# common
half = True # fp16量化
# onnx profile
onnx_export = True # 是否输出onnx格式
opset_version = 13 # 算子集版本
dynamic = False # Whether to enter dynamicallybatchsize,The following two options need to be set
input_names = ['inputs']
dynamic_axes = {
'inputs': {
0: 'batch', 1: 'kp28'}, # 动态batchsize设置
'output': {
0: 'batch', 1: 'classes'}}
simplify = True # whether to simplify
# libtorch profile
libtorch_export = True # 是否输出libtorch格式
optimize = False # 针对移动端优化,Not for mobile use
strict = False # 严格模式,设置False就行
if __name__ == '__main__':
model.load_state_dict(torch.load(model_path))
model.cpu().eval()
if half:
inp, model = inp.half(), model.half()
if onnx_export:
prefix = colorstr('ONNX:')
f = model_path.replace('.pth', '.onnx') # filename
torch.onnx.export(model, inp, f, verbose=False, opset_version=opset_version, input_names=input_names,
training=torch.onnx.TrainingMode.EVAL,
do_constant_folding=True,
dynamic_axes=dynamic_axes if dynamic else None)
# Checks
model_onnx = onnx.load(f) # load onnx model
onnx.checker.check_model(model_onnx) # check onnx model
# print(onnx.helper.printable_graph(model_onnx.graph)) # print
# Simplify
if simplify:
try:
import onnxsim
print(f'simplifying with onnx-simplifier {
onnxsim.__version__}...')
model_onnx, check = onnxsim.simplify(
model_onnx,
dynamic_input_shape=dynamic,
input_shapes={
'images': list(inp.shape)} if dynamic else None)
assert check, 'assert check failed'
onnx.save(model_onnx, f)
except Exception as e:
print(f'{
prefix} simplifier failure: {
e}')
print(f'{
prefix} export success, saved as {
f} ({
file_size(f):.1f} MB)')
if libtorch_export:
prefix = colorstr('TorchScript:')
try:
print(f'\n{
prefix} starting export with torch {
torch.__version__}...')
f = model_path.replace('.pt', '.torchscript.pt') # filename
ts = torch.jit.trace(model, inp, strict=strict)
(optimize_for_mobile(ts) if optimize else ts).save(f)
print(f'{
prefix} export success, saved as {
f} ({
file_size(f):.1f} MB)')
except Exception as e:
print(f'{
prefix} export failure: {
e}')
边栏推荐
猜你喜欢
随机推荐
Win11 keeps popping up User Account Control how to fix it
让深度学习歇一会吧
BLE蓝牙5.2-PHY6222系统级芯片(SoC)智能手表/手环
The overlapping effect of the two surfaceviews is similar to the video and handout practice in the live effect
STM32LL库使用——SPI通信
【使用Pytorch实现VGG16网络模型】
GICv3/v4-软件概述
图像配置分类及名词解释
PyTorch⑥---卷积神经网络_池化层
Win10安装了固态硬盘还是有明显卡顿怎么办?
win10无法直接用照片查看器打开图片怎么办
Win7遇到错误无法正常开机进桌面怎么解决?
Win10系统设置application identity自动提示拒绝访问怎么办
神经网络的设计过程
Bash shell位置参数
系统线性、时不变、因果判断
LORA芯片ASR6505无线远距离传输8位MCU
Letter combination of LeetCode2 phone number
How to set the win10 taskbar does not merge icons
arm ldr系列指令









