MATLAB Answers

Error while trying learning transfer example script Deep Learning Onramp

3 views (last 30 days)
Hello,
While trying the transfer learning example (4/4) on Deep Learning Onramp, I get the following error
>> flower_ds = imageDatastore('Flowers','IncludeSubfolders',true,'LabelSource','foldernames');
>> [trainImgs,testImgs] = splitEachLabel(flower_ds,0.6);
>> numClasses = numel(categories(flower_ds.Labels));
>> net = alexnet;
>> layers = net.Layers;
>> layers(end-2) = fullyConnectedLayer(numClasses);
>> layers(end) = classificationLayer;
>> options = trainingOptions('sgdm','InitialLearnRate', 0.001);
>> [flowernet,info] = trainNetwork(trainImgs, layers, options);
Error using trainNetwork (line 170)
The training images are of size 224x224x3 but the input layer expects images of size 227x227x3.
Any help would appreciated, thanks
Fer

Accepted Answer

Prateek Rai
Prateek Rai on 16 Aug 2021
To my understanding, you are trying to run the transfer learning example (4/4) on Deep Learning Onramp and getting the error. The error states that the "The training images are of size 224x224x3 but the input layer expects images of size 227x227x3."
The error is coming because you are giving input image of size 224x224x3 while the input layer of alex net takes an image of size 227x227x3. So firstly you have to resize your input image to 227x227x3 so that it can be considered a valid input image for AlexNet network.
For resizing, you can create an ImageDatastore object and specify the 'ReadFcn' parameter. You can use the 'ReadFcn' to make resize over all the images from the data set.
flower_ds = imageDatastore('Flowers','IncludeSubfolders',true,'LabelSource','foldernames','ReadFcn', @preProcess);
Note: Here @preProcess is a function handle of the function which will takes an image location as an input, read the image, resize the image and output the resized image.
function imageOut = preProcess(imageLoc)
imageIn = imread(imageLoc);
imageOut = imresize(imageIn,227/224);
end
Here is the full code for your reference:
flower_ds = imageDatastore('Flowers','IncludeSubfolders',true,'LabelSource','foldernames','ReadFcn',@preProcess);
[trainImgs,testImgs] = splitEachLabel(flower_ds,0.6);
numClasses = numel(categories(flower_ds.Labels));
net = alexnet;
layers = net.Layers;
layers(end-2) = fullyConnectedLayer(numClasses);
layers(end) = classificationLayer;
options = trainingOptions('sgdm','InitialLearnRate', 0.001);
[flowernet,info] = trainNetwork(trainImgs, layers, options);
%% function for ReadFcn Parameter
function imageOut = preProcess(imageLoc)
imageIn = imread(imageLoc);
imageOut = imresize(imageIn,227/224);
end
You can refer to AlexNet MathWorks documentation page to learn more on AlexNet convolutional neural network in MATLAB. Moreover, you can also refer to imageDatastore MathWorks documentation page to find more on ImageDatastore object.
  2 Comments

Sign in to comment.

More Answers (0)

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!