Main Content

このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。

rcnnObjectDetector

R-CNN 深層学習の検出器を使用したオブジェクトの検出

説明

rcnnObjectDetector オブジェクトは、R-CNN (Region-based Convolution Neural Networks) オブジェクト検出器を使用して、イメージからオブジェクトを検出します。イメージ内のオブジェクトを検出するには、学習済みの検出器を関数 detect に渡します。イメージ領域を分類するには、検出器を関数 classifyRegions に渡します。

rcnnObjectDetector を使用するには、Statistics and Machine Learning Toolbox™ および Deep Learning Toolbox™ が必要です。

rcnnObjectDetector と共に関数 detect または classifyRegions を使用する場合、CUDA® 対応 NVIDIA® GPU の使用が強く推奨されます。この GPU は計算時間を大幅に短縮します。GPU を使用するには、Parallel Computing Toolbox™ が必要です。サポートされる Compute Capability の詳細については、GPU 計算の要件 (Parallel Computing Toolbox)を参照してください。

作成

学習データを指定して関数 trainRCNNObjectDetector を呼び出して、rcnnObjectDetector オブジェクトを作成します (Deep Learning Toolbox が必要)。

detector = trainRCNNObjectDetector(trainingData,...)

プロパティ

すべて展開する

R-CNN 検出器内で使用される畳み込みニューラル ネットワーク (CNN)。SeriesNetwork (Deep Learning Toolbox) オブジェクトまたは DAGNetwork (Deep Learning Toolbox) オブジェクトとして指定します。

カスタム領域提案メソッド。関数名として指定します。カスタム関数 proposalFcn は次の関数形式でなければなりません。

 [bboxes,scores] = proposalFcn(I)

入力引数 I はイメージです。関数は、四角形の境界ボックスを M 行 4 列の配列で返さなければなりません。bboxes の各行には、境界ボックスの左上隅とサイズをピクセル単位で指定する 4 要素ベクトル [x,y,width,height] が含まれます。また、関数は、各境界ボックスのスコアを M 行 1 列のベクトルで返さなければなりません。スコア値が高い場合、境界ボックスにオブジェクトが含まれている可能性が高いことを示します。

オブジェクト クラス名。cell 配列として指定します。この配列には、R-CNN 検出器が検出対象として学習済みのオブジェクト クラスの名前が含まれます。

この プロパティ は読み取り専用です。

境界ボックス回帰層の名前。文字ベクトルとして指定します。このプロパティは、学習時に trainRCNNObjectDetectorBoxRegressionLayer 引数を使用して設定されます。

オブジェクト関数

detectDetect objects using R-CNN deep learning detector
classifyRegionsClassify objects in image regions using R-CNN object detector

すべて折りたたむ

学習データとネットワーク層を読み込みます。

load('rcnnStopSigns.mat', 'stopSigns', 'layers')

MATLAB パスにイメージ ディレクトリを追加します。

imDir = fullfile(matlabroot, 'toolbox', 'vision', 'visiondata',...
  'stopSignImages');
addpath(imDir);

ミニバッチのサイズ 32 を使用して GPU メモリ使用量を削減するようにネットワーク学習オプションを設定します。InitialLearnRate の値を小さくして、ネットワーク パラメーターの変更レートを下げます。これは事前学習済みのネットワークを微調整するときに便利で、ネットワークが急激に変化しないようにできます。

options = trainingOptions('sgdm', ...
  'MiniBatchSize', 32, ...
  'InitialLearnRate', 1e-6, ...
  'MaxEpochs', 10);

R-CNN 検出器に学習させます。学習は、完了するのに 2 ~ 3 分かかることがあります。

rcnn = trainRCNNObjectDetector(stopSigns, layers, options, 'NegativeOverlapRange', [0 0.3]);
*******************************************************************
Training an R-CNN Object Detector for the following object classes:

* stopSign

--> Extracting region proposals from 27 training images...done.

--> Training a neural network to classify objects in training data...

Training on single CPU.
Initializing input data normalization.
|========================================================================================|
|  Epoch  |  Iteration  |  Time Elapsed  |  Mini-batch  |  Mini-batch  |  Base Learning  |
|         |             |   (hh:mm:ss)   |   Accuracy   |     Loss     |      Rate       |
|========================================================================================|
|       1 |           1 |       00:00:01 |       96.88% |       0.1651 |      1.0000e-06 |
|       2 |          50 |       00:00:15 |       96.88% |       0.0807 |      1.0000e-06 |
|       3 |         100 |       00:00:27 |       96.88% |       0.1340 |      1.0000e-06 |
|       5 |         150 |       00:00:38 |       96.88% |       0.0225 |      1.0000e-06 |
|       6 |         200 |       00:00:49 |       93.75% |       0.6584 |      1.0000e-06 |
|       8 |         250 |       00:01:00 |       93.75% |       0.5233 |      1.0000e-06 |
|       9 |         300 |       00:01:11 |      100.00% |   2.9456e-05 |      1.0000e-06 |
|      10 |         350 |       00:01:23 |      100.00% |       0.0009 |      1.0000e-06 |
|========================================================================================|
Training finished: Max epochs completed.

Network training complete.

--> Training bounding box regression models for each object class...100.00%...done.

Detector training complete.
*******************************************************************

R-CNN 検出器をテスト イメージでテストします。

img = imread('stopSignTest.jpg');

