Compartilhar via


Passo a passo do Windows ML

Este breve tutorial explica como usar o Windows ML para executar o modelo de classificação de imagem ResNet-50 no Windows, detalhando as etapas de aquisição e pré-processamento do modelo. A implementação envolve a seleção dinâmica de provedores de execução para desempenho de inferência otimizado.

O modelo ResNet-50 é um modelo PyTorch destinado à classificação de imagem.

Neste tutorial, você adquirirá o modelo ResNet-50 do Hugging Face e o converterá no formato QDQ ONNX usando o Kit de Ferramentas de IA.

Em seguida, você carregará o modelo, preparará tensores de entrada e executará a inferência usando as APIs do Windows ML, incluindo etapas pós-processamento para aplicar softmax e recuperar as previsões principais.

Aquisição do modelo e pré-processamento

Você pode adquirir o ResNet-50 do Hugging Face (a plataforma onde a comunidade de ML se reúne para colaborar em modelos, conjuntos de dados e aplicativos). Você converterá ResNet-50 no formato QDQ ONNX usando o Kit de Ferramentas de IA (consulte converter modelos no formato ONNX para obter mais informações).

O objetivo deste código de exemplo é aproveitar o runtime do Windows ML para fazer o trabalho pesado.

O runtime do Windows ML será:

  • Carregue o modelo.
  • Selecione dinamicamente o provedor de execução (EP) preferido fornecido por IHV para o modelo e baixe seu EP sob demanda na Microsoft Store.
  • Execute a inferência no modelo usando o EP.

Para referência de API, consulte OrtSessionOptions e a classe ExecutionProviderCatalog .

// Create a new instance of EnvironmentCreationOptions
EnvironmentCreationOptions envOptions = new()
{
    logId = "ResnetDemo",
    logLevel = OrtLoggingLevel.ORT_LOGGING_LEVEL_ERROR
};

// Pass the options by reference to CreateInstanceWithOptions
OrtEnv ortEnv = OrtEnv.CreateInstanceWithOptions(ref envOptions);

// Use Windows ML to download and register Execution Providers
var catalog = Microsoft.Windows.AI.MachineLearning.ExecutionProviderCatalog.GetDefault();
Console.WriteLine("Ensuring and registering execution providers...");
await catalog.EnsureAndRegisterCertifiedAsync();

//Create Onnx session
Console.WriteLine("Creating session ...");
var sessionOptions = new SessionOptions();
// Set EP Selection Policy
sessionOptions.SetEpSelectionPolicy(ExecutionProviderDevicePolicy.MIN_OVERALL_POWER);

Compilação de EP

Se o modelo ainda não tiver sido compilado para o EP (o que pode variar dependendo do dispositivo), ele precisará primeiro ser compilado para esse EP. Esse é um processo único. O código de exemplo abaixo o manipula compilando o modelo na primeira execução e armazenando-o localmente. As execuções subsequentes do código captam a versão compilada e executam-na; resultando em inferências rápidas otimizadas.

Para referência de API, consulte o struct Ort::ModelCompilationOptions, o struct Ort::Status e o struct Ort::CompileModel.

// Prepare paths
string executableFolder = Path.GetDirectoryName(Assembly.GetEntryAssembly()!.Location)!;
string labelsPath = Path.Combine(executableFolder, "ResNet50Labels.txt");
string imagePath = Path.Combine(executableFolder, "dog.jpg");
            
// TODO: Please use AITK Model Conversion tool to download and convert Resnet, and paste the converted path here
string modelPath = @"";
string compiledModelPath = @"";

