【发布时间】:2020-08-02 06:54:23
【问题描述】:
我正在尝试比较使用不同推理方法评估预构建张量流模型的性能。我目前在 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, @"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; }
}
问题
- 1 使用
LoadTensorFlowModel创建模型或用于创建管道的正确方法是什么? - 2
HalfPlusTwoData是结构化输入数据的正确方法吗? - 3 'HalfPlusTwoPrediction' 是构建预测类的正确方法吗?
【问题讨论】:
标签: c# tensorflow real-time inference ml.net