我正在尝试运行 ONNX 模型
import onnxruntime as ort
import onnxruntime.backend
model_path = "model.onnx"
#https://microsoft.github.io/onnxruntime/
ort_sess = ort.InferenceSession(model_path)
print( ort.get_device() )
Run Code Online (Sandbox Code Playgroud)
这打印出来
cpu
Run Code Online (Sandbox Code Playgroud)
我怎样才能让它在我的 GPU 上运行?我如何确认它正在工作?
C = torch.cat((A,B),1)
Run Code Online (Sandbox Code Playgroud)
张量的形状:
A is (1, 128, 128, 256)
B is (1, 1, 128, 256)
Run Code Online (Sandbox Code Playgroud)
预期C
值为(1, 129, 128, 256)
这段代码可以在pytorch上运行,但是在转换为core-ml时会出现以下错误:
"Error while converting op of type: {}. Error message: {}\n".format(node.op_type, err_message, )
TypeError: Error while converting op of type: Concat. Error message: unable to translate constant array shape to CoreML shape"
Run Code Online (Sandbox Code Playgroud) 我在 PyTorch 中有一个 seq2seq 模型,我想用 CoreML 运行它。将模型导出到 ONNX 时,输入尺寸固定为导出期间使用的张量的形状,并再次从 ONNX 转换为 CoreML。
import torch
from onnx_coreml import convert
x = torch.ones((32, 1, 1000)) # N x C x W
model = Model()
torch.onnx.export(model, x, 'example.onnx')
mlmodel = convert(model='example.onnx', minimum_ios_deployment_target='13')
mlmodel.save('example.mlmodel')
Run Code Online (Sandbox Code Playgroud)
对于 ONNX 导出,您可以导出动态维度 -
torch.onnx.export(
model, x, 'example.onnx',
input_names = ['input'],
output_names = ['output'],
dynamic_axes={
'input' : {0 : 'batch', 2: 'width'},
'output' : {0 : 'batch', 1: 'owidth'},
}
)
Run Code Online (Sandbox Code Playgroud)
但这会导致RunTimeWarning
转换为CoreML
-
RuntimeWarning:您将无法在此 Core ML 模型上运行 …
我正在尝试将自定义 sklearn 管道保存为 onnx 模型,但在此过程中出现错误。
示例代码:
from sklearn.preprocessing import OneHotEncoder
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.pipeline import Pipeline
from sklearn import svm
from winmltools import convert_coreml
import copy
from IPython.display import display
# https://github.com/pandas-dev/pandas/issues/8918
class MyEncoder(TransformerMixin):
def __init__(self, columns=None):
self.columns = columns
def transform(self, X, y=None, **kwargs):
return pd.get_dummies(X, dtype=np.float, columns=['ID'])
def fit(self, X, y=None, **kwargs):
return self
# data
X = pd.DataFrame([[100, 1.1, 3.1], [200, 4.1, 5.1], [100, 4.1, 2.1]], columns=['ID', 'X1', 'X2'])
Y = pd.Series([3, 2, 4]) …
Run Code Online (Sandbox Code Playgroud)