使用ONNX进行PyTorch模型部署

程序错误

torch转换onnx

pip install onnx
pip install onnxruntime // cpu
pip install onnxruntime-gpu

https://onnxruntime.ai/

import torch
import torch.nn as nn
import onnx
import numpy as np

class Model(nn.Module):
    def __init__(self):
        super(Model,self).__init__()
        self.conv1=nn.Conv2d(3,3, kernel_size=3, stride=2,padding=1)
    def forward(self,x,y):
        result1=self.conv1(x)
        result2=self.conv1(y)
        return result1,result2

model=Model()
model.eval() # 若存在batchnorm、dropout层则一定要eval()再export

input_names = ["input_0","input_1"]
output_names = ["output_0","output_1"]

x=torch.randn((1,3,12,12))
y=torch.randn((1,3,6,6))

torch.onnx.export(model,(x,y),'model.onnx',input_names=input_names,output_names=output_names, dynamic_axes={'input_0':[0],'output_0':[0]})

onnx进行量化

报错 1 AssertionError: Only Softmax or Logsoftmax operation on Axis 1 is supported if input is non-constant, but got axis 2 in layer Softmax_91

torch版本1.1好使

2 UnicodeDecodeError: 'gbk' codec can't decode byte 0xac in position 3261: illegal multibyte sequence

3
在这里插入图片描述
raise StopIteration
StopIteration

   try:
        images, target = next(batch_iterator)
    except StopIteration:
        batch_iterator = iter(data.DataLoader(dataset, batch_size, shuffle=True, num_workers=num_workers, collate_fn=detection_collate))
        images, target = next(batch_iterator)
   images = images.numpy()
   yield {'input':images}

4 ImportError: libcudart.so.11.0: cannot open shared object file: No such file or directory
环境问题

2

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