Error using TrainNetwork. Output Size of the Last Layer does not match the Response Size
1 view (last 30 days)
Show older comments
(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
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.
Answers (1)
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!
See Also
Categories
Find more on Image Data Workflows in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!