Error using TrainNetwork. Output Size of the Last Layer does not match the Response Size

4 views (last 30 days)
(Exact Error at bottom)
The purpose of this project is to classify 3D MRI scans for the predication of Alzheimer's Disease.
The image data is multiple greyscale MRI scans of the head with varying image sizes. The sizes range from 256x256x170 to 192x192x160. I resize them to the lowest image size (192x192x160). The files extension is .nii which is read with the niftiread() function.
outputSize = ([192 192 160]);
start_path = 'C:\Users\Desktop\Gais_Lab\ADNI_Study_Environment';
imds = imageDatastore(start_path, 'FileExtensions', '.nii', 'IncludeSubfolders', 1, 'ReadFcn',@(x) niftiread(x));
Labels = f_labels_ds(imds); % assign labels to the datastore
imds.Labels = Labels;
aug_imds = transform(imds, @(x) imresize3(x, outputSize));
Input to the Neural Network is a transformed image datastore as shown above.
The Network Layers are shown here. The data is classified into 3 categories, 'AD', 'MCI,' and 'CN'.
function layers = f_layers()
layers = [
image3dInputLayer([192 192 160 1],"Name","image3dinput")
convolution3dLayer([5 5 5],6,"Name","conv3d_1","Padding","same","Stride",[5 5 5])
batchNormalizationLayer("Name","batchnorm_1")
reluLayer("Name","relu_1")
maxPooling3dLayer([3 3 3],"Name","maxpool3d_1")
convolution3dLayer([3 3 3],12,"Name","conv3d_2","Padding","same","Stride",[3 3 3])
batchNormalizationLayer("Name","batchnorm_2")
reluLayer("Name","relu_2")
maxPooling3dLayer([2 2 2],"Name","maxpool3d_2")
fullyConnectedLayer(3,"Name","fc")
softmaxLayer("Name","softmax")
classificationLayer("Name","classoutput")];
I combine everything in this line. (I attached the options just in case it matters)
net = trainNetwork(aug_imds, layers, options);
So, why am I getting the error...
"Error using trainNetwork (line 165)
Invalid training data. The output size ([1 1 1 3]) of the last layer does not match the response size ([0 0 1 1])."
... How do I resolve it?
Thank you for reading, let me know if anything more is needed.
  2 Comments
drummer
drummer on 26 Oct 2020
Did you solve it?
It appears that the last element of output size is the number of classes.
I'm having the same problem, but I'm combining the original imds and the transformed one.
If I only use the transformed, I have no error.

Sign in to comment.

Answers (1)

Divya Gaddipati
Divya Gaddipati on 6 Jan 2020
The size of the output of your network should match with the size of the label.
The size of your labels is [0 0 1 1] and the output of your network is of size [1 1 1 3], which don't match.
You have to change the size of either one to match with the other.
Hope this helps!
  1 Comment
Patrick O'Brien
Patrick O'Brien on 12 Jan 2020
Edited: Patrick O'Brien on 21 Jan 2020
So my labels are saved in the image datastore as a 244x1 categorical. How does this translate to [0 0 1 1]? How can I change it to [1 1 1 3]?
I prefer to change the labels because I believe it makes sense that the output of the network is [1 1 1 3] as there are 3 categorical classes.
Should it be 1 for the correct class and then 0 for all others? I believe it should accept categoricals right?
Thank you for responding! It is extremely encouraging to recieve help

Sign in to comment.

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!