Skip to content

Commit

Permalink
Create README.md and update project properties for nuget package (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
nietras authored Jan 24, 2021
1 parent d75063e commit 2600ea8
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 46 deletions.
3 changes: 2 additions & 1 deletion OnnxSharp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ VisualStudioVersion = 16.0.30907.101
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OnnxSharp", "src\OnnxSharp\OnnxSharp.csproj", "{226093F1-29E7-477D-B7D6-9662B94A41D4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OnnxSharpConsole", "src\OnnxSharpConsole\OnnxSharpConsole.csproj", "{1EC63E50-3866-4148-BCC1-54561A564D38}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OnnxSharpConsole", "src\OnnxSharpConsole\OnnxSharpConsole.csproj", "{1EC63E50-3866-4148-BCC1-54561A564D38}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "root", "root", "{604A7FA2-17D1-4B9E-B1D3-15F186389B0B}"
ProjectSection(SolutionItems) = preProject
Expand All @@ -18,6 +18,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "root", "root", "{604A7FA2-1
LICENSE = LICENSE
nuget.config = nuget.config
pack.ps1 = pack.ps1
README.md = README.md
rename.ps1 = rename.ps1
restore.ps1 = restore.ps1
test.ps1 = test.ps1
Expand Down
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
![Build and test](https://github.com/nietras/OnnxSharp/workflows/.NET/badge.svg)
[![NuGet](https://img.shields.io/nuget/v/OnnxSharp)](https://www.nuget.org/packages/OnnxSharp/)
[![Downloads](https://img.shields.io/nuget/dt/OnnxSharp)](https://www.nuget.org/packages/OnnxSharp/)
[![Stars](https://img.shields.io/github/stars/nietras/OnnxSharp)](https://github.com/nietras/OnnxSharp/stargazers)
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE.md)

# OnnxSharp
ONNX format parsing and manipulation in C#.

# Status
Pretty much just:
```
.\protoc.exe .\onnx.proto3 --csharp_out=OnnxSharp
```

# Example Code
```csharp
using System.IO;
using System.Linq;
using Google.Protobuf;

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

using var file = File.OpenRead(onnxInputFilePath);

var model = Onnx.ModelProto.Parser.ParseFrom(file);

var graph = model.Graph;
var inputs = graph.Input;
var values = graph.ValueInfo;
var outputs = graph.Output;
```
18 changes: 16 additions & 2 deletions src/OnnxSharp/OnnxSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,23 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Description></Description>
</PropertyGroup>

<Description>ONNX format parsing and manipulation in C#.</Description>
<GenerateDocumentationFile>true</GenerateDocumentationFile>

<PackageIcon>packageIcon.png</PackageIcon>
<PackageIconFullPath>$(MSBuildThisFileDirectory)packageIcon.png</PackageIconFullPath>
<PackageTags>onnx</PackageTags>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/nietras/OnnxSharp</PackageProjectUrl>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<IncludeSymbols>true</IncludeSymbols>
</PropertyGroup>

<ItemGroup>
<None Include="packageIcon.png" Pack="true" PackagePath="" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Google.Protobuf" Version="3.14.0" />
<!--<PackageReference Include="Google.Protobuf.Tools" Version="3.14.0" />-->
Expand Down
Binary file added src/OnnxSharp/packageIcon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
77 changes: 34 additions & 43 deletions src/OnnxSharpConsole/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,45 @@
using System.Linq;
using Google.Protobuf;

namespace OnnxSharpConsole
{
class Program
{
static void Main(string[] args)
{
// Examples see https://github.com/onnx/models
var onnxInputFilePath = @"mnist-8.onnx";
// Examples see https://github.com/onnx/models
var onnxInputFilePath = @"mnist-8.onnx";

var onnxInputFileName = Path.GetFileNameWithoutExtension(onnxInputFilePath);
var outputDirectory = Path.GetDirectoryName(onnxInputFilePath);
var onnxInputFileName = Path.GetFileNameWithoutExtension(onnxInputFilePath);
var outputDirectory = Path.GetDirectoryName(onnxInputFilePath);

using (var file = File.OpenRead(onnxInputFilePath))
{
var model = Onnx.ModelProto.Parser.ParseFrom(file);
var graph = model.Graph;
// Shapes are defined in inputs, values and outputs
var inputs = graph.Input;
var values = graph.ValueInfo;
var outputs = graph.Output;
using (var file = File.OpenRead(onnxInputFilePath))
{
var model = Onnx.ModelProto.Parser.ParseFrom(file);
var graph = model.Graph;
// Shapes are defined in inputs, values and outputs
var inputs = graph.Input;
var values = graph.ValueInfo;
var outputs = graph.Output;

foreach (var value in inputs.Concat(values).Concat(outputs))
{
var shape = value.Type.TensorType.Shape;
var dims = shape.Dim;
var dim = dims[0];
//dim.DimValue = -1;
dim.ClearValue();
dim.DimValue = -1; // Or don't set it
//dim.DimParam = "None"; // Or don't set it, unset dimension means dynamic
}
foreach (var value in inputs.Concat(values).Concat(outputs))
{
var shape = value.Type.TensorType.Shape;
var dims = shape.Dim;
var dim = dims[0];
//dim.DimValue = -1;
dim.ClearValue();
dim.DimValue = -1; // Or don't set it
//dim.DimParam = "None"; // Or don't set it, unset dimension means dynamic
}

var fileNameSuffix = "-dynamic-leading-dimension";
var outputFilePathPrefix = Path.Combine(outputDirectory, onnxInputFileName + fileNameSuffix);
var fileNameSuffix = "-dynamic-leading-dimension";
var outputFilePathPrefix = Path.Combine(outputDirectory, onnxInputFileName + fileNameSuffix);

var onnxOutputFilePath = outputFilePathPrefix + ".onnx";
using (var outputFile = File.Create(onnxOutputFilePath))
{
model.WriteTo(outputFile);
}
var onnxOutputFilePath = outputFilePathPrefix + ".onnx";
using (var outputFile = File.Create(onnxOutputFilePath))
{
model.WriteTo(outputFile);
}

var jsonOnnxOutputFilePath = outputFilePathPrefix + ".json";
using (var output = new StreamWriter(jsonOnnxOutputFilePath))
{
var fmt = new JsonFormatter(JsonFormatter.Settings.Default);
fmt.Format(model, output);
}
}
}
var jsonOnnxOutputFilePath = outputFilePathPrefix + ".json";
using (var output = new StreamWriter(jsonOnnxOutputFilePath))
{
var fmt = new JsonFormatter(JsonFormatter.Settings.Default);
fmt.Format(model, output);
}
}

0 comments on commit 2600ea8

Please sign in to comment.