-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocess_eft_fits.py
353 lines (317 loc) · 13.5 KB
/
preprocess_eft_fits.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import json
import numpy as np
import torch
import os
from train.criterion import rotation_matrix_to_angle_axis
from tqdm import tqdm
# eft_lspet = '/home/wzeng/mydata/eft_fit/LSPet_ver01.json'
# lspet_npz = '/home/wzeng/mydata/DecoMR/hr-lspet_train.npz'
# lspet_out = lspet_npz[:-4] + '_eft.npz'
#
# with open(eft_lspet, 'r') as f:
# eft_data = json.load(f)
#
# data = np.load(lspet_npz)
#
# imgnames = data['imgname']
# new_data_dict = {key: data[key] for key in data.keys()}
# del new_data_dict['fit_errors']
# new_data_dict['pose'] = np.zeros([len(imgnames), 72])
# new_data_dict['shape'] = np.zeros([len(imgnames), 10])
# new_data_dict['has_smpl'] = np.zeros(len(imgnames))
#
# for item in tqdm(eft_data['data']):
# pose_eft = np.array(item['parm_pose'])
# # Convert rotation matrices to axis-angle
# rotmat = torch.tensor(pose_eft).float()
# rotmat_hom = torch.cat(
# [rotmat.view(-1, 3, 3),
# torch.tensor([0, 0, 1], dtype=torch.float32, device=rotmat.device).view(1, 3, 1).expand(24, -1, -1)],
# dim=-1)
# pose_eft = rotation_matrix_to_angle_axis(rotmat_hom).contiguous().view(-1)
#
# # tgm.rotation_matrix_to_angle_axis returns NaN for 0 rotation, so manually hack it
# pose_eft[torch.isnan(pose_eft)] = 0.0
# pose_eft = pose_eft.numpy()
#
# beta_eft = np.array(item['parm_shape'])
# img_eft = item['imageName']
#
# idx = np.argwhere(imgnames == img_eft)
# if len(idx) == 1:
# idx = idx[0][0]
# img = imgnames[idx]
# assert img_eft == img
# new_data_dict['pose'][idx] = pose_eft
# new_data_dict['shape'][idx] = beta_eft
# new_data_dict['has_smpl'][idx] = 1
# print(idx)
# else:
# print('new img !!!')
# part_eft = np.array(item['gt_keypoint_2d'])
# part_eft = part_eft[25:, :] # remove 25 openpose joints
# scale_eft = np.array('bbox_scale')
# center_eft = np.array('bbox_center')
#
# new_data_dict['imgname'] = np.concatenate((new_data_dict['imgname'], np.array(img_eft)), axis=0)
# new_data_dict['center'] = np.concatenate((new_data_dict['center'], center_eft[None, :]), axis=0)
# new_data_dict['scale'] = np.concatenate((new_data_dict['scale'], scale_eft[None, :]), axis=0)
# new_data_dict['part'] = np.concatenate((new_data_dict['part'], scale_eft[None, :]), axis=0)
# new_data_dict['iuvnames'] = np.concatenate((new_data_dict['part'], np.array('')), axis=0)
# new_data_dict['pose'] = np.concatenate((new_data_dict['pose'], pose_eft[None, :]), axis=0)
# new_data_dict['shape'] = np.concatenate((new_data_dict['shape'], beta_eft[None, :]), axis=0)
# new_data_dict['has_smpl'] = np.concatenate((new_data_dict['has_smpl'], np.ones(1)), axis=0)
#
#
# np.savez(lspet_out, **new_data_dict)
# eft_mpii = '/home/wzeng/mydata/eft_fit/MPII_ver01.json'
# mpii_npz = '/home/wzeng/mydata/DecoMR/mpii_train.npz'
# mpii_out = mpii_npz[:-4] + '_eft.npz'
#
# with open(eft_mpii, 'r') as f:
# eft_data = json.load(f)
#
# data = np.load(mpii_npz)
#
# imgnames = data['imgname']
# new_data_dict = {key: data[key] for key in data.keys()}
# del new_data_dict['fit_errors']
# new_data_dict['pose'] = np.zeros([len(imgnames), 72])
# new_data_dict['shape'] = np.zeros([len(imgnames), 10])
# new_data_dict['has_smpl'] = np.zeros(len(imgnames))
#
# for item in tqdm(eft_data['data']):
# pose_eft = np.array(item['parm_pose'])
# # Convert rotation matrices to axis-angle
# rotmat = torch.tensor(pose_eft).float()
# rotmat_hom = torch.cat(
# [rotmat.view(-1, 3, 3),
# torch.tensor([0, 0, 1], dtype=torch.float32, device=rotmat.device).view(1, 3, 1).expand(24, -1, -1)],
# dim=-1)
# pose_eft = rotation_matrix_to_angle_axis(rotmat_hom).contiguous().view(-1)
#
# # tgm.rotation_matrix_to_angle_axis returns NaN for 0 rotation, so manually hack it
# pose_eft[torch.isnan(pose_eft)] = 0.0
# pose_eft = pose_eft.numpy()
#
# beta_eft = np.array(item['parm_shape'])
# img_eft = item['imageName']
# img_eft = 'images/' + img_eft
#
# idx = np.argwhere(imgnames == img_eft)
# if len(idx) == 1:
# idx = idx[0][0]
# img = imgnames[idx]
# assert img_eft == img
# new_data_dict['pose'][idx] = pose_eft
# new_data_dict['shape'][idx] = beta_eft
# new_data_dict['has_smpl'][idx] = 1
# print(idx)
# elif len(idx) > 1:
# print('multi bbox in 1 image')
# center_eft = np.array(item['bbox_center'])
# centers = new_data_dict['center'][idx, :][:, 0, :]
# tmp = centers - center_eft[None, :]
# tmp = np.abs(tmp).sum(axis=1)
# id_t = np.argmin(tmp)
# idx = idx[id_t, 0]
#
# img = imgnames[idx]
# center = new_data_dict['center'][idx]
# assert img_eft == img
# assert np.allclose(center, center_eft)
# new_data_dict['pose'][idx] = pose_eft
# new_data_dict['shape'][idx] = beta_eft
# new_data_dict['has_smpl'][idx] = 1
# print(idx)
#
# else:
# print('new img !!!')
# part_eft = np.array(item['gt_keypoint_2d'])
# part_eft = part_eft[25:, :] # remove 25 openpose joints
# scale_eft = np.array(item['bbox_scale'])
# center_eft = np.array(item['bbox_center'])
#
# new_data_dict['imgname'] = np.concatenate((new_data_dict['imgname'], np.array(img_eft)), axis=0)
# new_data_dict['center'] = np.concatenate((new_data_dict['center'], center_eft[None, :]), axis=0)
# new_data_dict['scale'] = np.concatenate((new_data_dict['scale'], scale_eft[None, :]), axis=0)
# new_data_dict['part'] = np.concatenate((new_data_dict['part'], scale_eft[None, :]), axis=0)
# new_data_dict['iuvnames'] = np.concatenate((new_data_dict['part'], np.array('')), axis=0)
# new_data_dict['pose'] = np.concatenate((new_data_dict['pose'], pose_eft[None, :]), axis=0)
# new_data_dict['shape'] = np.concatenate((new_data_dict['shape'], beta_eft[None, :]), axis=0)
# new_data_dict['has_smpl'] = np.concatenate((new_data_dict['has_smpl'], np.ones(1)), axis=0)
#
#
# np.savez(mpii_out, **new_data_dict)
# eft_coco = '/home/wzeng/mydata/eft_fit/COCO2014-Part-ver01.json'
# coco_npz = '/home/wzeng/mydata/DecoMR/coco_2014_train.npz'
# coco_out = coco_npz[:-4] + '_eft.npz'
#
# with open(eft_coco, 'r') as f:
# eft_data = json.load(f)
#
# data = np.load(coco_npz)
#
# imgnames = data['imgname']
# new_data_dict = {key: data[key] for key in data.keys()}
# del new_data_dict['fit_errors']
# new_data_dict['pose'] = np.zeros([len(imgnames), 72])
# new_data_dict['shape'] = np.zeros([len(imgnames), 10])
# new_data_dict['has_smpl'] = np.zeros(len(imgnames))
#
#
# for item in tqdm(eft_data['data']):
# pose_eft = np.array(item['parm_pose'])
# # Convert rotation matrices to axis-angle
# rotmat = torch.tensor(pose_eft).float()
# rotmat_hom = torch.cat(
# [rotmat.view(-1, 3, 3),
# torch.tensor([0, 0, 1], dtype=torch.float32, device=rotmat.device).view(1, 3, 1).expand(24, -1, -1)],
# dim=-1)
# pose_eft = rotation_matrix_to_angle_axis(rotmat_hom).contiguous().view(-1)
#
# # tgm.rotation_matrix_to_angle_axis returns NaN for 0 rotation, so manually hack it
# pose_eft[torch.isnan(pose_eft)] = 0.0
# pose_eft = pose_eft.numpy()
#
# beta_eft = np.array(item['parm_shape'])
# img_eft = item['imageName']
# img_eft = 'train2014/' + img_eft
#
# idx = np.argwhere(imgnames == img_eft)
# if len(idx) == 1:
# idx = idx[0][0]
# img = imgnames[idx]
# assert img_eft == img
# annot_id = new_data_dict['id'][idx]
# annot_id_eft = item['annotId']
# assert annot_id == annot_id_eft
#
# new_data_dict['pose'][idx] = pose_eft
# new_data_dict['shape'][idx] = beta_eft
# new_data_dict['has_smpl'][idx] = 1
# print(idx)
# elif len(idx) > 1:
# print('multi bbox in 1 image')
# center_eft = np.array(item['bbox_center'])
# centers = new_data_dict['center'][idx, :][:, 0, :]
# tmp = centers - center_eft[None, :]
# tmp = np.abs(tmp).sum(axis=1)
# id_t = np.argmin(tmp)
# idx = idx[id_t, 0]
#
# img = imgnames[idx]
# center = new_data_dict['center'][idx]
# assert img_eft == img
# annot_id = new_data_dict['id'][idx]
# annot_id_eft = item['annotId']
# assert annot_id == annot_id_eft
# assert np.allclose(center, center_eft)
# new_data_dict['pose'][idx] = pose_eft
# new_data_dict['shape'][idx] = beta_eft
# new_data_dict['has_smpl'][idx] = 1
# print(idx)
#
# else:
# exit(1)
# print('new img !!!')
# part_eft = np.array(item['gt_keypoint_2d'])
# part_eft = part_eft[25:, :] # remove 25 openpose joints
# scale_eft = np.array(item['bbox_scale'])
# center_eft = np.array(item['bbox_center'])
#
# new_data_dict['imgname'] = np.concatenate((new_data_dict['imgname'], np.array(img_eft)), axis=0)
# new_data_dict['center'] = np.concatenate((new_data_dict['center'], center_eft[None, :]), axis=0)
# new_data_dict['scale'] = np.concatenate((new_data_dict['scale'], scale_eft[None, :]), axis=0)
# new_data_dict['part'] = np.concatenate((new_data_dict['part'], scale_eft[None, :]), axis=0)
# new_data_dict['iuvnames'] = np.concatenate((new_data_dict['part'], np.array('')), axis=0)
# new_data_dict['pose'] = np.concatenate((new_data_dict['pose'], pose_eft[None, :]), axis=0)
# new_data_dict['shape'] = np.concatenate((new_data_dict['shape'], beta_eft[None, :]), axis=0)
# new_data_dict['has_smpl'] = np.concatenate((new_data_dict['has_smpl'], np.ones(1)), axis=0)
#
#
# np.savez(coco_out, **new_data_dict)
#
# eft_coco = '/home/wzeng/mydata/eft_fit/COCO2014-Part-ver01.json'
# coco_npz = '/home/wzeng/mydata/DecoMR/coco_2014_train.npz'
# coco_out = coco_npz[:-4] + '_eft.npz'
eft_coco = '/home/wzeng/mydata/eft_fit/COCO2014-All-ver01.json'
coco_npz = '/home/wzeng/mydata/DecoMR/coco_2014_train.npz'
coco_out = coco_npz[:-4] + '_eft_all.npz'
with open(eft_coco, 'r') as f:
eft_data = json.load(f)
data = np.load(coco_npz)
imgnames = data['imgname']
new_data_dict = {key: data[key] for key in data.keys()}
del new_data_dict['fit_errors']
new_data_dict['pose'] = np.zeros([len(imgnames), 72])
new_data_dict['shape'] = np.zeros([len(imgnames), 10])
new_data_dict['has_smpl'] = np.zeros(len(imgnames))
out_num = 0
for item in tqdm(eft_data['data']):
pose_eft = np.array(item['parm_pose'])
# Convert rotation matrices to axis-angle
rotmat = torch.tensor(pose_eft).float()
rotmat_hom = torch.cat(
[rotmat.view(-1, 3, 3),
torch.tensor([0, 0, 1], dtype=torch.float32, device=rotmat.device).view(1, 3, 1).expand(24, -1, -1)],
dim=-1)
pose_eft = rotation_matrix_to_angle_axis(rotmat_hom).contiguous().view(-1)
# tgm.rotation_matrix_to_angle_axis returns NaN for 0 rotation, so manually hack it
pose_eft[torch.isnan(pose_eft)] = 0.0
pose_eft = pose_eft.numpy()
beta_eft = np.array(item['parm_shape'])
img_eft = item['imageName']
img_eft = 'train2014/' + img_eft
annot_id_eft = item['annotId']
idx = np.argwhere(new_data_dict['id'] == annot_id_eft)
if len(idx) == 1:
idx = idx[0][0]
img = imgnames[idx]
annot_id = new_data_dict['id'][idx]
center = new_data_dict['center'][idx]
scale = new_data_dict['scale'][idx]
part = new_data_dict['part'][idx]
scale_eft = item['bbox_scale']
center_eft = np.array(item['bbox_center'])
part_eft = np.array(item['gt_keypoint_2d'])
part_eft = part_eft[25:, :] # remove 25 openpose joints
assert img_eft == img
assert annot_id == annot_id_eft
assert scale == scale_eft
assert np.allclose(center, center_eft)
assert np.allclose(part, part_eft)
new_data_dict['pose'][idx] = pose_eft
new_data_dict['shape'][idx] = beta_eft
new_data_dict['has_smpl'][idx] = 1
# print(idx)
elif len(idx) > 1:
print('error: the same id occurs twice!')
exit(1)
else:
out_num += 1
# print('new img !!!')
part_eft = np.array(item['gt_keypoint_2d'])
part_eft = part_eft[25:, :] # remove 25 openpose joints
scale_eft = np.array([item['bbox_scale']])
center_eft = np.array(item['bbox_center'])
new_data_dict['imgname'] = np.concatenate((new_data_dict['imgname'], np.array([img_eft])), axis=0)
new_data_dict['center'] = np.concatenate((new_data_dict['center'], center_eft[None, :]), axis=0)
new_data_dict['scale'] = np.concatenate((new_data_dict['scale'], scale_eft), axis=0)
new_data_dict['part'] = np.concatenate((new_data_dict['part'], part_eft[None, :]), axis=0)
new_data_dict['iuv_names'] = np.concatenate((new_data_dict['iuv_names'], np.array([''])), axis=0)
new_data_dict['pose'] = np.concatenate((new_data_dict['pose'], pose_eft[None, :]), axis=0)
new_data_dict['shape'] = np.concatenate((new_data_dict['shape'], beta_eft[None, :]), axis=0)
new_data_dict['has_smpl'] = np.concatenate((new_data_dict['has_smpl'], np.ones(1)), axis=0)
new_data_dict['id'] = np.concatenate((new_data_dict['id'], np.array([annot_id_eft])), axis=0)
print(out_num)
np.savez(coco_out, **new_data_dict)
print(out_num)
print(out_num)
'''
COCO NPZ: 28344
COCO PART:
28062 in npz, 0 out npz
COCO all:
74834 = 27923 in npz + 46911 out npz
'''