オブジェクト検出 C++ デモ

example

このデモでは、非同期 API を使用したオブジェクト検出ネットワークの推論を紹介します。非同期 API を使用すると、アプリは推論の完了を待機することなく、アクセラレーターがビジー状態でもホスト上で処理を続行できるため、アプリケーション全体のフレームレートが向上します。具体的には、このデモでは、nireq フラグを使用して設定した推論要求の数が保持されます。一部の推論要求は OpenVINO™ ランタイムによって処理されますが、他の要求には新しいフレームデータを入力して非同期で開始したり、推論要求から次の出力を取得して表示することができます。

この手法は、利用可能な任意の並列スラックに一般化できます。例えば、推論を実行し、結果として得られる (前の) フレームを同時にエンコードしたり、顔検出の結果に加えて感情検出を実行するなど、推論を追加して実行できます。ただし、パフォーマンスに関する重要な注意事項があります。例えば、並列実行されるタスクでは、共有計算リソースのオーバーサブスクライブを避ける必要があります。例えば、推論が iGPU で実行され、CPU が基本的にアイドル状態である場合、CPU で並行して処理を実行するのが合理的です。しかし、推論が、例えば GPU 上で実行される場合、デバイスはすでにビジー状態であるため、(結果のビデオ) エンコードを同じ GPU 上で並行して実行してもほとんど利益は得られません。

非同期 API に関するその他のパフォーマンスへの影響とヒントについては、最適化ガイドで説明されています。

デモの目的は次のとおりです。

  • OpenCV* による入力ビデオのサポート

  • 結果の境界ボックスとテキストラベル (ラベルファイルから、-labels オプションを参照) またはクラス番号 (ファイルが提供されていない場合) の視覚化

  • OpenCV は、結果として得られる境界ボックスやラベルの描画に使用されます

  • 非同期 API の動作のデモンストレーション

  • 1 つのアプリケーションでの複数のモデル・アーキテクチャーをサポート (前処理および後処理を含む) するデモンストレーション

どのように動作するか

起動時に、アプリケーションはコマンドライン・パラメーターを受け取り、モデルを OpenVINO™ ランタイムプラグインにロードします。OpenCV VideoCapture からフレームを取得すると、推論を実行して結果を表示します。

注: デフォルトでは、Open Model Zoo のデモは BGR チャネル順序での入力を期待します。RGB 順序で動作するようにモデルをトレーニングした場合は、サンプルまたはデモ・アプリケーションでデフォルトのチャネル順序を手動で再配置するか、--reverse_input_channels 引数を指定したモデル・オプティマイザー・ツールを使用してモデルを再変換する必要があります。引数の詳細については、[前処理計算の埋め込み](@ref openvino_docs_MO_DG_Additional_Optimization_Use_Cases) の入力チャネルを反転するセクションを参照してください。

このデモは、以下のコードに示すように、入力/出力をカプセル化し、スケジュールと結果の待機を分離する “推論要求” を使用することで非同期的に動作します。

    while (true) {
        capture frame
        take empty InferRequest from pool
        if(empty InferRequest available) {
            populate empty InferRequest
            set completion callback
            submit InferRequest
        }

        while (there're completed InferRequests) {
            get inference results from InferRequest
            process inference results
            display the frame
        }
    }

非同期実行を含む要求ベースの OpenVINO™ ランタイム API の詳細については、OpenVINO™ ランタイムとアプリケーションの統合を参照してください。

実行の準備

デモの入力画像またはビデオファイルについては、Open Model Zoo デモの概要デモに使用できるメディアファイルのセクションを参照してください。デモでサポートされるモデルリストは、<omz_dir>/demos/object_detection_demo/cpp/models.lst ファイルにあります。このファイルは、モデル・ダウンローダーおよびコンバーターのパラメーターとして使用され、モデルをダウンロードし、必要に応じて OpenVINO IR 形式 (*.xml + *.bin) に変換できます。

モデル・ダウンローダーの使用例:

omz_downloader --list models.lst

モデル・コンバーターの使用例:

omz_converter --list models.lst

