How can I load weight and bias in Pretrained vgg19?

username: isaac1644
cooked: <p>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?</p>

username: ptrblck
cooked: <p>If you want to just use the pretrained model from <code>torchvision</code>, you could just initialize the model with:</p>
<pre><code class="lang-python">import torchvision.models as models

model = models.vgg16(pretrained=True)
</code></pre>
<p>Alternatively, if you would like to get the <code>weight</code> and <code>bias</code> directly from a particular layer, this should work:</p>
<pre><code class="lang-python">print(model.features[0].weight)
</code></pre>

username: isaac1644
cooked: <p>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.</p>

username: ptrblck
cooked: <p>You could load the pretrained parameters into your model with some code like this:</p>
<pre><code class="lang-python">with torch.no_grad():
    new_model.conv1.weight.copy_(vgg.features[0].weight)
    new_model.conv1.bias.copy_(vgg.features.bias)
    ...
</code></pre>
<p>Would that work for you?</p>

username: isaac1644
cooked: <p>should I change the “new_model.conv1.bias.copy_(vgg.features.bias)” to “new_model.conv1.bias.copy_(vgg.features[0].bias)”</p>

username: ptrblck
cooked: <p>Yes, that is a typo in my code sample and you should index the layer directly.</p>

username: isaac1644
cooked: <p>but it goes wrong:</p>
<pre><code class="lang-auto">RuntimeError: a leaf Variable that requires grad has been used in an in-place operation.
</code></pre>
<p>when I use the same way as you referred in pre-reflection by</p>
<pre><code class="lang-auto">nn.ReflectionPad2d((1, 1, 1, 1)),   # block 4
nn.Conv2d(256, 512, 3, 1, 0)
</code></pre>
<p>instead of using</p>
<pre><code class="lang-auto">nn.Conv2d(256,  512, 3, 1, 1)
</code></pre>

username: ptrblck
cooked: <p>Do you get this error message even if you warp the manipulation into the <code>torch.no_grad()</code> block?<br>
If so, could you post some code so that we can have a look?</p>

username: isaac1644
cooked: <p>Thank you! I’m not sure about that. What I did in <strong>init</strong>() of my net class shows as following:</p>
<ol>
<li>Loading vgg pre-trained model with:</li>
</ol>
<pre><code class="lang-auto">vgg_pretrained_features = models.vgg19(pretrained=True).features
</code></pre>
<ol start="2">
<li>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).</li>
<li>Loading the conv layers weights and biases to my net with:<br>
<code>nn.Sequential[1].weight.copy_(vgg_pretrained_features[0].weight) nn.Sequential[1].bias.copy_(vgg_pretrained_features[0].bias)</code>
</li>
<li>freezing the grad refer to <a href="https://github.com/pytorch/examples/blob/97304e232807082c2e7b54c597615dc0ad8f6173/fast_neural_style/neural_style/vgg.py#L23" rel="nofollow noopener">examples/fast_neural_style</a>
</li>
</ol>
<p>So far, I don’t the no_grad() to forbid computing the grad.</p>

username: ptrblck
cooked: <p>Could you wrap all parameter manipulations, e.g. <code>.weight.copy_()</code>, in a <code>with torch.np_grad()</code> block and run your code again?</p>

username: Shandilya21
cooked: <p>you can directly<br>
import torchvision.models as models</p>
<p>model = models.vgg16(pretrained = True)<br>
you can directly<br>
use print(model.features[0].weight)<br>
weight_data.requires_grad = False</p>

username: isaac1644
cooked: <p>Hi, thank you so much. It can load parameters to my network.</p>