bug fix for partial model initialization, if model is not initialized, it is tried to init model partially with only matching layers in size

This commit is contained in:
Eren Golge 2018-12-13 18:19:02 +01:00
parent 481105ccfa
commit a1fe867371
1 changed files with 15 additions and 11 deletions

View File

@ -360,17 +360,21 @@ def main(args):
if args.restore_path: if args.restore_path:
checkpoint = torch.load(args.restore_path) checkpoint = torch.load(args.restore_path)
model.load_state_dict(checkpoint['model']) try:
# Partial initialization: if there is a mismatch with new and old layer, it is skipped. model.load_state_dict(checkpoint['model'])
# 1. filter out unnecessary keys except:
pretrained_dict = { print(" > Partial model initialization.")
k: v model_dict = model.state_dict()
for k, v in checkpoint['model'].items() if k in model_dict # Partial initialization: if there is a mismatch with new and old layer, it is skipped.
} # 1. filter out unnecessary keys
# 2. overwrite entries in the existing state dict pretrained_dict = {
model_dict.update(pretrained_dict) k: v
# 3. load the new state dict for k, v in checkpoint['model'].items() if k in model_dict
model.load_state_dict(model_dict) }
# 2. overwrite entries in the existing state dict
model_dict.update(pretrained_dict)
# 3. load the new state dict
model.load_state_dict(model_dict)
if use_cuda: if use_cuda:
model = model.cuda() model = model.cuda()
criterion.cuda() criterion.cuda()