MATLAB Answers

Why I cannot use Alexnet on my images dataset

조회 수: 9(최근 30일)
Hiba Basim Alwan
Hiba Basim Alwan 27 Jan 2018
Answered: Rasaq Kotun 12 Feb 2019
clc;
clear;
images = imageDatastore('C:\Users\HIBA\Documents\Cancellable Biometrics\Datasets\NIST Faces 1','IncludeSubfolders',true,'LabelSource','foldernames');
[trainingImages,testingImages] = splitEachLabel(images,0.7,'randomized');
net = alexnet;
net.Layers;
layer = 'fc7';
trainingFeatures = activations(net,trainingImages,layer,'outputAs','channels');
testFeatures = activations(net,testingImages,layer);

  댓글 수: 0

로그인 to comment.

답변 수 (3)

Walter Roberson
Walter Roberson 28 Jan 2018
alexnet needs R2016b or later, and requires Neural Network toolbox and requires Neural Network Toolbox Model for AlexNet Network support package

  댓글 수: 5

표시 이전 댓글 수: 2
Hiba Basim Alwan
Hiba Basim Alwan 28 Jan 2018
Yes, I confirm
the error message is:
Error using nnet.internal.cnn.MiniBatchDatasourceDispatcher>iCellTo4DArray (line 328)
Unexpected image size: All images must have the same size.
Error in nnet.internal.cnn.MiniBatchDatasourceDispatcher/readObservations (line 253)
observations = iCellTo4DArray(X);
Error in nnet.internal.cnn.MiniBatchDatasourceDispatcher/readData (line 245)
miniBatchData = this.readObservations(X);
Error in nnet.internal.cnn.MiniBatchDatasourceDispatcher/next (line 144)
[miniBatchData, miniBatchResponse] = readData(this);
Error in SeriesNetwork/activations (line 812)
[X, ~, i] = dispatcher.next();
Walter Roberson
Walter Roberson 28 Jan 2018
One of your images is a different size than the others.
The below code assumes you are using a newer release of MATLAB, which you indicate you are.
projectdir = 'C:\Users\HIBA\Documents\Cancellable Biometrics\Datasets\NIST Faces 1';
dinfo = dir(fullfile(projectdir, '**', '*.*'));
dinfo([dinfo.isdir]) = [];
filenames = fullfile({dinfo.folder}, {dinfo.name});
numfile = length(filenames);
info_struct = struct('filename', '', 'Width', nan, 'Height', nan, 'ColorType', '', 'Message', 'not a valid image file');
image_info(numfile) = info_struct;
info_valid = false(numfile, 1);
for idx = 1 : numfile
thisfile = filenames{idx};
this_info = info_struct;
this_info.filename = thisfile;
try
lastwarn('');
info = imfinfo(thisfile);
msgstr = lastwarn();
if ~isempty(msgstr)
this_info.Message = msgstr;
info_valid(idx) = true;
elseif length(info) == 1
this_info.Message = '';
info_valid(idx) = true;
elseif length(info) > 1
this_info.Message = 'image file with multiple images';
info = info(1);
info_valid(idx) = true;
end
if length(info) == 1
this_info.Width = info.Width;
this_info.Height = info.Height;
this_info.ColorType = info.ColorType;
end
catch ME
end
image_info(idx) = this_info;
end
image_info = image_info(info_valid);
After this, image_info will be a struct with information about all of the image files under the given directory. You can examine, for example
unique([image_info.Width])
unique([image_info.Height])
Sometimes imfinfo produces warnings as it goes: the warnings will be stored in the Message field of the structure. Some image files store multiple images, and in some image file formats those are not guaranteed to be the same size. Only the size information about the first image is returned by the above code, but in such case the Message field is set to 'image file with multiple images'
Hiba Basim Alwan
Hiba Basim Alwan 29 Jan 2018
Thank you so much for your helping

로그인 to comment.


Rabia Afzal
Rabia Afzal 4 Feb 2018
i'm also facing the same issue. Can anyone help me in my code?

  댓글 수: 2

Walter Roberson
Walter Roberson 6 Feb 2018
Start with the code I posted in https://www.mathworks.com/matlabcentral/answers/379353-why-i-cannot-use-alexnet-on-my-images-dataset#comment_529351 but replace projectdir with a character vector that is the top level directory under which all of the images of interest are stored. Run the code and examine the returned information, such as unique([image_info.Width]) to determine what widths actually occur in your files.
Hiba Basim Alwan
Hiba Basim Alwan 6 Feb 2018
Dear Rabia Afzal
I run the code provided by Walter Roberson and I found that my images is not on the accepted size stated by Alexnet ([227, 227, 3], but in your mind that your images should be color images with three channels). So, I embedded the below code found on Matlab Answers in my program to fix all my images size to 227, 227.
Regards,
images_set.ReadFcn=@(images_set)imresize(imread(images_set),[227 227]);

로그인 to comment.


Rasaq Kotun
Rasaq Kotun 12 Feb 2019
i used this and changed the dimensions to 227 x 277 and worked perfectsly

  댓글 수: 0

로그인 to comment.

이 질문에 답변하려면 로그인을(를) 수행하십시오.


Translated by