サポートされるモデル

  • architecture_type = centernet

    • ctdet_coco_dlav0_512

  • architecture_type = faceboxes

    • faceboxes-pytorch

  • architecture_type = ssd

    • efficientdet-d0-tf

    • efficientdet-d1-tf

    • face-detection-0200

    • face-detection-0202

    • face-detection-0204

    • face-detection-0205

    • face-detection-0206

    • face-detection-adas-0001

    • face-detection-retail-0004

    • face-detection-retail-0005

    • faster-rcnn-resnet101-coco-sparse-60-0001

    • faster_rcnn_inception_resnet_v2_atrous_coco

    • faster_rcnn_resnet50_coco

    • pedestrian-and-vehicle-detector-adas-0001

    • pedestrian-detection-adas-0002

    • person-detection-0106

    • person-detection-0200

    • person-detection-0201

    • person-detection-0202

    • person-detection-0203

    • person-detection-0301

    • person-detection-0302

    • person-detection-0303

    • person-detection-retail-0013

    • person-vehicle-bike-detection-2000

    • person-vehicle-bike-detection-2001

    • person-vehicle-bike-detection-2002

    • person-vehicle-bike-detection-2003

    • person-vehicle-bike-detection-2004

    • product-detection-0001

    • retinaface-resnet50-pytorch

    • retinanet-tf

    • rfcn-resnet101-coco-tf

    • ssd-resnet34-1200-onnx

    • ssd_mobilenet_v1_coco

    • ssd_mobilenet_v1_fpn_coco

    • ssdlite_mobilenet_v2

    • vehicle-detection-0200

    • vehicle-detection-0201

    • vehicle-detection-0202

    • vehicle-detection-adas-0002

    • vehicle-license-plate-detection-barrier-0106

    • vehicle-license-plate-detection-barrier-0123

  • architecture_type = yolo

    • mobilenet-yolo-v4-syg

    • person-vehicle-bike-detection-crossroad-yolov3-1020

    • yolo-v3-tf

    • yolo-v3-tiny-tf

    • yolo-v1-tiny-tf

    • yolo-v2-ava-0001

    • yolo-v2-ava-sparse-35-0001

    • yolo-v2-ava-sparse-70-0001

    • yolo-v2-tf

    • yolo-v2-tiny-ava-0001

    • yolo-v2-tiny-ava-sparse-30-0001

    • yolo-v2-tiny-ava-sparse-60-0001

    • yolo-v2-tiny-tf

    • yolo-v2-tiny-vehicle-detection-0001

    • yolo-v4-tf

    • yolo-v4-tiny-tf

    • yolof

  • architecture_type = yolov3-onnx

    • yolo-v3-onnx

    • yolo-v3-tiny-onnx

  • architecture_type = yolox

    • yolox-tiny

注: 各種デバイス向けのモデル推論サポートの詳細については、インテルの事前トレーニング・モデルのデバイスサポートパブリックの事前トレーニング・モデルのデバイスサポートの表を参照してください。

実行

-h オプションを指定してデモを実行すると、ヘルプメッセージが表示されます。

object_detection_demo [OPTION]
Options:

                                    -h                        Print a usage message.
                                    -at "<type>"              Required. Architecture type: centernet, faceboxes, retinaface, retinaface-pytorch, ssd, yolo, yolov3-onnx or yolox
                                    -i                        Required. An input to process. The input must be a single image, a folder of images, video file or camera id.
                                    -m "<path>"               Required. Path to an .xml file with a trained model.
                                    -o "<path>"               Optional. Name of the output file(s) to save. Frames of odd width or height can be truncated. See https://github.com/opencv/opencv/pull/24086
                                    -limit "<num>"            Optional. Number of frames to store in output. If 0 is set, all frames are stored.
                                    -d "<device>"             Optional. Specify the target device to infer on (the list of available devices is shown below). Default value is CPU. Use "-d HETERO:<comma-separated_devices_list>" format to specify HETERO plugin. The demo will look for a suitable plugin for a specified device.
                                    -labels "<path>"          Optional. Path to a file with labels mapping.
                                    -layout "<string>"        Optional. Specify inputs layouts. Ex. NCHW or input0:NCHW,input1:NC in case of more than one input.
                                    -r                        Optional. Inference results as raw values.
                                    -t                        Optional. Probability threshold for detections.
                                    -iou_t                    Optional. Filtering intersection over union threshold for overlapping boxes.
                                    -auto_resize              Optional. Enables resizable input with support of ROI crop & auto resize.
                                    -nireq "<integer>"        Optional. Number of infer requests. If this option is omitted, number of infer requests is determined automatically.
                                    -nthreads "<integer>"     Optional. Number of threads.
                                    -nstreams                 Optional. Number of streams to use for inference on the CPU or/and GPU in throughput mode (for HETERO and MULTI device cases use format <device1>:<nstreams1>,<device2>:<nstreams2> or just <nstreams>)
                                    -loop                     Optional. Enable reading the input in a loop.
                                    -no_show                  Optional. Don't show output.
                                    -output_resolution        Optional. Specify the maximum output window resolution in (width x height) format. Example: 1280x720. Input frame size used by default.
                                    -u                        Optional. List of monitors to show initially.
                                    -yolo_af                  Optional. Use advanced postprocessing/filtering algorithm for YOLO.
                                    -anchors                  Optional. A comma separated list of anchors. By default used default anchors for model. Only for YOLOV4 architecture type.
                                    -masks                    Optional. A comma separated list of mask for anchors. By default used default masks for model. Only for YOLOV4 architecture type.
                                    -reverse_input_channels   Optional. Switch the input channels order from BGR to RGB.
                                    -mean_values              Optional. Normalize input by subtracting the mean values per channel. Example: "255.0 255.0 255.0"
                                    -scale_values             Optional. Divide input by scale values per channel. Division is applied after mean values subtraction. Example: "255.0 255.0 255.0"

