Error while trying learning transfer example script Deep Learning Onramp
10 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Prateek Rai
2021-8-16
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 个评论
Walter Roberson
2021-8-16
编辑:Walter Roberson
2021-8-16
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Data Workflows 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!