Initialization of VGG layer weights

Trishna_Barman: <p>In config file, VGG layer weights are initialized using this way:</p>
<blockquote>
<p>from easydict import EasyDict as edict<br>
MODEL = edict()<br>
MODEL.VGG_LAYER_WEIGHTS = dict(conv3_4=1/8, conv4_4=1/4, conv5_4=1/2)</p>
</blockquote>
<p>But how to initialize it using a parser? I have tried to do this the following way:</p>
<blockquote>
<p>parser.add_argument(‘–VGG_LAYER_WEIGHTS’,type=dict, default=conv3_4=1/8, conv4_4=1/4, conv5_4=1/2, help=‘VGG_LAYER_WEIGHTS’)</p>
</blockquote>
<p>But got error. Please help me to write it correctly.</p>

ptrblck: <p>Your “config” file looks strange. Could you explain more about the use case, which config (package) you are using and how it’s initializing a PyTorch model, please?</p>

Trishna_Barman: <p>Firstly, my actually problem is that I could not able to download the VGG-19 pretrained weights due to the system proxy.<br>
VGG19 pretrained weight downloading failed from <strong>“<a href="https://download.pytorch.org/models/vgg19-dcbb9e9d.pth" rel="noopener nofollow ugc">https://download.pytorch.org/models/vgg19-dcbb9e9d.pth</a>” to C:\Users\TB/.cache\torch\hub\checkpoints\vgg19-dcbb9e9d.pth</strong> with this error <strong>"urlopen error Tunnel connection failed:407 Proxy Authentication Required"</strong><br>
Secondly, VGG-19 layer weights need to be intilalized using config file. In the the following link, layer weights are initialized using config file:<br>
<a href="https://github.com/dvlab-research/Simple-SR/blob/master/exps/BebyGAN/config.py" class="inline-onebox" rel="noopener nofollow ugc">Simple-SR/config.py at master · dvlab-research/Simple-SR · GitHub</a><br>
But how to write this using parse arugument.</p>

ptrblck: <aside class="quote no-group" data-username="Trishna_Barman" data-post="3" data-topic="152581">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/t/bcef8e/48.png" class="avatar"> Trishna_Barman:</div>
<blockquote>
<p>Firstly, my actually problem is that I could not able to download the VGG-19 pretrained weights due to the system proxy.</p>
</blockquote>
</aside>
<p>Maybe you could try to download this file manually if you have trouble allowing Python to do it.</p>
<aside class="quote no-group" data-username="Trishna_Barman" data-post="3" data-topic="152581">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/t/bcef8e/48.png" class="avatar"> Trishna_Barman:</div>
<blockquote>
<p>Secondly, VGG-19 layer weights need to be intilalized using config file.</p>
</blockquote>
</aside>
<p>This is not the case as the <code>config.py</code> script you’ve linked to is a custom config from a public repository and not from PyTorch or <code>torchvision</code>.</p>
<p>The desired <code>.pth</code> file contains the <code>state_dict</code> of <code>vgg19</code> and can be manually loaded via:</p>
<pre data-code-wrap="python"><code class="lang-python">s = torch.load("./vgg19-dcbb9e9d.pth")
print(s.keys())
# odict_keys(['features.0.weight', 'features.0.bias', 'features.2.weight', 'features.2.bias', 'features.5.weight', 'features.5.bias', 'features.7.weight', 'features.7.bias', 'features.10.weight', 'features.10.bias', 'features.12.weight', 'features.12.bias', 'features.14.weight', 'features.14.bias', 'features.16.weight', 'features.16.bias', 'features.19.weight', 'features.19.bias', 'features.21.weight', 'features.21.bias', 'features.23.weight', 'features.23.bias', 'features.25.weight', 'features.25.bias', 'features.28.weight', 'features.28.bias', 'features.30.weight', 'features.30.bias', 'features.32.weight', 'features.32.bias', 'features.34.weight', 'features.34.bias', 'classifier.0.weight', 'classifier.0.bias', 'classifier.3.weight', 'classifier.3.bias', 'classifier.6.weight', 'classifier.6.bias'])

model = models.vgg19()
model.load_state_dict(s)
# &lt;All keys matched successfully&gt;
</code></pre>
<p>once you’ve downloaded it to a local folder.</p>

