Neural Network input images size error

2 views (last 30 days)
tpolgar
tpolgar on 31 May 2017
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 Comments
Joss Knight
Joss Knight on 7 Jun 2017
Okay, so convert all your image files to RGB.
Limyandi Vicotrico
Limyandi Vicotrico on 3 Oct 2018
how to do this? convert all the imagedatastore to RGB ?

Sign in to comment.

Answers (1)

Aasma Aslam
Aasma Aslam on 4 Jan 2018
inputSize = net.Layers(1).InputSize(1:2) images.ReadFcn = @(loc)imresize(imread(loc),inputSize); use this command for resizing it will work.
  4 Comments
Rabia Afzal
Rabia Afzal on 1 Feb 2018
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
Limyandi Vicotrico
Limyandi Vicotrico on 3 Oct 2018
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);

Sign in to comment.

Categories

Find more on Deep Learning Toolbox 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!