Can change pretrained model's weight filter shape?

wonchulSon: <p>For example, I loaded pretrained Vgg16 model and want to keep same architecture (BN, maxpool, ReLU etc.) and only change several layer’s filter size (3x3-&gt;5x5).<br>
Is this possible? How can I do this?</p>
<p>Thank you.<br>
Son.</p>

JuanFMontesinos: <p>You can do that buy you will have to retrain the network or at least fine-tune it, as replaced convolutions won’t have pretrained weights.<br>
To do so, you have to check network’s code and replace those convolution whose kernel is (3,3) by (5,5)</p>

wonchulSon: <p><a class="mention" href="/u/juanfmontesinos">@JuanFMontesinos</a><br>
Yeah right, that’s what I want to do, to reduce effort.<br>
But don’t know how to do replace the convolution kernel</p>

JuanFMontesinos: <p>Hi, I’m having a look at the code and it seems network is dinamically generated.<br>
<aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/pytorch/vision/blob/f677ea31db8f45dbfec2fe5e519da82853815776/torchvision/models/vgg.py#L70" target="_blank" rel="nofollow noopener">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/pytorch/vision/blob/f677ea31db8f45dbfec2fe5e519da82853815776/torchvision/models/vgg.py#L70" target="_blank" rel="nofollow noopener">pytorch/vision/blob/f677ea31db8f45dbfec2fe5e519da82853815776/torchvision/models/vgg.py#L70</a></h4>
<pre class="onebox"><code class="lang-py"><ol class="start lines" start="60" style="counter-reset: li-counter 59 ;">
<li>                nn.init.constant_(m.bias, 0)</li>
<li>
</li>
<li>
</li>
<li>def make_layers(cfg, batch_norm=False):</li>
<li>    layers = []</li>
<li>    in_channels = 3</li>
<li>    for v in cfg:</li>
<li>        if v == 'M':</li>
<li>            layers += [nn.MaxPool2d(kernel_size=2, stride=2)]</li>
<li>        else:</li>
<li class="selected">            conv2d = nn.Conv2d(in_channels, v, kernel_size=3, padding=1)</li>
<li>            if batch_norm:</li>
<li>                layers += [conv2d, nn.BatchNorm2d(v), nn.ReLU(inplace=True)]</li>
<li>            else:</li>
<li>                layers += [conv2d, nn.ReLU(inplace=True)]</li>
<li>            in_channels = v</li>
<li>    return nn.Sequential(*layers)</li>
<li>
</li>
<li>
</li>
<li>cfgs = {</li>
<li>    'A': [64, 'M', 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'],</li>
</ol></code></pre>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>
</p>
<p>If you replace kernel_size there, you would replace all the convolutions.</p>
<p>You can look for another implementation in which you can tune all the convolutions statically, however it will be a mess to load pytorch’s pretrained weights.</p>
<p>Regards</p>
<p>EDIT, you may be able to extend the function to arbitrary choose convolution sizes for each layer as  network is built here:<br>
<aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/pytorch/vision/blob/f677ea31db8f45dbfec2fe5e519da82853815776/torchvision/models/vgg.py#L90" target="_blank" rel="nofollow noopener">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/pytorch/vision/blob/f677ea31db8f45dbfec2fe5e519da82853815776/torchvision/models/vgg.py#L90" target="_blank" rel="nofollow noopener">pytorch/vision/blob/f677ea31db8f45dbfec2fe5e519da82853815776/torchvision/models/vgg.py#L90</a></h4>
<pre class="onebox"><code class="lang-py"><ol class="start lines" start="80" style="counter-reset: li-counter 79 ;">
<li>    'A': [64, 'M', 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'],</li>
<li>    'B': [64, 64, 'M', 128, 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'],</li>
<li>    'D': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512, 'M', 512, 512, 512, 'M'],</li>
<li>    'E': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 256, 'M', 512, 512, 512, 512, 'M', 512, 512, 512, 512, 'M'],</li>
<li>}</li>
<li>
</li>
<li>
</li>
<li>def _vgg(arch, cfg, batch_norm, pretrained, progress, **kwargs):</li>
<li>    if pretrained:</li>
<li>        kwargs['init_weights'] = False</li>
<li class="selected">    model = VGG(make_layers(cfgs[cfg], batch_norm=batch_norm), **kwargs)</li>
<li>    if pretrained:</li>
<li>        state_dict = load_state_dict_from_url(model_urls[arch],</li>
<li>                                              progress=progress)</li>
<li>        model.load_state_dict(state_dict)</li>
<li>    return model</li>
<li>
</li>
<li>
</li>
<li>def vgg11(pretrained=False, progress=True, **kwargs):</li>
<li>    r"""VGG 11-layer model (configuration "A") from</li>
<li>    `"Very Deep Convolutional Networks For Large-Scale Image Recognition" &lt;https://arxiv.org/pdf/1409.1556.pdf&gt;`_</li>
</ol></code></pre>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>
</p>
<p>if you make make_layers function to accept another parameter you can set them as you want to.</p>