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

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)
I am not sure why this is coming up, please help!

10 commentaires

To confirm, lines32 is YTest = imdsTest.Labels correct? Could you post a subset of your data for testing please?
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.
Hi Mark,
No line 32 is in the read image function,
function [data, info] = readimage(imds, index)
%READIMAGE Read a specified image from the ImageDatastore.
% IMG = READIMAGE(IMDS,I) reads the I-th image from IMDS.
% By default, IMG is an
% [MxN] Integer - For grayscale images
% [MxNx3] Integer - For color images
% [MxNx4] Integer - For CMYK images
%
% [IMG,INFO] = READIMAGE(IMDS,I) also returns a structure with
% additional information about IMG. The fields of INFO are:
% Filename - Name of the file from which the image was read
% FileSize - Size of the file in bytes
% Label - Label for the file
%
% Example:
% --------
% folders = fullfile(matlabroot,'toolbox','matlab',{'demos','imagesci'});
% exts = {'.jpg','.png','.tif'};
% imds = imageDatastore(folders,'FileExtensions',exts);
%
% img2 = readimage(imds,2); % Read 2nd image
%
% See also imageDatastore, read, readall, hasdata, preview, reset.
% Copyright 2015 The MathWorks, Inc.
try
validateattributes(index, {'numeric'}, ...
{'scalar', 'positive', 'integer', '<=', imds.NumFiles}, ...
'ImageDatastore', 'INDEX', 2);
[data, info] = readUsingSplitReader(imds, index);
catch ME
throw(ME);
end
end
Line 32 in here is: throw(ME);
Hi Walter,
I currently have 28 images in the database, I will increase it later but I'm just trying to get it working first
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.
I am only allowed to post 10 images per 24 hours, i will post some tomorrow
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: https://fei.edu.br/~cet/facedatabase.html

Connectez-vous pour commenter.

 Réponse acceptée

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

2 commentaires

Yes, it only reads up to the 8th image and won't work after that, do you know how to solve this issue?
Thanks
[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

Connectez-vous pour commenter.

Plus de réponses (0)

Produits

Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by