-
Notifications
You must be signed in to change notification settings - Fork 13
/
calculate_mupots_gt.py
81 lines (71 loc) · 2.47 KB
/
calculate_mupots_gt.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from models import networkgcn, networktcn
import torch
import numpy as np
from TorchSUL import Model as M
from tqdm import tqdm
import torch.nn.functional as F
import pickle
import glob
import os
from collections import defaultdict
import scipy.io as sio
order = np.int64([10, 8, 14,15,16, 11,12,13, 1,2,3,4,5,6, 0, 7, 9])
bone_pairs = [[8,9],[9,10], [8,14],[14,15],[15,16], [8,11],[12,13],[11,12], [8,7],[7,0], [4,5],[5,6],[0,4], [0,1],[1,2],[2,3]]
bone_matrix = np.zeros([16,17], dtype=np.float32)
for i, pair in enumerate(bone_pairs):
bone_matrix[i, pair[0]] = -1
bone_matrix[i, pair[1]] = 1
bone_matrix_inv = np.linalg.pinv(bone_matrix)
bone_matrix_inv = torch.from_numpy(bone_matrix_inv)
bone_matrix = torch.from_numpy(bone_matrix)
seq_len = 243
netgcn = networkgcn.TransNet(256, 17)
nettcn = networktcn.Refine2dNet(17, seq_len)
# initialize the network with dumb input
x_dumb = torch.zeros(2,17,2)
affb = torch.ones(2,16,16) / 16
affpts = torch.ones(2,17,17) / 17
netgcn(x_dumb, affpts, affb, bone_matrix, bone_matrix_inv)
x_dumb = torch.zeros(2,243, 17*3)
nettcn(x_dumb)
# load networks
M.Saver(netgcn).restore('./ckpts/model_gcnwild/')
M.Saver(nettcn).restore('./ckpts/model_tcn/')
# push to gpu
netgcn.cuda()
netgcn.eval()
nettcn.cuda()
nettcn.eval()
bone_matrix = bone_matrix.cuda()
bone_matrix_inv = bone_matrix_inv.cuda()
# create result folder
if not os.path.exists('mupots/pred/'):
os.makedirs('mupots/pred/')
# run prediction
results = defaultdict(list)
for ptsfile in glob.glob('mupots/p2ds/*.pkl'):
ptsfile = ptsfile.replace('\\','/') # for windows
print(ptsfile)
p2d = pickle.load(open(ptsfile, 'rb'))
p2d = torch.from_numpy(p2d).cuda() / 1024
scale = p2d[:,8:9, 1:2] - p2d[:, 0:1, 1:2]
p2d = p2d / scale
p2d = p2d - p2d[:,0:1]
bsize = p2d.shape[0]
affb = torch.ones(bsize,16,16).cuda() / 16
affpts = torch.ones(bsize,17,17).cuda() / 17
with torch.no_grad():
pred = netgcn(p2d, affpts, affb, bone_matrix, bone_matrix_inv)
pred = pred.unsqueeze(0).unsqueeze(0)
pred = F.pad(pred, (0,0,0,0,seq_len//2, seq_len//2), mode='replicate')
pred = pred.squeeze()
pred = nettcn.evaluate(pred)
# pickle.dump(pred.cpu().numpy(), open(ptsfile.replace('p2ds/', 'pred/'), 'wb'))
pred = pred.cpu().numpy()
video_ind = int(ptsfile.split('/')[-1].split('_')[0])
results[video_ind+1].append(pred)
for k in results:
pred = np.stack(results[k], axis=1)
pred = np.transpose(pred, (0, 1, 3, 2))
pred = pred[:,:,:,order]
pickle.dump(pred, open('mupots/pred/%d.pkl'%k, 'wb'))