MATLAB Answers


How do I solve this error: Error using​datastore.​ImageDatas​tore/readi​mage (line 32)?

Asked by Sujit Mistry on 7 Dec 2018
Latest activity Edited by Walter Roberson
on 14 Dec 2018
I am trying to use feature extraction to recognise faces, however when I run the code these errors come up:
Error using (line 32)
Expected input number 2, INDEX, to be a scalar with value <= 8.
Error in Alexnet_Feature_Test (line 31)
I = readimage(imdsTest,idx(i));
Below is my code, I have tried using vgg19 and Alexnet and get the same errors for both
%Load zipped images
imds = imageDatastore('ATTDatabase','IncludeSubfolders',true,'LabelSource','foldernames');
[imdsTrain,imdsTest] = splitEachLabel(imds,0.7,'randomized');
%Display sample images from zip file
numTrainImages = numel(imdsTrain.Labels);
idx = randperm(numTrainImages,10);
for i = 1:10
I = readimage(imdsTrain,idx(i));
%Load pretrained network (AlexNet)
net = vgg19();
inputSize = net.Layers(1).InputSize;
%Extracting image features
augimdsTrain = augmentedImageDatastore(inputSize(1:2),imdsTrain);
augimdsTest = augmentedImageDatastore(inputSize(1:2),imdsTest);
layer = 'fc7';
featuresTrain = activations(net,augimdsTrain,layer,'OutputAs','rows');
featuresTest = activations(net,augimdsTest,layer,'OutputAs','rows');
%Extracting class labels
YTrain = imdsTrain.Labels;
YTest = imdsTest.Labels;
%Image classifier
classifier = fitcecoc(featuresTrain, YTrain);
%Classification from test
YPred = predict(classifier,featuresTest);
%Showing 4 images with labels
idx = [1 5 10 15];
for i = 1:numel(idx)
I = readimage(imdsTest,idx(i));
label = YPred(idx(i));
%Accuracy of model
accuracy = mean(YPred == YTest)
I am not sure why this is coming up, please help!


Sorry I initially tried using the AT&T database but as they are black and white, they won't work. Here is the link for the images i used:

Sign in to comment.

1 Answer

Answer by Sean de Wolski
on 13 Dec 2018
 Accepted Answer

It looks like the datastore (probably imdsTest) only sees 8 images so you can't read the 10th or 15th one.


Yes, it only reads up to the 8th image and won't work after that, do you know how to solve this issue?
[imdsTrain,imdsTest] = splitEachLabel(imds,0.7,'randomized');
Why do you care about image 10, 15? The validation step is randomly grabbing 8 images (30%) from the original datastore so the indices 10,15 don't mean much anyway. Why not validate against the 8 it picked?
idx = [1 2 5 7]; % or whatever

Sign in to comment.