カスタムノードと動的形状#

はじめに

ここでは、入力データをモデルに渡す前に入力のサイズ変更を実行するカスタムノードを使用して、単純な有向非巡回グラフ (DAG) を構成する方法を示します。

以下のノードはデモ用として提供されています。カスタムノードのビルドおよび使用方法については、画像変換を参照してください。

この設定で推論を実行するには、以下を使用します:

face_detection.py スクリプトで face_detection_retail_0004 モデルを使用すると、画像が読み込まれ、指定する幅と高さにサイズ変更されます。次に、サーバーからの出力が処理され、検出された顔の周囲に境界ボックスが描画された推論結果が表示されます。

ステップ#

OpenVINO™ モデルサーバー GitHub リポジトリーのクローンを作成し、model_server ディレクトリーに移動します。

git clone https://github.com/openvinotoolkit/model_server.git 
cd model_server

事前トレーニング済みモデルをダウンロード#

モデルファイルをダウンロードして、models ディレクトリーに保存します:

mkdir -p models/face_detection/1 
curl https://storage.openvinotoolkit.org/repositories/open_model_zoo/2022.1/models_bin/3/face-detection-retail-0004/FP32/face-detection-retail-0004.bin https://storage.openvinotoolkit.org/repositories/open_model_zoo/2022.1/models_bin/3/face-detection-retail-0004/FP32/face-detection-retail-0004.xml -o models/face_detection/1/face-detection-retail-0004.bin -o models/face_detection/1/face-detection-retail-0004.xml

最新モデルのサーバーイメージをプル#

OpenVINO™ モデルサーバーの最新バージョンを Docker Hub から取得します :

docker pull openvino/model_server:latest

カスタムノードをビルド#

  1. カスタムノードのサンプル・ディレクトリーに移動します:

    cd src/custom_nodes
  2. カスタムノードをビルドします:

    # UBI ベースイメージを使用する場合は 'redhat` に置き換えます 
    export BASE_OS=ubuntu make 
    BASE_OS=${BASE_OS} 
    NODES=image_transformation
  3. カスタムノードを models リポジトリーにコピーします。

    cp lib/${BASE_OS}/libcustom_node_image_transformation.so ../../models/libcustom_node_image_transformation.so

モデルサーバー構成ファイルの作成#

models ディレクトリーに移動します:

cd ../../models

config.json という名前の新しいファイルを models ディレクトリーに作成します:

echo '{ 
    "model_config_list": [ 
        { 
            "config": { 
                "name": "face_detection_retail", 
                "base_path": "/models/face_detection" 
            } 
        } 
    ], 
    "custom_node_library_config_list": [ 
        {"name": "image_transformation", 
        "base_path": "/models/libcustom_node_image_transformation.so"} 
    ], 
    "pipeline_config_list": [ 
    { 
        "name": "face_detection", 
        "inputs": ["data"], 
        "nodes": [ 
            { 
                "name": "image_transformation_node", 
                "library_name": "image_transformation", 
                "type": "custom", 
                "params": { 
                    "target_image_width": "300", 
                    "target_image_height": "300", 

                    "mean_values": "[123.675,116.28,103.53]", 
                    "scale_values": "[58.395,57.12,57.375]", 

                    "original_image_color_order": "BGR", 
                    "target_image_color_order": "BGR", 

                    "original_image_layout": "NCHW", 
                    "target_image_layout": "NCHW" 
                }, 
                "inputs": [ 
                    {"image": { 
                        "node_name": "request", 
                        "data_item": "data"}}], 
                "outputs": [ 
                    {"data_item": "image", 
                    "alias": "transformed_image"}] 
            }, 
            { 
                "name": "face_detection_node", 
                "model_name": "face_detection_retail", 
                "type": "DL model", 
                "inputs": [ 
                    {"data": 
                        { 
                            "node_name": "image_transformation_node", 
                            "data_item": "transformed_image" 
                        } 
                    } 
                ], 
                "outputs": [ 
                    {"data_item": "detection_out", 
                    "alias": "face_detection_output"} 
                ]
             } 
        ], 
        "outputs": [ 
            {"detection_out": { 
                "node_name": "face_detection_node", 
                "data_item": "face_detection_output"}} 
        ] 
    } 
 ] 
}' >> config.json

ダウンロードしたモデルを使用してモデル・サーバー・コンテナを起動#

前の手順で取得したイメージを使用してコンテナを起動し、<models_dir> をマウントします:

docker run --rm -d -v ${PWD}:/models -p 9000:9000 openvino/model_server:latest --config_path /models/config.json --port 9000

クライアントの実行#

cd ../demos/face_detection/python 
virtualenv .venv ..venv/bin/activate 
pip install -r ../../common/python/requirements.txt 
mkdir results_500x500 

python face_detection.py --grpc_port 9000 --width 500 --height 500 --input_images_dir ../../common/static/images/people --output_dir results_500x500 --model_name 
face_detection 

mkdir results_600x400 

python face_detection.py --grpc_port 9000 --width 600 --height 400 --input_images_dir ../../common/static/images/people --output_dir results_600x400 --model_name 
face_detection

クライアントの実行結果は、--output_dir に指定されるディレクトリーで利用可能になります: