Sdcb.Paddle2Onnx is an open-source project that provides C#/.NET wrapper for Paddle2Onnx, a tool to convert PaddlePaddle models to ONNX models. It allows users to convert PaddlePaddle models to ONNX models and use them in various deep learning frameworks.
NOTE: This project is only supported in Windows.
To use Sdcb.Paddle2Onnx, you need to install .NET standard 2.0 or later.
To install Sdcb.Paddle2Onnx, you can use the following command in the Package Manager Console:
Install-Package Sdcb.Paddle2Onnx
Install-Package Sdcb.Paddle2Onnx.runtime.win64
NuGet Package 💼 | Version 📌 | Description 📚 |
---|---|---|
Sdcb.Paddle2Onnx | Paddle2Onnx C++ API .NET binding | |
Sdcb.Paddle2Onnx.runtime.win64 | win-x64 Paddle2Onnx runtime |
To check if a PaddlePaddle model can be converted to an ONNX model, you can use the Paddle2OnnxConverter.CanConvert
method. You can provide either the file paths or the byte arrays of the model and the parameter files.
string modelFile = "model.pdmodel";
string paramsFile = "model.pdiparams";
bool can = Paddle2OnnxConverter.CanConvert(modelFile, paramsFile);
To convert a PaddlePaddle model to an ONNX model, you can use the Paddle2OnnxConverter.ConvertToOnnx
method. You can provide either the file paths or the byte arrays of the model and the parameter files.
byte[] modelBuffer = File.ReadAllBytes("model.pdmodel");
byte[] paramsBuffer = File.ReadAllBytes("model.pdiparams");
byte[] onnxModel = Paddle2OnnxConverter.ConvertToOnnx(modelBuffer, paramsBuffer);
To describe a PaddlePaddle model, you can use the Paddle2OnnxConverter.DescribePaddleModel
method. You need to provide the byte array of the model file.
byte[] modelBuffer = File.ReadAllBytes("model.pdmodel");
PaddleModelInfo info = Paddle2OnnxConverter.DescribePaddleModel(modelBuffer);
Console.WriteLine($"Input shapes: {string.Join(", ", info.InputShapes)}");
Console.WriteLine($"Output shapes: {string.Join(", ", info.OutputShapes)}");
To describe an ONNX model, you can use the Paddle2OnnxConverter.DescribeOnnxModel
method. You need to provide the byte array of the ONNX model.
OnnxModelInfo info = Paddle2OnnxConverter.DescribeOnnxModel(onnxModel);
Console.WriteLine("Input shapes:");
Console.WriteLine(string.Join("\n", info.Inputs.Select(x => x.ToString())));
Console.WriteLine();
Console.WriteLine("Output shapes:");
Console.WriteLine(string.Join("\n", info.Outputs.Select(x => x.ToString())));
To use custom operators when converting a PaddlePaddle model to an ONNX model, you can provide an array of CustomOp
objects to the Paddle2OnnxConverter.CanConvert
method.
CustomOp[] ops = new[]
{
new CustomOp("fc", "FC"),
new CustomOp("softmax", "Softmax")
};
bool can = Paddle2OnnxConverter.CanConvert(modelFile, paramsFile, customOps: ops);
To use a different backend when converting a PaddlePaddle model to an ONNX model, you can provide the name of the backend to the Paddle2OnnxConverter.CanConvert
method. Currently, the only supported backend is TensorRT.
bool can = Paddle2OnnxConverter.CanConvert(modelFile, paramsFile, deployBackend: "tensorrt");