How do I solve this error: Error using matlab.io.datastore.ImageDatastore/readimage (line 32)?
Show older comments
I am trying to use feature extraction to recognise faces, however when I run the code these errors come up:
Error using matlab.io.datastore.ImageDatastore/readimage (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
unzip('ATTDatabase.zip');
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);
figure
for i = 1:10
subplot(2,5,i)
I = readimage(imdsTrain,idx(i));
imshow(I)
end
%Load pretrained network (AlexNet)
net = vgg19();
net.Layers
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];
figure
for i = 1:numel(idx)
subplot(2,2,i)
I = readimage(imdsTest,idx(i));
label = YPred(idx(i));
imshow(I)
title(char(label))
end
%Accuracy of model
accuracy = mean(YPred == YTest)
The majority of the code can be found here: https://uk.mathworks.com/help/deeplearning/examples/feature-extraction-using-alexnet.html
I am not sure why this is coming up, please help!
10 Comments
Mark Sherstan
on 11 Dec 2018
To confirm, lines32 is YTest = imdsTest.Labels correct? Could you post a subset of your data for testing please?
Walter Roberson
on 11 Dec 2018
you assume that there are 15 images in the database which is not necessarily true . The code appears to be taken from an example with more images.
Sujit Mistry
on 11 Dec 2018
Edited: Sujit Mistry
on 11 Dec 2018
Sujit Mistry
on 11 Dec 2018
Edited: Sujit Mistry
on 12 Dec 2018
Walter Roberson
on 11 Dec 2018
You only post 10 images, but
idx = [1 5 10 15];
...
I = readimage(imdsTest,idx(i));
so you are reading up to image idx(4) = 15.
Sujit Mistry
on 12 Dec 2018
Sujit Mistry
on 13 Dec 2018
Walter Roberson
on 13 Dec 2018
you can zip files together .
Walter Roberson
on 13 Dec 2018
Edited: Walter Roberson
on 14 Dec 2018
Did you obtain the AT&T face database from http://www.cl.cam.ac.uk/research/dtg/attarchive/facedatabase.html ?
Sujit Mistry
on 14 Dec 2018
Accepted Answer
More Answers (0)
Categories
Find more on Image Processing and Computer Vision 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!