1

我使用Pix2PixHD训练了一个模型,然后将其导出到 onnx,我想使用 UWP 在 C# 中进行推理。

Pix2PixHD 所需的输入为:

  • 1 张语义标签图像(512x1024 灰度)

  • 1 个图像实例(512x1024 灰度)

  • 训练期间用作热转换器的标签数量(例如“--label_nc 16”)

我可以在 python 中测试我的模型,但我不知道如何在 C# 中绑定它,所以我非常感谢任何帮助。

这就是我在 WinML Dashboard 中的模型:

这就是它在 WinMLRunner 中的样子:

这是从 Visual Studio 生成的代码:

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Windows.Media;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.AI.MachineLearning;
namespace Onnx_test_02
{

    public sealed class Input
    {
        public TensorFloat label; // shape(1,1,512,1024)
        public TensorInt8Bit inst; // shape(1,1,512,1024)
    }

    public sealed class Output
    {
        public TensorFloat synthesized_image; // shape(1,3,512,1024)
    }

    public sealed class Model
    {
        private LearningModel model;
        private LearningModelSession session;
        private LearningModelBinding binding;
        public static async Task<Model> CreateFromStreamAsync(IRandomAccessStreamReference stream)
        {
            Model learningModel = new Model();
            learningModel.model = await LearningModel.LoadFromStreamAsync(stream);
            learningModel.session = new LearningModelSession(learningModel.model);
            learningModel.binding = new LearningModelBinding(learningModel.session);
            return learningModel;
        }
        public async Task<Output> EvaluateAsync(Input input)
        {
            binding.Bind("label", input.label);
            binding.Bind("inst", input.inst);
            var result = await session.EvaluateAsync(binding, "0");
            var output = new Output();
            output.synthesized_image = result.Outputs["synthesized_image"] as TensorFloat;
            return output;
        }
    }
}
4

0 回答 0