前回、C#からTensorFlowが使えるTensorFlow.NETというライブラリを試したが、まだ実装されていない機能があるので、他にTensorFlowをフルで使えるライブラリを探すことにした。
TensorFlowSharpが十分な機能が実装されているので、.NET Coreへポーティングすれば使えそうだ。
.NET Core 2.0にポーティングしている人もいるようだ。
https://github.com/jcapellman/TensorFlowSharp
ベースにしているコミットが少し古いため最新のコミットを反映するには作業が必要になりそう。
他にないか調べたところ、MicrosoftがリリースしているML.NETでもTensorFlowが使えることがわかった。
ただし、モデルはPythonでTensorFlowを使って記述して、.pb形式で保存したモデルを読み込む必要がある。
少し手順が煩雑になるが、その代わりTensorFlowの機能をフルに使ってモデルを記述できる。
ということで、ML.NETからTensorFlowを使ってみた。
モデル定義
まずはPythonでモデルを定義する。
今回はKerasで定義した。
import tensorflow as tf model = tf.keras.models.Sequential([ tf.keras.layers.Flatten(input_shape=(28, 28)), tf.keras.layers.Dense(512, activation=tf.nn.relu), tf.keras.layers.Dropout(0.2), tf.keras.layers.Dense(10, activation=tf.nn.softmax) ]) model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) def model_to_pb(model, output_dir="./", output_graph_name='model.pb', output_names=["dense_1/Softmax"]): ksess = tf.keras.backend.get_session() constants_graph = tf.graph_util.convert_variables_to_constants(ksess, ksess.graph.as_graph_def(), output_names) tf.io.write_graph(constants_graph, output_dir, output_graph_name, as_text=False) print(output_names) model_to_pb(model)
実行すると、グラフ定義と初期パラメータが、model.pbに保存される。
.NET Coreでプロジェクト作成
mkdir MLDotNetMNIST cd MLDotNetMNIST dotnet new console dotnet add package Microsoft.ML dotnet add package Microsoft.ML.TensorFlow
C#のコード
model.pbを読み込んでMNISTのサンプルデータを学習する。
Program.cs
using System; using System.Linq; using Microsoft.ML; using Microsoft.ML.Data; namespace MLDotNetMNIST { class Program { static void Main(string[] args) { var mlContext = new MLContext(); var tensorFlowModel = mlContext.Model.LoadTensorFlowModel(@"model.pb"); //var schema = tensorFlowModel.GetModelSchema(); var data = GetTensorData(); var idv = mlContext.Data.LoadFromEnumerable(data); var pipeline = tensorFlowModel.ScoreTensorFlowModel( new[] { "dense_1/Softmax" }, new[] { "flatten_input" }, addBatchDimensionInput: true); var model = pipeline.Fit(idv); var engine = mlContext.Model.CreatePredictionEngine<TensorData, OutputScores>(model); var result = engine.Predict(data[0]); var maxValue = result.Output.Max(); var maxIndex = result.Output.ToList().IndexOf(maxValue); Console.WriteLine(maxIndex); } private static TensorData[] GetTensorData() { var data = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.027450980392156862, 0.09411764705882353, 0.5019607843137255, 0.5450980392156862, 0.5411764705882353, 0.7490196078431373, 0.7058823529411765, 0.9921568627450981, 0.7490196078431373, 0.5411764705882353, 0.18823529411764706, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.16862745098039217, 0.1843137254901961, 0.47058823529411764, 0.7294117647058823, 0.9882352941176471, 0.9882352941176471, 0.9921568627450981, 0.9882352941176471, 0.9882352941176471, 0.9882352941176471, 0.9882352941176471, 0.9921568627450981, 0.9882352941176471, 0.8901960784313725, 0.11372549019607843, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.07450980392156863, 0.6431372549019608, 0.9647058823529412, 0.9921568627450981, 0.9882352941176471, 0.9882352941176471, 0.8901960784313725, 0.7176470588235294, 0.7215686274509804, 0.6352941176470588, 0.27058823529411763, 0.27058823529411763, 0.27058823529411763, 0.30980392156862746, 0.8901960784313725, 0.9882352941176471, 0.17647058823529413, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.27450980392156865, 0.9882352941176471, 0.9882352941176471, 0.9921568627450981, 0.9215686274509803, 0.30196078431372547, 0.11372549019607843, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.03529411764705882, 0.7607843137254902, 0.8901960784313725, 0.11372549019607843, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.027450980392156862, 0.2549019607843137, 0.5372549019607843, 0.788235294117647, 0.6823529411764706, 0.12549019607843137, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.7098039215686275, 0.9882352941176471, 0.7176470588235294, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.03529411764705882, 0.5019607843137255, 1.0, 0.9764705882352941, 0.45098039215686275, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.4470588235294118, 0.9882352941176471, 0.9921568627450981, 0.5176470588235295, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.5254901960784314, 0.9411764705882353, 0.9882352941176471, 0.47843137254901963, 0.09803921568627451, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.03529411764705882, 0.6509803921568628, 0.9411764705882353, 0.9882352941176471, 0.6588235294117647, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.20784313725490197, 0.7098039215686275, 0.9882352941176471, 0.9882352941176471, 0.4549019607843137, 0.00784313725490196, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.027450980392156862, 0.25882352941176473, 0.9529411764705882, 1.0, 0.9764705882352941, 0.24705882352941178, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.2, 0.7294117647058823, 0.9882352941176471, 0.9882352941176471, 0.8549019607843137, 0.29411764705882354, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.19215686274509805, 0.8941176470588236, 0.9882352941176471, 0.9882352941176471, 0.8666666666666667, 0.12549019607843137, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.5333333333333333, 0.9137254901960784, 0.9882352941176471, 0.8901960784313725, 0.4666666666666667, 0.09803921568627451, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.12549019607843137, 0.8235294117647058, 0.9803921568627451, 0.9921568627450981, 0.9058823529411765, 0.18823529411764706, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.34901960784313724, 0.8705882352941177, 0.9921568627450981, 0.9921568627450981, 0.6196078431372549, 0.0, 0.0, 0.0, 0.043137254901960784, 0.13333333333333333, 0.4627450980392157, 0.027450980392156862, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.6313725490196078, 0.9882352941176471, 0.9882352941176471, 0.41568627450980394, 0.0, 0.03529411764705882, 0.1843137254901961, 0.34901960784313724, 0.796078431372549, 0.9921568627450981, 0.9568627450980393, 0.2196078431372549, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.6313725490196078, 0.9882352941176471, 0.9882352941176471, 0.7450980392156863, 0.7254901960784313, 0.7725490196078432, 0.9882352941176471, 0.9882352941176471, 0.8666666666666667, 0.6784313725490196, 0.2196078431372549, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.47058823529411764, 0.9882352941176471, 0.9882352941176471, 0.9882352941176471, 0.9921568627450981, 0.9882352941176471, 0.9882352941176471, 0.9882352941176471, 0.3764705882352941, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0196078431372549, 0.21176470588235294, 0.5372549019607843, 0.5372549019607843, 0.7450980392156863, 0.5372549019607843, 0.21176470588235294, 0.08627450980392157, 0.00784313725490196, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}; return new TensorData[] { new TensorData() { Input = data.Select(x => (float)x).ToArray() } }; } } public class TensorData { [ColumnName("flatten_input")] [VectorType(28, 28)] public float[] Input { get; set; } } class OutputScores { [ColumnName("dense_1/Softmax")] [VectorType(10)] public float[] Output { get; set; } } }
ソースは以下のサイトを参考にした。
https://www.cnblogs.com/kenwoo/p/10902431.html
損失関数がScoreTensorFlowModelに隠ぺいされていて、メソッド名も直感的にわかりにくい。
Softmax交差エントロピー以外の損失関数にするにはどうするかとか、損失の値を表示するにはどうするかも不明。