このページの翻訳は最新ではありません。ここをクリックして、英語の最新版を参照してください。
YOLO v3 深層学習を使用したオブジェクト検出用のコード生成
この例では、カスタム層を使用した You Only Look Once (YOLO) v3 オブジェクト検出器用の CUDA® MEX を生成する方法を示します。YOLO v3 は YOLO v2 を改良したもので、複数のスケールにおける検出を追加してより小さなオブジェクトを検出できるようになっています。さらに、学習で使用される損失関数は、境界ボックス回帰用の平均二乗誤差と、オブジェクト分類用のバイナリ交差エントロピーに分割されており、検出精度が向上しています。この例で使用する YOLO v3 ネットワークは、Computer Vision Toolbox (TM) の "YOLO v3 深層学習を使用したオブジェクトの検出" の例を使用して学習させたものです。詳細は、YOLO v3 深層学習を使用したオブジェクトの検出 (Computer Vision Toolbox) を参照してください。
サードパーティの必要条件
必須
CUDA 対応 NVIDIA® GPU および互換性のあるドライバー。
オプション
スタティック ライブラリ、ダイナミック ライブラリ、または実行可能ファイルなどの MEX 以外のビルドについて、この例では以下の要件も適用されます。
NVIDIA CUDA Toolkit。
NVIDIA cuDNN ライブラリ。
コンパイラおよびライブラリの環境変数。詳細は、サードパーティ ハードウェアと前提条件となる製品の設定を参照してください。
GPU 環境の検証
この例を実行するためのコンパイラおよびライブラリが正しく設定されていることを確認するために、関数coder.checkGpuInstall
を使用します。
envCfg = coder.gpuEnvConfig('host'); envCfg.DeepLibTarget = 'cudnn'; envCfg.DeepCodegen = 1; envCfg.Quiet = 1; coder.checkGpuInstall(envCfg);
YOLO v3 ネットワーク
この例の YOLO v3 ネットワークは、squeezenet
(Deep Learning Toolbox)がベースとなっています。このネットワークは、SqueezeNet の特徴抽出ネットワークを使用し、最後に 2 つの検出ヘッドが追加されています。2 番目の検出ヘッドのサイズは、最初の検出ヘッドの 2 倍となっているため、小さなオブジェクトをより的確に検出できます。検出するオブジェクトのサイズに基づいて、さまざまなサイズの検出ヘッドを任意の数で指定できます。YOLO v3 ネットワークは、学習データを使用して推定されたアンカー ボックスを使用します。これにより、データセットの種類に対応した初期の事前確率が改善され、ボックスを正確に予測できるようにネットワークに学習させることができます。アンカー ボックスの詳細については、アンカー ボックスによるオブジェクトの検出 (Computer Vision Toolbox)を参照してください。
この例の YOLO v3 ネットワークを次の図に示します。
各検出ヘッドは、境界ボックス座標 (x、y、幅、高さ)、オブジェクトの信頼度、および各アンカー ボックス マスクに対するクラスの確率を予測します。そのため、各検出ヘッドにおける最終畳み込み層の出力フィルターの数は、アンカー ボックス マスクの数と、アンカー ボックスあたりの予測要素の数を乗算したものになります。この検出ヘッドは、ネットワークの出力層を構成します。
事前学習済みの YOLO v3 ネットワーク
この例では、事前学習済みの YOLO v3 ネットワークを含む yolov3SqueezeNetVehicleExample_21a.zip
ファイルを使用します。このファイルのサイズは約 9 MB です。MathWorks の Web サイトからファイルをダウンロードし、ファイルを解凍します。
fileName = matlab.internal.examples.downloadSupportFile('vision/data/','yolov3SqueezeNetVehicleExample_21a.zip'); unzip(fileName);
この例で使用する YOLO v3 ネットワークの学習は、YOLO v3 深層学習を使用したオブジェクトの検出 (Computer Vision Toolbox)で説明した手順を使用して行っています。
matFile = 'yolov3SqueezeNetVehicleExample_21a.mat';
pretrained = load(matFile);
net = pretrained.net;
YOLO v3 ネットワークは、resize2dLayer
(Image Processing Toolbox)を使用し、隣接するピクセル値をスケーリング係数 2 で複製することによって、2 次元の入力イメージのサイズを変更します。コード生成がサポートされているカスタム層として、resize2DLayer が実装されています。詳細については、コード生成用のカスタム深層学習層の定義 (Deep Learning Toolbox)を参照してください。
メモ: 事前学習済みの検出器ネットワークは、Computer Vision Toolbox™ Model for YOLO v3 Object Detection サポート パッケージからも利用できます。
この事前学習済みネットワークを使用するには、最初にアドオン エクスプローラーから Computer Vision Toolbox Model for YOLO v3 Object Detection をインストールしなければなりません。アドオンのインストールの詳細については、アドオンの取得と管理を参照してください。
次に、ネットワークを yolov3ObjectDetector
オブジェクトから MAT ファイルに保存して続行します。次に例を示します。
detector = yolov3ObjectDetector('darknet53-coco'); net = detector.Network; matFile = 'pretrainedYOLOv3Detector.mat'; save(matFile,'net');
エントリポイント関数 yolov3Detect
エントリポイント関数 yolov3Detect
は、入力イメージを受け取り、それを関数 yolov3Predict
を介して予測用の学習済みネットワークに渡します。関数 yolov3Predict
は、ネットワーク オブジェクトを MAT ファイルから永続変数に読み込み、以降の予測呼び出しでその永続オブジェクトを再利用します。具体的には、この関数は、YOLO v3 深層学習を使用したオブジェクトの検出 (Computer Vision Toolbox)の例で学習させたネットワークのdlnetwork
(Deep Learning Toolbox)表現を使用します。次に、yolov3Predict
呼び出しから取得した YOLO v3 グリッド セル座標からの予測が、サポート関数 generateTiledAnchors
および applyAnchorBoxOffsets
を使用して境界ボックス座標に変換されます。
type('yolov3Detect.m')
function [bboxes,scores,labelsIndex] = yolov3Detect(matFile, im,... networkInputSize, networkOutputs, confidenceThreshold,... overlapThreshold, classes) % The yolov3Detect function detects the bounding boxes, scores, and % labelsIndex in an image. %#codegen % Copyright 2020-2021 The MathWorks, Inc. %% Preprocess Data % This example applies all the preprocessing transforms to the data set % applied during training, except data augmentation. Because the example % uses a pretrained YOLO v3 network, the input data must be representative % of the original data and left unmodified for unbiased evaluation. % Specifically the following preprocessing operations are applied to the % input data. % 1. Resize the images to the network input size, as the images are % bigger than networkInputSize. 2. Scale the image pixels in the range % [0 1]. 3. Convert the resized and rescaled image to a dlarray object. im = dlarray(preprocessData(im, networkInputSize), "SSCB"); imageSize = size(im,[1,2]); %% Define Anchor Boxes % Specify the anchor boxes estimated on the basis of the preprocessed % training data used when training the YOLO v3 network. These anchor box % values are same as mentioned in "Object Detection Using YOLO v3 Deep % Learning" example. For details on estimating anchor boxes, see "Anchor % Boxes for Object Detection". anchors = [ 41 34; 163 130; 98 93; 144 125; 33 24; 69 66]; % Specify anchorBoxMasks to select anchor boxes to use in both the % detection heads of the YOLO v3 network. anchorBoxMasks is a cell array of % size M-by-1, where M denotes the number of detection heads. Each % detection head consists of a 1-by-N array of row index of anchors in % anchorBoxes, where N is the number of anchor boxes to use. Select anchor % boxes for each detection head based on size-use larger anchor boxes at % lower scale and smaller anchor boxes at higher scale. To do so, sort the % anchor boxes with the larger anchor boxes first and assign the first % three to the first detection head and the next three to the second % detection head. area = anchors(:, 1).*anchors(:, 2); [~, idx] = sort(area, 'descend'); anchors = anchors(idx, :); anchorBoxMasks = {[1,2,3],[4,5,6]}; %% Predict on Yolov3 % Predict and filter the detections based on confidence threshold. predictions = yolov3Predict(matFile,im,networkOutputs,anchorBoxMasks); %% Generate Detections % indices corresponding to x,y,w,h predictions for bounding boxes anchorIndex = 2:5; tiledAnchors = generateTiledAnchors(predictions,anchors,anchorBoxMasks,... anchorIndex); predictions = applyAnchorBoxOffsets(tiledAnchors, predictions,... networkInputSize, anchorIndex); [bboxes,scores,labelsIndex] = generateYOLOv3DetectionsForCodegen(predictions,... confidenceThreshold, overlapThreshold, imageSize, classes); end function YPredCell = yolov3Predict(matFile,im,networkOutputs,anchorBoxMask) % Predict the output of network and extract the confidence, x, y, % width, height, and class. % load the deep learning network for prediction persistent net; if isempty(net) net = coder.loadDeepLearningNetwork(matFile); end YPredictions = cell(coder.const(networkOutputs), 1); [YPredictions{:}] = predict(net, im); YPredCell = extractPredictions(YPredictions, anchorBoxMask); % Apply activation to the predicted cell array. YPredCell = applyActivations(YPredCell); end
オブジェクト検出用のエントリポイント関数の評価
次の手順に従って、テスト データからのイメージに対するエントリポイント関数を評価します。
信頼度しきい値に 0.5 を指定し、信頼度スコアがこの値より高い検出のみ保持します。
オーバーラップしきい値に 0.5 を指定し、オーバーラップしている検出を削除します。
入力データからイメージを読み取ります。
エントリポイント関数
yolov3Detect
を使用して、予測された境界ボックス、信頼度スコア、およびクラス ラベルを取得します。境界ボックスと信頼度スコアと共にイメージを表示します。
必要なしきい値を定義します。
confidenceThreshold = 0.5; overlapThreshold = 0.5;
学習済みネットワークのネットワーク入力サイズと、ネットワーク出力の数を指定します。
networkInputSize = [227 227 3]; networkOutputs = numel(net.OutputNames);
YOLO v3 深層学習を使用したオブジェクトの検出 (Computer Vision Toolbox)の例のラベル付きデータ セットから取得したサンプル イメージ データを読み取ります。このイメージには、車両タイプのオブジェクトのインスタンスが 1 つ含まれています。
I = imread('vehicleImage.jpg');
クラス名を指定します。
classNames = {'vehicle'};
YOLO v3 ネットワークで検出メソッドを呼び出し、結果を表示します。
[bboxes,scores,labelsIndex] = yolov3Detect(matFile,I,... networkInputSize,networkOutputs,confidenceThreshold,overlapThreshold,classNames); labels = classNames(labelsIndex); % Display the detections on the image IAnnotated = insertObjectAnnotation(I,'rectangle',bboxes,strcat(labels,{' - '},num2str(scores))); figure imshow(IAnnotated)
CUDA MEX の生成
エントリポイント関数 yolov3Detect
用の CUDA® コードを生成するには、MEX ターゲットの GPU コード構成オブジェクトを作成し、ターゲット言語を C++ に設定します。関数coder.DeepLearningConfig
を使用して CuDNN 深層学習構成オブジェクトを作成し、それを GPU コード構成オブジェクトの DeepLearningConfig
プロパティに割り当てます。
cfg = coder.gpuConfig('mex'); cfg.TargetLang = 'C++'; cfg.DeepLearningConfig = coder.DeepLearningConfig(TargetLibrary='cudnn'); args = {coder.Constant(matFile),I,coder.Constant(networkInputSize),... coder.Constant(networkOutputs),confidenceThreshold,... overlapThreshold,classNames}; codegen -config cfg yolov3Detect -args args -report
Code generation successful: View report
TensorRT ターゲット用の CUDA® コードを生成するには、CuDNN 構成オブジェクトではなく、TensorRT 深層学習構成オブジェクトを使用します。同様に、MKLDNN ターゲット用のコードを生成するには、CPU コード構成オブジェクトを作成し、その DeepLearningConfig
プロパティとして MKLDNN 深層学習構成オブジェクトを使用します。
生成された MEX の実行
前回と同じイメージ入力 I
を使用して生成された CUDA MEX を呼び出し、結果を表示します。
[bboxes,scores,labelsIndex] = yolov3Detect_mex(matFile,I,... networkInputSize,networkOutputs,confidenceThreshold,... overlapThreshold,classNames); labels = classNames(labelsIndex); figure; IAnnotated = insertObjectAnnotation(I,'rectangle',bboxes,strcat(labels,{' - '},num2str(scores))); imshow(IAnnotated);
ユーティリティ関数
以下のユーティリティ関数は、YOLO v3 深層学習を使用したオブジェクトの検出 (Computer Vision Toolbox)の例で使用されたものをベースとし、コード生成に適合するように変更されています。
type('applyActivations.m')
function YPredCell = applyActivations(YPredCell) %#codegen % Copyright 2020-2021 The MathWorks, Inc. numCells = size(YPredCell, 1); for iCell = 1:numCells for idx = 1:3 YPredCell{iCell, idx} = sigmoidActivation(YPredCell{iCell,idx}); end end for iCell = 1:numCells for idx = 4:5 YPredCell{iCell, idx} = exp(YPredCell{iCell, idx}); end end for iCell = 1:numCells YPredCell{iCell, 6} = sigmoidActivation(YPredCell{iCell, 6}); end end function out = sigmoidActivation(x) out = 1./(1+exp(-x)); end
type('extractPredictions.m')
function predictions = extractPredictions(YPredictions, anchorBoxMask) %#codegen % Copyright 2020-2021 The MathWorks, Inc. numPredictionHeads = size(YPredictions, 1); predictions = cell(numPredictionHeads,6); for ii = 1:numPredictionHeads % Get the required info on feature size. numChannelsPred = size(YPredictions{ii},3); numAnchors = size(anchorBoxMask{ii},2); numPredElemsPerAnchors = numChannelsPred/numAnchors; allIds = (1:numChannelsPred); stride = numPredElemsPerAnchors; endIdx = numChannelsPred; YPredictionsData = extractdata(YPredictions{ii}); % X positions. startIdx = 1; predictions{ii,2} = YPredictionsData(:,:,startIdx:stride:endIdx,:); xIds = startIdx:stride:endIdx; % Y positions. startIdx = 2; predictions{ii,3} = YPredictionsData(:,:,startIdx:stride:endIdx,:); yIds = startIdx:stride:endIdx; % Width. startIdx = 3; predictions{ii,4} = YPredictionsData(:,:,startIdx:stride:endIdx,:); wIds = startIdx:stride:endIdx; % Height. startIdx = 4; predictions{ii,5} = YPredictionsData(:,:,startIdx:stride:endIdx,:); hIds = startIdx:stride:endIdx; % Confidence scores. startIdx = 5; predictions{ii,1} = YPredictionsData(:,:,startIdx:stride:endIdx,:); confIds = startIdx:stride:endIdx; % Accumulate all the non-class indexes nonClassIds = [xIds yIds wIds hIds confIds]; % Class probabilities. % Get the indexes which do not belong to the nonClassIds classIdx = setdiff(allIds, nonClassIds, 'stable'); predictions{ii,6} = YPredictionsData(:,:,classIdx,:); end end
type('generateTiledAnchors.m')
function tiledAnchors = generateTiledAnchors(YPredCell,anchorBoxes,... anchorBoxMask,anchorIndex) % Generate tiled anchor offset for converting the predictions from the YOLO % v3 grid cell coordinates to bounding box coordinates %#codegen % Copyright 2020-2021 The MathWorks, Inc. numPredictionHeads = size(YPredCell,1); tiledAnchors = cell(numPredictionHeads, size(anchorIndex, 2)); for i = 1:numPredictionHeads anchors = anchorBoxes(anchorBoxMask{i}, :); [h,w,~,n] = size(YPredCell{i,1}); [tiledAnchors{i,2},tiledAnchors{i,1}] = ndgrid(0:h-1,0:w-1,... 1:size(anchors,1),1:n); [~,~,tiledAnchors{i,3}] = ndgrid(0:h-1,0:w-1,anchors(:,2),1:n); [~,~,tiledAnchors{i,4}] = ndgrid(0:h-1,0:w-1,anchors(:,1),1:n); end end
type('applyAnchorBoxOffsets.m')
function YPredCell = applyAnchorBoxOffsets(tiledAnchors,YPredCell,... inputImageSize,anchorIndex) %#codegen % Convert the predictions from the YOLO v3 grid cell coordinates to % bounding box coordinates % Copyright 2020-2021 The MathWorks, Inc. for i = 1:size(YPredCell,1) [h,w,~,~] = size(YPredCell{i,1}); YPredCell{i,anchorIndex(1)} = (tiledAnchors{i,1}+... YPredCell{i,anchorIndex(1)})./w; YPredCell{i,anchorIndex(2)} = (tiledAnchors{i,2}+... YPredCell{i,anchorIndex(2)})./h; YPredCell{i,anchorIndex(3)} = (tiledAnchors{i,3}.*... YPredCell{i,anchorIndex(3)})./inputImageSize(2); YPredCell{i,anchorIndex(4)} = (tiledAnchors{i,4}.*... YPredCell{i,anchorIndex(4)})./inputImageSize(1); end end
type('preprocessData.m')
function image = preprocessData(image, targetSize) % Resize the images and scale the pixels to between 0 and 1. %#codegen % Copyright 2020-2021 The MathWorks, Inc. imgSize = size(image); % Convert an input image with single channel to 3 channels. if numel(imgSize) < 1 image = repmat(image,1,1,3); end image = im2single(rescale(image)); image = iLetterBoxImage(image,coder.const(targetSize(1:2))); end function Inew = iLetterBoxImage(I,targetSize) % LetterBoxImage returns a resized image by preserving the width and height % aspect ratio of input Image I. 'targetSize' is a 1-by-2 vector consisting % the target dimension. % % Input I can be uint8, uint16, int16, double, single, or logical, and must % be real and non-sparse. [Irow,Icol,Ichannels] = size(I); % Compute aspect Ratio. arI = Irow./Icol; % Preserve the maximum dimension based on the aspect ratio. if arI<1 IcolFin = targetSize(1,2); IrowFin = floor(IcolFin.*arI); else IrowFin = targetSize(1,1); IcolFin = floor(IrowFin./arI); end % Resize the input image. Itmp = imresize(I,[IrowFin,IcolFin]); % Initialize Inew with gray values. Inew = ones([targetSize,Ichannels],'like',I).*0.5; % Compute the offset. if arI<1 buff = targetSize(1,1)-IrowFin; else buff = targetSize(1,2)-IcolFin; end % Place the resized image on the canvas image. if (buff==0) Inew = Itmp; else buffVal = floor(buff/2); if arI<1 Inew(buffVal:buffVal+IrowFin-1,:,:) = Itmp; else Inew(:,buffVal:buffVal+IcolFin-1,:) = Itmp; end end end
参考文献
1.Redmon, Joseph, and Ali Farhadi. "YOLOv3: An Incremental Improvement." Preprint, submitted April 8, 2018. https://arxiv.org/abs/1804.02767.
参考
関数
オブジェクト
coder.gpuConfig
|coder.gpuEnvConfig
|coder.TensorRTConfig
|coder.CuDNNConfig
|dlarray
(Deep Learning Toolbox) |dlnetwork
(Deep Learning Toolbox)
関連する例
- YOLO v3 深層学習を使用したオブジェクトの検出 (Computer Vision Toolbox)