Semantic Segmentation Output & Response size

12 views (last 30 days)
when training a pretrained model (resnet18) for image segmentation, I always get the following error:
Error using trainNetwork (line 170)
Invalid training data. The output size ([250 1000 2]) of the last layer does not match the response size
([250 1000 1]).
Error in SemanticSegmentation (line 45)
[net, info] = trainNetwork(pximds,lgraph,options);
Already checke the network with the deep network designer and it says no errors.
Here you can see my code:
imds=imageDatastore('\\tuwzs9a-vm-02\homes\ga54saw\windows\dokumente\QuaBu\Geteilte Bilder\test')
classNames = ["noKnot", "Knot"];
pixelLabelID = [0; 1];
pxds = pixelLabelDatastore('\\tuwzs9a-vm-02\homes\ga54saw\windows\dokumente\QuaBu',classNames,pixelLabelID);
pximds = pixelLabelImageDatastore(imds,pxds);
[imdsTrain, imdsVal, imdsTest, pxdsTrain, pxdsVal, pxdsTest] = partitionData(imds,pxds);
% Specify the network image size. This is typically the same as the traing image sizes.
imageSize = [250 1000 3];
% Specify the number of classes.
numClasses = 2;
% Create DeepLab v3+.
lgraph = deeplabv3plusLayers(imageSize, numClasses, "resnet18");
tbl = countEachLabel(pxds);
imageFreq = tbl.PixelCount ./ tbl.ImagePixelCount;
classWeights = median(imageFreq) ./ imageFreq;
pxLayer = pixelClassificationLayer('Name','labels','Classes',tbl.Name,'ClassWeights',classWeights);
lgraph = replaceLayer(lgraph,"classification",pxLayer);
% Define validation data.
pximdsVal = pixelLabelImageDatastore(imdsVal,pxdsVal);
% Define training options.
options = trainingOptions('sgdm', ...
'Momentum',0.9, ...
'InitialLearnRate',1e-3, ...
'L2Regularization',0.005, ...
'MaxEpochs',30, ...
'MiniBatchSize',2, ...
'Shuffle','every-epoch', ...
'CheckpointPath', tempdir, ...
'ValidationPatience', 4);
%Start Training
pximds = pixelLabelImageDatastore(imdsTrain,pxdsTrain);
net = trainNetwork(pximds,lgraph,options);
I = readimage(imdsTest,9);
C = semanticseg(I, net);
B = labeloverlay(I,C,'Colormap',cmap,'Transparency',0.4);
pixelLabelColorbar(cmap, classes);
Would be really happy to get some help to solve the problem.
Thank you very much.

Answers (1)

Divya Gaddipati
Divya Gaddipati on 10 Aug 2020
As it is evident from the error message, the size at the output layer should be same as the ground truth image size. Try changing the output layer size to [250 1000 1].
Here's a link to Semantic Segmentation example in MATLAB:

Community Treasure Hunt

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

Start Hunting!