Problem with image size for AlexNet

Hello everyone!
I'm trying to train an AlexNet model to Feature Extraction and i'm getting the following problems:
I have the following imageDatastore:
imds = imageDatastore(fullfile(path),...
'IncludeSubfolders', true, 'LabelSource', 'foldernames',...
'ReadFcn', @preProcess);
As you can see, i'm using the 'ReadFcn' to make resize over all the images from the data set.
My 'preProcess' function:
image = imread(imgFile);
image = imresize(image, [227, 227]);
And my AlexNet model:
net = alexnet;
layers = net.Layers;
layers(end-2) = fullyConnectedLayer(102);
layers(end) = classificationLayer;
options = trainingOptions('sgdm', ...
'MaxEpochs', 6, 'MiniBatchSize', 64, 'ExecutionEnvironment', 'GPU');
[mynet, netinfo] = trainNetwork(imds, layers, options);
I'm getting the following error:
Caused by:
Error using nnet.internal.cnn.MiniBatchDatasourceDispatcher>iCellTo4DArray (line 328)
Unexpected image size: All images must have the same size.
Someone can help me?
Thank you for the support!

2 件のコメント

Adam
Adam 2018 年 4 月 5 日
It shouldn't be causing your problem (I can't see off-hand what would cause it), but don't name a variable 'image', there is a function of the same name that you are over-riding by doing this.
Arthur
Arthur 2018 年 4 月 5 日
Hi, Adam, I changed the identifier of the function variable to "img", as you mentioned and i'm still getting this error:
Error using trainNetwork (line 140)
Unexpected image size: All images must have the same size.
Error in modelNet (line 11)
[mynet, netinfo] = trainNetwork(imds, layers, options);
Error in mainFile (line 27)
[mynet, netinfo] = modelNet(numClasses, options, imds);
Caused by:
Error using nnet.internal.cnn.MiniBatchDatasourceDispatcher>iCellTo4DArray (line 328)
Unexpected image size: All images must have the same size.
Thank you for the correction, i forgot the image function!

サインインしてコメントする。

 採用された回答

Arthur
Arthur 2018 年 4 月 5 日

1 投票

Hi everyone! On MATLAB 2017b version, i solved my problem with:
imageSize = [277 277 3];
auimds = augmentedImageSource(imageSize, imds);

3 件のコメント

YanNuan Li
YanNuan Li 2018 年 6 月 18 日
Hi, I have got the same problem with yours. Could you provide the full code of your training code?
Onkar Mulay
Onkar Mulay 2020 年 2 月 5 日
imds=imageDatastore('C:\Users\Onkar\Desktop\HE\brats_test\Testing\HGG_LGG\brats_2013_pat0103_1\VSD.Brain.XX.O.MR_Flair.54193\Dataset','IncludeSubfolders',true,'LabelSource','foldernames');
imageSize = [277 277 3];
auimds = augmentedImageSource(imageSize, imds);
net=alexnet;
layer='fc8';
[imdsTrain,imdsValidation]=splitEachLabel(imds,0.7,'randomized');
featuresTrain=activations(net,imdsTrain,layer,'outputAs','rows');
featuresTest=activations(net,imdsValidation,layer,'outputAs','rows');
svmtrain=fitcsvm(featuresTrain,imdsTrain.Labels);
ypred=predict(svmtrain,featuresTest);
plotconfusion(imdsValidation.Labels,ypred);
This code still gives same error.
Jithin Nambiar J
Jithin Nambiar J 2020 年 6 月 8 日
@Onkar Mulay
Alexnet accepts images of size [227 227 3]. The code gives you the same error since you are passing the original datastore imds and not auidms. Using the augmented image datastore function won't support using the splitEachLabel function.
If you want to resize the image and split them to imdsTrain and imdsValidation use
imds=imageDatastore('C:\Users\Onkar\Desktop\HE\brats_test\Testing\HGG_LGG\brats_2013_pat0103_1\VSD.Brain.XX.O.MR_Flair.54193\Dataset','IncludeSubfolders',true,'LabelSource','foldernames');
audimds=augmentedImageDatastore([227 227],imds);
% Instead of this line [trainImgs,testImgs] = splitEachLabel(auds,0.85);
% Note that these images are not Randmomized
imdsTrain=partitionByIndex(audimds,[1:900]);
imdsValidation=partitionByIndex(audimds,[901:920]);
numClasses = numel(categories(imds.Labels));

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeDeep Learning Toolbox についてさらに検索

質問済み:

2018 年 4 月 4 日

コメント済み:

2020 年 6 月 8 日

Community Treasure Hunt

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

Start Hunting!

Translated by