当前位置:网站首页>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}')
边栏推荐
猜你喜欢
随机推荐
STM32LL库——USART中断接收不定长信息
【使用Pytorch实现VGG16网络模型】
PyTorch①---加载数据、tensorboard的使用
单端K总线收发器DP9637兼容L9637
推开机电的大门《电路》(三):说说不一样的电阻与电导
7. How to add the Click to RecyclerView and LongClick events
PyTorch⑦---卷积神经网络_非线性激活
win10 system update error code 0x80244022 how to do
Binder机制(下篇)
arm ldr系列指令
DP4301无线收发SUB-1G芯片兼容CC1101智能家居
日常-笔记
Use tencent cloud builds a personal blog
FP7195转模拟调光技术解决智能家居调光频闪和电感噪音的原理
Win10 cannot directly use photo viewer to open the picture
win11一直弹出用户账户控制怎么解决
2.4G无线小模块CI24R1超低成本
Win10系统设置application identity自动提示拒绝访问怎么办
用U盘怎么重装Win7系统?如何使用u盘重装系统win7?
jest测试,组件测试