-
Notifications
You must be signed in to change notification settings - Fork 22
/
generate_data_parallel.py
202 lines (164 loc) · 7.26 KB
/
generate_data_parallel.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
import argparse
from pathlib import Path
import numpy as np
import open3d as o3d
import scipy.signal as signal
from tqdm import tqdm
import multiprocessing as mp
from vgn.grasp import Grasp, Label
from vgn.io import *
from vgn.perception import *
from vgn.simulation import ClutterRemovalSim
from vgn.utils.transform import Rotation, Transform
from vgn.utils.implicit import get_mesh_pose_list_from_world
OBJECT_COUNT_LAMBDA = 4
MAX_VIEWPOINT_COUNT = 6
def main(args, rank):
GRASPS_PER_SCENE = args.grasps_per_scene
np.random.seed()
seed = np.random.randint(0, 1000) + rank
np.random.seed(seed)
sim = ClutterRemovalSim(args.scene, args.object_set, gui=args.sim_gui)
finger_depth = sim.gripper.finger_depth
grasps_per_worker = args.num_grasps // args.num_proc
pbar = tqdm(total=grasps_per_worker, disable=rank != 0)
if rank == 0:
(args.root / "scenes").mkdir(parents=True)
write_setup(
args.root,
sim.size,
sim.camera.intrinsic,
sim.gripper.max_opening_width,
sim.gripper.finger_depth,
)
if args.save_scene:
(args.root / "mesh_pose_list").mkdir(parents=True)
for _ in range(grasps_per_worker // GRASPS_PER_SCENE):
# generate heap
object_count = np.random.poisson(OBJECT_COUNT_LAMBDA) + 1
sim.reset(object_count)
sim.save_state()
# render synthetic depth images
n = MAX_VIEWPOINT_COUNT
depth_imgs, extrinsics = render_images(sim, n)
depth_imgs_side, extrinsics_side = render_side_images(sim, 1, args.random)
# reconstrct point cloud using a subset of the images
tsdf = create_tsdf(sim.size, 120, depth_imgs, sim.camera.intrinsic, extrinsics)
pc = tsdf.get_cloud()
# crop surface and borders from point cloud
bounding_box = o3d.geometry.AxisAlignedBoundingBox(sim.lower, sim.upper)
pc = pc.crop(bounding_box)
# o3d.visualization.draw_geometries([pc])
if pc.is_empty():
print("Point cloud empty, skipping scene")
continue
# store the raw data
scene_id = write_sensor_data(args.root, depth_imgs_side, extrinsics_side)
if args.save_scene:
mesh_pose_list = get_mesh_pose_list_from_world(sim.world, args.object_set)
write_point_cloud(args.root, scene_id, mesh_pose_list, name="mesh_pose_list")
for _ in range(GRASPS_PER_SCENE):
# sample and evaluate a grasp point
point, normal = sample_grasp_point(pc, finger_depth)
grasp, label = evaluate_grasp_point(sim, point, normal)
# store the sample
write_grasp(args.root, scene_id, grasp, label)
pbar.update()
pbar.close()
print('Process %d finished!' % rank)
def render_images(sim, n):
height, width = sim.camera.intrinsic.height, sim.camera.intrinsic.width
origin = Transform(Rotation.identity(), np.r_[sim.size / 2, sim.size / 2, 0.0])
extrinsics = np.empty((n, 7), np.float32)
depth_imgs = np.empty((n, height, width), np.float32)
for i in range(n):
r = np.random.uniform(1.6, 2.4) * sim.size
theta = np.random.uniform(0.0, np.pi / 4.0)
phi = np.random.uniform(0.0, 2.0 * np.pi)
extrinsic = camera_on_sphere(origin, r, theta, phi)
depth_img = sim.camera.render(extrinsic)[1]
extrinsics[i] = extrinsic.to_list()
depth_imgs[i] = depth_img
return depth_imgs, extrinsics
def render_side_images(sim, n=1, random=False):
height, width = sim.camera.intrinsic.height, sim.camera.intrinsic.width
origin = Transform(Rotation.identity(), np.r_[sim.size / 2, sim.size / 2, sim.size / 3])
extrinsics = np.empty((n, 7), np.float32)
depth_imgs = np.empty((n, height, width), np.float32)
for i in range(n):
if random:
r = np.random.uniform(1.6, 2.4) * sim.size
theta = np.random.uniform(np.pi / 4.0, 5.0 * np.pi / 12.0)
phi = np.random.uniform(- 5.0 * np.pi / 5, - 3.0 * np.pi / 8.0)
else:
r = 2 * sim.size
theta = np.pi / 3.0
phi = - np.pi / 2.0
extrinsic = camera_on_sphere(origin, r, theta, phi)
depth_img = sim.camera.render(extrinsic)[1]
extrinsics[i] = extrinsic.to_list()
depth_imgs[i] = depth_img
return depth_imgs, extrinsics
def sample_grasp_point(point_cloud, finger_depth, eps=0.1):
points = np.asarray(point_cloud.points)
normals = np.asarray(point_cloud.normals)
ok = False
while not ok:
# TODO this could result in an infinite loop, though very unlikely
idx = np.random.randint(len(points))
point, normal = points[idx], normals[idx]
ok = normal[2] > -0.1 # make sure the normal is poitning upwards
grasp_depth = np.random.uniform(-eps * finger_depth, (1.0 + eps) * finger_depth)
point = point + normal * grasp_depth
return point, normal
def evaluate_grasp_point(sim, pos, normal, num_rotations=6):
# define initial grasp frame on object surface
z_axis = -normal
x_axis = np.r_[1.0, 0.0, 0.0]
if np.isclose(np.abs(np.dot(x_axis, z_axis)), 1.0, 1e-4):
x_axis = np.r_[0.0, 1.0, 0.0]
y_axis = np.cross(z_axis, x_axis)
x_axis = np.cross(y_axis, z_axis)
R = Rotation.from_matrix(np.vstack((x_axis, y_axis, z_axis)).T)
# try to grasp with different yaw angles
yaws = np.linspace(0.0, np.pi, num_rotations)
outcomes, widths = [], []
for yaw in yaws:
ori = R * Rotation.from_euler("z", yaw)
sim.restore_state()
candidate = Grasp(Transform(ori, pos), width=sim.gripper.max_opening_width)
outcome, width = sim.execute_grasp(candidate, remove=False)
outcomes.append(outcome)
widths.append(width)
# detect mid-point of widest peak of successful yaw angles
# TODO currently this does not properly handle periodicity
successes = (np.asarray(outcomes) == Label.SUCCESS).astype(float)
if np.sum(successes):
peaks, properties = signal.find_peaks(
x=np.r_[0, successes, 0], height=1, width=1
)
idx_of_widest_peak = peaks[np.argmax(properties["widths"])] - 1
ori = R * Rotation.from_euler("z", yaws[idx_of_widest_peak])
width = widths[idx_of_widest_peak]
return Grasp(Transform(ori, pos), width), int(np.max(outcomes))
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("root", type=Path)
parser.add_argument("--scene", type=str, choices=["pile", "packed"], default="pile")
parser.add_argument("--object-set", type=str, default="blocks")
parser.add_argument("--num-grasps", type=int, default=10000)
parser.add_argument("--grasps-per-scene", type=int, default=120)
parser.add_argument("--num-proc", type=int, default=1)
parser.add_argument("--save-scene", action="store_true")
parser.add_argument("--random", action="store_true", help="Add distrubation to camera pose")
parser.add_argument("--sim-gui", action="store_true")
args = parser.parse_args()
args.save_scene = True
if args.num_proc > 1:
pool = mp.Pool(processes=args.num_proc)
for i in range(args.num_proc):
pool.apply_async(func=main, args=(args, i))
pool.close()
pool.join()
else:
main(args, 0)