首页 > 解决方案 > 如何使用张量流模型对图像进行单一预测?

问题描述

我刚开始使用 tensorflow 和 ml.net。文档到处都是。最初我只想加载一个狗或猫的张量流模型,然后向它扔一个猫的图像并得到预测。据我了解,该模型已经“训练”过,因此我不需要将任何输入数据塞入其中。我认为我只需要输入图像并得出预测。

using System;
using Microsoft.ML;

namespace DogOrCatConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create MLContext
            MLContext mlContext = new MLContext();

            using var tf = mlContext.Model.LoadTensorFlowModel("model.pb");
            var inputSchema = tf.GetInputSchema();
            var modelSchema = tf.GetModelSchema();
        
        }
    }
}

到目前为止,我已经使用 Nuget 引入了几个不同的库:

我从 Microsoft 教程中获得了模型,他们展示了如何使用 python http 函数与该模型进行交互。我已经将图像缩小到 256x256,所以我跳过了 python 示例中的一些预处理步骤。我正在努力找出我在 python 示例中看到的内容与 c# 之间的 api 差异。

标签: c#tensorflowml.net

解决方案


上面的代码只加载了 TensorFlow 模型。预测代码不存在。你看过https://docs.microsoft.com/en-us/dotnet/machine-learning/tutorials/image-classification它使用 *.pb 文件和 LoadTensorFlowModel() API。您需要添加分数张量流模型才能进行预测。下面的几行就是这样做的。

.Append(mlContext.Model.LoadTensorFlowModel(_inceptionTensorFlowModel).
    ScoreTensorFlowModel(outputColumnNames: new[] { "softmax2_pre_activation" }, inputColumnNames: new[] { "input" }, addBatchDimensionInput: true))

推荐阅读