MATLAB Answers

"Index exceeds the number of array elements" in YoloV2ObjectDetector detect function.

6 views (last 30 days)
After running the following code, I complete the training for the YOLOV2 and obtain the detector object. When I try to use the detector, I obtain the following compile error:
THIS IS FOR RELEASE R2020a despite saying R2019b. R2020a is not a available option in drop down menu.
Index exceeds the number of array elements (2).
Error in yolov2ObjectDetector>iPredictUsingFeatureMap (line 868)
featureMap = reshape(featureMap,gridSize(1)*gridSize(2),gridSize(3),1,[]);
Error in yolov2ObjectDetector>iPostProcessActivations (line 982)
outputPrediction = iPredictUsingFeatureMap(featureMap, params.Threshold, info.PreprocessedImageSize,
anchorBoxes, params.FractionDownsampling, params.WH2HW);
Error in yolov2ObjectDetector>iPredictUsingDatastore (line 931)
iPostProcessActivations(fmap, batchInfo{ii}, anchorBoxes, params);
Error in yolov2ObjectDetector/detect (line 397)
varargout{1} = iPredictUsingDatastore(ds, this.Network, params, anchors, layerName);
Please help, thanks in advance.
%train set
imdsTrain = imageDatastore(table1{:,'imagefilename'},'ReadFcn',@fitsread);
bldsTrain = boxLabelDatastore(traintbl);
trainData = combine(imdsTrain, bldsTrain);
imdsTest = imageDatastore(table2{:,'imagefilename'},'ReadFcn',@fitsread);
bldsTest = boxLabelDatastore(testtbl);
testData = combine(imdsTest, bldsTest);
layers = [
imageInputLayer([2560 2560],"Name","imageinput")
convolution2dLayer([40 40],48,"Name","conv_1","Padding","same","Stride",[7 7])
batchNormalizationLayer("Name","batchnorm_1")
reluLayer("Name","relu_1")
maxPooling2dLayer([2 2],"Name","maxpool_1","Padding","same","Stride",[2 2])
convolution2dLayer([25 25],128,"Name","conv_2","Padding","same","Stride",[5 5])
batchNormalizationLayer("Name","batchnorm_2")
reluLayer("Name","relu_2")
convolution2dLayer([15 15],128,"Name","conv_4","Padding","same","Stride",[4 4])
batchNormalizationLayer("Name","batchnorm_4")
reluLayer("Name","relu_4")
maxPooling2dLayer([2 2],"Name",'maxpool_2',"Padding","same",'stride',[2 2])
convolution2dLayer([9 9],128,"Name","conv_6","Padding","same","Stride",[3 3])
batchNormalizationLayer("Name","batchnorm_6")
reluLayer("Name","relu_6")
convolution2dLayer([9 9],128,"Name","conv_5","Padding","same","Stride",[3 3])
batchNormalizationLayer("Name","batchnorm_5")
reluLayer("Name","relu_5")
maxPooling2dLayer([2 2],"Name","maxpool_3","Padding","same",'stride',[2 2])
convolution2dLayer([7 7],128,"Name","conv_9","Padding","same","Stride",[2 2])
batchNormalizationLayer("Name","batchnorm_9")
reluLayer("Name","relu_9")
convolution2dLayer([7 7],128,"Name","conv_8","Padding","same","Stride",[2 2])
batchNormalizationLayer("Name","batchnorm_8")
reluLayer("Name","relu_8")];
lgraph_homemade=layerGraph(layers);
%%%%%%%%%%make our own yolo from resnet50
numAnchors = 7;
[anchorBoxes,~] = estimateAnchorBoxes(trainData,numAnchors);
featureLayer = 'relu_8';
inputSize = [2560 2560];
numClasses = 1;
lgraph2 = yolov2Layers(inputSize,numClasses,anchorBoxes,lgraph_homemade,featureLayer);
options = trainingOptions('adam',...
'InitialLearnRate',0.005,...
'LearnRateSchedule','piecewise', ...
'LearnRateDropFactor',0.5, ...
'LearnRateDropPeriod',1, ...
'Verbose',true,...
'MiniBatchSize',8,...
'MaxEpochs',4,...
'Shuffle','never',...
'VerboseFrequency',1);
[detector,info] = trainYOLOv2ObjectDetector(trainData,lgraph2,options);
res=detect(detector,testData)

  0 Comments

Sign in to comment.

Accepted Answer

Anurag Pratap Singh
Anurag Pratap Singh on 22 Jun 2020
Hi Ryan
It is my understanding that you are trying to use the detector object.However you're getting the error Index exceeds the number of array elements (2).
This error is occuring because on line868 the gridSize array is of size 2(which is also indicated in the error) and you are accesing the 3rd element in gridSize by gridSize(3) which is not present.
Please refer to the https://www.mathworks.com/help/matlab/math/array-indexing.html (array indexing) documentation for more information on accessing the array elements.

  0 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!