Semantic Segmentation Issue with output size

3 vues (au cours des 30 derniers jours)
Gabriel VH
Gabriel VH le 29 Oct 2017
Commenté : Santo le 6 Sep 2024
Hello everyone,
I am trying to do a segmentation of a Brain Tumor MRI dataset, available in BRATS. But, after I ran my code, I got an error.
"Error using trainNetwork (line 140) Invalid training data. The output size (4) of the last layer doesn't match the number of classes (4).
Error in import_data_alternate2 (line 102) cnn = trainNetwork(trainingData,net,options);
Caused by: Error using nnet.internal.cnn.util.TrainNetworkDataValidator/assertCorrectResponseSizeForOutputLayer (line 217) Invalid training data. The output size (4) of the last layer doesn't match the number of classes (4)."
clear;
clc;
%Image dataset
pxl = dir('C:\Users\Osvaldo\Downloads\BRATS Data\Imagens\Patient 07\GT\*.png')';
img = fullfile('C:\Users\Osvaldo\Downloads\BRATS Data\Imagens\Patient 07\T1c\');
%Vector preallocation
ground_truth = cell(1,numel(pxl));
gt = cell(1,numel(pxl));
training_data = imageDatastore(img);
%Ground truth images
for k = 1:numel(pxl)
image = imageDatastore(pxl(k).name);
ground_truth{k} = image;
end
for k = 1:numel(pxl)
loc = ground_truth{1,k}.Files;
gt(k) = loc;
end
gt = gt';
classes = ["Edema" "Non-enhancing tumor" "Necrosis" "Enhancing tumor"];
labelIDs = [ ...
127 127 127; ... % "Edema"
190 190 190; ... % "Non-enhancing tumor"
63 63 63; ... % "Necrosis"
255 255 255; % "Enhancing tumor"
];
groundtruth = pixelLabelDatastore(gt,classes,labelIDs);
%CNN creation
inputSize = [429 492 3];
imgLayer = imageInputLayer(inputSize);
filterSize = 3;
numFilters = 32;
conv = convolution2dLayer(filterSize,numFilters,'Padding',1);
relu = reluLayer();
poolSize = 2;
maxPoolDownsample2x = maxPooling2dLayer(poolSize,'Stride',2);
downsamplingLayers = [
conv
relu
maxPoolDownsample2x
conv
relu
maxPoolDownsample2x
conv
relu
maxPoolDownsample2x
];
filterSize = 4;
transposedConvUpsample2x = transposedConv2dLayer(4,numFilters,'Stride',2,'Cropping',1);
upsamplingLayers = [
transposedConvUpsample2x
relu
transposedConvUpsample2x
relu
];
numClasses = 4;
conv1x1 = convolution2dLayer(1,numClasses);
finalLayers = [
conv1x1
softmaxLayer()
pixelClassificationLayer()
];
net = [
imgLayer
downsamplingLayers
upsamplingLayers
finalLayers
];
%CNN Training
trainingData = pixelLabelImageSource(training_data,groundtruth);
options = trainingOptions('sgdm', ...
'InitialLearnRate', 1e-3, ...
'MaxEpochs', 100, ...
'MiniBatchSize', 64);
cnn = trainNetwork(trainingData,net,options);
Can someone help me?
  1 commentaire
Santo
Santo le 6 Sep 2024
Classification of brain tumor of brain mri images using cnn in matlab code . Please can you give?

Connectez-vous pour commenter.

Réponse acceptée

Arthur Fernandes
Arthur Fernandes le 7 Nov 2017
Hi Gabriel,
Apparently there is an difference between the output size and your classes. It is difficult to debug that since matlab doesn't explicit say the size of the output matrix... For your model to work you need to have the output at the same size as the ground truth. However, since you want to do segmentation, a better way to approach this in matlab is to use the function segnetLayers since it will ensure that the output is in accordance to the ground-truth and will automatically define the number of nodes in each layer. But you still have the flexibility to define the architecture of your network.
  2 commentaires
Gabriel VH
Gabriel VH le 28 Nov 2017
You, mister, are a genius! Thank you very much!
Farrukh nazir
Farrukh nazir le 15 Août 2020
Modifié(e) : Farrukh nazir le 15 Août 2020
Yes, i was also receiving error between output size and network size using u-net. However using segnet, netwotk just started training.
Thanks.

Connectez-vous pour commenter.

Plus de réponses (1)

abdulkader helwan
abdulkader helwan le 25 Déc 2017
numClasses = numel(categories(trainDigitData.Labels)); Then use this variable in the fully connected layer:
fullyConnectedLayer(numClasses).

Catégories

En savoir plus sur Image Data Workflows dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by