Unexpected input size / why does Error accrue at trainNetwork function
1 次查看(过去 30 天)
显示 更早的评论
hi, i'm trying the example of deep learning.
i downloaded the example code at mathworks and run it whit my own data set.
but this kind of error accrue
this is a code i used
alex = alexnet;
layers = alex.Layers
%%%
layers(23) = fullyConnectedLayer(1);
%%% This is the only line I changed. and yes, i have only one data set
layers(25) = classificationLayer
allImages = imageDatastore('myImages', 'IncludeSubfolders', true, 'LabelSource', 'foldernames');
[trainingImages, testImages] = splitEachLabel(allImages, 0.8, 'randomize');
opts = trainingOptions('sgdm', 'InitialLearnRate', 0.001, 'MaxEpochs', 20, 'MiniBatchSize', 64);
%%%
myNet = trainNetwork(trainingImages, layers, opts);
%%% This is where error accrues
predictedLabels = classify(myNet, testImages);
accuracy = mean(predictedLabels == testImages.Labels)
I'm very new to deep learning.
so i have no idea what the error means.
can anyone tell me what is the meaning of the error messege or what the problem is?
any advice or sloution would be very helpful to me...
1 个评论
Engin Uzun
2023-1-6
The reason of error you get, your images are not same size you should resize your images via :
outputSize should be 227x227x3 to train alexnet properly.
imds is your allImages imagedatastore.
回答(1 个)
Varun Sai Alaparthi
2023-1-9
Hello YongBin,
This error might be due to few different reasons
- The Images in your dataset might have different number of channels (some grayscale and some RGB etc.)
- The Images might not be of same expected shape by alexnet(227,227,3)
The following workflow might help resolve your issue
Create a custom ‘ReadFcn ‘ for your custom Image datastore to resize and adjust the channels accordingly. The following code helps you to do so:
imageDS = imageDatastore(imgPath);
imageDS.ReadFcn = @customReadDatstoreImage;
function data = customReadDatastoreImage(filename)
data = imread(filename);
data = data(:,:,min(1:3, end));% helps you set sets all the images to have the same number of channels as the image with the fewest channels.
data = imresize(data,[227 227]);% this is to resize all the images to the size the model accepts.
end
If still the issue persists and if your dataset has some grayscale images and number of channels became 1, you can use the following code to make the images RGB
auimds = augmentedImageDatastore(outputSize,imds, 'ColorPreprocessing', 'gray2rgb')
Run your script again now using ‘auimds’ as your datastore.
At this point, it is possible that you are getting a different, more specific error message, referring to incompatible image formats. If this is the case, you will need to remove the incompatible images from your dataset. The error message should point out which images are incompatible. Remove those images and try classifying again.
I hope this information helps and please reach out for any further issues.
Sincerely
Varun
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!