How can I load weight and bias in Pretrained vgg19?

I use vgg19 as encoder and I want to load the weight and bias of conv layers of pytorch pretrained model. how should I do for achieving it?

If you want to just use the pretrained model from torchvision, you could just initialize the model with:

import torchvision.models as models

model = models.vgg16(pretrained=True)

Alternatively, if you would like to get the weight and bias directly from a particular layer, this should work:

print(model.features[0].weight)

Well, I want to use its 1~3blocks and first of 4block conv layers’ parameters since I changed the MaxPool2d with “return _indices=True “. I have defined my network, and I really don’t know how to utilise the pretrained vgg19 in my network.

You could load the pretrained parameters into your model with some code like this:

with torch.no_grad():
    new_model.conv1.weight.copy_(vgg.features[0].weight)
    new_model.conv1.bias.copy_(vgg.features.bias)
    ...

Would that work for you?

should I change the “new_model.conv1.bias.copy_(vgg.features.bias)” to “new_model.conv1.bias.copy_(vgg.features[0].bias)”

Yes, that is a typo in my code sample and you should index the layer directly.

but it goes wrong:

RuntimeError: a leaf Variable that requires grad has been used in an in-place operation.

when I use the same way as you referred in pre-reflection by

nn.ReflectionPad2d((1, 1, 1, 1)),   # block 4
nn.Conv2d(256, 512, 3, 1, 0)

instead of using

nn.Conv2d(256,  512, 3, 1, 1)

Do you get this error message even if you warp the manipulation into the torch.no_grad() block?
If so, could you post some code so that we can have a look?

Thank you! I’m not sure about that. What I did in init() of my net class shows as following:

  1. Loading vgg pre-trained model with:
vgg_pretrained_features = models.vgg19(pretrained=True).features
  1. Defining my net in nn.Sequential() where different from origin vgg in using "nn.ReflectionPad((1,1,1,1)) "and “nn.Conv2d(in_channels, out_channels, 3, 1, 0)” instead of “nn.Conv2d(in_channels, out_channels, 3, 1, 1)”. Meanwhile, changing the nn.MaxPool2d(2,2) to nn.MaxPool2d(2,2,return_indices=True).
  2. Loading the conv layers weights and biases to my net with:
    nn.Sequential[1].weight.copy_(vgg_pretrained_features[0].weight) nn.Sequential[1].bias.copy_(vgg_pretrained_features[0].bias)
  3. freezing the grad refer to examples/fast_neural_style

So far, I don’t the no_grad() to forbid computing the grad.

Could you wrap all parameter manipulations, e.g. .weight.copy_(), in a with torch.np_grad() block and run your code again?

you can directly
import torchvision.models as models

model = models.vgg16(pretrained = True)
you can directly
use print(model.features[0].weight)
weight_data.requires_grad = False

Hi, thank you so much. It can load parameters to my network.