Error while trying learning transfer example script Deep Learning Onramp

3 Ansichten (letzte 30 Tage)
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

Akzeptierte Antwort

Prateek Rai
Prateek Rai am 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 Kommentare
Walter Roberson
Walter Roberson am 16 Aug. 2021
Bearbeitet: Walter Roberson am 16 Aug. 2021
Instead of using a custom read function, I suspect you could use an Augmented Image Datastore https://www.mathworks.com/help/deeplearning/ref/augmentedimagedatastore.html to specify a resize transform
raw_flower_ds = imageDatastore('Flowers','IncludeSubfolders',true,'LabelSource','foldernames');
flower_ds = augmentedImageDatastore([227, 227], raw_flower_ds);
and the rest the same

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by