MATLAB Answers

0

Unexpected image size: All images must have the same size.

Asked by Andre Brandao on 5 Jul 2019
Latest activity Commented on by Andre Brandao on 8 Aug 2019
Hi, I'm having some problems with a bench of chest xray images. I tryed to use the code from the link below, but it did not work.
Error using trainNetwork (line 165)
Unexpected image size: All images must have the same size.
Error in chestXray1 (line 49)
net = trainNetwork(imdsTrain,layers,options);
inputSize = [224 224 1];
numClasses = 2;
layers = [
imageInputLayer(inputSize)
convolution2dLayer(5,20)
batchNormalizationLayer
reluLayer
fullyConnectedLayer(numClasses)
softmaxLayer
classificationLayer];
options = trainingOptions('sgdm', ...
'MaxEpochs',3, ...
'ValidationData',imdsValidation, ...
'ValidationFrequency',30, ...
'Verbose',false, ...
'Plots','training-progress');
net = trainNetwork(imdsTrain,layers,options);

  8 Comments

Andre - what is the output when you run the following code
ext = 'jpeg'; %adjust as appropriate
projectdir = '/Users/andrebr4/Documents/MATLAB/chestXray/chest_xray';
dinfo = dir(fullfile(projectdir, '**', ['.' ext]);
allfiles = fullfile({dinfo.folder}, {dinfo.name});
for K = 1 : length(allfiles)
thisfile = allfiles{K};
try
thisinfo = imfinfo(thisfile);
if ~isfield(thisinfo, 'Width') || ~isfield(thisinfo, 'Height') || ~isfield(thisinfo, 'SamplesPerPixel')
fprintf('image information for file "%s" is missing width or height or #channels\n', thisfile);
else
if thisinfo.Width ~= 224
fprintf('File "%s" width expected 224 got %d\n', thisfile, thisinfo.Width);
end
if thisinfo.Height ~= 224
fprintf('File "%s" height expected 224 got %d\n', thisfile, thisinfo.Height);
end
if thisinfo.SamplesPerPixel ~= 1
fprintf('File "%s" expected 1 color channels, got %d\n', thisfile, thisinfo.SamplesPerPixel);
end
end
catch ME
fprintf('Could not get image information for file "%s"', thisfile);
end
end
is missing width or height or #channels
try putting a breakpoint at the line
allfiles = fullfile({dinfo.folder}, {dinfo.name});
and then run the code. When the debugger pauses at thisline, step through the subsequent lines. What is thisfile set to? What is thisinfo?

Sign in to comment.

Products


Release

R2019a

1 Answer

Answer by Dheeraj Singh on 5 Aug 2019
 Accepted Answer

You can use augmentedImageDataStore to resize all images to same size.
Use the following code for your problem:
dataChest = fullfile('/Users/andrebr4/Documents/MATLAB/chestXray/chest_xray');
imds = imageDatastore(dataChest, ...
'IncludeSubfolders',true, ...
'LabelSource','foldernames');
%% Dividir o conjunto de dados em cada categoria
numTrainingFiles = 750;
[imdsTrain,imdsValidation] = splitEachLabel(imds,numTrainingFiles,'randomize');
%%%%%%%code for resizing
inputSize=[224 224 1];
imdsTrain=augmentedImageDatastore(inputSize, imdsTrain,'ColorPreprocessing','rgb2gray');
imdsValidation=augmentedImageDatastore(inputSize, imdsValidation,'ColorPreprocessing','rgb2gray');
%% Configurar a rede neural
inputSize = [224 224 1];
numClasses = 2;
layers = [
imageInputLayer(inputSize)
convolution2dLayer(5,20)
batchNormalizationLayer
reluLayer
fullyConnectedLayer(numClasses)
softmaxLayer
classificationLayer];
%% Opções de treino
options = trainingOptions('sgdm', ...
'MaxEpochs',5, ...
'ValidationData',imdsValidation, ...
'ValidationFrequency',30, ...
'Verbose',false, ...
'Plots','training-progress');
%% Treinar a rede neural
net = trainNetwork(imdsTrain,layers,options);
%% Executar rede treinada no conjunto de teste
YPred = classify(net,imdsValidation);
YValidation = imdsValidation.Labels;
%% Calcular a precisão
accuracy = sum(YPred == YValidation)/numel(YValidation)

  1 Comment

Sign in to comment.