我正在尝试比较使用不同推理方法评估预构建张量流模型的性能。我目前在Ubuntu docker VM中提供了tensorflow服务half_plus_two
玩具模型,该模型会产生以下结果:
[GRPC:每秒约1700次预测
REST:大约每秒800次预测
我的最终用途应用程序(带有API的框架4.5)是一个C#环境。我想通过在最终用途应用程序中使用ML.NET预测/预测引擎,将ML.NET的性能与tensorflow服务的REST和GRPC进行比较。
saved_model
格式保存class TestProgram
{
static void Main(string[] args)
{
try
{
new ModelBuilder();
}
catch (Exception e)
{
// investigate
}
}
}
public class ModelBuilder
{
private readonly MLContext mlContext;
private string userDesktop = Environment.SpecialFolder.Desktop.ToString();
// feed these values to the pretrained tf model.
// expected results are 2.0, 3.0, 4.0, 5.0 respectively
private float[] testData = new float[] { 0.0f, 2.0f, 4.0f, 6.0f };
public ModelBuilder()
{
this.mlContext = new MLContext();
var tfPretrainedModel = this.mlContext.Model.LoadTensorFlowModel(Path.Combine(userDesktop, @"TF\half_plus_two\1\saved_model.pb"));
var predictionFunction = this.mlContext.Model.CreatePredictionEngine<HalfPlusTwoData, HalfPlusTwoPrediction>(tfPretrainedModel);
HalfPlusTwoPrediction prediction = null;
for (int i = 0; i < this.testData.Length; i++)
{
prediction = predictionFunction.Predict(new HalfPlusTwoData() { Input = this.testData[i] });
Console.WriteLine($"Input {this.testData[i]}, Prediction {prediction.Prediction}, Expected {(this.testData[i] / 2) + 2}");
}
}
}
public class HalfPlusTwoData
{
[LoadColumn(0), ColumnName("Label")]
public float Input;
}
public class HalfPlusTwoPrediction
{
[ColumnName("PredictedLabel")]
public float Prediction { get; set; }
}
LoadTensorFlowModel
创建模型或用于创建管道的正确方法是什么?HalfPlusTwoData
是构造输入数据的正确方法吗?阅读文档后,您应该从该方法中获得一个TensorFlowModel
对象。
MS Docs:
TensorFlowModel tensorFlowModel = mlContext.Model.LoadTensorFlowModel(_modelPath);
至于您的2个班级,就是这样做的方式。但是,您通常会有一些数据过时,因此应该包括在第一类中。与您的预测类类似,它应产生额外的列,例如得分和概率。但是请注意,只有某些模型具有与二元分类模型一样的概率。
MS Docs(情感分析):
public class SentimentData
{
[LoadColumn(0)]
public string SentimentText;
[LoadColumn(1), ColumnName("Label")]
public bool Sentiment;
}
public class SentimentPrediction : SentimentData
{
[ColumnName("PredictedLabel")]
public bool Prediction { get; set; }
public float Probability { get; set; }
public float Score { get; set; }
}
忽略它们具有不同的列号。这完全基于您的数据设置方式。
链接:
封面输入和预测类布局:https://docs.microsoft.com/en-us/dotnet/machine-learning/tutorials/sentiment-analysis
特别是关于Tensorflow模型:https://docs.microsoft.com/en-us/dotnet/machine-learning/tutorials/text-classification-tf