使用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
环境问题