Error using trainNetwork Invalid training data. The output size ([256 256 2]) of the last layer does not match the response size ([501 401 2]).

1 view (last 30 days)
direct = dir('*.jpg');
n = length(direct);
direct = natsortfiles(direct);
for i = 1:n
AAA = imread(direct(i).name);
BBB = imresize(AAA,[256 256]);
BBB = repmat(BBB,1,1,3);
imwrite(BBB,sprintf('im%d.jpg',i))
end
dataDir = fullfile('C:\Users\39371\OneDrive\Desktop\fedetesi');
imDir = fullfile(dataDir,'scala di grigi vetro');
imds = imageDatastore(imDir);
pxDir = fullfile(dataDir,'warp_vetro_dnn');
classNames = ["warp","background"];
labelIDs = [1 0];
pxds = pixelLabelDatastore(pxDir,classNames,labelIDs);
imageSize = [256 256];
numClasses = numel(classNames);
lgraph = deeplabv3plusLayers(imageSize,numClasses,'resnet18');
cds = combine(imds,pxds);
opts = trainingOptions('sgdm',...
'MiniBatchSize',8,...
'MaxEpochs',10);
net = trainNetwork(cds,lgraph,opts);

Answers (1)

Aman
Aman on 26 Jul 2023
Hi,
I understand that you are trying to do image segmentation, for that you are using the DeepLab v3+ CNN model and getting the size mismatch error when putting the model into the training.
This error occurs when the size of the output layer differs from the size of the response, which is nothing but the size of the actual ground truth.
To solve the issue by fixing response size, kindly refer this:
Without the actual data, it is difficult to point out what is causing this issue, but generally it can be solved by either fixing the output layer size or by reshaping the response size.
To solve the issue by fixing output size, kindly refer this:
Please refer to the following documentation to learn more about Image Segmentation using DeepLab v3+ CNN model
I hope it helps!

Products

Community Treasure Hunt

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

Start Hunting!