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!