このページの翻訳は最新ではありません。ここをクリックして、英語の最新版を参照してください。
rcnnObjectDetector
R-CNN 深層学習の検出器を使用したオブジェクトの検出
説明
rcnnObjectDetector
オブジェクトは、R-CNN (Regions with Convolutional 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,...)
プロパティ
Network
— 系列ネットワーク オブジェクト
SeriesNetwork
| DAGNetwork
畳み込みニューラル ネットワーク (CNN) を表す系列ネットワーク オブジェクト。SeriesNetwork
(Deep Learning Toolbox) または DAGNetwork
(Deep Learning Toolbox) として指定します。このオブジェクトは R-CNN 検出器内で使用されます。
RegionProposalFcn
— カスタム領域提案
関数ハンドル
カスタム領域提案関数ハンドル。関数名として指定します。カスタム関数 proposalFcn
は次の関数形式でなければなりません。
[bboxes,scores] = proposalFcn(I)
入力引数 I
はイメージです。関数は、四角形の境界ボックスを M 行 4 列の配列で返さなければなりません。bboxes
の各行には、境界ボックスの左上隅とサイズをピクセル単位で指定する 4 要素ベクトル [x,y,width,height] が含まれます。また、関数は、各境界ボックスのスコアを M 行 1 列のベクトルで返さなければなりません。スコア値が高い場合、境界ボックスにオブジェクトが含まれている可能性が高いことを示します。
ClassNames
— オブジェクト クラス名
cell 配列
オブジェクト クラス名。cell 配列として指定します。この配列には、R-CNN 検出器が検出対象として学習済みのオブジェクト クラスの名前が含まれます。
BoxRegressionLayer
— 境界ボックス回帰層
文字ベクトル
この プロパティ は読み取り専用です。
境界ボックス回帰層の名前。文字ベクトルとして指定します。このプロパティは、学習時に trainRCNNObjectDetector
の
引数を使用して設定されます。BoxRegressionLayer
オブジェクト関数
detect | Detect objects using R-CNN deep learning detector |
classifyRegions | Classify objects in image regions using R-CNN object detector |
例
R-CNN 一時停止標識検出器の学習
学習データとネットワーク層を読み込みます。
load('rcnnStopSigns.mat', 'stopSigns', 'layers')
MATLAB パスにイメージ ディレクトリを追加します。
imDir = fullfile(matlabroot, 'toolbox', 'vision', 'visiondata',... 'stopSignImages'); addpath(imDir);
ミニバッチのサイズ 32 を使用して GPU メモリ使用量を削減するようにネットワーク学習オプションを設定します。InitialLearningRate の値を小さくして、ネットワーク パラメーターの変更レートを下げます。これは事前学習済みのネットワークを微調整するときに便利で、ネットワークが急激に変化しないようにできます。
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 Step 1 of 3: Extracting region proposals from 27 training images...done. Step 2 of 3: Training a neural network to classify objects in training data... |=========================================================================================| | Epoch | Iteration | Time Elapsed | Mini-batch | Mini-batch | Base Learning| | | | (seconds) | Loss | Accuracy | Rate | |=========================================================================================| | 3 | 50 | 9.27 | 0.2895 | 96.88% | 0.000001 | | 5 | 100 | 14.77 | 0.2443 | 93.75% | 0.000001 | | 8 | 150 | 20.29 | 0.0013 | 100.00% | 0.000001 | | 10 | 200 | 25.94 | 0.1524 | 96.88% | 0.000001 | |=========================================================================================| Network training complete. Step 3 of 3: Training bounding box regression models for each object class...100.00%...done. R-CNN 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 オブジェクト検出器の学習の再開
追加のデータを使用して、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);
マルチクラス R-CNN オブジェクト検出用のネットワークの作成
犬と猫の 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 オブジェクト検出器での保存したネットワークの使用
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
バージョン履歴
R2016b で導入
参考
アプリ
関数
SeriesNetwork
(Deep Learning Toolbox) |trainNetwork
(Deep Learning Toolbox) |trainRCNNObjectDetector
|fastRCNNObjectDetector
|fasterRCNNObjectDetector
|vision.CascadeObjectDetector
|selectStrongestBboxMulticlass
MATLAB コマンド
次の MATLAB コマンドに対応するリンクがクリックされました。
コマンドを MATLAB コマンド ウィンドウに入力して実行してください。Web ブラウザーは MATLAB コマンドをサポートしていません。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)