#AI – #Lobe, exporting to ONNX, and running in C# #csharp @lobe_ai

Buy Me A Coffee

Hi !

Follow up post after yesterday post on Lobe, and today focusing on ONNX and C# code. And, it all started because someone asked in twitter about an ETA to export the model to ONNX

I decided to give a try to the TensorFlow to Onnx tool, and it worked great ! (see references). I use the following command to convert my model

python -m tf2onnx.convert --saved-model model --output model.onnx

From the PB exported model from yesterday, and I got my 2 models

And, here I got an amazing surprise. Before I started to write some C# code, I found some NuGet packages available to use

  • lobe
  • lobe.Onnx
  • lobe.ImageSharp
lobe 100 install nuget packages

And, after a quick search I found some sample code in GitHub about how to use these packages. So, I pickup the original Code and make a few changes to perform estimations on 2 manual drawings.

Remember my model was trained to analyze drawings and detect: humans, fish and flowers.

I created a new C# Console App and

  • copy the generated [model.onnx]
  • copy the 2 test files: [fishy.png] and [human.png]
  • copy the original [signature.json] file generated on the Lobe TensorFlow export

I edited the [signature.json] file and change the values

  • format to onnx
  • filename to the generated exported filename

And I was ready to run my code:

using System;
using System.IO;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using lobe.ImageSharp;
using lobe;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
var signatureFilePath = "signature.json";
ImageClassifier.Register("onnx", () => new OnnxImageClassifier());
using var classifier = ImageClassifier.CreateFromSignatureFile(
new FileInfo(signatureFilePath));
// Images
ShowResults("fishy.png", classifier);
ShowResults("human.png", classifier);
}
private static void ShowResults(string imagePath, ImageClassifier classifier)
{
var results = classifier.Classify(Image
.Load(imagePath).CloneAs<Rgb24>());
Console.WriteLine();
Console.WriteLine($"Image : {imagePath}");
Console.WriteLine($"Top Label: {results.Classification.Label}");
foreach (var res in results.Classifications)
{
Console.WriteLine($" – Label: {res.Label} – Confidence: {res.Confidence}");
}
}
}
}

And the output is fast and great, as we are used to do with Onnx

Lobe looks great !

Happy coding!

Greetings

El Bruno

More posts in my blog ElBruno.com.

More info in https://beacons.ai/elbruno


References

Advertisement

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: