使用 ONNX Runtime 與 DirectML 加速 ResNet50 影像分類推理

Posted on Fri 12 January 2024 in Machine Learning

背景

由於最近生成式 AI 的興起,如 ChatGPT 與 DALL·E,微軟攜手廠商們開始推廣 AI PC。 因此,客戶端 ( 邊緣 ) 裝置的 AI 模型推理速度變得很重要。為了實現加速推理各種的 AI 模型,ONNX Runtime 是微軟與 Meta 等公司主推的 AI 加速方案。 所以我這裡就來測試如何使用 ONNX Runtime 實現對 ResNet50 分類器的加速推理。

ONNX Runtime

ONNX Runtime 是一種跨平台的 AI 加速框架,支援主流的作業系統與程式語言,以及主流的深度學習框架所訓練出來的模型,如 PyTorch 與 TensorFlow。 ONNX 支援多種的加速方式 (Execution Providers, EP),常見的像是 CPU, CUDA, DirectML 等等,其餘的 EP 與細節可以參考官方的文件。 這裡以 DirectML EP 來實作,因為 DirectML 理論上可以支援所有相容 DirectX 12 的 GPU,應該在 Windows 上有最好的泛用性。

實作

這裡以官方的 ResNet50 C# 範例為基礎來實作。 由於範例的文件比較舊,我做了一些修改以適用最新版本的 ONNX 與執行環境。

環境準備

  1. 安裝 Visual Studio 2022 (VS) 或更新的版本。
  2. 在 VS 中創建一個 C# Console App,.NET 版本選擇 6.0 或更高。
  3. 在方案的相依性安裝支援 DirectML 的 ONNX Runtime
  4. 安裝前處理用的相依性
  5. 下載 ONNX 模型檔案
  6. 下載一張待推理的影像,或其他影像。

程式碼

  1. 先準備分類標籤
  2. 主程式 Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.ML.OnnxRuntime.Tensors;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using static System.Net.Mime.MediaTypeNames;

namespace Microsoft.ML.OnnxRuntime.ResNet50v2Sample
{
    class Program
    {
        public static void Main(string[] args)
        {
            // Read paths
            string modelFilePath = "Path\\to\\model\\resnet50-v2-7.onnx";
            string imageFilePath = "Path\\to\\image\\dog.jpeg";

            // Read image
            using Image<Rgb24> image = SixLabors.ImageSharp.Image.Load<Rgb24>(imageFilePath);

            // Resize image
            image.Mutate(x =>
            {
                x.Resize(new ResizeOptions
                {
                    Size = new Size(224, 224),
                    Mode = ResizeMode.Crop
                });
            });

            // Preprocess image
            Tensor<float> input = new DenseTensor<float>(new[] { 1, 3, 224, 224 });
            var mean = new[] { 0.485f, 0.456f, 0.406f };
            var stddev = new[] { 0.229f, 0.224f, 0.225f };
            image.ProcessPixelRows(accessor =>
            {
                for (int y = 0; y < accessor.Height; y++)
                {
                    Span<Rgb24> pixelSpan = accessor.GetRowSpan(y);
                    for (int x = 0; x < accessor.Width; x++)
                    {
                        input[0, 0, y, x] = ((pixelSpan[x].R / 255f) - mean[0]) / stddev[0];
                        input[0, 1, y, x] = ((pixelSpan[x].G / 255f) - mean[1]) / stddev[1];
                        input[0, 2, y, x] = ((pixelSpan[x].B / 255f) - mean[2]) / stddev[2];
                    }
                }
            });

            // Setup inputs
            var inputs = new List<NamedOnnxValue>
            {
                NamedOnnxValue.CreateFromTensor("data", input)
            };

            // Enable the DirectML EP
            SessionOptions sessionOptions = new SessionOptions();
            sessionOptions.GraphOptimizationLevel = GraphOptimizationLevel.ORT_ENABLE_ALL;
            sessionOptions.AppendExecutionProvider_DML(0);

            // Run inference
            using var session = new InferenceSession(modelFilePath, sessionOptions);
            using IDisposableReadOnlyCollection<DisposableNamedOnnxValue> results = session.Run(inputs);

            // Postprocess to get softmax vector
            IEnumerable<float> output = results.First().AsEnumerable<float>();
            float sum = output.Sum(x => (float)Math.Exp(x));
            IEnumerable<float> softmax = output.Select(x => (float)Math.Exp(x) / sum);

            // Extract top 10 predicted classes
            IEnumerable<Prediction> top10 = softmax.Select((x, i) => new Prediction { Label = LabelMap.Labels[i], Confidence = x })
                               .OrderByDescending(x => x.Confidence)
                               .Take(10);

            // Print results to console
            Console.WriteLine("Top 10 predictions for ResNet50 v2...");
            Console.WriteLine("--------------------------------------------------------------");
            foreach (var t in top10)
            {
                Console.WriteLine($"Label: {t.Label}, Confidence: {t.Confidence}");
            }
        }
    }

    internal class Prediction
    {
        public string Label { get; set; }
        public float Confidence { get; set; }
    }
}

輸出結果:

Top 10 predictions for ResNet50 v2...
--------------------------------------------------------------
Label: Golden Retriever, Confidence: 0.70073485
Label: Kuvasz, Confidence: 0.17292742
Label: Otterhound, Confidence: 0.019890321
Label: Clumber Spaniel, Confidence: 0.018660849
Label: Saluki, Confidence: 0.011093369
Label: Sussex Spaniel, Confidence: 0.0072962483
Label: Labrador Retriever, Confidence: 0.007059277
Label: Pyrenean Mountain Dog, Confidence: 0.006370869
Label: Tibetan Terrier, Confidence: 0.006168284
Label: English Setter, Confidence: 0.004178419

心得

經過測試,此範例在 NVIDIA RTX 4070、Intel Iris Xe Graphics (Intel Core i5-11300H),甚至是高通的 Adreno 8CX Gen 3 (Microsoft SQ 3),雖然在 SQ3 上會回報未知的 ARM CPU,但都可以順利執行加速推理。 在這個簡單的分類器範例,DirectML 的相容性看起來不錯,希望微軟持續維護 DirectML EP。

分享到: DiasporaTwitterFacebookLinkedInHackerNewsEmailReddit