[bbox, score, label] = detect(rcnn, img, MiniBatchSize=32);

最も強い検出結果を表示します。

[score, idx] = max(score);

bbox = bbox(idx, :);
annotation = sprintf('%s: (Confidence = %f)', label(idx), score);

detectedImg = insertObjectAnnotation(img, 'rectangle', bbox, annotation);

figure
imshow(detectedImg)

パスからイメージ ディレクトリを削除します。

rmpath(imDir);

追加のデータを使用して、R-CNN オブジェクト検出器の学習を再開します。この手順について説明するために、最初の検出器の学習にグラウンド トゥルース データの半分を使用します。次に、すべてのデータを使用して学習を再開します。

学習データを読み込んで学習オプションを初期化します。

load('rcnnStopSigns.mat', 'stopSigns', 'layers')

stopSigns.imageFilename = fullfile(toolboxdir('vision'),'visiondata', ...
    stopSigns.imageFilename);

options = trainingOptions('sgdm', ...
    'MiniBatchSize', 32, ...
    'InitialLearnRate', 1e-6, ...
    'MaxEpochs', 10, ...
    'Verbose', false);

グラウンド トゥルースの一部を使用して R-CNN 検出器に学習させます。

rcnn = trainRCNNObjectDetector(stopSigns(1:10,:), layers, options, 'NegativeOverlapRange', [0 0.3]);

検出器から学習済みのネットワーク層を取得します。ネットワーク層の配列を trainRCNNObjectDetector に渡すと、そのネットワーク層をそのまま使用して学習が続行されます。

network = rcnn.Network;
layers = network.Layers;

すべての学習データを使用して学習を再開します。

rcnnFinal = trainRCNNObjectDetector(stopSigns, layers, options);

犬と猫の 2 つのオブジェクト クラスを対象とした R-CNN オブジェクト検出器を作成します。

objectClasses = {'dogs','cats'};

trainRCNNObjectDetector を使用してネットワークに学習させるには、ネットワークで犬と猫の両方と "背景" クラスを分類できなければなりません。この例では、背景を含むネットワークが追加されます。

numClassesPlusBackground = numel(objectClasses) + 1;

ネットワークの最終的な全結合層は、ネットワークで分類できるクラスの数を定義します。クラスの数および背景クラスと同じ出力サイズになるように最終的な全結合層を設定します。

layers = [ ...
    imageInputLayer([28 28 1])
    convolution2dLayer(5,20)        
    fullyConnectedLayer(numClassesPlusBackground);
    softmaxLayer()
    classificationLayer()];

これらのネットワーク層を使用して R-CNN 2 クラス オブジェクト検出器に学習させることができるようになりました。

R-CNN オブジェクト検出器を作成して、保存したネットワーク チェックポイントを使用するように設定します。ネットワーク チェックポイントは、trainingOptions の 'CheckpointPath' パラメーターが設定されている場合、ネットワークの学習中のエポックごとに保存されます。ネットワーク チェックポイントは、学習セッションが異常終了した場合に役立ちます。

一時停止標識学習データを読み込みます。

load('rcnnStopSigns.mat','stopSigns','layers')

イメージ ファイルの絶対パスを追加します。

stopSigns.imageFilename = fullfile(toolboxdir('vision'),'visiondata', ...
      stopSigns.imageFilename);

関数 trainingOptions を使用して 'CheckpointPath' を設定します。

checkpointLocation = tempdir;
options = trainingOptions('sgdm','Verbose',false, ...
    'CheckpointPath',checkpointLocation);

いくつかのイメージを使用して R-CNN オブジェクト検出器に学習させます。

rcnn = trainRCNNObjectDetector(stopSigns(1:3,:),layers,options);

保存したネットワーク チェックポイントを読み込みます。

wildcardFilePath = fullfile(checkpointLocation,'convnet_checkpoint__*.mat');
contents = dir(wildcardFilePath);

いずれかのチェックポイント ネットワークを読み込みます。

filepath = fullfile(contents(1).folder,contents(1).name);
checkpoint = load(filepath);

checkpoint.net
ans = 

  SeriesNetwork with properties:

    Layers: [15×1 nnet.cnn.layer.Layer]

新しい R-CNN オブジェクト検出器を作成して、保存したネットワークを使用するように設定します。

rcnnCheckPoint = rcnnObjectDetector();
rcnnCheckPoint.RegionProposalFcn = @rcnnObjectDetector.proposeRegions;

Network を保存したネットワーク チェックポイントに設定します。

rcnnCheckPoint.Network = checkpoint.net
rcnnCheckPoint = 

  rcnnObjectDetector with properties:

              Network: [1×1 SeriesNetwork]
           ClassNames: {'stopSign'  'Background'}
    RegionProposalFcn: @rcnnObjectDetector.proposeRegions

参照

[1] Girshick, Ross, et al. “Rich Feature Hierarchies for Accurate Object Detection and Semantic Segmentation.” 2014 IEEE Conference on Computer Vision and Pattern Recognition, IEEE, 2014, pp. 580–87. DOI.org (Crossref), https://doi.org/10.1109/CVPR.2014.81.

[2] Girshick, Ross. “Fast R-CNN.” 2015 IEEE International Conference on Computer Vision (ICCV), IEEE, 2015, pp. 1440–48. DOI.org (Crossref), https://doi.org/10.1109/ICCV.2015.169.

バージョン履歴

R2016b で導入