Problem with image size for AlexNet
12 views (last 30 days)
Show older comments
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 Comments
Adam
on 5 Apr 2018
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.
Accepted Answer
Arthur
on 5 Apr 2018
3 Comments
Onkar Mulay
on 5 Feb 2020
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
on 8 Jun 2020
@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));
More Answers (0)
See Also
Categories
Find more on Deep Learning Toolbox 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!