我已将一个简单的 pytorch 模型转换为 onnx 格式,但未能尝试通过 onnxruntime 在单独的文件中加载和评估模型。它给出了错误消息:
NotImplemented: [ONNXRuntimeError] : 9 : NOT_IMPLEMENTED : Could not find an implementation for the node LeakyRelu_1:LeakyRelu(6)
但是文档清楚地列出了leakyrelu 运算符:https ://github.com/microsoft/onnxruntime/blob/master/docs/OperatorKernels.md
转换脚本非常简单。
import torch.onnx
import torch
torch.set_default_dtype(torch.float64)
Model = torch.load("Test.pth")
inputs = torch.randn(1,2)
torch.onnx.export(Model,inputs, "Test.onnx", export_params=True)
加载转换模型的脚本也很简单,
import onnx
import onnxruntime as rt
import numpy as np
onnx_model = onnx.load("Test.onnx")
onnx.checker.check_model(onnx_model)
#print(onnx_model.graph)
print("[Graph Input] name: {}, shape: {}".format(onnx_model.graph.input[0].name,
[dim.dim_value for dim in onnx_model.graph.input[0].type.tensor_type.shape.dim]))
print("[Graph Output] name: {}, shape: {}".format(onnx_model.graph.output[0].name,
[dim.dim_value for dim in onnx_model.graph.output[0].type.tensor_type.shape.dim]))
print(onnx.helper.printable_graph(onnx_model.graph))
sess = rt.InferenceSession("Test.onnx")
打印出来的图是
graph torch-jit-export (
%input.1[DOUBLE, 1x2]
) initializers (
%0.weight[DOUBLE, 228x2]
%0.bias[DOUBLE, 228]
%2.weight[DOUBLE, 70x228]
%2.bias[DOUBLE, 70]
%4.weight[DOUBLE, 1x70]
%4.bias[DOUBLE, 1]
) {
%7 = Gemm[alpha = 1, beta = 1, transB = 1](%input.1, %0.weight, %0.bias)
%8 = LeakyRelu[alpha = 0.00999999977648258](%7)
%9 = Gemm[alpha = 1, beta = 1, transB = 1](%8, %2.weight, %2.bias)
%10 = LeakyRelu[alpha = 0.00999999977648258](%9)
%11 = Gemm[alpha = 1, beta = 1, transB = 1](%10, %4.weight, %4.bias)
return %11
}
我想知道是否已经实现了leakyrelu,或者我只是在转换中错过了一些东西。感谢您的帮助!