forked from YunzhuLi/DPI-Net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
303 lines (215 loc) · 9.45 KB
/
utils.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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import argparse
import numpy as np
import cv2
import copy
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.collections import PatchCollection
from matplotlib.patches import Rectangle, Circle
import h5py
import os
import torch
from torch.autograd import Variable
def rand_int(lo, hi):
return np.random.randint(lo, hi)
def rand_float(lo, hi):
return np.random.rand() * (hi - lo) + lo
def count_parameters(model):
return sum(p.numel() for p in model.parameters() if p.requires_grad)
def to_variable(tensor, use_gpu, requires_grad=False):
if use_gpu:
return Variable(torch.FloatTensor(tensor).cuda(),
requires_grad=requires_grad)
else:
return Variable(torch.FloatTensor(tensor),
requires_grad=requires_grad)
def visualize_point_clouds(point_clouds, c=['b', 'r'], view=None, store=False, store_path=''):
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_aspect('equal')
frame = plt.gca()
frame.axes.xaxis.set_ticklabels([])
frame.axes.yaxis.set_ticklabels([])
frame.axes.zaxis.set_ticklabels([])
for i in range(len(point_clouds)):
points = point_clouds[i]
ax.scatter(points[:, 0], points[:, 1], points[:, 2], c=c[i], s=10, alpha=0.3)
X, Y, Z = point_clouds[0][:, 0], point_clouds[0][:, 1], point_clouds[0][:, 2]
max_range = np.array([X.max()-X.min(), Y.max()-Y.min(), Z.max()-Z.min()]).max()
Xb = 0.5*max_range*np.mgrid[-1:2:2,-1:2:2,-1:2:2][0].flatten() + 0.5*(X.max()+X.min())
Yb = 0.5*max_range*np.mgrid[-1:2:2,-1:2:2,-1:2:2][1].flatten() + 0.5*(Y.max()+Y.min())
Zb = 0.5*max_range*np.mgrid[-1:2:2,-1:2:2,-1:2:2][2].flatten() + 0.5*(Z.max()+Z.min())
# Comment or uncomment following both lines to test the fake bounding box:
for xb, yb, zb in zip(Xb, Yb, Zb):
ax.plot([xb], [yb], [zb], 'w')
ax.grid(False)
if view is None:
view = 0, 0
ax.view_init(view[0], view[1])
plt.show()
# plt.pause(5)
if store:
fig.savefig(store_path, bbox_inches='tight')
def sample_control_RiceGrip():
dis = np.random.rand() * 0.5
angle = np.random.rand() * np.pi * 2.
x = np.cos(angle) * dis
z = np.sin(angle) * dis
d = np.random.rand() * 0.3 + 0.7 # (0.6, 0.9)
return x, z, d
def sample_control_FluidShake(x_box, time_step, dt):
control = np.zeros(time_step)
v_box = 0.
for step in range(time_step):
control[step] = v_box
x_box += v_box * dt
v_box += rand_float(-0.15, 0.15) - x_box * 0.1
return control
def quatFromAxisAngle(axis, angle):
axis /= np.linalg.norm(axis)
half = angle * 0.5
w = np.cos(half)
sin_theta_over_two = np.sin(half)
axis *= sin_theta_over_two
quat = np.array([axis[0], axis[1], axis[2], w])
return quat
def quatFromAxisAngle_var(axis, angle):
axis /= torch.norm(axis)
half = angle * 0.5
w = torch.cos(half)
sin_theta_over_two = torch.sin(half)
axis *= sin_theta_over_two
quat = torch.cat([axis, w])
# print("quat size", quat.size())
return quat
def calc_shape_states_RiceGrip(t, dt, shape_state_dim, gripper_config):
rest_gripper_dis = 1.8
x, z, d = gripper_config
s = (rest_gripper_dis - d) / 2.
half_rest_gripper_dis = rest_gripper_dis / 2.
time = max(0., t) * 5
lastTime = max(0., t - dt) * 5
states = np.zeros((2, shape_state_dim))
dis = np.sqrt(x**2 + z**2)
angle = np.array([-z / dis, x / dis])
quat = quatFromAxisAngle(np.array([0., 1., 0.]), np.arctan(x / z))
e_0 = np.array([x + z * half_rest_gripper_dis / dis, z - x * half_rest_gripper_dis / dis])
e_1 = np.array([x - z * half_rest_gripper_dis / dis, z + x * half_rest_gripper_dis / dis])
e_0_curr = e_0 + angle * np.sin(time) * s
e_1_curr = e_1 - angle * np.sin(time) * s
e_0_last = e_0 + angle * np.sin(lastTime) * s
e_1_last = e_1 - angle * np.sin(lastTime) * s
states[0, :3] = np.array([e_0_curr[0], 0.6, e_0_curr[1]])
states[0, 3:6] = np.array([e_0_last[0], 0.6, e_0_last[1]])
states[0, 6:10] = quat
states[0, 10:14] = quat
states[1, :3] = np.array([e_1_curr[0], 0.6, e_1_curr[1]])
states[1, 3:6] = np.array([e_1_last[0], 0.6, e_1_last[1]])
states[1, 6:10] = quat
states[1, 10:14] = quat
return states
def calc_shape_states_RiceGrip_var(t, dt, gripper_config):
rest_gripper_dis = Variable(torch.FloatTensor([1.8]).cuda())
x, z, d = gripper_config[0:1], gripper_config[1:2], gripper_config[2:3]
s = (rest_gripper_dis - d) / 2.
half_rest_gripper_dis = rest_gripper_dis / 2.
time = max(0., t) * 5
lastTime = max(0., t - dt) * 5
dis = torch.sqrt(x**2 + z**2)
angle = torch.cat([-z / dis, x / dis])
quat = quatFromAxisAngle_var(Variable(torch.FloatTensor([0., 1., 0.]).cuda()), torch.atan(x / z))
e_0 = torch.cat([x + z * half_rest_gripper_dis / dis, z - x * half_rest_gripper_dis / dis])
e_1 = torch.cat([x - z * half_rest_gripper_dis / dis, z + x * half_rest_gripper_dis / dis])
e_0_curr = e_0 + angle * np.sin(time) * s
e_1_curr = e_1 - angle * np.sin(time) * s
e_0_last = e_0 + angle * np.sin(lastTime) * s
e_1_last = e_1 - angle * np.sin(lastTime) * s
y = Variable(torch.FloatTensor([0.6]).cuda())
states_0 = torch.cat([e_0_curr[0:1], y, e_0_curr[1:2], e_0_last[0:1], y, e_0_last[1:2], quat, quat])
states_1 = torch.cat([e_1_curr[0:1], y, e_1_curr[1:2], e_1_last[0:1], y, e_1_last[1:2], quat, quat])
# print(states_0.requires_grad, states_1.requires_grad)
# print("gripper #0:", states_0.size())
# print("gripper #1:", states_1.size())
return torch.cat([states_0.view(1, -1), states_1.view(1, -1)], 0)
def calc_box_init_FluidShake(dis_x, dis_z, height, border):
center = np.array([0., 0., 0.])
quat = np.array([1., 0., 0., 0.])
boxes = []
# floor
halfEdge = np.array([dis_x/2., border/2., dis_z/2.])
boxes.append([halfEdge, center, quat])
# left wall
halfEdge = np.array([border/2., (height+border)/2., dis_z/2.])
boxes.append([halfEdge, center, quat])
# right wall
boxes.append([halfEdge, center, quat])
# back wall
halfEdge = np.array([(dis_x+border*2)/2., (height+border)/2., border/2.])
boxes.append([halfEdge, center, quat])
# front wall
boxes.append([halfEdge, center, quat])
return boxes
def calc_shape_states_FluidShake(x_curr, x_last, box_dis, height, border):
dis_x, dis_z = box_dis
quat = np.array([1., 0., 0., 0.])
states = np.zeros((5, 14))
states[0, :3] = np.array([x_curr, border/2., 0.])
states[0, 3:6] = np.array([x_last, border/2., 0.])
states[1, :3] = np.array([x_curr-(dis_x+border)/2., (height+border)/2., 0.])
states[1, 3:6] = np.array([x_last-(dis_x+border)/2., (height+border)/2., 0.])
states[2, :3] = np.array([x_curr+(dis_x+border)/2., (height+border)/2., 0.])
states[2, 3:6] = np.array([x_last+(dis_x+border)/2., (height+border)/2., 0.])
states[3, :3] = np.array([x_curr, (height+border)/2., -(dis_z+border)/2.])
states[3, 3:6] = np.array([x_last, (height+border)/2., -(dis_z+border)/2.])
states[4, :3] = np.array([x_curr, (height+border)/2., (dis_z+border)/2.])
states[4, 3:6] = np.array([x_last, (height+border)/2., (dis_z+border)/2.])
states[:, 6:10] = quat
states[:, 10:] = quat
return states
def calc_shape_states_FluidShake_var(x_curr, x_last, box_dis, height, border):
dis_x, dis_z = box_dis
dis_x = Variable(torch.FloatTensor([dis_x]).cuda())
dis_z = Variable(torch.FloatTensor([dis_z]).cuda())
height = Variable(torch.FloatTensor([height]).cuda())
border = Variable(torch.FloatTensor([border]).cuda())
zero = Variable(torch.FloatTensor([0.]).cuda())
quat = Variable(torch.FloatTensor([1., 0., 0., 0.]).cuda())
state_0 = torch.cat([
x_curr, border/2., zero, x_last, border/2., zero, quat, quat]).view(1, -1)
state_1 = torch.cat([
x_curr-(dis_x+border)/2., (height+border)/2., zero,
x_last-(dis_x+border)/2., (height+border)/2., zero,
quat, quat]).view(1, -1)
state_2 = torch.cat([
x_curr+(dis_x+border)/2., (height+border)/2., zero,
x_last+(dis_x+border)/2., (height+border)/2., zero,
quat, quat]).view(1, -1)
state_3 = torch.cat([
x_curr, (height+border)/2., -(dis_z+border)/2.,
x_last, (height+border)/2., -(dis_z+border)/2.,
quat, quat]).view(1, -1)
state_4 = torch.cat([
x_curr, (height+border)/2., (dis_z+border)/2.,
x_last, (height+border)/2., (dis_z+border)/2.,
quat, quat]).view(1, -1)
states = torch.cat([state_0, state_1, state_2, state_3, state_4], 0)
# print("states size", states.size())
return states
class ChamferLoss(torch.nn.Module):
def __init__(self):
super(ChamferLoss, self).__init__()
def chamfer_distance(self, x, y):
# x: [N, D]
# y: [M, D]
x = x.repeat(y.size(0), 1, 1) # x: [M, N, D]
x = x.transpose(0, 1) # x: [N, M, D]
y = y.repeat(x.size(0), 1, 1) # y: [N, M, D]
dis = torch.norm(torch.add(x, -y), 2, dim=2) # dis: [N, M]
dis_xy = torch.mean(torch.min(dis, dim=1)[0]) # dis_xy: mean over N
dis_yx = torch.mean(torch.min(dis, dim=0)[0]) # dis_yx: mean over M
return dis_xy + dis_yx
def __call__(self, pred, label):
return self.chamfer_distance(pred, label)