ML.NET tensorflow half_plus_two



我试图比较不同推理方法评估预构建tensorflow模型的性能。我目前在Ubuntu docker VM中使用tensorflow服务half_plus_two玩具模型,该模型产生以下结果:

GRPC:1700预测每秒

REST:~800预测每秒

我的最终用途应用程序(带有API的框架4.5(是一个C#环境。我想通过在我的最终用途应用程序中使用ML.NET预测/预测引擎,将ML.NET的性能与tensorflow服务的REST和GRPC进行比较。

环境

  • 模型已经过训练
  • 推理是通过向模型提供单个浮点值,然后处理返回的预测来完成的
  • 终端应用程序将以每秒极高的速率提供实时数据
  • Tensorflow 2.x创建模型并以saved_model格式保存

ML.NET代码

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, @"TFhalf_plus_two1saved_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; }
}

问题

  • 1使用LoadTensorFlowModel创建模型或用于创建管道的正确方法是什么
  • 2HalfPlusTwoData是构建输入数据的正确方式吗
  • 3"HalfPlusTwoPrediction"是构建预测类的正确方法吗

阅读文档后,您应该从该方法中获得一个TensorFlowModel对象。

MS文档:

TensorFlowModel tensorFlowModel = mlContext.Model.LoadTensorFlowModel(_modelPath);

至于你的两个类,这就是方法。然而,你通常有一些数据会被夸大,应该包括在第一个类中。与您的预测类类似,它应该生成额外的列,如分数和概率。不过请注意,只有一些模型会像二进制分类模型一样具有概率。

MS文档(情绪分析(:

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://learn.microsoft.com/en-us/dotnet/machine-learning/tutorials/sentiment-analysis

关于Tensorflow模型:https://learn.microsoft.com/en-us/dotnet/machine-learning/tutorials/text-classification-tf

最新更新