Main Content

YOLO v2 および Intel MKL-DNN を使用したオブジェクト検出の C++ コードの生成

この例では、Intel® プロセッサで YOLO v2 オブジェクト検出ネットワークの C++ コードを生成する方法を説明します。生成されたコードは Intel Math Kernel Library for Deep Neural Networks (MKL-DNN) を使用します。

詳細については、YOLO v2 深層学習を使用したオブジェクトの検出 (Computer Vision Toolbox)を参照してください。

前提条件

  • Intel Math Kernel Library for Deep Neural Networks (MKL-DNN)

  • MKL-DNN ライブラリをサポートするプロセッサのリストを調べるには MKLDNN CPU Support を参照してください。

  • MATLAB® Coder™ (C++ コード生成のため)

  • MATLAB Coder Interface for Deep Learning サポート パッケージ

  • DAGNetwork オブジェクトの使用のための Deep Learning Toolbox™

  • ビデオ I/O 操作のための Computer Vision Toolbox™

サポートされるコンパイラとライブラリのバージョンの詳細については、サードパーティ ライブラリを使用するコードの生成を参照してください。

この例は、Linux®、Windows® および macOS プラットフォームでサポートされており、MATLAB Online ではサポートされていません。

事前学習済みの DAGNetwork オブジェクトの取得

DAG ネットワークには、畳み込み層、ReLU 層、バッチ正規化層、および YOLO v2 変換層と YOLO v2 出力層を含む、150 の層が含まれています。

net = getYOLOv2();
Downloading pretrained detector (98 MB)...

コマンド net.Layers を使用して、ネットワークのすべての層を確認します。

net.Layers

関数 yolov2_detection のコード生成

サンプルの付いた関数 yolov2_detection は、イメージ入力を取り、yolov2ResNet50VehicleExample.mat に保存されたネットワークを使用して、イメージに対して検出機能を実行します。この関数は yolov2ResNet50VehicleExample.mat から永続変数 yolov2Obj にネットワーク オブジェクトを読み込みます。後続の関数の呼び出しでは、永続的なオブジェクトが検出に再利用されます。

type('yolov2_detection.m')
function outImg = yolov2_detection(in)

%   Copyright 2018-2019 The MathWorks, Inc.

% A persistent object yolov2Obj is used to load the YOLOv2ObjectDetector object.
% At the first call to this function, the persistent object is constructed and
% set up. Subsequent calls to the function reuse the same object to call detection 
% on inputs, thus avoiding having to reconstruct and reload the
% network object.
persistent yolov2Obj;

if isempty(yolov2Obj)
    yolov2Obj = coder.loadDeepLearningNetwork('yolov2ResNet50VehicleExample.mat');
end

% pass in input
[bboxes,~,labels] = yolov2Obj.detect(in,'Threshold',0.5);
outImg = in;

% convert categorical labels to cell array of character vectors 
labels = cellstr(labels);


if ~(isempty(bboxes) && isempty(labels))
% Annotate detections in the image.
    outImg = insertObjectAnnotation(in,'rectangle',bboxes,labels);
end

コードを生成するには、MEX ターゲット向けのコード構成オブジェクトを作成し、ターゲット言語を C++ に設定します。関数 coder.DeepLearningConfig を使用して MKL-DNN 深層学習構成オブジェクトを作成します。そのオブジェクトをコード構成オブジェクトの DeepLearningConfig プロパティに割り当てます。入力のサイズを codegen コマンドへの引数として指定します。この例では、YOLO v2 ネットワークの入力層のサイズは [224,224,3] です。

cfg = coder.config('mex');
cfg.TargetLang = 'C++';
cfg.DeepLearningConfig = coder.DeepLearningConfig('mkldnn');
codegen -config cfg yolov2_detection -args {ones(224,224,3,'uint8')} -report
Code generation successful: To view the report, open('codegen\mex\yolov2_detection\html\report.mldatx')

生成された MEX 関数の入力例での実行

ビデオ ファイル リーダーを設定して入力ビデオ例 highway_lanechange.mp4 を読み取ります。ビデオと出力検出を表示するビデオ プレイヤーを作成します。

videoFile = 'highway_lanechange.mp4';
videoFreader = VideoReader(videoFile);
depVideoPlayer = vision.DeployableVideoPlayer('Size','Custom','CustomSize',[640 480]);

ビデオ入力をフレームごとに読み取り、検出器を使用してビデオ内の車両を検出します。

while hasFrame(videoFreader)
    I = readFrame(videoFreader);
    in = imresize(I,[224,224]);
    out = yolov2_detection_mex(in);
    depVideoPlayer(out);
    cont = hasFrame(videoFreader) && isOpen(depVideoPlayer); % Exit the loop if the video player figure window is closed
end
clear videoFreader

参考文献

[1] Redmon, Joseph, and Ali Farhadi. "YOLO9000:Better, Faster, Stronger."In 2017 IEEE Conference on Computer Vision and Pattern Recognition (CVPR), 6517–25. Honolulu, HI:IEEE, 2017.

参考

|

関連する例

詳細