Trishna_Barman: <p>I have downloaded the VGG-19 weights and placed it to **C:\Users\TB/.cache\torch\hub\checkpoints**. Here is the complete error:</p>
<p>`---------------------------------------------------------------------------<br>
OSError                                   Traceback (most recent call last)<br>
~\anaconda3\envs\AN\lib\urllib\request.py in do_open(self, http_class, req, **http_conn_args)<br>
1353             try:<br>
 → 1354                 h.request(req.get_method(), req.selector, req.data, headers,<br>
1355                           encode_chunked=req.has_header(‘Transfer-encoding’))</p>
<p>~\anaconda3\envs\AN\lib\http\client.py in request(self, method, url, body, headers, encode_chunked)<br>
1251         “”“Send a complete request to the server.”""<br>
 → 1252         self._send_request(method, url, body, headers, encode_chunked)<br>
1253</p>
<p>~\anaconda3\envs\AN\lib\http\client.py in _send_request(self, method, url, body, headers, encode_chunked)<br>
1297             body = _encode(body, ‘body’)<br>
 → 1298         self.endheaders(body, encode_chunked=encode_chunked)<br>
1299</p>
<p>~\anaconda3\envs\AN\lib\http\client.py in endheaders(self, message_body, encode_chunked)<br>
1246             raise CannotSendHeader()<br>
 → 1247         self._send_output(message_body, encode_chunked=encode_chunked)<br>
1248</p>
<p>~\anaconda3\envs\AN\lib\http\client.py in _send_output(self, message_body, encode_chunked)<br>
1006         del self._buffer[:]<br>
 → 1007         self.send(msg)<br>
1008</p>
<p>~\anaconda3\envs\AN\lib\http\client.py in send(self, data)<br>
946             if self.auto_open:<br>
 → 947                 self.connect()<br>
948             else:</p>
<p>~\anaconda3\envs\AN\lib\http\client.py in connect(self)<br>
1413<br>
 → 1414             super().connect()<br>
1415</p>
<p>~\anaconda3\envs\AN\lib\http\client.py in connect(self)<br>
922         if self._tunnel_host:<br>
 → 923             self._tunnel()<br>
924</p>
<p>~\anaconda3\envs\AN\lib\http\client.py in _tunnel(self)<br>
900             self.close()<br>
 → 901             raise OSError(“Tunnel connection failed: %d %s” % (code,<br>
902                                                                message.strip()))</p>
<p><strong>OSError: Tunnel connection failed: 407 Proxy Authentication Required</strong></p>
<p><strong>During handling of the above exception, another exception occurred:</strong></p>
<p>URLError                                  Traceback (most recent call last)<br>
~\AppData\Local\Temp/ipykernel_8004/3520862733.py in <br>
1 config,extra = get_config()<br>
----&gt; 2 main(config)</p>
<p>~\AppData\Local\Temp/ipykernel_8004/929139210.py in main(config)<br>
12     data_loader = get_loader(config.image_size, config.scale_factor, config.batch_size, config.sample_batch_size)<br>
13     trainer = Trainer(config, data_loader)<br>
—&gt; 14     trainer.train()</p>
<p>~\AppData\Local\Temp/ipykernel_8004/3677316706.py in train(self)<br>
71         self.bp_criterion = nn.L1Loss(reduction=‘mean’)<br>
72         if self.use_pcp:<br>
—&gt; 73             <strong>self.pcp_criterion = PerceptualLoss(layer_weights=self.VGG_LAYER_WEIGHTS,</strong><br>
**     74                                                 vgg_type=config.VGG_TYPE,**<br>
**     75                                                 use_input_norm=config.USE_INPUT_NORM,**</p>
<p>~\AppData\Local\Temp/ipykernel_8004/3161286985.py in <strong>init</strong>(self, layer_weights, vgg_type, use_input_norm, use_pcp_loss, use_style_loss, norm_img, criterion)<br>
196         self.use_style_loss = use_style_loss<br>
197         self.layer_weights = layer_weights<br>
 → 198         <strong>self.vgg = VGGFeatureExtractor(</strong><br>
199             layer_name_list=list(layer_weights.keys()),<br>
200             vgg_type=vgg_type,</p>
<p>~\AppData\Local\Temp/ipykernel_8004/825357570.py in <strong>init</strong>(self, layer_name_list, vgg_type, use_input_norm, requires_grad, remove_pooling, pooling_stride)<br>
96                 max_idx = idx<br>
97<br>
—&gt; 98         <strong>features = getattr(vgg, vgg_type)(pretrained=True).features[:max_idx + 1]</strong><br>
99<br>
100         modified_net = OrderedDict()</p>
<p>~\anaconda3\envs\AN\lib\site-packages\torchvision\models\vgg.py in vgg19(pretrained, progress, **kwargs)<br>
177         progress (bool): If True, displays a progress bar of the download to stderr<br>
178     “”"<br>
 → 179     **return _vgg(‘vgg19’, ‘E’, False, pretrained, progress, <strong>kwargs)</strong><br>
180<br>
181</p>
<p>~\anaconda3\envs\AN\lib\site-packages\torchvision\models\vgg.py in _vgg(arch, cfg, batch_norm, pretrained, progress, **kwargs)<br>
97     model = VGG(make_layers(cfgs[cfg], batch_norm=batch_norm), <strong>kwargs)<br>
98     if pretrained:<br>
—&gt; 99         <strong>state_dict = load_state_dict_from_url(model_urls[arch],</strong><br>
**    100                                               progress=progress)</strong><br>
101         model.load_state_dict(state_dict)</p>
<p>~\anaconda3\envs\AN\lib\site-packages\torch\hub.py in load_state_dict_from_url(url, model_dir, map_location, progress, check_hash, file_name)<br>
522             r = HASH_REGEX.search(filename)  # r is Optional[Match[str]]<br>
523             hash_prefix = r.group(1) if r else None<br>
 → 524         download_url_to_file(url, cached_file, hash_prefix, progress=progress)<br>
525<br>
526     if _is_legacy_zip_format(cached_file):</p>
<p>~\anaconda3\envs\AN\lib\site-packages\torch\hub.py in download_url_to_file(url, dst, hash_prefix, progress)<br>
392     # certificates in older Python<br>
393     req = Request(url, headers={“User-Agent”: “torch.hub”})<br>
 → 394     u = urlopen(req)<br>
395     meta = u.info()<br>
396     if hasattr(meta, ‘getheaders’):</p>
<p>~\anaconda3\envs\AN\lib\urllib\request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context)<br>
220     else:<br>
221         opener = _opener<br>
 → 222     return opener.open(url, data, timeout)<br>
223<br>
224 def install_opener(opener):</p>
<p>~\anaconda3\envs\AN\lib\urllib\request.py in open(self, fullurl, data, timeout)<br>
523<br>
524         sys.audit(‘urllib.Request’, req.full_url, req.data, req.headers, req.get_method())<br>
 → 525         response = self._open(req, data)<br>
526<br>
527         # post-process response</p>
<p>~\anaconda3\envs\AN\lib\urllib\request.py in _open(self, req, data)<br>
540<br>
541         protocol = req.type<br>
 → 542         result = self._call_chain(self.handle_open, protocol, protocol +<br>
543                                   ‘_open’, req)<br>
544         if result:</p>
<p>~\anaconda3\envs\AN\lib\urllib\request.py in _call_chain(self, chain, kind, meth_name, *args)<br>
500         for handler in handlers:<br>
501             func = getattr(handler, meth_name)<br>
 → 502             result = func(*args)<br>
503             if result is not None:<br>
504                 return result</p>
<p>~\anaconda3\envs\AN\lib\urllib\request.py in https_open(self, req)<br>
1395<br>
1396         def https_open(self, req):<br>
 → 1397             return self.do_open(http.client.HTTPSConnection, req,<br>
1398                 context=self._context, check_hostname=self._check_hostname)<br>
1399</p>
<p>~\anaconda3\envs\AN\lib\urllib\request.py in do_open(self, http_class, req, **http_conn_args)<br>
1355                           encode_chunked=req.has_header(‘Transfer-encoding’))<br>
1356             except OSError as err: # timeout error<br>
 → 1357                 raise URLError(err)<br>
1358             r = h.getresponse()<br>
1359         except:</p>
<p><strong>URLError: &lt;urlopen error Tunnel connection failed: 407 Proxy Authentication Required&gt;</strong><br>
`</p>

ptrblck: <p>Try to load the file directly via <code>torch.load</code> and then <code>model.load_state_dict</code> as seen in my code snippet.</p>