Main Content

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

Fast R-CNN オブジェクト検出ネットワークの作成

この例は、上記のR-CNN オブジェクト検出ネットワークの作成の例に基づいています。ROI プーリング層と境界ボックス回帰層を追加して、事前学習済みの ResNet-50 ネットワークを Fast R-CNN オブジェクト検出ネットワークに変換します。その後、Fast R-CNN ネットワークは、trainFastRCNNObjectDetector を使用して学習させることができます。

R-CNN ネットワークの作成

まず、Fast R-CNN の基礎となる R-CNN ネットワークを作成します。R-CNN オブジェクト検出ネットワークの作成の例で、このコード セクションを詳細に説明しています。

% Load pretrained ResNet-50.
net = resnet50;
lgraph = layerGraph(net);

% Remove the the last 3 layers from ResNet-50. 
layersToRemove = {
    'fc1000'
    'fc1000_softmax'
    'ClassificationLayer_fc1000'
    };
lgraph = removeLayers(lgraph, layersToRemove);

% Specify the number of classes the network should classify.
numClasses = 2;
numClassesPlusBackground = numClasses + 1;

% Define new classification layers.
newLayers = [
    fullyConnectedLayer(numClassesPlusBackground, 'Name', 'rcnnFC')
    softmaxLayer('Name', 'rcnnSoftmax')
    classificationLayer('Name', 'rcnnClassification')
    ];

% Add new layers.
lgraph = addLayers(lgraph, newLayers);

% Connect the new layers to the network. 
lgraph = connectLayers(lgraph, 'avg_pool', 'rcnnFC');

境界ボックス回帰層の追加

領域提案ボックスに適用する一連のボックス オフセットを学習するためのボックス回帰層を追加します。学習したオフセットによって、領域提案ボックスが元のグラウンド トゥルース境界ボックスに近くなるように変換されます。この変換により、Fast R-CNN の位置推定パフォーマンスが向上します。

ボックス回帰層は、全結合層とその後に続く R-CNN ボックス回帰層で構成されます。全結合層は、各クラスに対して、4 つのボックス オフセットのセットを出力するように設定されています。背景クラスは、背景境界ボックスが調整されていないため、除外されます。

% Define the number of outputs of the fully connected layer.
numOutputs = 4 * numClasses;

% Create the box regression layers.
boxRegressionLayers = [
    fullyConnectedLayer(numOutputs,'Name','rcnnBoxFC')
    rcnnBoxRegressionLayer('Name','rcnnBoxDeltas')
    ];

% Add the layers to the network
lgraph = addLayers(lgraph, boxRegressionLayers);

ボックス回帰層は通常、分類分岐が結合されているのと同じ層に結合されます。

% Connect the regression layers to the layer named 'avg_pool'.
lgraph = connectLayers(lgraph,'avg_pool','rcnnBoxFC');

% Display the classification and regression branches of Fast R-CNN.
figure
plot(lgraph)
ylim([-5 16])

ROI の最大プーリング層の追加

次の手順は、特徴抽出層として使用するネットワークの層を選択することです。この層は、プリーングされた領域の分類のために特徴量をプーリングする ROI の最大プーリング層に結合します。特徴抽出層の選択には、実証的評価が必要です。ResNet-50 の場合、一般的な特徴抽出層は、畳み込みの 4 番目のブロックの出力です。これは、activation40_relu という名前の層に対応します。

featureExtractionLayer = 'activation_40_relu';

figure
plot(lgraph)
ylim([30 42])

ROI 最大プーリング層を挿入するには、まず、特徴抽出層に結合されている層 res5a_branch2a および res5a_branch1 を切り離します。

% Disconnect the layers attached to the selected feature extraction layer.
lgraph = disconnectLayers(lgraph, featureExtractionLayer,'res5a_branch2a');
lgraph = disconnectLayers(lgraph, featureExtractionLayer,'res5a_branch1');

% Add ROI max pooling layer.
outputSize = [14 14]
outputSize = 1×2

    14    14

roiPool = roiMaxPooling2dLayer(outputSize,'Name','roiPool');

lgraph = addLayers(lgraph, roiPool);

% Connect feature extraction layer to ROI max pooling layer.
lgraph = connectLayers(lgraph, 'activation_40_relu','roiPool/in');

% Connect the output of ROI max pool to the disconnected layers from above.
lgraph = connectLayers(lgraph, 'roiPool','res5a_branch2a');
lgraph = connectLayers(lgraph, 'roiPool','res5a_branch1');

% Show the result after adding and connecting the ROI max pooling layer.
figure
plot(lgraph)
ylim([30 42])

最後に、ROI 入力層を ROI 最大プーリング層の 2 番目の入力に結合します。

% Add ROI input layer.
roiInput = roiInputLayer('Name','roiInput');
lgraph = addLayers(lgraph, roiInput);

% Connect ROI input layer to the 'roi' input of the ROI max pooling layer.
lgraph = connectLayers(lgraph, 'roiInput','roiPool/roi');

% Show the resulting faster adding and connecting the ROI input layer.
figure
plot(lgraph)
ylim([30 42])

これで、trainFastRCNNObjectDetector を使用してネットワークに学習させる準備が整いました。