Print keys with different name or different size when loading models. 1. Print keys with different names. 2. If strict=False, print the same key but with different tensor size. It also ignore these keys with different sizes (not load). Args: crt_net
(self, crt_net, load_net, strict=True)
| 256 | # raise IOError(f'Cannot save {save_path}.') |
| 257 | |
| 258 | def _print_different_keys_loading(self, crt_net, load_net, strict=True): |
| 259 | """Print keys with different name or different size when loading models. |
| 260 | |
| 261 | 1. Print keys with different names. |
| 262 | 2. If strict=False, print the same key but with different tensor size. |
| 263 | It also ignore these keys with different sizes (not load). |
| 264 | |
| 265 | Args: |
| 266 | crt_net (torch model): Current network. |
| 267 | load_net (dict): Loaded network. |
| 268 | strict (bool): Whether strictly loaded. Default: True. |
| 269 | """ |
| 270 | crt_net = self.get_bare_model(crt_net) |
| 271 | crt_net = crt_net.state_dict() |
| 272 | crt_net_keys = set(crt_net.keys()) |
| 273 | load_net_keys = set(load_net.keys()) |
| 274 | |
| 275 | logger = get_root_logger() |
| 276 | if crt_net_keys != load_net_keys: |
| 277 | logger.warning('Current net - loaded net:') |
| 278 | for v in sorted(list(crt_net_keys - load_net_keys)): |
| 279 | logger.warning(f' {v}') |
| 280 | logger.warning('Loaded net - current net:') |
| 281 | for v in sorted(list(load_net_keys - crt_net_keys)): |
| 282 | logger.warning(f' {v}') |
| 283 | |
| 284 | # check the size for the same keys |
| 285 | if not strict: |
| 286 | common_keys = crt_net_keys & load_net_keys |
| 287 | for k in common_keys: |
| 288 | if crt_net[k].size() != load_net[k].size(): |
| 289 | logger.warning(f'Size different, ignore [{k}]: crt_net: ' |
| 290 | f'{crt_net[k].shape}; load_net: {load_net[k].shape}') |
| 291 | load_net[k + '.ignore'] = load_net.pop(k) |
| 292 | |
| 293 | def load_network(self, net, load_path, strict=True, param_key='params'): |
| 294 | """Load network. |
no test coverage detected