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

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 2017년 6월 7일
Okay, so convert all your image files to RGB.
how to do this? convert all the imagedatastore to RGB ?

댓글을 달려면 로그인하십시오.

답변 (1개)

Aasma Aslam
Aasma Aslam 2018년 1월 4일

0 개 추천

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

댓글 수: 4

Aasma Aslam
Aasma Aslam 2018년 1월 4일
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 2018년 2월 1일
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);

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Deep Learning Toolbox에 대해 자세히 알아보기

질문:

2017년 5월 31일

댓글:

2018년 10월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by