error in image size

2 views (last 30 days)
soukaina MEZIANE
soukaina MEZIANE on 28 Feb 2018
Commented: Walter Roberson on 25 Apr 2018
i want to train alexnet by transfer learning for classifying two classes I followed steps in tutorials. all seems good . I did all steps. I prepared data for processing all images are 227-227-3 but when i write
newnet=trainNetwork(net,data,options)
i get this error:
Trial>> [newnet,info] = trainNetwork(trainImgs, layers, options);
Training on single CPU.
Initializing image normalization.
Error using trainNetwork (line 140)
Unexpected image size: All images must have the same size.
Caused by:
Error using nnet.internal.cnn.MiniBatchDatasourceDispatcher>iCellTo4DArray (line 328)
Unexpected image size: All images must have the same size.
I croped and resized all images in my folder. They all have 227-227-3 uint8 but it doesn't work. I'm blocked any help

Accepted Answer

Walter Roberson
Walter Roberson on 1 Mar 2018
This check requires a somewhat recent MATLAB, but on the other hand alexnet requires a very recent MATLAB:
ext = 'png'; %adjust as appropriate
projectdir = 'Some\Appropriate\Directory';
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 ~= 227
fprintf('File "%s" width expected 227 got %d\n', thisfile, thisinfo.Width);
end
if thisinfo.Height ~= 227
fprintf('File "%s" height expected 227 got %d\n', thisfile, thisinfo.Height);
end
if thisinfo.SamplesPerPixel ~= 3
fprintf('File "%s" expected 3 color channels, got %d\n', thisfile, thisinfo.SamplesPerPixel);
end
end
catch ME
fprintf('Could not get image information for file "%s"', thisfile);
end
end
This will look for all of the png files under the directory you name in projectdir and check all of them for the right size.
This code requires R2016b or later for an extension to the functionality of dir() to be able to look in deeper directories, but that should be fine because earlier versions did not implement alexnet so you must not be using an earlier version.
  2 Comments
soukaina MEZIANE
soukaina MEZIANE on 1 Mar 2018
Thank you for your answer. It works. I use matlab2017 and i found that the depth differs from images and i chosed only the same depth for all images (.jpg)and compile the code that you gave me and it works. thanks very much for your time
Walter Roberson
Walter Roberson on 25 Apr 2018
The functionality of the above that needs R2016b or later is the recursive directory search, and the putting of the directory information into the the folder field .
You can replace that logic by calling one of the routines from the File Exchange such as https://www.mathworks.com/matlabcentral/fileexchange/19550-recursive-directory-listing

Sign in to comment.

More Answers (0)

Categories

Find more on Convert Image Type 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!