| 74 | |
| 75 | |
| 76 | def load_model(model, model_path, optimizer=None, resume=False, |
| 77 | lr=None, lr_step=None, gamma=None): |
| 78 | start_epoch = 0 |
| 79 | checkpoint = torch.load(model_path, map_location=lambda storage, loc: storage) |
| 80 | print('loaded {}, epoch {}'.format(model_path, checkpoint['epoch'])) |
| 81 | state_dict_ = checkpoint['state_dict'] |
| 82 | state_dict = {} |
| 83 | |
| 84 | # convert data_parallal to model |
| 85 | for k in state_dict_: |
| 86 | if k.startswith('module') and not k.startswith('module_list'): |
| 87 | state_dict[k[7:]] = state_dict_[k] |
| 88 | else: |
| 89 | state_dict[k] = state_dict_[k] |
| 90 | model_state_dict = model.state_dict() |
| 91 | |
| 92 | # check loaded parameters and created model parameters |
| 93 | msg = 'If you see this, your model does not fully load the ' + \ |
| 94 | 'pre-trained weight. Please make sure ' + \ |
| 95 | 'you have correctly specified --arch xxx ' + \ |
| 96 | 'or set the correct --num_classes for your own dataset.' |
| 97 | for k in state_dict: |
| 98 | if k in model_state_dict: |
| 99 | if state_dict[k].shape != model_state_dict[k].shape: |
| 100 | print('Skip loading parameter {}, required shape{}, '\ |
| 101 | 'loaded shape{}. {}'.format( |
| 102 | k, model_state_dict[k].shape, state_dict[k].shape, msg)) |
| 103 | state_dict[k] = model_state_dict[k] |
| 104 | else: |
| 105 | print('Drop parameter {}.'.format(k) + msg) |
| 106 | for k in model_state_dict: |
| 107 | if not (k in state_dict): |
| 108 | print('No param {}.'.format(k) + msg) |
| 109 | state_dict[k] = model_state_dict[k] |
| 110 | model.load_state_dict(state_dict, strict=False) |
| 111 | |
| 112 | # resume optimizer parameters |
| 113 | if optimizer is not None and resume: |
| 114 | if 'optimizer' in checkpoint: |
| 115 | optimizer.load_state_dict(checkpoint['optimizer']) |
| 116 | start_epoch = checkpoint['epoch'] + 1 |
| 117 | start_lr = lr |
| 118 | for step in lr_step: |
| 119 | if start_epoch >= step: |
| 120 | start_lr *= gamma |
| 121 | for param_group in optimizer.param_groups: |
| 122 | param_group['lr'] = start_lr |
| 123 | print('Resumed optimizer with start lr', start_lr) |
| 124 | else: |
| 125 | print('No optimizer parameters in checkpoint.') |
| 126 | return model, optimizer, start_epoch |
| 127 | |
| 128 | |
| 129 | def save_model(path, epoch, model, optimizer=None): |