#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

#Coding4Fun – Sorting Arrays in #JavaScript, #Python and #CSharp … choose your own adventure

Buy Me A Coffee

Hi !

Not my usual set of posts, however this keep me thinking a lot. It all started with this tweet; an usual joke about JavaScript, and how it works with types.

array sorting in Javascript

I noticed that some responses focused on the “you need to know how the language works” before posting this. And it is correct, you need to know how JavaScript converts the elements into strings and then sort, to understand the crappy not so accurate output.

However, I am not a JavaScript expert, and I do not want to be one. And, when I use a tool or a programming language, I expect to have a nice learning curve; the previous example is not a nice “Welcome to JavaScript, where arrays and type conversions will make you think twice until you get it!”.

I’ve been doing a lot of Python programming, so I did the same exercise, and it worked as you may expect !

array and sort in python

I also did a similar in C#, and it also worked !

array and sort in C#

Do not get me wrong, this is not an Anti-JavaScript post. JavaScript is awesome and is almost everywhere. I know a lot of awesome JavaScript programmers, who I really admire.

However as a developer, I expect to avoid something simple like an array sort issue. I will always focus on “the code is clean and everyone will understand how it works, just read the code!”.

I am used to this as a C# developer; and I also found that Python is cool for this. But, sometimes, just sometimes, JavaScript looks like hell !

Bonus: An old post in my last JavaScript adventure.

Happy coding!

Greetings

El Bruno

#Net – Learning C # using only a browser

Hi!

A few days ago, at a university, one of the attendees in a session asked me what was the best way to learn C#. As always, this question depends on the level of the person who wants to learn, although it has served as an excuse to see what’s new in the .Net ecosystem.

Well, in addition to the (now) classic Microsoft Docs, I found a resource that is rather interesting

.NET In Browser tutorial

The format is rather simple, a small tutorial on basic concepts of C #, a web code editor and the chance to compile and see the result on the same web.

I1

As entry point for concepts such as handling of strings, arrays, etc. it seems like a good starting point to start.

Greetings @ Burlington

El Bruno

References

#Net – Aprendiendo C# utilizando solo un navegador

Buenas!

Hace unos dĂ­as, en una universidad, uno de los asistentes en una sesiĂłn me pregunto cual era la mejor forma de aprender C#. Como siempre esta pregunta depende del nivel de la persona que quiere aprender, aunque me ha servido de excusa para ver que hay de nuevo en el ecosistema .Net.

Pues bien, además de los clásicos Docs, he encontrado un recurso que es más bien interesante

.NET In Browser tutorial

El formato es mas bien simple, un pequeño tutorial sobre conceptos básicos de C#, un editor web de código y la capacidad de compilar y ver el resultado en la misma web.

I1

Como punto de entrada para conceptos como manejo de strings, arrays, etc. me parece un buen punto de partida para comenzar.

Saludos @ Burlington

El Bruno

References