使用 ONNX Runtime 與 OpenVINO 加速影像分類模型推理

Posted on Wed 07 August 2024 in Machine Learning

背景

由於最近生成式 AI 的興起,如 ChatGPT 與 DALL·E,微軟攜手廠商們開始推廣 AI PC。 因此,客戶端 ( 邊緣 ) 裝置的 AI 模型推理速度變得很重要。為了實現加速推理各種的 AI 模型,老伙伴 Intel 為開發者提供了一套主要用來加速自家硬體推理速度的開發套件:OpenVINO。 在之前的測試中,實現了使用 ONNX Runtime 與 DirectML 實現對影像分類器的加速推理,這裡想嘗試使用基於 OpenVINO EP 的推理加速。

ONNX Runtime

ONNX Runtime 是一種跨平台的 AI 加速框架,支援主流的作業系統與程式語言,以及主流的深度學習框架所訓練出來的模型,如 PyTorch 與 TensorFlow。 ONNX 支援多種的加速方式 (Execution Providers, EP),常見的像是 CPU, CUDA, DirectML 等等,其餘的 EP 與細節可以參考官方的文件。 這裡以 OpenVINO EP 搭配 Intel GPU 來加速模型推理。

實作

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

環境準備

  1. 安裝 Visual Studio 2022 (VS) 或更新的版本。
  2. 在 VS 中創建一個 C# Console App,.NET 版本可選擇 6.0 或更高。
  3. 依照官方文件安裝 OpenVINO Runtime
  4. 在方案的相依性安裝支援 OpenVINO EP 的 ONNX Runtime。由於需要手動安裝 NuGet 套件,要在 Visual Studio 的 NuGet 管理頁面新增來源資料夾並勾選 Include prerelease 才能看到下載來的套件。
  5. 安裝前處理用的相依性
  6. 下載 ONNX 模型檔案
  7. 下載一張待推理的影像,或其他影像。

程式碼

  1. 先準備分類標籤
  2. 主程式 Program.cs
using System;
using System.Collections.Generic;
using System.Diagnostics;
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 = args[0]; // Path to model
            string imageFilePath = args[1]; // Path to image

            // 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 OpenVINO EP
            SessionOptions sessionOptions = new SessionOptions();
            sessionOptions.GraphOptimizationLevel = GraphOptimizationLevel.ORT_DISABLE_ALL;
            sessionOptions.AppendExecutionProvider_OpenVINO("GPU"); // CPU or GPU or NPU
            sessionOptions.AppendExecutionProvider_CPU(1);

            // Run inference
            Stopwatch stopwatch = Stopwatch.StartNew(); // Calculate inference time
            using var session = new InferenceSession(modelFilePath, sessionOptions);
            using IDisposableReadOnlyCollection<DisposableNamedOnnxValue> results = session.Run(inputs);
            stopwatch.Stop();
            Console.WriteLine($"Inference time: {stopwatch.ElapsedMilliseconds} ms");

            // 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...");
            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; }
    }
}

建置與執行

為了在 ONNX Runtime 中啟用 OpenVINO EP,執行程式前需要先設定環境變數,可用安裝包內提供的腳本實現。 從 Visual Studio 選單的 Tools -> Command Line -> Developer Command Prompt 啟動 CLI,執行:

提醒:若使用 Python 虛擬環境記得先啟用。
C:\<openvino_install_directory>\setupvars.bat

然後就可以在 CLI 中執行建置命令:

dotnet build

最後再用 CLI 執行程式:

dotnet run [path-to-model] [path-to-image]

輸出結果:

Inference time: 3205 ms (234 ms for DirectML)
Top 10 predictions...
--------------------------------------------------------------
Label: Golden Retriever, Confidence: 0.70304483
Label: Kuvasz, Confidence: 0.17094779
Label: Otterhound, Confidence: 0.019788643
Label: Clumber Spaniel, Confidence: 0.018589709
Label: Saluki, Confidence: 0.011143869
Label: Sussex Spaniel, Confidence: 0.0072231824
Label: Labrador Retriever, Confidence: 0.0070834733
Label: Pyrenean Mountain Dog, Confidence: 0.006349585
Label: Tibetan Terrier, Confidence: 0.0060825297
Label: English Setter, Confidence: 0.0042629093

心得

此範例在 Intel Iris Xe Graphics (Intel Core i5-11300H) 上測試,推理速度似乎不如 DirectML EP 快,出乎我的意料之外,本來以為專有硬體設計的 OpenVINO 會更快,也許是因為透過 ONNX 呼叫的關係,多了一些 API 的開銷,或真的是 DirectML 的實作方法更優。 目前來說,在 ONNX 搭配 OpenVINO EP 使用上還是比 DirectML EP 麻煩,NuGet 套件沒有上傳到官方的伺服器,安裝與更新不方便,而且執行程式前每次都需要啟用環境變數。希望以後使用上可以更方便一些。

分享到: DiasporaTwitterFacebookLinkedInHackerNewsEmailReddit