使用 ONNX Runtime generate() API 執行 Phi-3 vision 和 Phi-3.5 vision 模型

Phi-3 vision 和 Phi-3.5 vision 模型是小型但功能強大的多模態模型,允許您同時使用影像和文字來輸出文字。它們可用於詳細描述影像內容等場景。

onnxruntime-genai 0.5.1 及更高版本支援 Phi-3 vision 和 Phi-3.5 vision 模型。

您可以在此處下載模型

設定

  1. 安裝 git 大檔案系統擴充套件

    HuggingFace 使用 git 進行版本控制。要下載 ONNX 模型,您需要安裝 git lfs,如果尚未安裝。

    • Windows: winget install -e --id GitHub.GitLFS (如果您沒有 winget,請從官方源下載並執行 exe 檔案)
    • Linux: apt-get install git-lfs
    • MacOS: brew install git-lfs

    然後執行 git lfs install

  2. 安裝 HuggingFace CLI

    pip install huggingface-hub[cli]
    

選擇您的平臺

注意:根據您的硬體,只需一個軟體包和一個模型。也就是說,只需執行以下部分中的一個步驟。

使用 DirectML 執行

  1. 下載模型

    huggingface-cli download microsoft/Phi-3.5-vision-instruct-onnx --include gpu/gpu-int4-rtn-block-32/* --local-dir .
    

    此命令將模型下載到名為 gpu/gpu-int4-rtn-block-32 的資料夾中。

  2. 安裝 generate() API

    pip install onnxruntime-genai-directml
    
  3. 執行模型

    使用 model-vision.py 執行模型。

    curl https://raw.githubusercontent.com/microsoft/onnxruntime-genai/main/examples/python/model-vision.py -o model-vision.py
    pip install pyreadline3
    python model-vision.py -m gpu/gpu-int4-rtn-block-32 -e dml
    

    輸入影像檔案路徑和提示。模型將使用影像和提示來給出答案。

    例如:牌子上寫了什麼?

    coffee

    The sign says 'DO NOT ENTER'.
    

使用 CUDA 執行

  1. 下載模型

    huggingface-cli download microsoft/Phi-3.5-vision-instruct-onnx --include gpu/gpu-int4-rtn-block-32/* --local-dir .
    

    此命令將模型下載到名為 gpu/gpu-int4-rtn-block-32 的資料夾中。

  2. 設定您的 CUDA 環境

    安裝 CUDA 工具包

    確保 CUDA_PATH 環境變數已設定為您的 CUDA 安裝位置。

  3. 安裝 generate() API

    注意:此軟體包使用 CUDA 12。要使用 CUDA 11,您需要從原始碼構建和安裝

    pip install onnxruntime-genai-cuda
    
  4. 執行模型

    使用 model-vision.py 執行模型。

    curl https://raw.githubusercontent.com/microsoft/onnxruntime-genai/main/examples/python/model-vision.py -o model-vision.py
    pip install pyreadline3
    python model-vision.py -m gpu/gpu-int4-rtn-block-32 -e cuda
    

    輸入影像檔案路徑和提示,模型將使用影像和提示來給出答案。

    例如:描述這張圖片

    coffee

    The image shows a cup of coffee with a latte art design on top. The coffee is a light brown color,
    and the art is white with a leaf-like pattern. The cup is white and has a handle on one side.</s>
    

在 CPU 上執行

  1. 下載模型

    huggingface-cli download microsoft/Phi-3.5-vision-instruct-onnx --include cpu_and_mobile/cpu-int4-rtn-block-32-acc-level-4/* --local-dir .
    

    此命令將模型下載到名為 cpu_and_mobile/cpu-int4-rtn-block-32-acc-level-4 的資料夾中

  2. 為 CPU 安裝 generate() API

    pip install onnxruntime-genai
    
  3. 執行模型

    使用 model-vision.py 執行模型。

    curl https://raw.githubusercontent.com/microsoft/onnxruntime-genai/main/examples/python/model-vision.py -o model-vision.py
    pip install pyreadline3
    python model-vision.py -m cpu_and_mobile/cpu-int4-rtn-block-32-acc-level-4 -e cpu
    

    輸入影像檔案路徑和提示,模型將使用影像和提示來給出答案。

    例如:將此圖片轉換為 Markdown 格式

    Excel table with cookie sales figures

    | Product             | Qtr 1      | Qtr 2      | Grand Total |
    |---------------------|------------|------------|-------------|
    | Chocolade           | $744.60    | $162.56    | $907.16     |
    | Gummibarchen        | $5,079.60  | $1,249.20  | $6,328.80   |
    | Scottish Longbreads | $1,267.50  | $1,062.50  | $2,330.00   |
    | Sir Rodney's Scones | $1,418.00  | $756.00    | $2,174.00   |
    | Tarte au sucre      | $4,728.00  | $4,547.92  | $9,275.92   |
    | Chocolate Biscuits  | $943.89    | $349.60    | $1,293.49   |
    | Total               | $14,181.59 | $8,127.78  | $22,309.37  |
    
    The table lists various products along with their sales figures for Qtr 1, Qtr 2, and the Grand Total.
    The products include Chocolade, Gummibarchen, Scottish Longbreads, Sir Rodney's Scones, Tarte au sucre,
    and Chocolate Biscuits. The Grand Total column sums up the sales for each product across the two quarters.</s>