How to fix: "Index Exceeds number of array element " when training faster R-CNN
Show older comments
I am trying to train a faster rcnn detector to detect 5 classes. I created the network according to the mathworks "create faster rcnn network". when i try to train, i get the "Index Exceeds number of array element " error as shown in the attached file. Thank you in advance.
The created network
% load pretrained Resnet-50.
net = resnet50();
% convert network into layer graph
lgraph = layerGraph(net);
%plot lgraph
figure
plot(lgraph)
ylim([-5 16])
layersToRemove = {'fc1000'
'fc1000_softmax'
'ClassificationLayer_fc1000'};
lgraph = removeLayers(lgraph,layersToRemove);
%plot lgraph
figure
plot(lgraph)
ylim([-5 16])
% Specify the number of classes the network should classify.
numClasses = 5;
numClassesPlusBackground = 5 + 1;
%Define new classification layers
newLayers = [
fullyConnectedLayer(numClassesPlusBackground, 'Name', 'rcnnFc')
softmaxLayer('Name', 'rcnnSoftmax')
classificationLayer('Name', 'rcnnClassification')];
%Add new layers
lgraph = addLayers(lgraph, newLayers);
lgraph = connectLayers(lgraph, 'avg_pool', 'rcnnFc');
%plot lgraph
figure
plot(lgraph)
ylim([-5 16])
%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 added layers
lgraph = connectLayers(lgraph, 'avg_pool', 'rcnnBoxFc');
%display the classification and regression branches of the fast rcnn lgraph
figure
plot(lgraph)
ylim([-5 16])
%typical feature extraction layer of fast rcnn
featureExtractionLayer = 'activation_40_relu';
figure
plot(lgraph)
ylim([30 42])
% Disconnect layers attached to the feature extraction layer
lgraph = disconnectLayers(lgraph, featureExtractionLayer, 'res5a_branch1');
lgraph = disconnectLayers(lgraph, featureExtractionLayer, 'res5a_branch2a');
%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 roi max pool layer to the previously disconnected layers
lgraph = connectLayers(lgraph,'roiPool', 'res5a_branch2a');
lgraph = connectLayers(lgraph, 'roiPool', 'res5a_branch1');
% Display the resulting graph
figure
plot(lgraph)
ylim([30 42])
% 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');
% Show the network after adding the RPN layers.
figure
plot(lgraph)
ylim([30 42])
The training
data = load('objectgroundtruth.mat');
ObjectDataset(1:4,:);
I = imread(ObjectDataset.Imagefilename{10});
I = insertShape(I, 'Rectangle', ObjectDataset.Broom{10});
I = imresize(I,3);
figure;
imshow(I);
Layers = lgraph.Layers;
% Set random seed to ensure example training reproducibility.
rng(0);
%set training data
trainingData = ObjectDataset;
% Randomly split data into a training and test set.
shuffledIdx = randperm(height(ObjectDataset));
idx = floor(0.7 * height(ObjectDataset));
trainingData = ObjectDataset(shuffledIdx(1:idx),:);
testData = ObjectDataset(shuffledIdx(idx+1:end),:);
options = trainingOptions('sgdm', ...
'MiniBatchSize', 1, ...
'InitialLearnRate', 1e-3, ...
'MaxEpochs', 10, ...
'VerboseFrequency', 200, ...
'CheckpointPath', tempdir);
[detector, info] = trainFasterRCNNObjectDetector(trainingData, Layers, options);
1 Comment
Zohar Rimon
on 28 Aug 2019
Edited: Zohar Rimon
on 28 Aug 2019
Hello!
Did you end up solving this?
I am having the same error :(
SOLVED:
See solution in the Answers section
Answers (1)
Zohar Rimon
on 28 Aug 2019
You should change this line:
[detector, info] = trainFasterRCNNObjectDetector(trainingData, Layers, options);
To this one:
[detector, info] = trainFasterRCNNObjectDetector(trainingData, lgraph, options);
Categories
Find more on Object Detection in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!