Error using trainNetwork: Invalid training data. The output size of the last layer does not match the response size.
조회 수: 37 (최근 30일)
이전 댓글 표시
I was working on a neural network to identify deer from other wildlife, but when I tried to train it, this error popped up:
Error using trainNetwork (line 170)
Invalid training data. The output size ([1 1 1]) of the last layer does not match the response size ([300 400 3]).
I don't know what I am doing wrong here, if anyone could provide any suggestions/advice, that would be greatly appreciated. I've left the rest of the code for the program below if that would provide any additional insight into the problem.
nonDeerTrainSet = imageDatastore('/MATLAB Drive/Images/Training Images/Non-Deer');
nonDeerTrainSetSize = size(dir(['/MATLAB Drive/Images/Training Images/Non-Deer' '/*.jpg']),1);
%fprintf(1, 'nonDeerTrainSet made\n');
deerTrainSet = imageDatastore('/MATLAB Drive/Images/Training Images/Deer');
deerTrainSetSize = size(dir(['/MATLAB Drive/Images/Training Images/Deer' '/*.jpg']),1);
%fprintf(1, 'deerTrainSet made\n');
totalTrainingSetSize = deerTrainSetSize + nonDeerTrainSetSize;
trainingSet = combine(nonDeerTrainSet, deerTrainSet);
%Initialize the other factors needed for the net to run
netLayers = [imageInputLayer([400 600 3]),convolution2dLayer(12,25),reluLayer,fullyConnectedLayer(1),regressionLayer];
netOptions = trainingOptions("sgdm");
deerNet = trainNetwork(trainingSet, netLayers, netOptions);
댓글 수: 0
답변 (2개)
Srivardhan Gadila
2020년 4월 22일
Based on the above information and code, I think you are trying to build a network to classify between the classes deer and Non-deer.
I see that there are some mistakes and the following are some suggestions:
imds = imageDatastore('/MATLAB Drive/Images/Training Images', 'IncludeSubfolders',true, 'FileExtensions','.jpg','LabelSource','foldernames');
3. The outputSize value of the fullyConnectedLayer must be the number of classes/number of labels for the classification problem, which is 2 in this case.
4. You can make use of other Input Arguments of the trainingOptions like MiniBatchSize, Shuffle, etc.
5. Refer to Create Simple Deep Learning Network for Classification, Deep Learning Tips and Tricks & List of Deep Learning Layers
The following code might help you:
trainingSet = imageDatastore('/MATLAB Drive/Images/Training Images', 'IncludeSubfolders',true, 'FileExtensions','.jpg','LabelSource','foldernames');
countEachLabel(trainingSet)
netLayers = [imageInputLayer([400 600 3]),convolution2dLayer(12,25),reluLayer,fullyConnectedLayer(2),softmaxLayer,classificationLayer];
netOptions = trainingOptions("sgdm",'MiniBatchSize',32, ...
'InitialLearnRate',0.01, ...
'MaxEpochs',4, ...
'Shuffle','every-epoch', ...
'Plots','training-progress');
deerNet = trainNetwork(trainingSet, netLayers, netOptions);
drummer
2020년 10월 23일
편집: drummer
2020년 10월 23일
Hi,
PIPELINE:
% create imds
imds = imageDatastore('/MATLAB Drive/Images/Training Images', 'IncludeSubfolders',...
true, 'FileExtensions','.jpg','LabelSource','foldernames');
% split your deers and non-deers by LabelSource as in imds, using splitEachLabel.
[imdsTrain, imdsVal, imdsTest] = splitEachLabel(imds, 0.7, 0.2, 'randomized');
% resize by transforming your training set
augImdsTrain = transform(imdsTrain, @Transfc, 'IncludeInfo', true)
% From here, you could follow Srivardhan's suggestion
netLayers = [imageInputLayer([400 600 3]),...
convolution2dLayer(12,25),...
reluLayer,...
fullyConnectedLayer(2),...
softmaxLayer,...
classificationLayer];
netOptions = trainingOptions("sgdm",'MiniBatchSize',32, ...
'InitialLearnRate',0.01, ...
'MaxEpochs',4, ...
'Shuffle','every-epoch', ...
'Plots','training-progress');
deerNet = trainNetwork(augImdsTrain, netLayers, netOptions);
% function-handle - stays in the end of the code.
function [dataOut, info] = Transfc(data, info)
% here you resize your entire dataset properly
end
This way, you keep your original image sizes.
Cheers
댓글 수: 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!