コンテンツへスキップ

Triton 推論サーバーUltralytics YOLOv8

Triton Inference Server(旧名:TensorRT Inference Server)は、NVIDIAが開発したオープンソースのソフトウェアソリューションです。これは、NVIDIA GPU用に最適化されたクラウド推論ソリューションを提供します。Triton は、本番環境におけるAIモデルの大規模展開を簡素化します。Ultralytics YOLOv8 をTriton Inference Server と統合することで、スケーラブルで高性能なディープラーニング推論ワークロードを展開することができます。このガイドでは、統合のセットアップとテストの手順を説明します。



見るんだ: NVIDIATriton Inference Serverを使い始める。

Triton 推論サーバーとは?

Triton Inference Serverは、様々なAIモデルを本番環境で展開するために設計されている。TensorFlow 、PyTorch 、ONNX Runtime、その他多くのディープラーニングや機械学習フレームワークを幅広くサポートしています。主なユースケースは以下の通り:

  • 単一のサーバーインスタンスから複数のモデルを提供する。
  • サーバーを再起動することなく、モデルの動的なロードとアンロードが可能。
  • アンサンブル推論。複数のモデルを一緒に使用して結果を得ることができる。
  • A/Bテストとローリングアップデートのためのモデルのバージョニング。

前提条件

先に進む前に、以下の前提条件が揃っていることを確認してください:

  • あなたのマシンにインストールされているDocker。
  • インストール tritonclient:
    pip install tritonclient[all]
    

YOLOv8 からONNX 形式へのエクスポート

モデルをTriton にデプロイする前に、ONNX フォーマットにエクスポートする必要がある。ONNX (Open Neural Network Exchange)は、異なるディープラーニング・フレームワーク間でモデルを転送できるフォーマットです。を使用する。 export 関数から YOLO クラスである:

from ultralytics import YOLO

# Load a model
model = YOLO('yolov8n.pt')  # load an official model

# Export the model
onnx_file = model.export(format='onnx', dynamic=True)

Triton モデルリポジトリの設定

Triton モデル・リポジトリーは、Triton がモデルにアクセスし、ロードできる保存場所です。

  1. 必要なディレクトリ構造を作成する:

    from pathlib import Path
    
    # Define paths
    triton_repo_path = Path('tmp') / 'triton_repo'
    triton_model_path = triton_repo_path / 'yolo'
    
    # Create directories
    (triton_model_path / '1').mkdir(parents=True, exist_ok=True)
    
  2. エクスポートしたONNX モデルをTriton リポジトリに移動します:

    from pathlib import Path
    
    # Move ONNX model to Triton Model path
    Path(onnx_file).rename(triton_model_path / '1' / 'model.onnx')
    
    # Create config file
    (triton_model_path / 'config.pbtxt').touch()
    

Triton 推論サーバーの実行

Docker を使ってTriton Inference Server を実行する:

import subprocess
import time

from tritonclient.http import InferenceServerClient

# Define image https://catalog.ngc.nvidia.com/orgs/nvidia/containers/tritonserver
tag = 'nvcr.io/nvidia/tritonserver:23.09-py3'  # 6.4 GB

# Pull the image
subprocess.call(f'docker pull {tag}', shell=True)

# Run the Triton server and capture the container ID
container_id = subprocess.check_output(
    f'docker run -d --rm -v {triton_repo_path}:/models -p 8000:8000 {tag} tritonserver --model-repository=/models',
    shell=True).decode('utf-8').strip()

# Wait for the Triton server to start
triton_client = InferenceServerClient(url='localhost:8000', verbose=False, ssl=False)

# Wait until model is ready
for _ in range(10):
    with contextlib.suppress(Exception):
        assert triton_client.is_model_ready(model_name)
        break
    time.sleep(1)

その後、Triton Server モデルを使って推論を実行する:

from ultralytics import YOLO

# Load the Triton Server model
model = YOLO(f'http://localhost:8000/yolo', task='detect')

# Run inference on the server
results = model('path/to/image.jpg')

容器を片付ける:

# Kill and remove the container at the end of the test
subprocess.call(f'docker kill {container_id}', shell=True)

上記のステップに従うことで、Ultralytics YOLOv8 モデルをTriton Inference Server 上で効率的にデプロイして実行することができ、ディープラーニングの推論タスクにスケーラブルで高性能なソリューションを提供することができます。何か問題に直面したり、さらに質問がある場合は、 Triton 公式ドキュメントを参照するか、Ultralytics コミュニティにサポートを求めてください。



作成日:2023-11-12 更新日:2024-02-03
作成者:glenn-jocher(5)

コメント