Loading Tensorflow model in c# - c#

I am really struggling with the fact that i don't know how can I load and use a TensorFlow image classifier model in c#.
To be more precise I want to use a model trained in Teachable Machine that can be exported in multiple TensorFlow formats.
And if possible an example code will be really helpful.
I have tried to ask the same question but it got closed, I really need to find out how can I load and use the model so please let the question open. Thanks a lot for the support.

If you need only to evaluate model in C# code, then you need some lightweight library that can be used as loader and evaluator for your network.
There are many good libraries to work with tensorflow models in C#, like:
TensorFlowSharp
TensorFlow.NET
Unfortunately, all existing libraries has pre-defined logic for training model too, so that is a little ballast for your task.
To accomplish your purposes, please see this detailed article about how to load TF model into your C# application using TensorFlowSharp.

I used TensorFlowSharpin the past, but that library is still stuck with TensorFlow 1.x, and development seems quite dead. Because of that, I moved on to ML.NET.
Please see this article about how to use ML.NET with C# (disclaimer: I was the author of that article). Below is the gist of it.
Convert saved_model to onnx
Install tf2onnx.
pip install tf2onnx
Use it to convert saved_model to onnx.
python -m tf2onnx.convert --saved-model <path to saved_model folder> --output "model.onnx"
Use ML.NET to make prediction
Install the necessary packages.
dotnet add package Microsoft.ML
dotnet add package Microsoft.ML.OnnxRuntime
dotnet add package Microsoft.ML.OnnxTransformer
Define the input and output model classes.
public class Input
{
[VectorType(<input size>)]
[ColumnName("<input node name>")]
public float[] Data { get; set; } // Remember to change the type if your model does not use float
}
public class Output
{
[VectorType(<output size>)]
[ColumnName("<output node name>")]
public float[] Data { get; set; } // Remember to change the type if your model does not use float
}
Load the model in onnx format.
using Microsoft.ML;
var modelPath = "<path to onnx model>";
var outputColumnNames = new[] { "<output name>" };
var inputColumnNames = new[] { "<input name>" };
var mlContext = new MLContext();
var pipeline = _mlContext.Transforms.ApplyOnnxModel(outputColumnNames, inputColumnNames, modelPath);
Make prediction
var input_data= ... // code to load input into an array
var input = new Input { Data = data };
var dataView = mlContext.Data.LoadFromEnumerable(new[] { input });
var transformedValues = pipeline.Fit(dataView).Transform(dataView);
var output = mlContext.Data.CreateEnumerable<Output>(transformedValues, reuseRowObject: false);
// code to use the result in output

Related

Cannot load model using WinML

As WinML is still fairly new and ever changing, I was hoping to know if anyone else has come across this error when trying to load a ONNX file made via the Custom Vision Service Export.
Type Error: Type (map(string,tensor(float))) of output arg (loss) of node (ZipMap) does not match expected type (seq(map(string,tensor(float))))
using Windows.AI.MachineLearning;
Windows RS5 1809 (build 17763.1)
UWP SDK 17763
Testing code from this link:
https://learn.microsoft.com/en-us/uwp/api/windows.ai.machinelearning
// Load and create the model
var modelFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri($"ms-appx:///{_modelFileName}"));
_model = await LearningModel.LoadFromStorageFileAsync(modelFile);
Any help would be greatly appreciated.
Windows build 17763 (RS5) requires ONNX version 1.2 when Windows.AI.MachineLearning.
The Azure Custom Vision service supports this today .
Here is a post around a sample app that does this.
As a note, you need to make sure your output data type matches when you bind. In c# this looks something like this:
public sealed class ModelOutput
{
public TensorString ClassLabel = TensorString.Create(new long[] { 1, 1 });
public IList<IDictionary<string, float>> Loss = new List<IDictionary<string, float>>();
}
I received info from a Microsoft associate that stated that the windows.ai.machinelearning api only supports ONNX 2. He didn't supply a date to which we can expect the custom vision service to export version 2 files.

Octopus client, getting version from project name in C#

