MCPcopy Create free account
hub / github.com/SwinTransformer/Transformer-SSL / load_pretrained

Function load_pretrained

utils.py:20–34  ·  view source on GitHub ↗
(model, ckpt_path, logger)

Source from the content-addressed store, hash-verified

18
19
20def load_pretrained(model, ckpt_path, logger):
21 model_dict = model.state_dict()
22
23 state_dict = torch.load(ckpt_path, map_location='cpu')['model']
24 state_dict = {k.replace('encoder.', ''): v for k, v in state_dict.items() if 'encoder.' in k}
25
26 for k in model_dict.keys():
27 if 'head' in k:
28 state_dict[k] = model_dict[k]
29
30 missing_keys, unexpected_keys = model.load_state_dict(state_dict, strict=False)
31
32 logger.info(f'loaded pretrained checkpoint from: {ckpt_path}')
33 if len(missing_keys) > 0 or len(unexpected_keys) > 0:
34 logger.warning(f'Missing keys: {missing_keys}\nUnexpected keys: {unexpected_keys}')
35
36
37def load_checkpoint(config, model, optimizer, lr_scheduler, logger):

Callers 1

mainFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected