-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviz_h36m_3d.py
288 lines (248 loc) · 10.9 KB
/
viz_h36m_3d.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#!/usr/bin/python3.6
# -*- coding: utf-8 -*-
# @Time : 2021/5/31 21:08
# @Author : zhouhonghong
# @Email : zhouhonghong@bupt.edu.cn
"""
use the checkpoint to visualize the motions predicted on the CMU mocap dataset
"""
from __future__ import print_function, absolute_import, division
import os
os.environ["CUDA_VISIBLE_DEVICES"] = '2'
import time
import torch
import torch.nn as nn
import torch.optim
from torch.utils.data import DataLoader
from torch.autograd import Variable
from torch.nn import functional
import numpy as np
from progress.bar import Bar
import pandas as pd
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from utils import loss_funcs, utils as utils
from utils.opt import Options
from utils.h36motion3d import H36motion3D
import utils.model as nnmodel
import utils.data_utils as data_utils
from tqdm import tqdm
import imageio
class Ax3DPose(object):
def __init__(self, ax, p1_lcolor="#3498db", p1_rcolor="#e74c3c",label=['GT', 'Pred']):
"""
Create a 3d pose visualizer that can be updated with new poses.
Args
ax: 3d axis to plot the 3d pose on
lcolor: String. Colour for the left part of the body
rcolor: String. Colour for the right part of the body
"""
# Start and endpoints of two persons
self.I = np.array([1, 2, 3, 1, 7, 8, 1, 13, 14, 15, 14, 18, 19, 14, 26, 27]) - 1
self.J = np.array([2, 3, 4, 7, 8, 9, 13, 14, 15, 16, 18, 19, 20, 26, 27, 28]) - 1
"""
Left / right indicator:
pre p1 left: 0
pre p1 right: 1
gt: 2
"""
self.color_ind = np.array([1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1], dtype=int)
switch = {
0: p1_lcolor,
1: p1_rcolor,
2: "#BEBEBE"
}
self.ax = ax
vals = np.zeros((38, 3))
# Make connection matrix
self.plots = []
color = switch[2]
for i in np.arange(len(self.I)):
x = np.array([vals[self.I[i], 0], vals[self.J[i], 0]])
y = np.array([vals[self.I[i], 1], vals[self.J[i], 1]])
z = np.array([vals[self.I[i], 2], vals[self.J[i], 2]])
if i == 0:
self.plots.append(
self.ax.plot(x, z, y, lw=2, linestyle='--', c=color, label=label[0]))
else:
self.plots.append(self.ax.plot(x, y, z, lw=2, linestyle='--', c=color))
self.plots_pred = []
for i in np.arange(len(self.I)):
color = switch[self.color_ind[i]]
x = np.array([vals[self.I[i], 0], vals[self.J[i], 0]])
y = np.array([vals[self.I[i], 1], vals[self.J[i], 1]])
z = np.array([vals[self.I[i], 2], vals[self.J[i], 2]])
if i == 0:
self.plots_pred.append(self.ax.plot(x, y, z, lw=2, c=color, label=label[1]))
else:
self.plots_pred.append(self.ax.plot(x, y, z, lw=2, c=color))
self.ax.set_xlabel("x")
self.ax.set_ylabel("y")
self.ax.set_zlabel("z")
self.ax.set_axis_off()
# self.ax.axes.get_xaxis().set_visible(False)
# self.axes.get_yaxis().set_visible(False)
self.ax.legend(loc='lower left')
self.ax.view_init(120, -90)
def update(self, gt_vals, pred_vals):
"""
Update the plotted 3d pose.
Args
lcolor: String. Colour for the left part of the body.
rcolor: String. Colour for the right part of the body.
Returns
Nothing. Simply updates the axis with the new pose.
"""
switch = {
0: "#3498db",
1: "#e74c3c",
2: "#BEBEBE"
}
color = switch[2]
for i in np.arange(len(self.I)):
x = np.array([gt_vals[self.I[i], 0], gt_vals[self.J[i], 0]])
y = np.array([gt_vals[self.I[i], 1], gt_vals[self.J[i], 1]])
z = np.array([gt_vals[self.I[i], 2], gt_vals[self.J[i], 2]])
self.plots[i][0].set_xdata(x)
self.plots[i][0].set_ydata(y)
self.plots[i][0].set_3d_properties(z)
self.plots[i][0].set_color(color)
# self.plots_pred[i][0].set_alpha(0.7)
for i in np.arange(len(self.I)):
color = switch[self.color_ind[i]]
x = np.array([pred_vals[self.I[i], 0], pred_vals[self.J[i], 0]])
y = np.array([pred_vals[self.I[i], 1], pred_vals[self.J[i], 1]])
z = np.array([pred_vals[self.I[i], 2], pred_vals[self.J[i], 2]])
self.plots_pred[i][0].set_xdata(x)
self.plots_pred[i][0].set_ydata(y)
self.plots_pred[i][0].set_3d_properties(z)
self.plots_pred[i][0].set_color(color)
r = 750
xroot, yroot, zroot = gt_vals[0, 0], gt_vals[0, 1], gt_vals[0, 2]
self.ax.set_xlim3d([-r + xroot, r + xroot])
self.ax.set_zlim3d([-r + zroot, r + zroot])
self.ax.set_ylim3d([-r + yroot, r + yroot])
self.ax.set_aspect('equal')
def plot_predictions(gt_3d, pred_3d, ax, f_title, imgs_path):
# Load all the data
nframes_pred = gt_3d.shape[0]
# === Plot and animate ===
ob = Ax3DPose(ax)
# Plot the prediction
for i in range(nframes_pred):
ob.update(gt_3d[i, :], pred_3d[i, :])
f_title_new = f_title + 'frame:{}'.format(i + 1)
jpg_name = '%03d'%(i+1)+'.jpg'
ax.set_title(f_title_new, loc="left")
# plt.show(block=False)
seq_folder = os.path.join(imgs_path, f_title)
if not os.path.exists(seq_folder):
os.makedirs(seq_folder)
plt.savefig(os.path.join(seq_folder, jpg_name))
# fig.canvas.draw()
def save_gif(imgs_root, gif_root):
if not os.path.exists(gif_root):
os.makedirs(gif_root)
for img_folder in tqdm(os.listdir(imgs_root)):
img_path = os.path.join(imgs_root, img_folder)
images = []
imgs = sorted((fn for fn in os.listdir(img_path) if fn.endswith('.jpg')))
for img in imgs:
images.append(imageio.imread(os.path.join(img_path, img)))
imageio.mimsave(os.path.join(gif_root, img_folder.strip(',') + '.gif'), images, duration=0.2)
def test(data_loader, model, input_n=20, output_n=50, is_cuda=False, dim_used=[], dct_n=20):
targ_all_3d = None
pred_all_3d = None
for i, (inputs, targets, all_seq) in enumerate(data_loader):
if is_cuda:
inputs = Variable(inputs.cuda()).float()
all_seq = Variable(all_seq.cuda(async=True)).float()
outputs = model(inputs)
n, seq_len, dim_full_len = all_seq.data.shape
dim_used_len = len(dim_used)
_, idct_m = data_utils.get_dct_matrix(seq_len)
idct_m = Variable(torch.from_numpy(idct_m)).float().cuda()
outputs_t = outputs.view(-1, dct_n).transpose(0, 1)
outputs_3d = torch.matmul(idct_m[:, 0:dct_n], outputs_t).transpose(0, 1).contiguous().view(-1, dim_used_len,
seq_len).transpose(1,
2)
pred_3d = all_seq.clone()
dim_used = np.array(dim_used)
# joints at same loc
joint_to_ignore = np.array([16, 20, 23, 24, 28, 31])
index_to_ignore = np.concatenate((joint_to_ignore * 3, joint_to_ignore * 3 + 1, joint_to_ignore * 3 + 2))
joint_equal = np.array([13, 19, 22, 13, 27, 30])
index_to_equal = np.concatenate((joint_equal * 3, joint_equal * 3 + 1, joint_equal * 3 + 2))
pred_3d[:, :, dim_used] = outputs_3d
pred_3d[:, :, index_to_ignore] = pred_3d[:, :, index_to_equal]
pred_p3d = pred_3d.contiguous().view(n, seq_len, -1, 3)
targ_p3d = all_seq.contiguous().view(n, seq_len, -1, 3)
if i==0:
targ_all_3d = targ_p3d
pred_all_3d = pred_p3d
else:
targ_all_3d = torch.cat((targ_all_3d, targ_p3d))
pred_all_3d = torch.cat((pred_all_3d, pred_p3d))
return targ_all_3d, pred_all_3d
def main(opt,img_path,model_path_short, model_path_long):
is_cuda = torch.cuda.is_available()
# create model
print(">>> creating model")
input_n = opt.input_n
output_n = opt.output_n
dct_n = opt.dct_n
sample_rate = opt.sample_rate
model = nnmodel.MGCN(opt)
if is_cuda:
model.cuda()
print(">>> total params: {:.2f}M".format(sum(p.numel() for p in model.parameters()) / 1000000.0))
optimizer = torch.optim.Adam(model.parameters(), lr=opt.lr)
opt.is_load = True
if opt.is_load:
if dct_n == 20:
model_path_len = model_path_short
else:
model_path_len = model_path_long
print(">>> loading ckpt len from '{}'".format(model_path_len))
if is_cuda:
ckpt = torch.load(model_path_len)
else:
ckpt = torch.load(model_path_len, map_location='cpu')
start_epoch = ckpt['epoch']
err_best = ckpt['err']
lr_now = ckpt['lr']
model.load_state_dict(ckpt['state_dict'])
optimizer.load_state_dict(ckpt['optimizer'])
print(">>> ckpt len loaded (epoch: {} | err: {})".format(start_epoch, err_best))
# data loading
train_dataset = H36motion3D(path_to_data=opt.data_dir, actions='all', input_n=input_n, output_n=output_n,
split=0, dct_used=dct_n, sample_rate=sample_rate)
acts = data_utils.define_actions('all')
test_data = dict()
for act in acts:
test_dataset = H36motion3D(path_to_data=opt.data_dir, actions=act, input_n=input_n, output_n=output_n, split=1,
sample_rate=sample_rate, dct_used=dct_n)
test_data[act] = DataLoader(
dataset=test_dataset,
batch_size=opt.test_batch,
shuffle=False,
num_workers=opt.job,
pin_memory=True)
print(">>> data loaded !")
print(">>> train data {}".format(train_dataset.__len__()))
ax = plt.gca(projection='3d')
for act in acts:
# [8,10,38,3]
targ_3d, pred_3d = test(test_data[act],model,input_n=input_n,output_n=output_n,is_cuda=is_cuda,dim_used=train_dataset.dim_used,dct_n=dct_n)
for k in tqdm(range(targ_3d.shape[0])):
plt.cla()
figure_title = "act:{},seq:{},".format(act, k + 1)
plot_predictions(targ_3d[k,:,:,:].detach().cpu().numpy(), pred_3d[k,:,:,:].detach().cpu().numpy(), ax, figure_title, img_path)
if __name__ == "__main__":
option = Options().parse()
main(option,
img_path="/mnt/DataDrive164/zhouhonghong/outputs/MGCN/h36m_long_3d/imgs",
model_path_short='checkpoint/h36m_3d_short/ckpt_main_3d_3D_in10_out10_dct_n_20_best.pth.tar',
model_path_long='checkpoint/h36m_3d_long/ckpt_main_3d_3D_in10_out25_dct_n_35_best.pth.tar')
save_gif(imgs_root="/mnt/DataDrive164/zhouhonghong/outputs/MGCN/h36m_long_3d/imgs",
gif_root="/mnt/DataDrive164/zhouhonghong/outputs/MGCN/h36m_long_3d/gifs",)