見出し画像

ML.netでModel Builderを使ってみる その3

何とかMl.ConsoleAPをC#のクラスライブラリーに移植します。
移植さえ出来れば、このプロジェクトは削除しても問題ありません。

何とかMl.Modelはそのまま使いたいと思います。
こちらはプロジェクトのコンパイル部分をx64にしておきます。

C#のクラスライブラリーも参照を変更して、何とかML.Moddelを含めるようにします。
特に問題は無いと思います。

それから、何とかMl.ConsoleAPのModelBuilder.csをC#のクラスライブラリーにコピーします。
何とかMl.ConsoleAPは不要なので削除してしまいます。

C#で書いているクラスライブラリー部分を先程削除した何とかMl.ConsoleAPのProgram.csを真似して作ります。
ここまでで、今日の学習は終了とします。

このコード、以下のWebアドレスに掲載されているものを基本にしています。
https://docs.microsoft.com/ja-jp/dotnet/machine-learning/

印刷して学びたい場合もPDFファイルがあるので参考になります。
https://docs.microsoft.com/ja-jp/dotnet/opbuildpdf/machine-learning/toc.pdf?branch=live


#学習 #勉強 #AI #機械学習 #プログラミング #Visual #CSharp #BASIC #ソースコード #Windows #自動判定
#画像 #判定 #検査


ソースコードは以下のようになります。

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using Microsoft.ML;
using static Microsoft.ML.DataOperationsCatalog;
using Microsoft.ML.Vision;

namespace ClassLibrary1
{

public class Class1
{

public void SampleMachineLearning()
{
// Create single instance of sample data from first line of dataset for model input
MlTestML.Model.ModelInput sampleData = new MlTestML.Model.ModelInput()
{
//以下のファイル、PCの環境によって変わります。
ImageSource = @" 省略 \assets\CD\7001-115.jpg",
};

// Make a single prediction on the sample data and print results
var predictionResult = MlTestML.Model.ConsumeModel.Predict(sampleData);

Console.WriteLine("Using model to make single prediction -- Comparing actual Label with predicted Label from sample data...\n\n");
Console.WriteLine($"ImageSource: {sampleData.ImageSource}");
Console.WriteLine($"\n\nPredicted Label value {predictionResult.Prediction} \nPredicted Label scores: [{String.Join(",", predictionResult.Score)}]\n\n");
Console.WriteLine("=============== End of process, hit any key to finish ===============");
Console.ReadKey();

}


}
}


この記事が気に入ったらサポートをしてみませんか?