Faster R-CNN 車両検出器の学習のド​キュメンテーションの​学習データの読み込み​について

こちらのリンク のFaster R-CNN 車両検出器の学習のドキュメンテーションを参考にプログラムを作成しているのですが、学習データの読み込みでの
data = load('fasterRCNNVehicleTrainingData.mat');
fasterRCNNVehicleTrainingData.matは自分で作成するものなのでしょうか?
もしそうならどのように作成するのでしょうか?

 採用された回答

Kojiro Saito
Kojiro Saito 2023 年 12 月 20 日

1 投票

whichコマンドでファイルがMATLABの検索パスにあるか確認ができます。
which fasterRCNNVehicleTrainingData.mat
を実行すると、Computer Vision Toolboxがインストールされていれば、
C:\Program Files\MATLAB\R2023b\toolbox\vision\visiondata\fasterRCNNVehicleTrainingData.mat
のようにリターンされます。
インストールされていないと「'fasterRCNNVehicleTrainingData.mat' が見つかりません。」というリターンが返ってきます。
この例はDeep Learning ToolboxComputer Vision Toolboxが必要と書かれていますが、matファイルはComputer Vision Toolboxに含まれているため、インストールが必要です。

9 件のコメント

輝
2023 年 12 月 21 日
ご回答ありがとうございます。分かりにくい質問で申し訳ありません。
Faster R-CNN 車両検出器の学習のドキュメンテーションを参考にQRコードを検出するプログラムを作成しているのですが、fasterRCNNVehicleTrainingData.matは車両を検出するための学習データという認識で正しいでしょうか?
もしそうならばQRコード用のmatファイルを作成して、それに置き換えないといけないのでしょうか?
Kojiro Saito
Kojiro Saito 2023 年 12 月 21 日
fasterRCNNVehicleTrainingData.matは「toolbox\vision\visiondata\vehicles」に含まれている295枚の画像それぞれどの領域に車両が写っているのかの学習データ(vehicleTrainingData)や、検出器(detector)、レイヤー(layers)などのデータが構造体として含まれているサンプルデータです。
trainFasterRCNNObjectDetectorのドキュメントの入力引数「trainingData — ラベル付きのグラウンド トゥルース」を見ていただくと分かりますが、Faster R-CNN 深層学習オブジェクト検出器にはグラウンドトゥルースデータが必要になります。イメージラベラーなどを使ってこのグラウンドトゥルースデータを作成できますが、作り方はイメージラベラー入門のドキュメントを参考にしてみてください。
輝
2023 年 12 月 21 日
ご回答ありがとうございます。グランドトゥルースデータはイメージラベラーを用いて作成しました。
ではFaster R-CNN 深層学習オブジェクト検出器にはグラウンドトゥルースデータがあれば実行できて、学習データ(vehicleTrainingData)や、検出器(detector)、レイヤー(layers)などのデータは必要ないということでしょうか?
Kojiro Saito
Kojiro Saito 2023 年 12 月 21 日
検出器の学習をおこなうにはtrainFasterRCNNObjectDetectorの入力引数がtrainingData(ラベル付きのグラウンドトゥルース、つまり学習データ)、network(SeriesNetworkLayerLayerGraph)、options(trainingOptions)の3つとなっていますので、グランドトゥルースデータの他にネットワークは必要です。
ネットワークの作り方はこちらにサンプルがあります。
輝
2023 年 12 月 22 日
編集済み: 2023 年 12 月 22 日
すみません、編集させていただきます。
ご回答ありがとうございます。返信遅くなり申し訳ありません。
提示していただいたネットワークの作り方のサンプルを参考にネットワークを作成し、検出器の学習を行いました。すると以下のエラーが出たのですが、どうすればエラーを消せますか?
次を使用中のエラー: trainFasterRCNNObjectDetector
無効なネットワーク。
エラー: untitled2 (163)
detector = trainFasterRCNNObjectDetector(ds, lgraph, options, ...
原因:
'rcnnBoxDeltas': 入力サイズは 1×1×4 でなければなりません。この R-CNN ボックスの回帰層では、3 番目の入力次元がネットワークが
検出するべきオブジェクト クラスの数 (1 クラス) 4 倍である必要があります。Fast R-CNN または Faster R-CNN ネットワーク作成の
詳細については、ドキュメンテーションを参照してください。
'rcnnClassification': The input size must be 1×1×2. The classification layer expects the third input
dimension to be the number of object classes the network should detect (1 classes) plus 1. The additional
class is required for the "background" class. See the documentation for more details about creating Fast or
Faster R-CNN networks.
ネットワークを作成するコードは以下の通りです。
% Faster R-CNN オブジェクト検出ネットワークの作成
% Load a pretrained ResNet-50.
net = resnet50;
lgraph = layerGraph(net);
% Remove the last 3 layers.
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', 'Classes', categorical(1:numClassesPlusBackground))
];
% Add new object classification layers.
lgraph = addLayers(lgraph, newLayers);
% Connect the new layers to the network.
lgraph = connectLayers(lgraph, 'avg_pool', 'rcnnFC');
% 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');
% Select a feature extraction layer.
featureExtractionLayer = 'activation_40_relu';
% 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];
roiPool = roiMaxPooling2dLayer(outputSize,'Name','roiPool');
lgraph = addLayers(lgraph, roiPool);
% Connect feature extraction layer to ROI max pooling layer.
lgraph = connectLayers(lgraph, featureExtractionLayer,'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');
% Define anchor boxes.
anchorBoxes = [
16 16
32 16
16 32
];
% Create the region proposal layer.
proposalLayer = regionProposalLayer(anchorBoxes,'Name','regionProposal');
lgraph = addLayers(lgraph, proposalLayer);
% Number of anchor boxes.
numAnchors = size(anchorBoxes,1);
% Number of feature maps in coming out of the feature extraction layer.
numFilters = 1024;
rpnLayers = [
convolution2dLayer(3, numFilters,'padding',[1 1],'Name','rpnConv3x3')
reluLayer('Name','rpnRelu')
];
lgraph = addLayers(lgraph, rpnLayers);
% Connect to RPN to feature extraction layer.
lgraph = connectLayers(lgraph, featureExtractionLayer, 'rpnConv3x3');
% Add RPN classification layers.
rpnClsLayers = [
convolution2dLayer(1, numAnchors*2,'Name', 'rpnConv1x1ClsScores')
rpnSoftmaxLayer('Name', 'rpnSoftmax')
rpnClassificationLayer('Name','rpnClassification')
];
lgraph = addLayers(lgraph, rpnClsLayers);
% Connect the classification layers to the RPN network.
lgraph = connectLayers(lgraph, 'rpnRelu', 'rpnConv1x1ClsScores');
% Add RPN regression layers.
rpnRegLayers = [
convolution2dLayer(1, numAnchors*4, 'Name', 'rpnConv1x1BoxDeltas')
rcnnBoxRegressionLayer('Name', 'rpnBoxDeltas');
];
lgraph = addLayers(lgraph, rpnRegLayers);
% Connect the regression layers to the RPN network.
lgraph = connectLayers(lgraph, 'rpnRelu', 'rpnConv1x1BoxDeltas');
% Connect region proposal network.
lgraph = connectLayers(lgraph, 'rpnConv1x1ClsScores', 'regionProposal/scores');
lgraph = connectLayers(lgraph, 'rpnConv1x1BoxDeltas', 'regionProposal/boxDeltas');
% Connect region proposal layer to roi pooling.
lgraph = connectLayers(lgraph, 'regionProposal', 'roiPool/roi');
Kojiro Saito
Kojiro Saito 2023 年 12 月 24 日
指定しているクラス数がレイヤーによって変わってしまっているのが原因と思われます。
追加している
classificationLayer('Name', 'rcnnClassification', 'Classes', categorical(1:numClassesPlusBackground))
ではクラス数をnumClassesPlusBackgroundにしています。一方でドキュメント例ではクラス数を指定してしないのでこのクラス数は「auto」になっています。
ドキュメント例のように
classificationLayer('Name', 'rcnnClassification')
に戻してみてはいかがでしょうか?
輝
2023 年 12 月 25 日
ご回答ありがとうございます。指摘いただいた部分を修正したのですがまだエラーが出てしまいました。
以下のコードのようにnumClassesを2から1に変更したらエラーが出なくなったのですが、検出器の学習が始まるとMATLABがクラッシュして落とされるようになってしまいました。解決策はありますでしょうか?
% Specify the number of classes the network should classify.
numClasses = 1;
numClassesPlusBackground = numClasses + 1;
Kojiro Saito
Kojiro Saito 2023 年 12 月 25 日
クラス数を1にすると動くということですね。今回はQRコードの有無なので、QRコード有り(クラス1)、無し(背景)の1クラスということではないでしょうか?
クラッシュの方はこちらのAnswersでご質問いただいているので、重複にならないようにそちらで回答分かればコメントいたします。
輝
2023 年 12 月 25 日
ご回答ありがとうございます。理解することができました。ありがとうございます。
また検出器の学習で不明なところが出てきたら質問させていただくので、よろしくお願いいたします。

サインインしてコメントする。

その他の回答 (0 件)

製品

リリース

R2023b

タグ

質問済み:

輝
2023 年 12 月 20 日

コメント済み:

輝
2023 年 12 月 25 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!