Error while trying learning transfer example script Deep Learning Onramp
조회 수: 7 (최근 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에서 Deep Learning Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!