// Compile the model if not already compiled
bool isCompiled = File.Exists(compiledModelPath);
if (!isCompiled)
{
    Console.WriteLine("No compiled model found. Compiling model ...");
    using (var compileOptions = new OrtModelCompilationOptions(sessionOptions))
    {
        compileOptions.SetInputModelPath(modelPath);
        compileOptions.SetOutputModelPath(compiledModelPath);
        compileOptions.CompileModel();
        isCompiled = File.Exists(compiledModelPath);
        if (isCompiled)
        {
            Console.WriteLine("Model compiled successfully!");
        }
        else
        {
            Console.WriteLine("Failed to compile the model. Will use original model.");
        }
    }
}
else
{
    Console.WriteLine("Found precompiled model.");
}
var modelPathToUse = isCompiled ? compiledModelPath : modelPath;

Executando a inferência

A imagem de entrada é convertida em formato de dados tensor e, em seguida, a inferência é executada nela. Embora isso seja típico de todos os códigos que usam o ONNX Runtime, a diferença nesse caso é que se trata do uso do ONNX Runtime diretamente através do Windows ML. O único requisito é adicionar #include <winml/onnxruntime_cxx_api.h> ao código.

Consulte também Converter um modelo com o Kit de Ferramentas de IA para VS Code

Para referência de API, consulte struct Ort::Session, struct Ort::MemoryInfo, struct Ort::Value, struct Ort::AllocatorWithDefaultOptions, struct Ort::RunOptions.

using var session = new InferenceSession(modelPathToUse, sessionOptions);

Console.WriteLine("Preparing input ...");
// Load and preprocess image
var input = await PreprocessImageAsync(await LoadImageFileAsync(imagePath));
// Prepare input tensor
var inputName = session.InputMetadata.First().Key;
var inputTensor = new DenseTensor<float>(
    input.ToArray(),          // Use the DenseTensor<float> directly
    new[] { 1, 3, 224, 224 }, // Shape of the tensor
    false                     // isReversedStride should be explicitly set to false
);

// Bind inputs and run inference
var inputs = new List<NamedOnnxValue>
{
    NamedOnnxValue.CreateFromTensor(inputName, inputTensor)
};

Console.WriteLine("Running inference ...");
var results = session.Run(inputs);
for (int i = 0; i < 40; i++)
{
    results = session.Run(inputs);
}

// Extract output tensor
var outputName = session.OutputMetadata.First().Key;
var resultTensor = results.First(r => r.Name == outputName).AsEnumerable<float>().ToArray();

// Load labels and print results
var labels = LoadLabels(labelsPath);
PrintResults(labels, resultTensor);

Pós-processamento

A função softmax é aplicada à saída bruta retornada e os dados de rótulo são usados para mapear e imprimir os nomes com as cinco maiores probabilidades.

private static void PrintResults(IList<string> labels, IReadOnlyList<float> results)
{
    // Apply softmax to the results
    float maxLogit = results.Max();
    var expScores = results.Select(r => MathF.Exp(r - maxLogit)).ToList(); // stability with maxLogit
    float sumExp = expScores.Sum();
    var softmaxResults = expScores.Select(e => e / sumExp).ToList();

    // Get top 5 results
    IEnumerable<(int Index, float Confidence)> topResults = softmaxResults
        .Select((value, index) => (Index: index, Confidence: value))
        .OrderByDescending(x => x.Confidence)
        .Take(5);

    // Display results
    Console.WriteLine("Top Predictions:");
    Console.WriteLine("-------------------------------------------");
    Console.WriteLine("{0,-32} {1,10}", "Label", "Confidence");
    Console.WriteLine("-------------------------------------------");

    foreach (var result in topResults)
    {
        Console.WriteLine("{0,-32} {1,10:P2}", labels[result.Index], result.Confidence);
    }

    Console.WriteLine("-------------------------------------------");
}

Saída

Aqui está um exemplo do tipo de saída que se espera.

285, Egyptian cat with confidence of 0.904274
281, tabby with confidence of 0.0620204
282, tiger cat with confidence of 0.0223081
287, lynx with confidence of 0.00119624
761, remote control with confidence of 0.000487919

Exemplos de código completo

Os exemplos de código completo estão disponíveis no repositório github WindowsAppSDK-Samples. Consulte WindowsML.