forked from JDAI-CV/FaceX-Zoo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_loader.py
53 lines (44 loc) · 1.75 KB
/
model_loader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""
@author: Jun Wang
@date: 20201016
@contact: [email protected]
"""
import torch
class ModelLoader:
"""Load a model by network and weights file.
Attributes:
model(object): the model definition file.
"""
def __init__(self, backbone_factory):
self.model = backbone_factory.get_backbone()
def load_model_default(self, model_path):
"""The default method to load a model.
Args:
model_path(str): the path of the weight file.
Returns:
model(object): initialized model.
"""
self.model.load_state_dict(torch.load(model_path)['state_dict'], strict=True)
model = torch.nn.DataParallel(self.model).cuda()
return model
def load_model(self, model_path):
"""The custom method to load a model.
Args:
model_path(str): the path of the weight file.
Returns:
model(object): initialized model.
"""
model_dict = self.model.state_dict()
pretrained_dict = torch.load(model_path)['state_dict']
#pretrained_dict = torch.load(model_path)
new_pretrained_dict = {}
for k in model_dict:
new_pretrained_dict[k] = pretrained_dict['backbone.'+k] # tradition training
#new_pretrained_dict[k] = pretrained_dict['feat_net.'+k] # tradition training
#new_pretrained_dict[k] = pretrained_dict['module.'+k]
#new_pretrained_dict[k] = pretrained_dict['module.backbone.'+k]
#new_pretrained_dict[k] = pretrained_dict[k] # co-mining
model_dict.update(new_pretrained_dict)
self.model.load_state_dict(model_dict)
model = torch.nn.DataParallel(self.model).cuda()
return model