執行 TorchServe¶
本文內容¶
概述¶
TorchServe 可用於生產環境中的多種類型推論。 它提供易於使用的命令列介面,並利用基於 REST 的 API 來處理狀態預測請求。
例如,您想要開發一個應用程式,讓使用者可以拍攝照片,並告訴他們場景中偵測到的物件以及物件可能是什麼的預測。 您可以使用 TorchServe 來為物件偵測和識別模型提供預測端點,該模型接收影像,然後傳回預測。 您也可以使用自訂服務修改 TorchServe 行為並執行多個模型。 examples 資料夾中有自訂服務的範例。
技術細節¶
現在您已經對 TorchServe 有了高層次的了解,讓我們深入了解一下。 TorchServe 採用 Pytorch 深度學習模型,並將其封裝在一組 REST API 中。 目前,它配備了一個內建的 Web 伺服器,您可以從命令列執行。 此命令列呼叫接收您要服務的單個或多個模型,以及控制埠、主機和日誌記錄的其他可選參數。 TorchServe 支援執行自訂服務來處理特定的推論處理邏輯。 這些在自訂服務文件中進行了更詳細的介紹。
要立即嘗試 TorchServe 服務,您可以使用此範例載入自訂 MNIST 模型
在深入研究之後,您可能也會對以下內容感興趣
日誌記錄:可用的日誌記錄選項
指標:有關指標收集的詳細資訊
REST API 說明:有關伺服器端點的更多詳細資訊
自訂服務:了解有關服務不同種類的模型和推論類型的資訊
模型檔案¶
本主題的其餘部分將重點放在模型檔案的服務上,而不會過多討論模型檔案本身、它們的來源以及它們的製作方式。簡而言之:它是一個 zip 壓縮檔,包含定義已訓練模型的參數、權重和元數據。如果您想了解更多關於模型檔案的信息,請查看model-archiver 文件。
命令列介面¶
$ torchserve --help
usage: torchserve [-h] [-v | --version]
[--start]
[--stop]
[--ts-config TS_CONFIG]
[--model-store MODEL_STORE]
[--workflow-store WORKFLOW_STORE]
[--models MODEL_PATH1 MODEL_NAME=MODEL_PATH2... [MODEL_PATH1 MODEL_NAME=MODEL_PATH2... ...]]
[--log-config LOG_CONFIG]
torchserve
optional arguments:
-h, --help show this help message and exit
-v, --version Return TorchServe Version
--start Start the model-server
--stop Stop the model-server
--ts-config TS_CONFIG
Configuration file for TorchServe
--model-store MODEL_STORE
Model store location where models can be loaded.
It is required if "model_store" is not defined in config.properties.
--models MODEL_PATH1 MODEL_NAME=MODEL_PATH2... [MODEL_PATH1 MODEL_NAME=MODEL_PATH2... ...]
Models to be loaded using [model_name=]model_location
format. Location can be a HTTP URL, a model archive
file or directory contains model archive files in
MODEL_STORE.
--log-config LOG_CONFIG
Log4j configuration file for TorchServe
--ncs, --no-config-snapshots
Disable snapshot feature
--workflow-store WORKFLOW_STORE
Workflow store location where workflow can be loaded. Defaults to model-store
引數:¶
啟動時未載入任何模型的範例
torchserve --model-store /models
啟動伺服器沒有預設的必要引數
models:選填,<model_name>=<model_path> 的配對。
a) 模型路徑可以是本機 mar 檔案名稱,或是遠端 http 連結到 mar 檔案。b) 若要載入模型儲存區中的所有模型,請將模型值設定為 "all"
torchserve --model-store /models --start --models all
c) 模型檔案的副檔名為 .mar,它實際上是一個 zip 壓縮檔,副檔名為 .mar,其中包含已訓練的模型和模型簽章檔案。
d) 也支援透過指定多個名稱路徑配對來載入多個模型。
e) 關於啟動 TorchServe 時載入模型的不同方式的詳細資訊,請參閱使用 TorchServe 服務多個模型
model-store:必填,預設或本機模型的儲存位置。模型儲存區中可用的模型可以透過register api call 或啟動 TorchServe 時透過 models 參數在 TorchServe 中註冊。
workflow-store:必填,預設或本機工作流程的儲存位置。工作流程儲存區中可用的工作流程可以透過register api call在 TorchServe 中註冊。
ts-config:選填,以 config.properties 格式提供配置檔案。
log-config:選填,此參數將覆寫伺服器內預設的 log4j2.xml。
start:選填,更詳細的伺服器啟動方式。
stop:選填,如果伺服器已經在執行,則停止伺服器。
引數優先順序:¶
引數可以在多個位置設定(例如:命令列、config.properties)。以下是優先順序
命令列
Config 屬性
預設設定
範例:在 config.properties 和透過命令列設定 model-store
將會導致使用命令列位置並覆寫 config.properties。
進階功能¶
自訂服務¶
此主題在自訂服務文件頁面上有更詳細的介紹,但讓我們來談談如何使用自訂服務啟動您的 TorchServe 伺服器,以及您可能想要這樣做的原因。假設您有一個名為 super-fancy-net.mar
的模型,位於 /models
資料夾中,它可以檢測到很多東西,但您想要一個只檢測熱狗的 API 端點。您可以使用一個有意義的名稱,例如 “not-hot-dog” API。在這種情況下,我們可能會像這樣調用 TorchServe
torchserve --start --model-store /models --models not-hot-dog=super-fancy-net.mar
這將在 predictions/not-hot-dog/
提供預測端點,並在封存檔中運行您的自訂服務代碼,封存檔中的清單將指向進入點。
使用 TorchServe 服務多個模型¶
範例:在啟動 TorchServe 時載入 model_store
中所有可用的模型
torchserve --start --model-store /models --models all
範例:多個模型的使用
torchserve --start --model-store /models --models name=model_location name2=model_location2
以下是使用本機模型檔案運行 resnet-18 和 vgg16 模型的範例。
torchserve --start --model-store /models --models resnet-18=resnet-18.mar squeezenet=squeezenet_v1.1.mar