MATLAB Answers

Error with difference in the output size and response size during the model training

10 views (last 30 days)
I have been trying to develop a model to detect circular/psuedo-circular objects from grayscale images with low contrast. I have developed the network for the model, and currently trying to perform training on the data. However the following error has popped up which I have indicated below.
I am posting the coding for your reference.
An Error that I faced was:
Invalid Training data. The output size ([458 458 1]) of the last layer does not match the response size ([1 1 1]).
Can anyone please help me out, I have a deadline nearby.
dataDir = fullfile('D:\M.Sc Research Project\Data');
imDir = fullfile(dataDir, 'ImageSetRevised');
imds = imageDatastore(imDir, 'LabelSource', 'foldernames');
[trainSet,testSet] = splitEachLabel(imds,0.7,'randomized'); %Dividing the dataset into test and train
%%code for resizing
%% Saving training and test data into separate folders
location_train = 'D:\M.Sc Research Project\Deep Learning Approach\Deep Learning Method for Scale Detection\traindata\TrainingImages';
location_test = 'D:\M.Sc Research Project\Deep Learning Approach\Deep Learning Method for Scale Detection\testdata\TestingImages';
%writeall(trainSet,location_train);
%writeall(testSet,location_test);
PD = 0.30;
cv = cvpartition(size(gTruth.LabelData,1),'HoldOut',PD);
trainGroundTruth = groundTruth(groundTruthDataSource(gTruth.DataSource.Source(cv.training,:)),gTruth.LabelDefinitions,gTruth.LabelData(cv.training,:));
testGroundTruth = groundTruth(groundTruthDataSource(gTruth.DataSource.Source(cv.test,:)),gTruth.LabelDefinitions,gTruth.LabelData(cv.test,:));
dataSetDir = fullfile('D:\','M.Sc Research Project','Deep Learning Approach','Deep Learning Method for Scale Detection','traindata','TrainingImages');
imageDir = fullfile(dataSetDir,'ImageSetRevised','LabelSource','filenames');
imds_train = imageDatastore(imageDir);
classNames = ["scales","background"];
imageSize = [461 461];
numClasses = 2;
lgraph = layerGraph();
tempLayers = imageInputLayer([461 461 1],"Name","imageinput");
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
convolution2dLayer([3 3],64,"Name","conv1_1","Padding","same","PaddingValue",100)
reluLayer("Name","relu1_1")
maxPooling2dLayer([2 2],"Name","pool1")
convolution2dLayer([3 3],64,"Name","conv1_2","Padding","same","PaddingValue",100)
reluLayer("Name","relu1_2")
convolution2dLayer([3 3],128,"Name","conv2_1","Padding","same","PaddingValue",100)
reluLayer("Name","relu2_1")
convolution2dLayer([3 3],128,"Name","conv2_2","Padding","same","PaddingValue",1)
reluLayer("Name","relu2_2")
maxPooling2dLayer([2 2],"Name","pool2")
convolution2dLayer([3 3],256,"Name","conv3_1","Padding","same","PaddingValue",1)
reluLayer("Name","relu3_1")
convolution2dLayer([3 3],256,"Name","conv3_2","Padding","same","PaddingValue",1)
reluLayer("Name","relu3_2")
convolution2dLayer([3 3],256,"Name","conv3_3","Padding","same","PaddingValue",1)
reluLayer("Name","relu3_3")
maxPooling2dLayer([2 2],"Name","pool3")
convolution2dLayer([3 3],512,"Name","conv4_1","Padding","same","PaddingValue",1)
reluLayer("Name","relu4_1")
convolution2dLayer([3 3],512,"Name","conv4_2","Padding","same","PaddingValue",1)
reluLayer("Name","relu4_2")
convolution2dLayer([3 3],512,"Name","conv4_3","Padding","same","PaddingValue",1)
reluLayer("Name","relu4_3")
maxPooling2dLayer([2 2],"Name","pool4")];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = convolution2dLayer([2 2],1,"Name","score_pool4","Padding","same");
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
convolution2dLayer([3 3],512,"Name","conv5_1","Padding","same","PaddingValue",1)
reluLayer("Name","relu5_1")
convolution2dLayer([3 3],512,"Name","conv5_2","Padding","same","PaddingValue",1)
reluLayer("Name","relu5_2")
convolution2dLayer([3 3],512,"Name","conv5_3","Padding","same","PaddingValue",1)
reluLayer("Name","relu5_3")
maxPooling2dLayer([2 2],"Name","pool5")
convolution2dLayer([7 7],4096,"Name","fc6","Padding","same")
reluLayer("Name","relu6")
dropoutLayer(0.5,"Name","drop6")
convolution2dLayer([1 1],4096,"Name","fc7","Padding","same")
reluLayer("Name","relu7")
dropoutLayer(0.5,"Name","drop7")
convolution2dLayer([1 1],2,"Name","score_fr","Padding","same")
transposedConv2dLayer([2 2],4,"Name","upscore2")];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = crop2dLayer("centercrop","Name","score_pool4c");
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
additionLayer(2,"Name","fuse")
transposedConv2dLayer([2 2],32,"Name","upscore16")];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
crop2dLayer("centercrop","Name","score")
softmaxLayer("Name","softmax")
pixelClassificationLayer("Name","pixelLabels")];
lgraph = addLayers(lgraph,tempLayers);
% clean up helper variable
clear tempLayers;
lgraph = connectLayers(lgraph,"imageinput","conv1_1");
lgraph = connectLayers(lgraph,"imageinput","score/in");
lgraph = connectLayers(lgraph,"pool4","score_pool4");
lgraph = connectLayers(lgraph,"pool4","conv5_1");
lgraph = connectLayers(lgraph,"score_pool4","score_pool4c/ref");
lgraph = connectLayers(lgraph,"upscore2","score_pool4c/in");
lgraph = connectLayers(lgraph,"upscore2","fuse/in1");
lgraph = connectLayers(lgraph,"score_pool4c","fuse/in2");
lgraph = connectLayers(lgraph,"upscore16","score/ref");
plot(lgraph);
options = trainingOptions('sgdm', ...
'MaxEpochs',20,...
'InitialLearnRate',1e-4, ...
'Verbose',false, ...
'Plots','training-progress');
training_net = trainNetwork(imds_train,lgraph,options);

Accepted Answer

Prateek Rai
Prateek Rai on 17 Aug 2021
To my understanding, you are trying to develop a model to detect circular/psuedo-circular objects from grayscale images with low contrast. In the code provided, 'LabelSource' option of imageDatastore is set to 'foldernames' which is using the names of the folders containing the images as labels (i.e. output). By doing so, you are specifying the output of your model to be 1 × 1 × 1. But for the model given, the output size should be 458 × 458 × 1 which throws an error.
A possible workaround could be:
Step 1: Removing the 'LabelSource' option of imageDatastore.
Step 2: Using trainNetwork in this fashion
training_net = trainNetwork(images,responses,layers,options)
It will train the network using the images specified by "images" and responses specified by "responses".
Here, "responses" corresponds to the output of the given network and must have a size 458 × 458 × 1 .
You can refer to trainNetwork MathWorks documentation page to find more on different ways to train deep learning neural network.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!