#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


References

#AI – #Lobe, desktop tool to train custom machine learning models for Computer Vision @lobe_ai

Buy Me A Coffee

Hi !

There are 2 ways to describe Lobe. You may want to use their official description

Lobe helps you train machine learning models with a free, easy to use tool.

Lobe AI Homepage

Or you may want to use the tool. Let’s review the 2nd one with a NNNN step tutorial.

Data Source Images

For this tutorial I’ll use a set of 24 drawings for fish, human and flowers. I have 8 drawings for each category in different folders.

Import and label Images

Ok, you probably have already installed the app, so let’s create a new project. I’ll name this project drawings.

lobe 02 new project name drawings

The import button, will display a set of options. For this demo, I’ll go to DataSet, where I need to have all my images in a structured set of folders.

The import option also describes how to organize the images in folder.

Once we import the images, we will have the option to automatically label our images based on the folder name

Train

This is the most user friendly step, we don’t need to train the model. I mean, once we have our images, Lobe will automatically start the training process in the background.

Depending the size of your training dateset, this may take some time. Once the model is trained, we can also see some output around the correct and incorrect predictions.

Test with new images

This is also cool. Once the model is trained we can test is in the Play section. In example, I’ve uploaded a custom fish model, and it’s predicted as a fish. I can confirm or assign the correct label in the Play area.

I’ve uploaded a couple more test images, and the background training process created a perfect model !!! (I know, I know …)

lobe 09 100 acurracy

Export generated model

And another great feature is the Export option.

lobe 10 export options

There are several options:

  • TensorFlow
  • TensorFlow Lite
  • Local API

I haven’t used options 2 and 3, however option 1 is good enough to play around. It will include the TensorFlow frozen model, some supporting files, and a sample to use this in Python !

Super easy to start and learn without coding !

Happy coding!

Greetings

El Bruno


References