Invalid training data: The output size of the last layer does not match the response size

52 views (last 30 days)
layers = [
imageInputLayer([256 256 1],"Name","imageinput")
convolution2dLayer([7 7],3,'Stride',1,"Name","conv1","Padding","same")
reluLayer("Name","relu1")
%batchNormalizationLayer("Name","batchnorm")
maxPooling2dLayer(2,'Name','maxPooling1', 'stride', 1)
convolution2dLayer([5 5],3,'Stride',1,"Name","conv2","Padding","same")
reluLayer("Name","relu2")
maxPooling2dLayer(2,'Name','maxPooling2', 'stride', 1)
convolution2dLayer([3 3],2,'Stride',1,"Name","conv3","Padding","same")
reluLayer("Name","relu3")
maxPooling2dLayer(2,'Name','maxPooling3', 'stride', 1)
softmaxLayer("Name","softmax")
pixelClassificationLayer("Name","pixel-class", )
];
Error using trainNetwork (line 165)
Invalid training data. The output size ([253 253 2]) of the last layer does not match the response size ([256 256 2]).

Accepted Answer

Jyothis Gireesh
Jyothis Gireesh on 19 Sep 2019
This issue arises due to size mismatch between the ground truth and the network output and may be resolved by setting the “Padding’’ attribute of the “maxPooling2dLayer” appropriately.
In the above code, the stride value is set to 1 which is less than the pool layer size (2x2). This causes overlapping regions of the input to be processed by the pool layer. The output image size in this case is given by the following formula
(Input SizePool Size + 2*Padding)/Stride + 1.
For instance, consider the first pooling layer. Here
  • Input Size: 256*256*3
  • Pool Size: 2*2
  • Padding: 0
  • Stride: 1
Here the output size will be 255*255*3.
Each subsequent pooling layer will reduce the height and width of the input by 1. After 3 maxpooling layers output will be of size (253*253*3).
By setting the ‘Padding’ attribute to [1 1], the size of the image remains same. Please make use of the following syntax to change the ‘Padding
maxPooling2dLayer(2,'Name','maxPooling1', 'stride', 1, ’Padding’ ,[1 1])
Please refer to the following documentation link on maxpooling layer

More Answers (0)

Community Treasure Hunt

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

Start Hunting!