Windows ML 模型目錄來源結構描述參考

本頁提供 Windows ML 模型目錄來源所用 JSON 結構描述的參考檔,以定義模型目錄來源。 模型目錄來源是描述可用 AI 模型、其相容性和下載資訊的 JSON 檔案。

您的模型目錄來源檔案可以在線上託管在 URL 上 https:// ,或從您的應用程式參考為本機檔案。

結構描述概觀

模型目錄是包含模型定義陣列和選用中繼資料的 JSON 檔案。 該結構描述遵循標準 JSON 結構描述慣例,並設計為人類可讀和機器可解析。

目錄支援兩種類型的模型分佈:

  • 基於檔案的模型:以單獨檔案的形式分發的模型(ONNX 模型、配置等)
  • 基於套件的模型:以 Windows 套件(MSIX)形式發佈的模型

根目錄結構

{
  "models": [
    // Array of model objects
  ]
}

根屬性

房產 類型 為必填項目 Description
models 陣列 Yes 模型定義陣列

模型物件結構

陣列中的 models 每個模型都遵循以下結構:

{
  "id": "phi-3.5-cpu",
  "name": "phi-3.5",
  "version": "1.0.0",
  "publisher": "Publisher Name",
  "executionProviders": [
    {
      "name": "CPUExecutionProvider"
    }
  ],
  "modelSizeBytes": 13632917530,
  "license": "MIT",
  "licenseUri": "https://opensource.org/licenses/MIT",
  "licenseText": "License text content",
  "uri": "https://models.example.com/model-base",
  "files": [
    {
      "name": "model.onnx",
      "uri": "https://models.example.com/model.onnx",
      "sha256": "d7f93e79ba1284a3ff2b4cea317d79f3e98e64acfce725ad5f4e8197864aef73"
    }
  ]
}

模型屬性

房產 類型 為必填項目 Description
id 字串 Yes 此特定模型的目錄中唯一識別碼
name 字串 Yes 模型的通用名稱 (可跨變體共用)
version 字串 Yes 型號版本號
publisher 字串 Yes 建立模型的發行者或組織
executionProviders 陣列 Yes 模型支援的執行提供者物件陣列
modelSizeBytes 整數 否 模型大小 (以位元組為單位) (最小值:0)
license 字串 Yes 授權類型(例如「MIT」、「BSD」、「Apache」)
licenseUri 字串 Yes 授權檔的 URI
licenseText 字串 否 授權的文字內容
uri 字串 Yes 可存取模型的基本 URI
files 陣列 Conditional 與模型相關聯的檔案陣列
packages 陣列 Conditional 模型所需的套件陣列

附註: 模型必須具有 OR filespackages,但不能同時具有兩者。

執行提供者

欄位是 executionProviders 執行提供者物件的陣列。 每個執行提供者物件都必須至少具有屬性 name :

"executionProviders": [
  {
    "name": "CPUExecutionProvider"
  },
  {
    "name": "DmlExecutionProvider"
  }
]

如需所有可用執行提供者名稱的完整清單,請參閱 Windows ML 檔中支援的執行提供者 頁面。

檔案物件

檔案用於分發單個模型組件(ONNX 文件、配置等):

{
  "name": "model.onnx",
  "uri": "https://models.example.com/model.onnx",
  "sha256": "d7f93e79ba1284a3ff2b4cea317d79f3e98e64acfce725ad5f4e8197864aef73"
}
房產 類型 為必填項目 Description
name 字串 Yes 檔案名稱
uri 字串 否 可下載檔案的 URI
sha256 字串 Yes SHA256 雜湊 (64 個字元的十六進位字串) ,用於完整性驗證和重複資料刪除相同模型

附註: 如果未指定,則 uri 檔案 URI 是從模型的基本 uri 內容建構的。

封裝物件

套件可用來將模型散發為 Windows 套件或應用程式:

{
  "packageFamilyName": "Microsoft.Example_8wekyb3d8bbwe",
  "uri": "https://example.com/packages/model.msix",
  "sha256": "a1b2c3d4e5f6789..."
}
房產 類型 為必填項目 Description
packageFamilyName 字串 Yes Windows 套件系列名稱識別碼
uri 字串 Yes URI 可取得套件(HTTPS 或本地檔案路徑)
sha256 字串 Conditional SHA256 雜湊碼用於完整性驗證(HTTPS URI 必備)

分發方式

目錄支援數種散發方法:

基於文件的分發:

  • 直接 HTTPS 下載
  • 裝載在 GitHub、Azure 或其他網頁伺服器上的檔案
  • 模型檔案 (.onnx)、組態檔 (.json) 和支援資產

基於套件的分發:

  • 透過 HTTPS 直接下載套件
  • 套件的本地檔案路徑
  • MSIX 套件

完整範例

檔案型模型範例

以下是包含檔案型模型的範例目錄:

