-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrans_jsscript.py
67 lines (43 loc) · 1.87 KB
/
trans_jsscript.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
54
55
56
57
58
59
from show_emo import load_model
import torch
from lib.msra_resnet import get_pose_net
def main():
to_coords_model_pt("./pose_coords_model2.pt")
def to_model_pt(path):
vgcnn = load_model()
vgcnn.eval() ## put models in eval mode
x = torch.rand(1, 3, 224, 224)
m = torch.jit.trace(vgcnn, x)
torch.jit.save(m, path)
# torch.save(vgcnn, path)
def to_coords_model_pt(path):
num_layers = 50
# heads = {'hm': 16, 'depth': 16}
model = get_pose_net(num_layers)
load_model = './models/fusion_3d_var.pth'
# checkpoint = torch.load(load_model, map_location=lambda storage, loc: storage)
checkpoint = torch.load(load_model, map_location='cpu')
state_dict = checkpoint['state_dict']
model_state_dict = model.state_dict()
for key in state_dict:
if not 'deconv_layers' in key:
model_state_dict[key] = state_dict[key]
elif '1' in key:
model_state_dict[key.replace('deconv_layers.1.', 'deconv_layers.0.bn.')] = state_dict[key]
elif '4' in key:
model_state_dict[key.replace('deconv_layers.4.', 'deconv_layers.1.bn.')] = state_dict[key]
elif '7' in key:
model_state_dict[key.replace('deconv_layers.7.', 'deconv_layers.2.bn.')] = state_dict[key]
model_state_dict['deconv_layers.0.deconv.weight'] = state_dict['deconv_layers.0.weight']
model_state_dict['deconv_layers.1.deconv.weight'] = state_dict['deconv_layers.3.weight']
model_state_dict['deconv_layers.2.deconv.weight'] = state_dict['deconv_layers.6.weight']
model.load_state_dict(model_state_dict)
model.eval() ## put models in eval mode
x = torch.rand(1, 3, 256, 256)
m = torch.jit.trace(model, x)
torch.jit.save(m, path)
if __name__ == '__main__':
# main()
model = torch.jit.load("./pose_coords_model.pt")
output = model(torch.ones(1, 3, 256, 256))
print(output)