Initialization of VGG layer weights

In config file, VGG layer weights are initialized using this way:

from easydict import EasyDict as edict
MODEL = edict()
MODEL.VGG_LAYER_WEIGHTS = dict(conv3_4=1/8, conv4_4=1/4, conv5_4=1/2)

But how to initialize it using a parser? I have tried to do this the following way:

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’)

But got error. Please help me to write it correctly.

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?

1 Like

Firstly, my actually problem is that I could not able to download the VGG-19 pretrained weights due to the system proxy.
VGG19 pretrained weight downloading failed from https://download.pytorch.org/models/vgg19-dcbb9e9d.pth” to C:\Users\TB/.cache\torch\hub\checkpoints\vgg19-dcbb9e9d.pth with this error "urlopen error Tunnel connection failed:407 Proxy Authentication Required"
Secondly, VGG-19 layer weights need to be intilalized using config file. In the the following link, layer weights are initialized using config file:
Simple-SR/config.py at master · dvlab-research/Simple-SR · GitHub
But how to write this using parse arugument.

Maybe you could try to download this file manually if you have trouble allowing Python to do it.

This is not the case as the config.py script you’ve linked to is a custom config from a public repository and not from PyTorch or torchvision.

The desired .pth file contains the state_dict of vgg19 and can be manually loaded via:

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)
# <All keys matched successfully>

once you’ve downloaded it to a local folder.

I have downloaded the VGG-19 weights and placed it to **C:\Users\TB/.cache\torch\hub\checkpoints**. Here is the complete error:

