MATLAB Answers

Problem with building a specific CNN

6 views (last 30 days)
Elliott Fullerton
Elliott Fullerton on 28 Nov 2017
Commented: Javier Pinzón on 8 Feb 2019
Hi There,
I am really struggling to build the layers of the CNN displayed in the image attached. This is what I currently have but it is not working unfortunately.
layers = [
imageInputLayer([512 512 1])
convolution2dLayer(filterSize,numFilters,'Padding',1)
reluLayer()
transposedConv2dLayer(24,24,'Stride',2);
reluLayer()
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(12,48,'Padding',1)
reluLayer()
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(8,96,'Padding',1)
reluLayer()
maxPooling2dLayer(2,'Stride',48)
fullyConnectedLayer(2)
softmaxLayer()
pixelClassificationLayer()
]
Any help would be extremely appreciated.
  2 Comments
Elliott Fullerton
Elliott Fullerton on 4 Dec 2017
Hello Javier,
sorry for being not as specific. I am looking to segment cartillage from knee mri data. The network above has been proved to be very accurate but the for me I am getting the following error when I try and implement this in MATLAB
Invalid training data. The output size (2) of the last layer doesn't match the number of classes
(2).
I'm not sure whether this is an error or I am missing something simple.
Kind Regards
Elliott

Sign in to comment.

Answers (2)

Javier Pinzón
Javier Pinzón on 31 Jan 2018
Hello Elliott,
Sorry for the really late response.
As far as I can see, the error is located in the amount of classes that you are training, i.e. the number of categories or subfolders that you have in your training data. For example, in your network yo say:
fullyConnectedLayer(2)
But you have a training forlder with 3 subfolders, for example:
Training-
- Apple
- Pear
- Pineapple
so you have 3 categories but only 2 outputs in your last fully connected layer, so you need to put:
fullyConnectedLayer(3)
or only use 2 categories, ereasing or moving the no needed category.
Hope the help was not really late or helps other people.
Regards
Javier
  2 Comments
Javier Pinzón
Javier Pinzón on 8 Feb 2019
Hello Peter, Sorry for the late response.
on SegNet, first of all, you should have all your images in the same folder, because the outputs are the number of categories segmented. So, if you only use a category called Lessions, so your number of outputs are 1. But if you SEGMENTED the background as well (what is highly recommended for me), so your amount of categories are 2. so, the number of filters at the last convolution on the decoder must match this valor.
Hope it helps.
Regards,
Javier

Sign in to comment.


Rene
Rene on 19 Apr 2018
Are you using any kind of data preprocessing or augmentation before sending inputs to the network? I've been finding I get this error a lot when I either fiddle with data preprocessing using something like an augmentedImageSource or specifying 'randcrop' in the image input layer. It's possible that augmentation only works for classification, where you can augment the training image all you like and the label vector doesn't change. This would be problematic for segmentation networks where you'd also need to apply the same random transformation to the label as well. Perhaps this is leading to some kind of mismatch somewhere. I've also found that modifying the parameters of the convolutional layers causes this error. Some combos work and others don't. It's really unclear exactly where the problem lies with this error message. (NB all the times I've had this error message myself have been without the use of a fully connected layer in the network)
  1 Comment
Peter Eze
Peter Eze on 19 Dec 2018
Yes, this is true. I am currently having this problem with SegNet on VGG-16. There is no fully connected layer and I want to segment into lession and background. Any solution to this yet?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!