trainNetwork Error in line 170. Unexpected image size

5 ビュー (過去 30 日間)
Eline Geerits
Eline Geerits 2022 年 4 月 19 日
回答済み: T.Nikhil kumar 2024 年 4 月 17 日
I am trying to retrain the AlexNet but I keep getting the same Error and I have no idea whats wrong.
I get the Error: Error using trainNetworl (line 170) unexpected image size: All images must have the same size.
clc
clear all
close all
matlabpath = 'D:\Eline\Uni\Bachelorarbeit'
data = fullfile(matlabpath,'Training_Data_Set/')
train = imageDatastore(data,'IncludeSubfolders',true,"LabelSource","foldernames");
count = train.countEachLabel;
% load Trained Network %
net = alexnet;
layers = [imageInputLayer([227 227 3])
net(2:end-3)
fullyConnectedLayer(5)
softmaxLayer
classificationLayer()
]
% train Network %
options = trainingOptions('sgdm', 'Maxepoch', 5, 'InitialLearnRate', 0.0001)
training = trainNetwork(train, layers, options);
this is my code and when I jump to line 170 I have the following code
narginchk(3,4);
try
[layersOrGraph, opts, X, Y] = iParseInputArguments(varargin{:});
[trainedNet, info] = doTrainNetwork(layersOrGraph, opts, X, Y);
catch e
iThrowCNNException( e );%this is line 170
end
end
What do I have to do?

回答 (1 件)

T.Nikhil kumar
T.Nikhil kumar 2024 年 4 月 17 日
Hello Eline,
It appears that you want to train AlexNet on your own dataset but are facing an error with the images.
The error message states that not all images in your training dataset are of the same size. AlexNet requires all input images to be of size 227x227x3.
I suggest you to use the ‘augmentedImageDatastore’ to automatically resize the images from ‘imageDatastore’ to the required size for AlexNet. Include the below mentioned lines of code and use the ‘augmentedTrain’ with your ‘trainNetwork’ function.
inputSize = [227 227];
augmentedTrain = augmentedImageDatastore(inputSize, train);
I also can see some minor issues in the code that you’ve shared. In the ‘trainingOptions’ function, you have given the option name as ‘Maxepoch’. The correct name is ‘MaxEpochs’. Also, The way you are extracting layers from net is incorrect (using net(2:end-3)). The correct way is to use net.Layers(2:end-3).
I hope you will be able to proceed with your work with the help of these suggestions.

カテゴリ

Help Center および File ExchangeImage Data Workflows についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by