-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcontact_losses.py
187 lines (130 loc) · 5.09 KB
/
contact_losses.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
"""
Contact Losses
"""
import torch
import ChamferDistancePytorch.chamfer3D.dist_chamfer_3D as cham
import numpy as np
from global_vars import *
def vectorize_inds(inp, jump):
out = []
for i in range(len(inp)):
if len(inp[i]) == 0:
continue
else:
out.append(inp[i] + (i * jump))
if len(out) >0:
out = np.concatenate(out)
return out
def map_int_to_inds():
get_bin = lambda x, n: format(x, 'b').zfill(n)
verts_dict = get_verts_dict()
main_arr = []
for i in range(32):
arr = []
bin_num = get_bin(i, 5)
if int(bin_num[0]) == 1:
arr.append(verts_dict['back'])
if int(bin_num[1]) == 1:
arr.append(verts_dict['left_toe'])
if int(bin_num[2]) == 1:
arr.append(verts_dict['left_heel'])
if int(bin_num[3]) == 1:
arr.append(verts_dict['right_toe'])
if int(bin_num[4]) == 1:
arr.append(verts_dict['right_heel'])
if len(arr) > 0:
arr = np.concatenate(arr, axis = 0)
main_arr.append(arr)
return np.array(main_arr)
def map_contacts_to_num(right_heel, right_toe, left_heel, left_toe, back):
end = back.shape[0]
arr = map_int_to_inds()
nums = 16 * back + 8* left_toe[0:end] + 4* left_heel[0:end] + 2* right_toe[0:end] + right_heel[0:end]
nums = np.array(nums).astype(int)
return arr[nums]
def map_int_to_inds_feet():
get_bin = lambda x, n: format(x, 'b').zfill(n)
verts_dict = get_verts_dict()
main_arr = []
for i in range(16):
arr = []
bin_num = get_bin(i, 4)
if int(bin_num[0]) == 1:
arr.append(verts_dict['left_toe'])
if int(bin_num[1]) == 1:
arr.append(verts_dict['left_heel'])
if int(bin_num[2]) == 1:
arr.append(verts_dict['right_toe'])
if int(bin_num[3]) == 1:
arr.append(verts_dict['right_heel'])
if len(arr) > 0:
arr = np.concatenate(arr, axis = 0)
main_arr.append(arr)
return np.array(main_arr)
def map_contacts_to_num_feet(right_heel, right_toe, left_heel, left_toe):
arr = map_int_to_inds()
nums = 8* left_toe + 4* left_heel + 2* right_toe + right_heel
nums = np.array(nums).astype(int)
return arr[nums]
def get_foot_frames(file_name):
foot_contacts = get_foot_contacts(file_name)
frame_verts = map_contacts_to_num_feet(
foot_contacts['right_heel'],
foot_contacts['right_toe'],
foot_contacts['left_heel'],
foot_contacts['left_toe'],
)
return frame_verts
def get_foot_vel_frames(file_name):
foot_contacts = get_foot_contacts(file_name)
vel_conts = {}
for stra in feet_dict:
vel_conts[stra] = map_contacts_to_vels(foot_contacts[stra])
frame_verts = map_contacts_to_num_feet(
foot_contacts['right_heel'],
foot_contacts['right_toe'],
foot_contacts['left_heel'],
foot_contacts['left_toe'],
)
return frame_verts
def map_contacts_to_vels(contact):
new_contact1 = contact * np.roll(contact, -1)
new_contact1[len(contact) - 1] = 0
return new_contact1[0:len(contact) - 1]
def contact_constraint(scene_verts, smpl_verts, frame_verts, ):
distChamfer = cham.chamfer_3DDist()
smpl_verts = smpl_verts.reshape(-1, 3)
verts = smpl_verts[ frame_verts, :]
verts = verts.reshape(-1,3).unsqueeze(0)
temp_loss, _, _, _ = distChamfer(
verts.contiguous(), scene_verts.cuda())
loss = torch.mean(temp_loss)
return loss
def sit_contact_constraint(scene_verts, smpl_verts, indices, back_verts):
distChamfer = cham.chamfer_3DDist()
valid_verts = smpl_verts[indices][:, back_verts, :]
verts = valid_verts.reshape(-1,3).unsqueeze(0)
temp_loss, _, _, _ = distChamfer(
verts.contiguous(), scene_verts.cuda())
loss = torch.mean(temp_loss)
return loss
def velocity_constraint(smpl_verts, frame_verts):
vertex_diffs = smpl_verts[:-1] - smpl_verts[1:]
vertex_diffs = vertex_diffs.reshape(-1, 3)
valid_vertex_diffs = vertex_diffs[frame_verts, :]
normed_vertex_diffs = torch.norm(valid_vertex_diffs, p = 2, dim = 1)
loss = torch.mean(normed_vertex_diffs)
return loss
def sit_vel_constraint(smpl_verts, indices, back_verts):
vertex_diffs = smpl_verts[:-1] - smpl_verts[1:]
valid_vertex_diffs = vertex_diffs[indices, :,:][:, back_verts , :]
normed_vertex_diffs = torch.norm(valid_vertex_diffs, p = 2, dim = 1)
loss = torch.mean(normed_vertex_diffs)
return loss
def contacts2indices(contacts):
indices = np.array(range(1, contacts.shape[0]+1))
indices_valid = indices * contacts
indices_valid = indices_valid
indices_valid = indices_valid[indices_valid > 0 ]
indices_valid = indices_valid - 1
return indices_valid