Error using trainNetwork: Invalid training data. The output size of the last layer does not match the response size.

37 Ansichten (letzte 30 Tage)
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);

Antworten (2)

Srivardhan Gadila
Srivardhan Gadila am 22 Apr. 2020
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:
1. You can load the entire dataset using imageDatastore with labels as follows:
imds = imageDatastore('/MATLAB Drive/Images/Training Images', 'IncludeSubfolders',true, 'FileExtensions','.jpg','LabelSource','foldernames');
2. You have to use softmaxLayer followed by classificationLayer instead of regressionLayer
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.
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);
  1 Kommentar
Peter Gutstein
Peter Gutstein am 26 Apr. 2020
Thank you so much for the help, I really appreciate it. The initial error did go away, but now a new error is popping up. After it begins training, I get the following error message:
Error using trainNetwork (line 170)
Unexpected image size: All images must have the same size.
I've gone through and reformated all of my images to be 220x220x3 (and updated the imageInputLayer accordingly) using the code below (note that OLDNon-Deer and OLDDeer contain the images non-formatted):
currFolder = '/MATLAB Drive/Images/OLDNon-Deer';
filePattern = fullfile(currFolder, '*.jpg');
cameraFiles = dir(filePattern);
for i = 1 : length(cameraFiles)
filename = strcat('/MATLAB Drive/Images/OLDNon-Deer/',cameraFiles(i).name);
im = imread(filename);
k = imresize(im,[220,220]);
newfilename = strcat('/MATLAB Drive/Images/Training Images/Non-Deer/',cameraFiles(i).name);
imwrite(k,newfilename,'jpg');
end
currFolderD = '/MATLAB Drive/Images/OLDDeer';
filePatternD = fullfile(currFolderD, '*.jpg');
cameraFilesD = dir(filePatternD);
for d = 1 : length(cameraFilesD)
filenameD = strcat('/MATLAB Drive/Images/OLDDeer/',cameraFilesD(d).name);
imD = imread(filenameD);
kD = imresize(imD,[220,220]);
newfilenameD = strcat('/MATLAB Drive/Images/Training Images/Deer/',cameraFilesD(d).name);
imwrite(kD,newfilenameD,'jpg');
end
In order to check that the images are all of the same size, I have run the code below to make sure all images are of the same size (which it says they are):
currFolderD = '/MATLAB Drive/Images/Training Images/Deer';
filePatternD = fullfile(currFolderD, '*.jpg');
cameraFilesD = dir(filePatternD);
for d = 1 : length(cameraFilesD)
filenameD = strcat('/MATLAB Drive/Images/Training Images/Deer/',cameraFilesD(d).name);
imD = imread(filenameD);
[rows, columns, numberOfColorChannels] = size(imD);
if(rows ~= 220 && columns ~= 220)
fprintf('%s is not of size 220 x 220 but rather %d x %d\n',filenameD, rows, columns);
end
end
currFolder = '/MATLAB Drive/Images/Training Images/Non-Deer';
filePattern = fullfile(currFolder, '*.jpg');
cameraFiles = dir(filePattern);
for i = 1 : length(cameraFiles)
filename = strcat('/MATLAB Drive/Images/Training Images/Non-Deer/',cameraFiles(i).name);
imD = imread(filename);
[rows, columns, numberOfColorChannels] = size(imD);
if(rows ~= 220 && columns ~= 220)
fprintf('%s is not of size 220 x 220 but rather %d x %d\n',filename, rows, columns);
end
end
Any idea on what is causing the error and how to fix it? Thank you in advance for the assistance.

Melden Sie sich an, um zu kommentieren.


drummer
drummer am 23 Okt. 2020
Bearbeitet: drummer am 23 Okt. 2020
Hi,
Why are you resizing your original images, instead of using transform in your imds?
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

Kategorien

Mehr zu Image Data Workflows finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by