{
  "models": [
    {
      "id": "squeezenet-v1",
      "name": "squeezenet",
      "version": "1.0",
      "publisher": "WindowsAppSDK",
      "executionProviders": [
        {
          "name": "CPUExecutionProvider"
        }
      ],
      "modelSizeBytes": 13632917530,
      "license": "BSD",
      "licenseUri": "https://github.com/microsoft/WindowsAppSDK-Samples/raw/main/LICENSE",
      "licenseText": "BSD 3-Clause License",
      "uri": "https://github.com/microsoft/WindowsAppSDK-Samples/raw/main/models",
      "files": [
        {
          "name": "SqueezeNet.onnx",
          "uri": "https://github.com/microsoft/WindowsAppSDK-Samples/raw/main/models/SqueezeNet.onnx",
          "sha256": "d7f93e79ba1284a3ff2b4cea317d79f3e98e64acfce725ad5f4e8197864aef73"
        },
        {
          "name": "Labels.txt",
          "uri": "https://github.com/microsoft/WindowsAppSDK-Samples/raw/main/models/Labels.txt", 
          "sha256": "dc1fd0d4747096d3aa690bd65ec2f51fdb3e5117535bfbce46fa91088a8f93a9"
        }
      ]
    }
  ]
}

套件型模型範例

以下是包含套件型模型的範例目錄:

{
  "models": [
    {
      "id": "example-packaged-model-cpu",
      "name": "example-packaged-model",
      "version": "2.0.0",
      "publisher": "Microsoft",
      "executionProviders": [
        {
          "name": "CPUExecutionProvider"
        },
        {
          "name": "DmlExecutionProvider"
        }
      ],
      "license": "MIT",
      "licenseUri": "https://opensource.org/licenses/MIT",
      "licenseText": "MIT License - see URI for full text",
      "uri": "https://example.com/packages",
      "packages": [
        {
          "packageFamilyName": "Microsoft.ExampleAI_8wekyb3d8bbwe",
          "uri": "https://example.com/packages/ExampleAI.msix",
          "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
        }
      ]
    }
  ]
}

結構描述驗證

Windows ML 模型目錄遵循 JSON 結構描述規格 (草案 2020-12)。 您可以根據官方結構描述驗證您的目錄檔案,以確保相容性。

金鑰驗證規則

  1. 必填欄位:每個模型必須具備 id、 name、 versionpublisherexecutionProviderslicenselicenseUri和uri
  2. 獨佔分佈:模型必須指定其中一個 files OR packages,但不能同時指定兩者
  3. SHA256 格式:所有 SHA256 雜湊值必須正好是 64 個十六進位字元
  4. 執行提供者:必須是至少具有屬性的 name 物件
  5. URI 格式:所有 URI 都必須根據 RFC 3986 正確格式化
  6. HTTPS 套件完整性:透過 HTTPS 下載的套件必須包含 SHA256 雜湊值以驗證完整性

常見驗證錯誤

  • 缺少必填欄位:確保所有必填屬性都存在
  • 無效的 SHA256:檢查雜湊值是否正好是 64 個十六進位字元
  • 混合分佈:請勿同時針對相同模型指定 files 和packages
  • 無效的 URI:驗證所有 URI 的格式正確且可存取
  • HTTPS 套件缺少 SHA256:HTTPS 套件下載需要完整性驗證

建立目錄

步驟 1:選擇分發方式

在下列情況下使用檔案型分發:

  • 您的模型是具有支援資產的標準 ONNX 檔案
  • 您有模型檔案的網頁代管
  • 您想要簡單的 HTTPS 下載
  • 型號相對較小(< 每個文件 1GB)

在下列情況下使用套件型散發:

  • 您的模型需要系統整合
  • 模型包括執行提供者或相依性
  • 您需要 Windows 套件管理功能
  • 你想透過 MSIX 套件來發佈

步驟 2:建構您的模型

{
  "models": [
    {
      "id": "unique-identifier-cpu",
      "name": "unique-identifier",
      "version": "1.0.0",
      "publisher": "YourOrganization",
      "executionProviders": [
        {
          "name": "CPUExecutionProvider"
        }
      ],
      "license": "MIT",
      "licenseUri": "https://opensource.org/licenses/MIT",
      "uri": "https://yourserver.com/models/base-path",
      "files": []  // or "packages": []
    }
  ]
}

步驟 3:新增分發詳細資料

將取樣 sha256 值替換為每個檔案或套件的實際 SHA256 雜湊值。

對於檔案型模型:

"uri": "https://yourserver.com/models/base-path",
"files": [
  {
    "name": "model.onnx",
    "sha256": "your-calculated-sha256-hash"
  }
]

對於套件型模型:

"packages": [
  {
    "packageFamilyName": "YourPublisher.YourApp_randomstring",
    "uri": "https://yourserver.com/packages/YourApp.msix",
    "sha256": "your-calculated-sha256-hash"
  }
]

步驟 4:測試您的目錄

  1. 使用 JSON 驗證器驗證 JSON 語法
  2. 確認所有 URI 都可存取並傳回正確的內容
  3. 檢查 SHA256 雜湊值是否 與實際檔案內容相符
  4. 使用 Windows ML 模型目錄 API 測試模型下載。

