MATLAB Answers

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

31 ビュー (過去 30 日間)
Patrick O'Brien
Patrick O'Brien 2020 年 1 月 1 日
編集済み: Patrick O'Brien 2020 年 1 月 21 日
(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.

  0 件のコメント

サインイン to comment.

回答 (1 件)

Divya Gaddipati
Divya Gaddipati 2020 年 1 月 6 日
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 件のコメント

Patrick O'Brien
Patrick O'Brien 2020 年 1 月 12 日
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

サインイン to comment.

サインイン してこの質問に回答します。


Translated by