カスタム・モデル・ローダー

ファイルからモデルを直接ロードする前に、追加の処理やチェックが必要になる場合があります。典型的な例は、暗号化されたファイルのロードやモデルライセンスの確認などです。このような場合、このカスタム・ローダー・インターフェイスを使用すると、ユーザーは事前定義されたインターフェイスに基づいて独自のカスタム・モデル・ローダーを作成し、動的ライブラリーとして同じものをロードできます。

このドキュメントでは、カスタムローダー構成の追加、カスタム・ローダー・インターフェイス、その他ついて説明します。

カスタム・ローダー・インターフェイス

モデルサーバー構成ファイル

カスタムローダーを定義する新しいセクションが構成ファイルの構文に追加されます。

   "custom_loader_config_list":[
    {
            "config":{
            "loader_name": "#custom loader name",
            "library_path": "#Shared library path",
            "loader_config_file": "#Separate config file with custom loader specific details in json format"
            }
    }
    ]

上記の構文を使用すると、モデルサーバー構成ファイルで複数のカスタムローダーを定義できます。

カスタムローダーを使用して特定のモデルをロードするには、以下に示すようにモデル構成にパラメーターを追加します。

    "model_config_list":[
    {
            "config":{
            "name":"sampleloader-model",
            "base_path":"model/fdsample",
            "custom_loader_options": {"loader_name":  "#custom loader name", "#parameters for custom loader including file name etc in json format"}
            }
    }
    ]

カスタムローダー向けの C++ API インターフェイス

基本クラス CustomLoaderInterface とインターフェイス API は src/customloaderinterface.hpp で定義されています。

API の詳細については、このファイルを参照してください。

カスタムローダーを記述

基本クラス CustomLoaderInterface から新しいカスタム・ローダー・クラスを派生し、指定されたすべての仮想関数を定義します。ライブラリーにはCustomLoaderInterface createCustomLoader* という次の名前の関数が含まれます。この関数は、新しいカスタムローダーを割り当てて、基本クラスへのポインターを返します。

ファイルの読み取り、ロードに必要なバッファーを返すカスタムローダーの例が実装され、src/example/SampleCustomLoader にリファレンスとして提供されます。

このカスタムローダーはモデルサーバーのビルドで構築されており、openvino/model_server-build:latest で利用できます。共有ライブラリーは、この Docker からコピーするか、makefile を使用してビルドできます。Makefile の例がディレクトリー内にリファレンスとして提供されています。

カスタムローダーの実行例

カスタムローダーの例は “src/example/SampleCustomLoader” に実装されています。

カスタム・モデル・ローダーの例を使用するには、次の手順に従います。

ステップ 1: テスト・ディレクトリーを用意します。

モデルサーバーのコードをダウンロードし、docker をビルドします (make docker_build)。Docker の準備ができたら、すべてのアーティファクトをダウンロードするフォルダーを作成します。モデル、クライアント・コンポーネント、イメージがすべてこのフォルダーにダウンロードされていることを確認してください。さらに、このフォルダーに必要な json ファイルを作成します。

mkdir test_custom_loader

ステップ 2: カスタム・ローダー・ライブラリーのサンプルを準備します。

git clone https://github.com/openvinotoolkit/model_server.git
cd model_server/src/example/SampleCustomLoader
make docker_build

lib/libsampleloader.so パスにライブラリーが生成されます。

lib を、前に作成したディレクトリー test_custom_loader にコピーします。

cp -r lib ../../../../test_custom_loader/lib
cd ../../../../test_custom_loader

ステップ 3: モデルをダウンロードします。

curl --create-dirs https://storage.openvinotoolkit.org/repositories/open_model_zoo/2022.1/models_bin/2/face-detection-retail-0004/FP32/face-detection-retail-0004.xml https://storage.openvinotoolkit.org/repositories/open_model_zoo/2022.1/models_bin/2/face-detection-retail-0004/FP32/face-detection-retail-0004.bin -o model/fdsample/1/face-detection-retail-0004.xml -o model/fdsample/1/face-detection-retail-0004.bin

chmod -R 755 ./model

ステップ 4: 必要なクライアント・コンポーネントをダウンロードします。

curl --fail https://raw.githubusercontent.com/openvinotoolkit/model_server/releases/2024/0/demos/common/python/client_utils.py -o client_utils.py https://raw.githubusercontent.com/openvinotoolkit/model_server/releases/2024/0/demos/face_detection/python/face_detection.py -o face_detection.py https://raw.githubusercontent.com/openvinotoolkit/model_server/releases/2024/0/demos/common/python/requirements.txt -o requirements.txt

pip3 install --upgrade pip
pip3 install -r requirements.txt

ステップ 5: 推論データをダウンロードします。

