error while transferring weights of a trained CNN network to an empty CNN network

2 views (last 30 days)
Hi,
I am trying to transfer the weights of layer 11 from 'original_net' to layer 11 of 'layers_final'. Both have same structure and 'layer_final' is just the empty, untrained version of 'original net'. i am using the following command:
Layers_final(11).Weights = net_1.Layers(11).Weights
I get the following error while doing so:
Error using nnet.cnn.layer.TransposedConvolution2DLayer/set.Weights (line 204)
Expected input to be of size 4x4x8x1, but it is of size 4x4x8x8.
code for layers_final:
imageLayer_final = imageInputLayer([32,32,1]);
encodingLayers_final = [ ...
convolution2dLayer(3,16,'Padding','same'), ...
reluLayer, ...
maxPooling2dLayer(2,'Padding','same','Stride',2), ...
convolution2dLayer(3,8,'Padding','same'), ...
reluLayer, ...
maxPooling2dLayer(2,'Padding','same','Stride',2), ...
convolution2dLayer(3,8,'Padding','same'), ...
reluLayer, ...
maxPooling2dLayer(2,'Padding','same','Stride',2)];
decodingLayers_final = [ ...
createUpsampleTransponseConvLayer(2,8), ...
reluLayer, ...
createUpsampleTransponseConvLayer(2,8), ...
reluLayer, ...
createUpsampleTransponseConvLayer(2,16), ...
reluLayer, ...
convolution2dLayer(3,1,'Padding','same'), ...
clippedReluLayer(1.0), ...
regressionLayer];
layers_final = [imageLayer,encodingLayers,decodingLayers];
net_original attached with the question.
Thanks

Accepted Answer

Srivardhan Gadila
Srivardhan Gadila on 24 Feb 2020
If the function createUpsampleTransponseConvLayer is the helper function from the example Prepare Datastore for Image-to-Image Regression then change the 'NumChannels' Name-Value Pair Argument to 'auto' or don't mention it in the transposedConv2dLayer function.
% helper function from the example Prepare Datastore for Image-to-Image Regression
function out = createUpsampleTransponseConvLayer(factor,numFilters)
filterSize = 2*factor - mod(factor,2);
cropping = (factor-mod(factor,2))/2;
numChannels = 1;
out = transposedConv2dLayer(filterSize,numFilters, ...
'NumChannels',numChannels,'Stride',factor,'Cropping',cropping);
end
Since the layer is defined with 'NumChannels' (number of channels of the input to this transposedConv2dLayer) as 1 hence it can accept wieghts of size "filterSize x filterSize x numFilters x numChannels" which is 4x4x8x1 in this case.
Change the function as follows:
function out = createUpsampleTransponseConvLayer(factor,numFilters)
filterSize = 2*factor - mod(factor,2);
cropping = (factor-mod(factor,2))/2;
out = transposedConv2dLayer(filterSize,numFilters, ...
'Stride',factor,'Cropping',cropping);
end
and then define the layers.
  3 Comments
Radians
Radians on 15 Jan 2021
Hello
I know its been long now but if I try to perform the same operation with the transpose convolution function as follows:
O_dltconv1=dltranspconv(O_maxpool3,K_tconv_1,B_tconv_1,'Stride',2,'Cropping',1);
with:
K>> size(O_maxpool3)
ans =
4 4 8 500
K>> size(K_tconv_1)
ans =
4 4 8
K>> size(B_tconv_1)
ans =
1 1 8
I get the following error:
Number of channels to convolve (1, specified by the size of the 'U' dimension of the weights) must be equal
to the size of the 'C' dimension of the input data (8).
Could you please highlight what am I doing wrong, since I set the channels of the transpose convolution filter to 1 as you said?

Sign in to comment.

More Answers (0)

Tags

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by