Neural Network input images size error

Hello Matlab Community,
I try to train a convolutional nn with labelled images. Currently i just want to understand the usage, its not a specific task.
The inputs are:
files = imageDatastore({'D:\...\containingDirectory1','D:\...\containingDirectory2'});
Which are 200x200x1 grayscale images. I cropped them to 200x200 with a function and checked the sizes manually.
Labels made manually into a categorical struct, then added to the files:
files.Labels = labels;
Layers are:
layers = [ imageInputLayer([200,200,1])
convolution2dLayer(80,80)
reluLayer
maxPooling2dLayer(40,'Stride',40)
fullyConnectedLayer(2)
softmaxLayer
classificationLayer
];
Training options are:
opts = trainingOptions('sgdm','ExecutionEnvironment','cpu');
('cpu' because my GPU capacity is not enough)
Training:
convnet = trainNetwork(files, layers, opts)
Error:
Error using nnet.internal.cnn.ImageDatastoreDispatcher>iCellTo4DArray (line 246)
Unexpected image size: All images must have the same size.
The heights, widths, color channels and bit depths are the same. All of them are jpg-s. I tried to use just 2 images but it resulted the same error. I tried 2 full black images, one copied from the other, and the "training" ran successfully.
I upload two training images for an example.

4 Kommentare

Okay, so what does this say:
N = numel(files.Files);
for i = 1:N
fn = files.Files{i};
im = readimage(files, i);
fprintf('Filename: %s\nSize %d-by-%d-by-%d\n', fn, size(im,1), size(im,2), size(im,3));
end
Thanks for your comment! Interestingly, in some cases it say
Filename: D\...
Size 200-by-200-by-3
and another cases:
Filename: D:\...
Size 200-by-200-by-1
Working with just one type does not result error.
Joss Knight
Joss Knight am 7 Jun. 2017
Okay, so convert all your image files to RGB.
Limyandi Vicotrico
Limyandi Vicotrico am 3 Okt. 2018
how to do this? convert all the imagedatastore to RGB ?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Aasma Aslam
Aasma Aslam am 4 Jan. 2018

0 Stimmen

inputSize = net.Layers(1).InputSize(1:2) images.ReadFcn = @(loc)imresize(imread(loc),inputSize); use this command for resizing it will work.

4 Kommentare

Aasma Aslam
Aasma Aslam am 4 Jan. 2018
inputSize = net.Layers(1).InputSize(1:2) ds.ReadFcn = @(loc)imresize(imread(loc),inputSize); use this command for resizing, it will work. use this command after creating object of the imagedatastore. Ds is the object of datastore
Rabia Afzal
Rabia Afzal am 1 Feb. 2018
Miss Aasma can you please write it from scratch. I cant find this line of code to be working. what is this @loc and how to make this object. Kindly help!!
And what is this InputSize. Where should I use it. Plz help
if true
DatasetPath = fullfile(matlabroot,'MYDATA');
digitData = imageDatastore(DatasetPath,...
'IncludeSubfolders',true,'LabelSource','foldernames');
% inputSize = net.Layers(1).InputSize(1:2) images.ReadFcn = @(loc)imresize(imread(loc),inputSize);
% net=alexnet; % inputSize = net.Layers(1).InputSize(1:2) % im = imresize(im,inputSize); %digitData.ReadFcn = @(loc)imresize(imread(loc),inputSize);
figure; perm = randperm(20000,20); for i = 1:20 subplot(4,5,i); imshow(digitData.Files{perm(i)}); end
labelCount = countEachLabel(digitData)
img = readimage(digitData,20); size(img)
% trainNumFiles = 3000; % [trainDigitData,valDigitData] = splitEachLabel(digitData,trainNumFiles,'randomize'); % % % Define the convolutional neural network architecture. % layers = [ % imageInputLayer([480 640 3]) % % convolution2dLayer(3,16,'Padding',1) % batchNormalizationLayer % reluLayer % % maxPooling2dLayer(2,'Stride',2) % % convolution2dLayer(3,32,'Padding',1) % batchNormalizationLayer % reluLayer % % maxPooling2dLayer(2,'Stride',2) % % convolution2dLayer(3,64,'Padding',1) % batchNormalizationLayer % reluLayer % % fullyConnectedLayer(4) % softmaxLayer % classificationLayer]; % % options = trainingOptions('sgdm',... % 'MaxEpochs',3, ... % 'ValidationData',valDigitData,... % 'ValidationFrequency',30,... % 'Verbose',false,... % 'Plots','training-progress'); % % % net = trainNetwork(trainDigitData,layers,options); % % % predictedLabels = classify(net,valDigitData); % valLabels = valDigitData.Labels; % % accuracy = sum(predictedLabels == valLabels)/numel(valLabels) end
can someone elaborate more on how it work again? I cant get it working.
inputSize = net.Layers(1).InputSize(1:2);
imds.ReadFcn = @(loc)imresize(imread(loc),inputSize);

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Deep Learning Toolbox finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 31 Mai 2017

Community Treasure Hunt

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

Start Hunting!

Translated by