curl --fail --create-dirs https://raw.githubusercontent.com/openvinotoolkit/model_server/releases/2024/0/demos/common/static/images/people/people1.jpeg -o images/people1.jpeg

ステップ 6: 設定用の JSON を用意します。

設定ファイルの例: sampleloader.json ファイルを作成します。

echo '	
		{
	        "custom_loader_config_list":[
	        {
	                "config":{
	                "loader_name":"sampleloader",
	                "library_path": "/sampleloader/lib/libsampleloader.so",
	                "loader_config_file": "config.json"
	                }
	        }
	        ],
	        "model_config_list":[
	        {
	                "config":{
	                "name":"sampleloader-model",
	                "base_path":"/sampleloader/model/fdsample",
	                "custom_loader_options": {"loader_name":  "sampleloader", "model_file":  "face-detection-retail-0004.xml", "bin_file": "face-detection-retail-0004.bin", "enable_file": "face-detection-retail.status"}
	                }
	        }
	        ]
	}' >> sampleloader.json

ステップ 7: モデル・サーバー・コンテナを開始します。

docker run -d -v ${PWD}:/sampleloader -p 9000:9000 openvino/model_server:latest --config_path /sampleloader/sampleloader.json --port 9000  --log_level DEBUG

ステップ 8: 推論を実行して結果を確認します。

python3 face_detection.py --batch_size 1 --width 300 --height 300 --input_images_dir images --output_dir results --model_name sampleloader-model --grpc_port 9000

['people1.jpeg']
Start processing 1 iterations with batch size 1

Request shape (1, 3, 300, 300)
Response shape (1, 1, 200, 7)
image in batch item 0 , output shape (3, 300, 300)
detection 0 [[[0.         1.         0.9999999  0.22627862 0.35042182 0.27032945
   0.43312052]]]
x_min 67
y_min 105
x_max 81
y_max 129
detection 1 [[[0.         1.         0.9999999  0.7980574  0.35572374 0.8422255
   0.42749226]]]
x_min 239
y_min 106
x_max 252
y_max 128
detection 2 [[[0.         1.         0.9999927  0.4413453  0.29417545 0.48191014
   0.37180012]]]
x_min 132
y_min 88
x_max 144
y_max 111
detection 3 [[[0.         1.         0.99964225 0.55356365 0.30400735 0.59468836
   0.38264883]]]
x_min 166
y_min 91
x_max 178
y_max 114
detection 4 [[[0.         1.         0.9993523  0.32912934 0.38222942 0.36873418
   0.44978413]]]
x_min 98
y_min 114
x_max 110
y_max 134
detection 5 [[[0.         1.         0.9992501  0.33522347 0.6249954  0.38323137
   0.7104612 ]]]
x_min 100
y_min 187
x_max 114
y_max 213
detection 6 [[[0.        1.        0.9976745 0.6488881 0.5992611 0.6988456 0.6907843]]]
x_min 194
y_min 179
x_max 209
y_max 207
detection 7 [[[0.        1.        0.9962077 0.5180316 0.5640176 0.5703776 0.6516389]]]
x_min 155
y_min 169
x_max 171
y_max 195
detection 8 [[[0.        1.        0.722986  0.6746904 0.3287916 0.7198625 0.4061382]]]
x_min 202
y_min 98
x_max 215
y_max 121
detection 9 [[[0.         1.         0.566281   0.13994813 0.36546633 0.18363091
   0.44829145]]]
x_min 41
y_min 109
x_max 55
y_max 134
saving result to results/1_0.jpg
Iteration 1; Processing time: 21.92 ms; speed 45.61 fps

processing time for all iterations
average time: 21.00 ms; average speed: 47.62 fps
median time: 21.00 ms; median speed: 47.62 fps
max time: 21.00 ms; min speed: 47.62 fps
min time: 21.00 ms; max speed: 47.62 fps
time percentile 90: 21.00 ms; speed percentile 90: 47.62 fps
time percentile 50: 21.00 ms; speed percentile 50: 47.62 fps
time standard deviation: 0.00
time variance: 0.00

モデルを制限リストに登録

モデルが構成ファイルで指定されている場合でも、モデルライセンスの有効期限が切れているなど、特定の条件下ではモデルの無効化が必要となる場合があります。この機能を実証するために、このサンプルローダーでは、ユーザーが構成ファイルの “custom_loader_options” にオプションのパラメーター “enable_file” を指定できるようにしています。ファイルはモデル・バージョン・フォルダー (バージョン番号を含むベースとなるパス) に存在する必要があります。

モデルを無効にする場合、指定した名前でファイルを作成し、そのファイルに DISABLED という 1 行を追加します。カスタムローダーはこのファイルを定期的にチェックし、DISABLED の文字列が存在するとモデルをアンロードするようにマークします。モデルを再ロードするには、ファイルから文字列を削除するか、ファイルを削除します。