最佳做法

  1. 使用語義版本控制: 遵循字段的 version 語義版本控制(例如,“1.2.3”)
  2. 提供準確的 SHA256 雜湊值:始終包含正確的 SHA256 雜湊值以進行完整性驗證
  3. 一致的命名:對跨模型版本的 ID 和名稱使用一致的命名慣例
  4. 清晰的描述: 撰寫有助於解釋模型功能和用例的描述
  5. 適當的授權:一律包含完整的授權資訊 (類型、URI 和文字)
  6. 測試可訪問性: 確保所有 URI 都可訪問並返回預期內容
  7. 執行提供者相容性:確保執行提供者符合目標硬體功能
  8. 邏輯分組:使用名稱欄位將相關模型變體分組(相同基礎模型的不同EP版本)
  9. 檔案組織:將相關檔案放在一起並使用描述性檔案名稱
  10. 安全性:所有檔案下載均使用 HTTPS,並包括 SHA256 驗證

託管考量

裝載模型目錄時:

  1. 需要 HTTPS:一律透過 HTTPS 提供目錄和模型
  2. CORS 標頭:設定適當的 CORS 標頭以進行跨來源存取
  3. Content-Type:提供具有內容型別的 application/json 目錄JSON
  4. 快取頭:設定適當的快取頭以提高效能
  5. 身份驗證:如果需要,支援標準 HTTP 身份驗證

JSON 架構

你可以使用以下 JSON 架構來驗證你的 JSON 有效載荷。

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "WinML Model Catalog Schema",
  "description": "JSON schema for WindowsML Model catalog configuration",
  "type": "object",
  "required": [ "models" ],
  "properties": {
    "models": {
      "type": "array",
      "description": "Array of machine learning models in the catalog",
      "items": {
        "$ref": "#/$defs/Model"
      }
    }
  },
  "$defs": {
    "Model": {
      "type": "object",
      "required": [ "id", "name", "version", "publisher", "executionProviders", "license", "licenseUri", "uri" ],
      "properties": {
        "id": {
          "type": "string",
          "description": "Unique-in-the-catalog identifier for the model"
        },
        "name": {
          "type": "string",
          "description": "Common name of the model"
        },
        "version": {
          "type": "string",
          "description": "Version of the model"
        },
        "publisher": {
          "type": "string",
          "description": "Publisher or organization that created the model"
        },
        "executionProviders": {
          "type": "array",
          "description": "Array of execution providers supported by the model",
          "items": {
            "$ref": "#/$defs/ExecutionProvider"
          }
        },
        "modelSizeBytes": {
          "type": "integer",
          "minimum": 0,
          "description": "Size of the model in bytes"
        },
        "license": {
          "type": "string",
          "description": "License type (e.g., MIT, BSD, Apache)"
        },
        "licenseUri": {
          "type": "string",
          "format": "uri",
          "description": "URI to the license document"
        },
        "licenseText": {
          "type": "string",
          "description": "Text content of the license"
        },
        "uri": {
          "type": "string",
          "format": "uri",
          "description": "URI where the model can be accessed"
        },
        "files": {
          "type": "array",
          "description": "Array of files associated with the model",
          "items": {
            "$ref": "#/$defs/File"
          }
        },
        "packages": {
          "type": "array",
          "description": "Array of packages required for the model",
          "items": {
            "$ref": "#/$defs/Package"
          }
        }
      },
      "oneOf": [
        {
          "required": [ "files" ],
          "not": { "required": [ "packages" ] }
        },
        {
          "required": [ "packages" ],
          "not": { "required": [ "files" ] }
        }
      ]
    },
    "File": {
      "type": "object",
      "required": [ "name", "sha256" ],
      "properties": {
        "name": {
          "type": "string",
          "description": "Name of the file"
        },
        "uri": {
          "type": "string",
          "format": "uri",
          "description": "URI where the file can be downloaded"
        },
        "sha256": {
          "type": "string",
          "pattern": "^[a-fA-F0-9]{64}$",
          "description": "SHA256 hash of the file for integrity verification"
        }
      }
    },
    "Package": {
      "type": "object",
      "required": [ "packageFamilyName", "uri" ],
      "properties": {
        "packageFamilyName": {
          "type": "string",
          "description": "Windows package family name identifier"
        },
        "uri": {
          "type": "string",
          "format": "uri",
          "description": "URI where the package can be obtained (HTTPS or local file path)"
        },
        "sha256": {
          "type": "string",
          "pattern": "^[a-fA-F0-9]{64}$",
          "description": "SHA256 hash of the package for integrity verification"
        }
      },
      "if": {
        "properties": {
          "uri": {
            "pattern": "^https://"
          }
        }
      },
      "then": {
        "required": [ "packageFamilyName", "uri", "sha256" ]
      }
    },
    "ExecutionProvider": {
      "type": "object",
      "required": [ "name" ],
      "properties": {
        "name": {
          "type": "string",
          "description": "Name of the execution provider (e.g., CPUExecutionProvider)"
        }
      }
    }
  }
}

後續步驟