Skip to content

nietras/OnnxSharp

Repository files navigation

Build and test Stars License

What Links and Status
OnnxSharp NuGet Downloads
dotnet-onnx NuGet Downloads

OnnxSharp library and dotnet-onnx tool

ONNX format parsing and manipulation in C# and with command line .NET tool.

Quick Guide

Install latest version of .NET:

Code

What How
Install dotnet add PROJECT.csproj package OnnxSharp
Parse var model = ModelProto.Parser.ParseFromFile("mnist-8.onnx");
Info var info = model.Graph.Info();
Clean model.Graph.Clean();
SetDim model.Graph.SetDim();
Write model.WriteToFile("mnist-8-clean-dynamic.onnx");

Tool

What How
Install dotnet tool install dotnet-onnx -g
Info dotnet onnx info mnist-8.onnx
Info dotnet onnx info mnist-8.onnx
Clean dotnet onnx clean mnist-8.onnx mnist-8-clean.onnx
SetDim dotnet onnx setdim mnist-8.onnx mnist-8-setdim.onnx

Source Code

Base functionality is based on:

.\protoc.exe .\onnx.proto3 --csharp_out=OnnxSharp

Everything else written in beautiful C# 9.0 as extensions to this.

Example Code

using Onnx;

// Examples see https://github.com/onnx/models
var onnxInputFilePath = @"mnist-8.onnx";

var model = ModelProto.Parser.ParseFromFile(onnxInputFilePath);

var graph = model.Graph;
// Clean graph e.g. remove initializers from inputs that may prevent constant folding
graph.Clean();
// Set dimension in graph to enable dynamic batch size during inference
graph.SetDim(dimIndex: 0, DimParamOrValue.New("N"));
// Get summarized info about the graph
var info = graph.Info();

System.Console.WriteLine(info);

model.WriteToFile(@"mnist-8-clean-dynamic-batch-size.onnx");