I face this error "Caused by: Layer 17: Input size mismatch. Size of input to this layer is different from the expected input size. Inputs to this layer: from layer 16 (1×1×2 output) "

3 次查看(过去 30 天)
layers = [
imageInputLayer([8 1 1])
convolution2dLayer([4 1],8,'Padding','same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer([2 1],'Stride',2)
convolution2dLayer([4 1],16,'Padding','same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer([2 1],'Stride',2)
convolution2dLayer([4 1],4,'Padding','same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer([2 1],'Stride',2)
convolution2dLayer([4 1],2,'Padding','same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer([2 1],'Stride',2)
convolution2dLayer([4 1],1,'Padding','same')
batchNormalizationLayer
reluLayer
fullyConnectedLayer(2)
softmaxLayer
classificationLayer];
options = trainingOptions('sgdm',...
'ExecutionEnvironment', 'cpu',...
'InitialLearnRate',0.005,...
'MaxEpochs',30, ...
'Verbose',false,...
'ValidationData',{XValidation,YValidation}, ...
'ValidationFrequency',10, ...
'MiniBatchSize',30, ...
'Plots','training-progress');
net = trainNetwork(Xtrain,Ytrain,layers,options); %

回答(1 个)

Garmit Pant
Garmit Pant 2024-7-12
Hello Saddam
The error you have encountered is due to a size mismatch between the expected input size of the fourth max pooling layer and the actual size of the input of the layer.
On analysing the network using “analyzeNetwork” function, we can see that the output size after layer 14, ‘conv_4’, is 1x1x2x1. The size of the pool operation on layer 17, ‘maxpool_4’, is 2x1. Since the size of the pooling dimensions should be larger or equal to the pool size, the error you are encountering is occurring. You can use the following code snippet to check your network:
analyzeNetwork(layers)
Output:
This can be rectified by removing the fourth max pooling layer.
For further understanding, kindly refer to the following MathWorks Documentation:
I hope you find the above explanation and suggestions useful!

类别

Help CenterFile Exchange 中查找有关 Image Data Workflows 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by