필터 지우기
필터 지우기

Unexpected input size / why does Error accrue at trainNetwork function

조회 수: 2 (최근 30일)
YongBin Lee
YongBin Lee 2023년 1월 6일
답변: Varun Sai Alaparthi 2023년 1월 9일
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
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
Varun Sai Alaparthi 2023년 1월 9일
Hello YongBin,
This error might be due to few different reasons
  1. The Images in your dataset might have different number of channels (some grayscale and some RGB etc.)
  2. 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

카테고리

Help CenterFile Exchange에서 Image Data Workflows에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by