First of, I am completely new to octopus client, used it for the first time just before posting this.
So, I've been landed with this project to update the version number on a webpage monitoring some of our octopus deployed projects. I have been looking around the octopus client and not really gotten anywhere. The best I have so far is:
OctopusServerEndpoint endPoint = new OctopusServerEndpoint(server, apiKey);
OctopusRepository repo = new OctopusRepository(endPoint);
var releases = repo.Releases.FindAll();
From these releases I can get the ProjectId and even the Version, the issue is that releases is 600 strong and I am only looking for 15 of them.
The existing code I have to work from used to parse the version from local files so that is all out the window. Also, the existing code only deals with the actual names of the projects, like "AWOBridge", not their ProjectId, which is "Projects-27".
Right now my only option is to manually write up a keyList or map to correlate the names I have with the IDs in the octopus client, which I of course rather not since it is not very extendable or good code practice in my opinion.
So if anyone has any idea on how to use the names directly with octopus client and get the version number from that I would very much appriciate it.
I'll be getting down into octopus client while waiting. Let's see if I beat you to it!
Guess I beat you to it!
I'll just leave an answer here if anyone ever has the same problem.
I ended up using the dashboardto get what I needed:
OctopusServerEndpoint endPoint = new OctopusServerEndpoint(server, apiKey);
OctopusRepository repo = new OctopusRepository(endPoint);
DashboardResource dash = repo.Dashboards.GetDashboard();
List<DashboardItemResource> items = dash.Items;
DashboardItemResource item = new DashboardItemResource();
List<DashboardProjectResource> projs = dash.Projects;
var projID = projs.Find(x => x.Name == projectName).Id;
item = items.Find(x => x.ProjectId == projID && x.IsCurrent == true);
The dashboard is great since it contains all the info that the web dashboard shows. So you can use Project, Release, Deployment and Environment with all the information they contain.
Hope this helps someone in the future!
I'm using LINQPad to run C# snippets for Octopus automation using the Octopus Client library and I have come up with following to get any version of a project making use of Regular expression pattern. It works quite well if you use Pre-release semantic versioning.
For example to get latest release for a project:
var project = Repo.Projects.FindByName("MyProjectName");
var release = GetReleaseForProject(project);
To get specific release use that has 'rc1' in the version for example (also useful if you use source code branch name in the version published to Octopus:
var release = GetReleaseForProject(project, "rc1");
public ReleaseResource GetReleaseForProject(ProjectResource project, string versionPattern = "")
{
// create compiled regex expression to use for search
var regex = new Regex(versionPattern, RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
var releases = Repo.Projects.GetReleases(project);
if (!string.IsNullOrWhiteSpace(versionPattern) && !releases.Items.Any(r => regex.IsMatch(r.Version)))
{
return null;
}
return (!string.IsNullOrWhiteSpace(versionPattern)) ? releases.Items.Where(r => regex.IsMatch(r.Version))?.First() : releases.Items?.First();;
}

implementing maxmind's free GeoLiteCity DB on a C# webform?

Has anyone successfully utilized MaxMind's data and C# code on a webform? I would appreciate any insight. If you can provide a walk through of the implementation process and perhaps some sample C# code that works for you, I would truly appreciate it.
Implementing max-mind is very simple, in C# we have Nuget package for max-mind Db reader
Install-Package MaxMind.GeoIP2 -Version 2.6.0
After that you can use max-mind db reader to read your db.
Finally take the read data as an object and obtain the details you want.
Example:
string path = #"c:\MaxMindDb\GeoIP2-City.mmdb";
using (var reader = new Reader(path, MaxMind.Db.FileAccessMode.Memory))
{
var data = reader.Find(ip).ToString();
var GeoData= JToken.Parse(data);
string CountryCode = GeoData["continent"]["code"].ToString() ?? null;
...

Display 3D using HelixToolkit

Is there any code that I can use to display my 3D model ? , I Tried using HelixToolkit but i don't know how to use it, can anyone show me with a piece of code to just load a 3D Obj file
I'm assuming you've used Nuget to download the Helix3D libraries, but if you download the source code from GitHub then this includes an extensive ExampleBrowser application that gives guidance of how to use Helix3D
Helix3D on GitHub
My application is structured as follows:
XAML
<h:HelixViewport3D x:Name="HelixViewport3D" ShowViewCube="False">
<h:DefaultLights/>
<ModelVisual3D x:Name="MyModel"/>
</h:HelixViewport3D>
C#
var scene = new Model3DGroup();
scene.Add(Load3dObject(#"C:\filename.obj"));
MyModel.Content = scene;
Which calls this helper function Load3dObjectI created to help clean up the code.
private Model3D Load3dObject(string objName)
{
var reader = new ObjReader();
var modelGroup = reader.Read(objName);
return modelGroup;
}

C# code completion with NRefactory 5

I just found out about NRefactory 5 and I would guess, that it is the most suitable solution for my current problem. At the moment I'm developing a little C# scripting application for which I would like to provide code completion. Until recently I've done this using the "Roslyn" project from Microsoft. But as the latest update of this project requires .Net Framework 4.5 I can't use this any more as I would like the app to run under Win XP as well. So I have to switch to another technology here.
My problem is not the compilation stuff. This can be done, with some more effort, by .Net CodeDomProvider as well. The problem ist the code completion stuff. As far as I know, NRefactory 5 provides everything that is required to provide code completion (parser, type system etc.) but I just can't figure out how to use it. I took a look at SharpDevelop source code but they don't use NRefactory 5 for code completion there, they only use it as decompiler. As I couldn't find an example on how to use it for code completion in the net as well I thought that I might find some help here.
The situation is as follows. I have one single file containing the script code. Actually it is not even a file but a string which I get from the editor control (by the way: I'm using AvalonEdit for this. Great editor!) and some assemblies that needs to get referenced. So, no solution files, no project files etc. just one string of source code and the assemblies.
I've taken a look at the Demo that comes with NRefactory 5 and the article on code project and got up with something like this:
var unresolvedTypeSystem = syntaxTree.ToTypeSystem();
IProjectContent pc = new CSharpProjectContent();
// Add parsed files to the type system
pc = pc.AddOrUpdateFiles(unresolvedTypeSystem);
// Add referenced assemblies:
pc = pc.AddAssemblyReferences(new CecilLoader().LoadAssemblyFile(
System.Reflection.Assembly.GetAssembly(typeof(Object)).Location));
My problem is that I have no clue on how to go on. I'm not even sure if it is the right approach to accomplish my goal. How to use the CSharpCompletionEngine? What else is required? etc. You see there are many things that are very unclear at the moment and I hope you can bring some light into this.
Thank you all very much in advance!
I've just compiled and example project that does C# code completion with AvalonEdit and NRefactory.
It can be found on Github here.
Take a look at method ICSharpCode.NRefactory.CSharp.CodeCompletion.CreateEngine. You need to create an instance of CSharpCompletionEngine and pass in the correct document and the resolvers. I managed to get it working for CTRL+Space compltition scenario. However I am having troubles with references to types that are in other namespaces. It looks like CSharpTypeResolveContext does not take into account the using namespace statements - If I resolve the references with CSharpAstResolver, they are resolved OK, but I am unable to correctly use this resolver in code completition scenario...
UPDATE #1:
I've just managed to get the working by obtaining resolver from unresolved fail.
Here is the snippet:
var mb = new DefaultCompletionContextProvider(doc, unresolvedFile);
var resolver3 = unresolvedFile.GetResolver(cmp, loc); // get the resolver from unresolvedFile
var engine = new CSharpCompletionEngine(doc, mb, new CodeCompletionBugTests.TestFactory(resolver3), pctx, resolver3.CurrentTypeResolveContext );
Update #2:
Here is the complete method. It references classes from unit test projects, sou you would need to reference/copy them into your project:
public static IEnumerable<ICompletionData> DoCodeComplete(string editorText, int offset) // not the best way to put in the whole string every time
{
var doc = new ReadOnlyDocument(editorText);
var location = doc.GetLocation(offset);
string parsedText = editorText; // TODO: Why there are different values in test cases?
var syntaxTree = new CSharpParser().Parse(parsedText, "program.cs");
syntaxTree.Freeze();
var unresolvedFile = syntaxTree.ToTypeSystem();
var mb = new DefaultCompletionContextProvider(doc, unresolvedFile);
IProjectContent pctx = new CSharpProjectContent();
var refs = new List<IUnresolvedAssembly> { mscorlib.Value, systemCore.Value, systemAssembly.Value};
pctx = pctx.AddAssemblyReferences(refs);
pctx = pctx.AddOrUpdateFiles(unresolvedFile);
var cmp = pctx.CreateCompilation();
var resolver3 = unresolvedFile.GetResolver(cmp, location);
var engine = new CSharpCompletionEngine(doc, mb, new CodeCompletionBugTests.TestFactory(resolver3), pctx, resolver3.CurrentTypeResolveContext );
engine.EolMarker = Environment.NewLine;
engine.FormattingPolicy = FormattingOptionsFactory.CreateMono();
var data = engine.GetCompletionData(offset, controlSpace: false);
return data;
}
}
Hope it helps,
Matra
NRefactory 5 is being used in SharpDevelop 5. The source code for SharpDevelop 5 is currently available in the newNR branch on github. I would take a look at the CSharpCompletionBinding class which has code to display a completion list window using information from NRefactory's CSharpCompletionEngine.

Categories

Resources