ラベル ファイルを使用する場合、モデルの出力に対応する必要があります。デモでは、ファイルにリストされるラベルがインデックス 0 から始まるように、1 行に 1 つのラベルを使用します (つまり、最初の行には ID 0 のラベルが含まれます)。一部のモデルは 1 ~ N の範囲のラベル ID を返すケースがあることに注意してください。この場合、ラベルファイルの最初の行には “背景” ラベルが含まれている必要があります。

次のコマンドを使用すると、事前トレーニングされたオブジェクト検出モデルを使用して GPU で推論を実行できます。

./object_detection_demo \
  -d GPU \
  -i <path_to_video>/inputVideo.mp4 \
  -m <path_to_model>/efficientdet-d0-tf.xml \
  -at ssd \
  -labels <omz_dir>/data/dataset_classes/voc_20cl_bkgr.txt

注: 単一の画像を入力として指定すると、デモはすぐに処理してレンダリングし終了します。推論結果を画面上で継続的に視覚化するには、loop オプションを適用します。これにより、単一の画像がループで処理されます。

-o オプションを使用すると、処理結果を Motion JPEG AVI ファイル、または別の JPEG または PNG ファイルに保存できます。

  • 処理結果を AVI ファイルに保存するには、avi 拡張子を付けた出力ファイル名を指定します (例: -o output.avi)。

  • 処理結果を画像として保存するには、出力画像ファイルのテンプレート名を拡張子 jpg または png で指定します (例: -o output_%03d.jpg)。実際のファイル名は、実行時に正規表現 %03d をフレーム番号に置き換えることによってテンプレートから構築され、output_000.jpgoutput_001.jpg などになります。カメラなど連続入力ストリームでディスク領域のオーバーランを避けるため、limit オプションを使用して出力ファイルに保存されるデータの量を制限できます。デフォルト値は 1000 です。これを変更するには、-limit N オプションを適用します。ここで、N は保存するフレームの数です。

注: Windows* システムには、デフォルトでは Motion JPEG コーデックがインストールされていない場合があります。この場合、OpenVINO ™ インストール・パッケージに付属する、<INSTALL_DIR>/opencv/ffmpeg-download.ps1 にある PowerShell スクリプトを使用して OpenCV FFMPEG バックエンドをダウンロードできます。OpenVINO ™ がシステムで保護されたフォルダーにインストールされている場合 (一般的なケース)、スクリプトは管理者権限で実行する必要があります。あるいは、結果を画像として保存することもできます。

デモの出力

このデモでは、OpenCV を使用して、検出された結果のフレームを表示します (境界ボックスとラベルが提供されている場合は、それらとしてレンダリングされます)。
デモレポート:

  • FPS: ビデオフレーム処理の平均レート (1 秒あたりのフレーム数)。

  • レイテンシー: 1 フレームの処理 (フレームの読み取りから結果の表示まで) に必要な平均時間。

  • 次の各パイプライン・ステージのレイテンシー:

    • デコード — 入力データをキャプチャー。

    • 前処理 — 推論のためのデータの準備。

    • 推論 — 入力データ (画像) を推論して結果を取得。

    • 後処理 — 出力用の推論結果を準備。

    • レンダリング — 出力画像を生成。

これらのメトリックを使用して、アプリケーション・レベルのパフォーマンスを測定できます。