How to match output size in cnn

2 ビュー (過去 30 日間)
Hugo Brandão
Hugo Brandão 2017 年 3 月 25 日
コメント済み: Kanushka Gajjar 2019 年 6 月 24 日
I am trying to train a cnn to take as input a grayscale image (25x25) and output also an image (25x25). I have created the training as follows: (I1 is the input and I2 is the response)
[I1, I2] = generateImage();
X(:,:,:,i) = I1;
Y(i,:,:,:) = I2;
The I set the network and try to train it:
%create network layers
layers = [...
imageInputLayer([25 25 1])
convolution2dLayer([4 3],12)
reluLayer
crossChannelNormalizationLayer(4)
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(5,16)
reluLayer
crossChannelNormalizationLayer(4)
maxPooling2dLayer(2,'Stride',2)
fullyConnectedLayer(256)
reluLayer
fullyConnectedLayer(10)
regressionLayer];
%create training option
options = trainingOptions('sgdm','InitialLearnRate',0.001, ...
'MaxEpochs',15);
%create network
net = trainNetwork(X,Y,layers,options)
The message I get is:
Error using trainNetwork (line 92)
The output size [1 1 10] of the last layer doesn't match the response size [1 25 25].
Any ideias on to fix this?
  1 件のコメント
Kanushka Gajjar
Kanushka Gajjar 2019 年 6 月 24 日
Hi,
Did you find a solution to this problem?
I am having the same problem.
Thanks,
Kanushka

サインインしてコメントする。

回答 (1 件)

Abel Babu
Abel Babu 2017 年 3 月 28 日
This error is due to the ' fullyConnectedLayer'. If you see the following documentation it is clear that the output of this layer is a single dimension vector
The workaround is to unroll your input image matrix to make it single dimension vector and then training it. I have modified your code by taking random matrices as inputs and response.
layers = [...
imageInputLayer([25 25 1])
convolution2dLayer([4 3],12)
reluLayer
crossChannelNormalizationLayer(4)
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(5,16)
reluLayer
crossChannelNormalizationLayer(4)
maxPooling2dLayer(2,'Stride',2)
fullyConnectedLayer(256)
reluLayer
fullyConnectedLayer(25*25)%Change the dimensions to match the outputs.
regressionLayer;
];
%create training option
options = trainingOptions('sgdm','InitialLearnRate',0.001, ...
'MaxEpochs',15);
%create network
X(:,:,:,1) = rand(25);
Y=randn(1,1,25*25,1);
net = trainNetwork(X,Y,layers,options)
I hope the above code will help you.
Regards,
Darshan Bhat
  1 件のコメント
Osama Tabbakh
Osama Tabbakh 2019 年 4 月 11 日
編集済み: Osama Tabbakh 2019 年 5 月 14 日
I wounder why u put two fullyConnectedLayers. And can I somehow filter the output also?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeImage Data Workflows についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by