How to load the fc weights into its corresponding convolution layer weights?

I know how to convert the fc layer to convolution layer.
But its weights are initialized randomly.
I want to load the pretrained fc weights to the convolution layer transformed from this fc layer.
How should I do?

For the FC weights, you can use view. For example, let old_fc6_weight be the fc6 weight loaded from the state_dict of the pre-trained model:
new_fc6_param.data = old_fc6_weight.view(new_fc6_param.size())

1 Like

Thank you for your kind answer.
It works!
For convenient reference for others, I post my concrete code here.

self.fc6_to_conv = nn.Conv2d(512, 4096, kernel_size = 7, stride = 1, padding = 0)
self.fc6_to_conv.weight.data = vgg16.fc6.weight.data.view(self.fc6_to_conv.weight.size())
1 Like

However, it seems difficult to train.
An I find that it’s not necessary to train with converted conv layer.
I just need to convert fc layer to conv layer when testing. Am I right?

If your output is spatial (as in an FCN), then the fully connected layers must be made fully convolutional in order to preserve spatial dimensions (during training and testing).

Does loading the pretrained fc weights to weights to conv layers work? Seems unintuitive