Index exceeds the number of array elements (1511).

13 views (last 30 days)
Index exceeds the number of array elements (1511).
Error in kod (line 17)
imshow(imds.Files{perm(i)});
what should i do?
clc
clear all
close all
% verilerin okunması
digitDatasetPath = fullfile(matlabroot,'toolbox','nnet','nndemos', ...
'nndatasets','testverileri');
imds = imageDatastore(digitDatasetPath, ...
'IncludeSubfolders',true,'LabelSource','foldernames');
%verilerin gösterimi
figure;
perm = randperm(10000,20);
for i = 1:20
subplot(4,5,i);
imshow(imds.Files{perm(i)});
end
labelCount = countEachLabel(imds)
% verilerin ayrımı
[imdsTrain, imdsTest] = splitEachLabel(imds,0.75,'randomize');
%CNN tasarımı
layers = [
imageInputLayer([450 600 3])
convolution2dLayer(3,8,'Padding','same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(3,16,'Padding','same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(3,32,'Padding','same')
batchNormalizationLayer
reluLayer
fullyConnectedLayer(10)
softmaxLayer
classificationLayer];
%eÄŸitim parametreleri
options = trainingOptions('sgdm', ...
'InitialLearnRate',0.01, ...
'MaxEpochs',4, ...
'Shuffle','every-epoch', ...
'ValidationData',imdsTest, ...
'ValidationFrequency',30, ...
'Verbose',false, ...
'Plots','training-progress');
%EÄŸitim iÅŸlemi
net = trainNetwork(imdsTrain,layers,options);
% test iÅŸlemi
YPred = classify(net,imdsTest);
YTest = imdsTest.Labels;
accuracy = sum(YPred == YTest)/numel(YTest)

Answers (3)

Burhan Burak AKMAN
Burhan Burak AKMAN on 30 Dec 2021
May you use this method. .
img = readimage(imds,perm(i));
imshow(img);

Image Analyst
Image Analyst on 30 Dec 2021
Find out the value of i, and if it's more than 1511, figure out why you're trying to access element of perm that is more than 1511.
Or else find out the value of perm(i) and figure out why it's more than 1511 when the length of imds.Files is only 1511.
Before the error, put these two lines with no semicolons
i
perm(i)
If you want to display images in random order, then the code should look like
numFiles = length(imds.Files)
numImagesToDisplay = numFiles; % Or some number less than numfiles if you want only a subset of them.
randomIndexes = randperm(numFiles, numImagesToDisplay);
plotRows = ceil(sqrt(numImagesToDisplay))
for k = 1:numImagesToDisplay
subplot(plotRows, plotRows, k);
thisFileName = imds.Files{randomIndexes(k)};
rgbImage = imread(thisFileName);
imshow(rgbImage);
end

yanqi liu
yanqi liu on 31 Dec 2021
clc
clear all
close all
% verilerin okunması
digitDatasetPath = fullfile(matlabroot,'toolbox','nnet','nndemos', ...
'nndatasets','testverileri');
imds = imageDatastore(digitDatasetPath, ...
'IncludeSubfolders',true,'LabelSource','foldernames');
%verilerin gösterimi
figure;
perm = randperm(length(imds.Labels),20);
for i = 1:20
subplot(4,5,i);
imshow(imds.Files{perm(i)});
end
labelCount = countEachLabel(imds)
% verilerin ayrımı
[imdsTrain, imdsTest] = splitEachLabel(imds,0.75,'randomize');
%CNN tasarımı
layers = [
imageInputLayer([450 600 3])
convolution2dLayer(3,8,'Padding','same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(3,16,'Padding','same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(3,32,'Padding','same')
batchNormalizationLayer
reluLayer
fullyConnectedLayer(10)
softmaxLayer
classificationLayer];
%eÄŸitim parametreleri
options = trainingOptions('sgdm', ...
'InitialLearnRate',0.01, ...
'MaxEpochs',4, ...
'Shuffle','every-epoch', ...
'ValidationData',imdsTest, ...
'ValidationFrequency',30, ...
'Verbose',false, ...
'Plots','training-progress');
inputSize = layers(1).InputSize;
augimdsTrain = augmentedImageDatastore(inputSize(1:2),imdsTrain,'ColorPreprocessing','gray2rgb');
augimdsTest = augmentedImageDatastore(inputSize(1:2),imdsTest,'ColorPreprocessing','gray2rgb');
%EÄŸitim iÅŸlemi
net = trainNetwork(augimdsTrain,layers,options);
% test iÅŸlemi
YPred = classify(net,augimdsTest);
YTest = imdsTest.Labels;
accuracy = sum(YPred == YTest)/numel(YTest)

Community Treasure Hunt

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

Start Hunting!