転移学習におけるR-CNNのエラーについて

以下のプログラムを用いて alexnetを転移学習させた後、それのレイヤーを用いてRCNNを利用しようとしています。
転移学習のための学習データとして、マグカップ、マウス、キーボード、扇風機の画像を用意しています。 またRCNNのためにTraining Image Labelerを用いてそれぞれ四種類がラベル付けされたmatファイルも作成済みです。
%%Load a pre-trained, deep, convolutional network
net = alexnet;
layersfirst = net.Layers
%%Delete Full Connected Layer
layersTransfer = layersfirst(1:end-3)
%%Set up our training data
digitDataPath = fullfile(matlabroot,'ImageData','myImages');
allImages = imageDatastore(digitDataPath, 'IncludeSubfolders', true, 'LabelSource', 'foldernames');
trainingImages= allImages;
numClasses = numel(categories(trainingImages.Labels));
%%layers
layers = [layersTransfer
fullyConnectedLayer(numClasses,'WeightLearnRateFactor',20,'BiasLearnRateFactor',20)
softmaxLayer
classificationLayer]
%%Pre-train the Network
opts = trainingOptions('sgdm', 'InitialLearnRate', 0.001, 'MaxEpochs', 5, 'MiniBatchSize', 32);
myNet = trainNetwork(trainingImages, layers, opts);
%%RCNN
load ('TESTCHANGE1.mat')
rcnn = trainRCNNObjectDetector(TESTCHANGE,layers,opts,'NegativeOverlapRange',[0 0.3])
%%TEST
imDir = fullfile(matlabroot,'ImageData','TESTCHANGE');
addpath(imDir);
img = imread('Test.jpg');
[bbox,score,label]=detect(rcnnfinal,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);
しかし、いざプログラムを実行すようとすると
エラー: vision.internal.cnn.validation.checkNetworkClassificationLayer (line 6) ネットワーク分類層の数値オブジェクト クラスは、"Background" クラス用として入力 trainingData で定義されたクラスの数に 1 を加えた数と等しくなければなりません。
エラー: vision.internal.rcnn.parseInputs (line 35) vision.internal.cnn.validation.checkNetworkClassificationLayer(network, trainingData);
エラー: trainRCNNObjectDetector (line 185) params = vision.internal.rcnn.parseInputs(trainingData, network, options, mfilename, varargin{:});
エラー: RCNNCHANGE (line 33) rcnn = trainRCNNObjectDetector(TESTCHANGE,layers,opts,'NegativeOverlapRange',[0 0.3])
というエラーが吐き出されていまい実行できません。どのようにプログラム及び学習データの改善を行えばよいのでしょうか。
どうかよろしくお願いします。

 Risposta accettata

michio
michio il 26 Ott 2017

3 voti

trainRCNNObjectDetector を使用してネットワークを学習させるには、検出対象に加えて "背景"も分類できるネットワークを用意する必要があります。エラーメッセージの「クラスの数に 1 を加えた数」は背景の分です。
ところで、alexnetをそのままではなく、一旦CNNでの分類問題としてファインチューニングされる理由は何かありますか? 例えば
net = alexnet;
layersfirst = net.Layers
layersTransfer = layersfirst(1:end-3);
layers = [layersTransfer
fullyConnectedLayer(numClasses+1,'WeightLearnRateFactor',20,'BiasLearnRateFactor',20)
softmaxLayer
classificationLayer]
を trainRCNNObjectDetector で学習させることもできるかと思います。

4 Commenti

michio
michio il 27 Ott 2017
(Teiさんのコメントをこちらに記載し直します)
返信ありがとうございます。 早速、ご提案された方法で trainRCNNObjectDetectorを用いて学習させようと思ったのですが、 今度は以下のようなエラーが出てきてしまいやはり実行できません。この問題に対して、どのように解決したら良いでしょうか?
どうかよろしくお願いします。
エラー: tabular/rowfun>dfltErrHandler (line 497)
関数 '@(varargin)selectTrainingSamples(this,classNames,varargin{:})' A の 11 番目 行に適用した際に、次のエ
ラーが発生しました:
'bboxA' の値は無効です。 境界ボックスの幅と高さは正でなければなりません。
エラー: tabular/rowfun>@(s,varargin)dfltErrHandler(grouped,funName,s,varargin{:}) (line 268)
errHandler = @(s,varargin) dfltErrHandler(grouped,funName,s,varargin{:});
エラー: tabular/rowfun (line 288)
[b_data{igrp,:}] = errHandler(struct('identifier',ME.identifier, 'message',ME.message,
'index',igrp),inArgs{:});
エラー: vision.internal.rcnn.RegionReader (line 128)
this.TrainingSamples = rowfun(...
エラー: vision.internal.rcnn.TrainingRegionDispatcher (line 66)
vision.internal.rcnn.RegionReader(...
エラー: rcnnObjectDetector/createTrainingDispatcher (line 758)
dispatcher = vision.internal.rcnn.TrainingRegionDispatcher(...
エラー: rcnnObjectDetector.train (line 217)
dispatcher = createTrainingDispatcher(detector, groundTruth, regionProposals, opts,
layers(1).InputSize, params);
エラー: trainRCNNObjectDetector (line 197)
detector = rcnnObjectDetector.train(trainingData, layers, options, params);
エラー: RCNNCHANGE1 (line 29)
rcnn = trainRCNNObjectDetector(TESTCHANGE,layers,opts,'NegativeOverlapRange',[0 0.3])
michio
michio il 27 Ott 2017
'bboxA' の値は無効です。 境界ボックスの幅と高さは正でなければなりません。
とのエラーメッセージだけみると学習用画像内の物体を定義する境界に問題があるようですが、念のため確認して頂けますか?幅と高さが負の値になっている・・とのメッセージですが、Training Image Labelerを使用されたのであればその可能性は低そうではあります。
R2017aでは、学習用画像に認識対象(ここではマグカップ、マウス、キーボード、扇風機)がどれも存在しない画像があると、同様のエラーメッセージがでる事象を確認したことがありますが、使用されているMATLABバージョンはいくつでしょうか。
- Tei
- Tei il 27 Ott 2017
Modificato: - Tei il 27 Ott 2017
MATLABのバージョンはR2017aです。
また、Training Image Labeler を用いて作成したmatファイル内のTableに負の値を示すもの及び認識対象が存在しない画像は見当たりませんでした。
michio
michio il 27 Ott 2017
そうなるとエラーメッセージだけからの判断ではなかなか難しいですね。同じエラーが再現できるデータがあれば問題の検証も容易なのですが、流石にデータをここにアップロードして頂くわけにもいきませんし。
原因の検証方法ですが・・私であれば、まず学習画像数を少なくして実行できるかどうかを試し、問題がある画像の特定を試みます。エラーメッセージに
"11 番目 行に適用した際に"
とあるので、10枚だけで実行するか、11番目の画像を削除してみるなどなど。
もし保守サービス有効なアカデミックライセンスまたはコマーシャルライセンスをご使用中でしたら、弊社技術サポート窓口(下記)のご利用もどうぞご検討ください。こちらでしたら実際にエラーが再現するデータを送っていただいたりなども可能かと。 https://jp.mathworks.com/support/contact_us/?s_tid=sp_ban_cs

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su モデル化 in Centro assistenza e File Exchange

Richiesto:

il 26 Ott 2017

Commentato:

il 27 Ott 2017

Community Treasure Hunt

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

Start Hunting!