`---------------------------------------------------------------------------
OSError Traceback (most recent call last)
~\anaconda3\envs\AN\lib\urllib\request.py in do_open(self, http_class, req, **http_conn_args)
1353 try:
→ 1354 h.request(req.get_method(), req.selector, req.data, headers,
1355 encode_chunked=req.has_header(‘Transfer-encoding’))

~\anaconda3\envs\AN\lib\http\client.py in request(self, method, url, body, headers, encode_chunked)
1251 “”“Send a complete request to the server.”""
→ 1252 self._send_request(method, url, body, headers, encode_chunked)
1253

~\anaconda3\envs\AN\lib\http\client.py in _send_request(self, method, url, body, headers, encode_chunked)
1297 body = _encode(body, ‘body’)
→ 1298 self.endheaders(body, encode_chunked=encode_chunked)
1299

~\anaconda3\envs\AN\lib\http\client.py in endheaders(self, message_body, encode_chunked)
1246 raise CannotSendHeader()
→ 1247 self._send_output(message_body, encode_chunked=encode_chunked)
1248

~\anaconda3\envs\AN\lib\http\client.py in _send_output(self, message_body, encode_chunked)
1006 del self._buffer[:]
→ 1007 self.send(msg)
1008

~\anaconda3\envs\AN\lib\http\client.py in send(self, data)
946 if self.auto_open:
→ 947 self.connect()
948 else:

~\anaconda3\envs\AN\lib\http\client.py in connect(self)
1413
→ 1414 super().connect()
1415

~\anaconda3\envs\AN\lib\http\client.py in connect(self)
922 if self._tunnel_host:
→ 923 self._tunnel()
924

~\anaconda3\envs\AN\lib\http\client.py in _tunnel(self)
900 self.close()
→ 901 raise OSError(“Tunnel connection failed: %d %s” % (code,
902 message.strip()))

OSError: Tunnel connection failed: 407 Proxy Authentication Required

During handling of the above exception, another exception occurred:

URLError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_8004/3520862733.py in
1 config,extra = get_config()
----> 2 main(config)

~\AppData\Local\Temp/ipykernel_8004/929139210.py in main(config)
12 data_loader = get_loader(config.image_size, config.scale_factor, config.batch_size, config.sample_batch_size)
13 trainer = Trainer(config, data_loader)
—> 14 trainer.train()

~\AppData\Local\Temp/ipykernel_8004/3677316706.py in train(self)
71 self.bp_criterion = nn.L1Loss(reduction=‘mean’)
72 if self.use_pcp:
—> 73 self.pcp_criterion = PerceptualLoss(layer_weights=self.VGG_LAYER_WEIGHTS,
** 74 vgg_type=config.VGG_TYPE,**
** 75 use_input_norm=config.USE_INPUT_NORM,**

~\AppData\Local\Temp/ipykernel_8004/3161286985.py in init(self, layer_weights, vgg_type, use_input_norm, use_pcp_loss, use_style_loss, norm_img, criterion)
196 self.use_style_loss = use_style_loss
197 self.layer_weights = layer_weights
→ 198 self.vgg = VGGFeatureExtractor(
199 layer_name_list=list(layer_weights.keys()),
200 vgg_type=vgg_type,

~\AppData\Local\Temp/ipykernel_8004/825357570.py in init(self, layer_name_list, vgg_type, use_input_norm, requires_grad, remove_pooling, pooling_stride)
96 max_idx = idx
97
—> 98 features = getattr(vgg, vgg_type)(pretrained=True).features[:max_idx + 1]
99
100 modified_net = OrderedDict()

~\anaconda3\envs\AN\lib\site-packages\torchvision\models\vgg.py in vgg19(pretrained, progress, **kwargs)
177 progress (bool): If True, displays a progress bar of the download to stderr
178 “”"
→ 179 **return _vgg(‘vgg19’, ‘E’, False, pretrained, progress, kwargs)
180
181

~\anaconda3\envs\AN\lib\site-packages\torchvision\models\vgg.py in _vgg(arch, cfg, batch_norm, pretrained, progress, **kwargs)
97 model = VGG(make_layers(cfgs[cfg], batch_norm=batch_norm), kwargs)
98 if pretrained:
—> 99 state_dict = load_state_dict_from_url(model_urls[arch],
** 100 progress=progress)

101 model.load_state_dict(state_dict)

~\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)
522 r = HASH_REGEX.search(filename) # r is Optional[Match[str]]
523 hash_prefix = r.group(1) if r else None
→ 524 download_url_to_file(url, cached_file, hash_prefix, progress=progress)
525
526 if _is_legacy_zip_format(cached_file):

~\anaconda3\envs\AN\lib\site-packages\torch\hub.py in download_url_to_file(url, dst, hash_prefix, progress)
392 # certificates in older Python
393 req = Request(url, headers={“User-Agent”: “torch.hub”})
→ 394 u = urlopen(req)
395 meta = u.info()
396 if hasattr(meta, ‘getheaders’):

~\anaconda3\envs\AN\lib\urllib\request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context)
220 else:
221 opener = _opener
→ 222 return opener.open(url, data, timeout)
223
224 def install_opener(opener):

~\anaconda3\envs\AN\lib\urllib\request.py in open(self, fullurl, data, timeout)
523
524 sys.audit(‘urllib.Request’, req.full_url, req.data, req.headers, req.get_method())
→ 525 response = self._open(req, data)
526
527 # post-process response

~\anaconda3\envs\AN\lib\urllib\request.py in _open(self, req, data)
540
541 protocol = req.type
→ 542 result = self._call_chain(self.handle_open, protocol, protocol +
543 ‘_open’, req)
544 if result:

~\anaconda3\envs\AN\lib\urllib\request.py in _call_chain(self, chain, kind, meth_name, *args)
500 for handler in handlers:
501 func = getattr(handler, meth_name)
→ 502 result = func(*args)
503 if result is not None:
504 return result

~\anaconda3\envs\AN\lib\urllib\request.py in https_open(self, req)
1395
1396 def https_open(self, req):
→ 1397 return self.do_open(http.client.HTTPSConnection, req,
1398 context=self._context, check_hostname=self._check_hostname)
1399

~\anaconda3\envs\AN\lib\urllib\request.py in do_open(self, http_class, req, **http_conn_args)
1355 encode_chunked=req.has_header(‘Transfer-encoding’))
1356 except OSError as err: # timeout error
→ 1357 raise URLError(err)
1358 r = h.getresponse()
1359 except:

URLError: <urlopen error Tunnel connection failed: 407 Proxy Authentication Required>
`

Try to load the file directly via torch.load and then model.load_state_dict as seen in my code snippet.

1 Like