ML.NET在不久前发行了1.0版本,在考虑这一新轮子的实际用途时,最先想到的是其能否调用已有的模型,特别是最被广泛使用的Tensorflow模型。于是在查找了不少资料后,有了本篇示例。希望可以有抛砖引玉之功。

环境

Tensorflow 1.13.1

Microsoft.ML 1.0.0

Microsoft.ML.TensorFlow 0.12.0

netcoreapp2.2

训练模型

这里为了方便,利用Keras的API减少所需的代码。

import tensorflow as tf
mnist = tf.keras.datasets.mnist (x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0 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']) model.fit(x_train, y_train, epochs=5)
model.evaluate(x_test, y_test)
model.save('model.h5')

得到的模型精度在98%以上,不错的结果。

检验模型

加载已训练的模型,用某一测试数据验证结果。

with CustomObjectScope({'GlorotUniform': glorot_uniform()}):
model = load_model('model.h5') data = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.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] pred = model.predict(np.array(data).reshape(1, 28, 28))
print(pred.argmax())

执行以上脚本得到的结果为2。

可以将结果更形象地显示出来。

plt.imshow(np.array(data).reshape(28, 28), cmap='Greys')
plt.show()

转换模型文件

Keras保存的文件格式是h5,并不能直接被ML.NET调用,所以需要先转换成pb格式。

方法是使用开源脚本——keras_to_tensorflow,直接调用如下命令即可完成转换。

python keras_to_tensorflow.py
--input_model="path/to/keras/model.h5"
--output_model="path/to/save/model.pb"

ML.NET

在ML.NET中调用已训练的模型可分为这样几步:

  1. 建立MLContext
  2. 加载模型文件
  3. 创建IDataView对象,用作Fit方法的传入参数
  4. 建立模型管道,这里是TensorFlowEstimator对象
  5. 调用Fit方法,获得TensorFlowTransformer对象
  6. 构建预测引擎,其输入与输出对象对应模型中的输入层与输出层
  7. 执行预测方法

所有代码如下所示:

class Program
{
static void Main(string[] args)
{
var mlContext = new MLContext();
var tensorFlowModel = mlContext.Model.LoadTensorFlowModel(@"D:\workspace\tensorflow\saved_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; }
}

如果不清楚模型中的网络结构,可以用TensorFlowModel的GetModelSchema方法获悉详细的情况。

调试代码,可以看到结果数组中index为2时,概率最大,所以可以认为最终的预测结果为2。与python脚本的执行结果是一致的。

最新文章

  1. 学习Spring(一) 实例化Spring IoC容器
  2. 正确运用synchronized和二次判断 实现多线程安全
  3. BZOJ1483——[HNOI2009]梦幻布丁
  4. 数组里的数据绑定到dataset中
  5. 解决window删除文件时提示: 源文件名长度大于系统支持的长度
  6. 2Sigma OA prepare: Longest Chain
  7. Grunt完成对LESS实时编译
  8. WPF的图片操作效果(一):RenderTransform
  9. VirtualBox:Fatal:Could not read from Boot Medium! System Halted解决措施
  10. POJ2735/Gym 100650E Reliable Nets dfs
  11. gridview header增加排序图标
  12. Eclipse 安装反编译插件jadclipse
  13. c#里面的namespace基础(一)
  14. POJ 1847 Tram dij
  15. 如何使用 Java 构建微服务?
  16. PHP自定义函数与字符串处理
  17. FPGA调试光纤模块
  18. jq手风琴效果
  19. 产品经理教你如何构建电商电销 CRM 系统
  20. mongo2csv

热门文章

  1. KeContextToKframes函数逆向
  2. MVC教程:MVC区域路由
  3. 合格的施工图是如何绘制的?必须要get这四点,大多数人都不知道
  4. CSS设置文本的水平对齐方向
  5. WebLogic任意文件上传漏洞复现与分析 -【CVE-2018-2894 】
  6. JavaScript初探 五
  7. /cygdrive/c/MinGW/bin/autoconf-2.68: line 501: /mingw/bin/autom4te-2.68: No such file or directory
  8. [转]Spring Cloud在国内中小型公司能用起来吗?
  9. MySQL 同一Windows系统上安装多个数据库
  10. [日常] 安装windows+deepin双系统