-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_demonstrations.py
194 lines (168 loc) · 9.42 KB
/
generate_demonstrations.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
'''
This script has been adapted from Kai Mo's scripts for Foldsformer, along with some additional changes to extend its functionality
'''
import argparse
import numpy as np
from utils.visual import get_pixel_coord_from_world
import pyflex
import os
from tqdm import tqdm
import imageio
import pickle
from utils.visual import action_viz
from softgym.envs.foldenv import FoldEnv
from Demonstrator.demonstrator import Demonstrator
def main():
parser = argparse.ArgumentParser(description="Generate Demonstrations")
parser.add_argument("--gui", action="store_true", help="Run headless or not")
parser.add_argument("--task", type=str, default="DoubleTriangle", help="Task name")
parser.add_argument("--img_size", type=int, default=128, help="Size of rendered image")
parser.add_argument("--cached", type=str, help="Cached filename")
args = parser.parse_args()
# env settings
cached_path = os.path.join("cached configs", args.cached + ".pkl")
env = FoldEnv(cached_path, gui=args.gui, render_dim=args.img_size)
# demonstrator settings
demonstrator = Demonstrator[args.task]()
# save settings
save_path = os.path.join("data", "demonstrations", args.task, args.cached)
os.makedirs(save_path, exist_ok=True)
# other settings
rgb_shape = (args.img_size, args.img_size)
num_data = env.num_configs
for config_id in tqdm(range(num_data)):
# folders
save_folder = os.path.join(save_path, str(config_id))
save_folder_rgb = os.path.join(save_folder, "rgb")
save_folder_depth = os.path.join(save_folder, "depth")
os.makedirs(save_folder, exist_ok=True)
os.makedirs(save_folder_rgb, exist_ok=True)
os.makedirs(save_folder_depth, exist_ok=True)
pick_pixels = []
place_pixels = []
rgbs = []
# env reset
env.reset(config_id=config_id)
camera_params = env.camera_params
rgb, depth = env.render_image()
imageio.imwrite(os.path.join(save_folder_rgb, str(0) + ".png"), rgb)
depth = depth * 255
depth = depth.astype(np.uint8)
imageio.imwrite(os.path.join(save_folder_depth, str(0) + ".png"), depth)
rgbs.append(rgb)
if args.task == "DoubleTriangle":
pick_idxs_comb = demonstrator.pick_idxs
for ind, pick_idxs in enumerate(pick_idxs_comb):
for i, pick_idx in enumerate(pick_idxs):
curr_corners = env.get_corners()
pick_pos, place_pos = demonstrator.get_action(curr_corners, pick_idx)
pick_pixel = get_pixel_coord_from_world(pick_pos, rgb_shape, camera_params)
place_pixel = get_pixel_coord_from_world(place_pos, rgb_shape, camera_params)
env.pick_and_place(pick_pos.copy(), place_pos.copy())
rgb, depth = env.render_image()
# save
pick_pixels.append(pick_pixel)
place_pixels.append(place_pixel)
imageio.imwrite(os.path.join(save_folder_rgb, str(i + 1) + '-' + str(ind) + ".png"), rgb)
depth = depth * 255
depth = depth.astype(np.uint8)
imageio.imwrite(os.path.join(save_folder_depth, str(i + 1) + '-' + str(ind) + ".png"), depth)
rgbs.append(rgb)
# Saving the particle positions for each of the different possible configurations
particle_pos = pyflex.get_positions().reshape(-1, 4)[:, :3]
with open(os.path.join(save_folder, "info-" + str(ind) + ".pkl"), "wb+") as f:
data = {"pick": pick_pixels, "place": place_pixels, "pos": particle_pos}
pickle.dump(data, f)
env.reset(config_id=config_id)
elif args.task == "AllCornersInward":
pick_idxs = np.arange(4)
curr_corners = env.get_corners()
center = env.get_center()
for ind in range(9):
for (i, pick_idx) in enumerate(pick_idxs):
center_place = False
if ind == 8:
center_place = True
pick_pos, place_pos = demonstrator.get_action(curr_corners, center, pick_idx, center_place)
pick_pixel = get_pixel_coord_from_world(pick_pos, rgb_shape, camera_params)
place_pixel = get_pixel_coord_from_world(place_pos, rgb_shape, camera_params)
env.pick_and_place(pick_pos.copy(), place_pos.copy())
rgb, depth = env.render_image()
# save
pick_pixels.append(pick_pixel)
place_pixels.append(place_pixel)
imageio.imwrite(os.path.join(save_folder_rgb, str(i + 1) + '-' + str(ind) + ".png"), rgb)
depth = depth * 255
depth = depth.astype(np.uint8)
imageio.imwrite(os.path.join(save_folder_depth, str(i + 1) + '-' + str(ind) + ".png"), depth)
rgbs.append(rgb)
# Saving the particle positions for each of the different possible configurations
particle_pos = pyflex.get_positions().reshape(-1, 4)[:, :3]
with open(os.path.join(save_folder, "info-" + str(ind) + ".pkl"), "wb+") as f:
data = {"pick": pick_pixels, "place": place_pixels, "pos": particle_pos}
pickle.dump(data, f)
env.reset(config_id=config_id)
elif args.task == "CornersEdgesInward":
center = env.get_center()
pickplace_idx_comb = demonstrator.pickplace_idxs
for (ind, pickplace_idxs) in enumerate(pickplace_idx_comb):
for (i, pickplace_idx) in enumerate(pickplace_idxs):
curr_corners = env.get_corners()
edge_middles = env.get_edge_middles()
pick_pos, place_pos = demonstrator.get_action(curr_corners, edge_middles, center, pickplace_idx)
pick_pixel = get_pixel_coord_from_world(pick_pos, rgb_shape, camera_params)
place_pixel = get_pixel_coord_from_world(place_pos, rgb_shape, camera_params)
env.pick_and_place(pick_pos.copy(), place_pos.copy())
rgb, depth = env.render_image()
# save
pick_pixels.append(pick_pixel)
place_pixels.append(place_pixel)
imageio.imwrite(os.path.join(save_folder_rgb, str(i + 1) + '-' + str(ind) + ".png"), rgb)
depth = depth * 255
depth = depth.astype(np.uint8)
imageio.imwrite(os.path.join(save_folder_depth, str(i + 1) + '-' + str(ind) + ".png"), depth)
rgbs.append(rgb)
# Saving the particle positions for each of the different possible configurations
particle_pos = pyflex.get_positions().reshape(-1, 4)[:, :3]
with open(os.path.join(save_folder, "info-" + str(ind) + ".pkl"), "wb+") as f:
data = {"pick": pick_pixels, "place": place_pixels, "pos": particle_pos}
pickle.dump(data, f)
env.reset(config_id=config_id)
elif args.task == "DoubleStraight":
pickplace_idx_comb = demonstrator.pickplace_idxs
for (ind, pickplace_idxs) in enumerate(pickplace_idx_comb):
for (i, pickplace_idx) in enumerate(pickplace_idxs):
curr_corners = env.get_corners()
edge_middles = env.get_edge_middles()
pick_pos, place_pos = demonstrator.get_action(curr_corners, edge_middles, pickplace_idx)
pick_pixel = get_pixel_coord_from_world(pick_pos, rgb_shape, camera_params)
place_pixel = get_pixel_coord_from_world(place_pos, rgb_shape, camera_params)
env.pick_and_place(pick_pos.copy(), place_pos.copy())
rgb, depth = env.render_image()
# save
pick_pixels.append(pick_pixel)
place_pixels.append(place_pixel)
imageio.imwrite(os.path.join(save_folder_rgb, str(i + 1) + '-' + str(ind) + ".png"), rgb)
depth = depth * 255
depth = depth.astype(np.uint8)
imageio.imwrite(os.path.join(save_folder_depth, str(i + 1) + '-' + str(ind) + ".png"), depth)
rgbs.append(rgb)
# Saving the particle positions for each of the different possible configurations
particle_pos = pyflex.get_positions().reshape(-1, 4)[:, :3]
with open(os.path.join(save_folder, "info-" + str(ind) + ".pkl"), "wb+") as f:
data = {"pick": pick_pixels, "place": place_pixels, "pos": particle_pos}
pickle.dump(data, f)
env.reset(config_id=config_id)
## Commenting the action visualization saving for now
# # action viz
# save_folder_viz = os.path.join(save_folder, "rgbviz")
# os.makedirs(save_folder_viz, exist_ok=True)
# num_actions = len(pick_pixels)
# for i in range(num_actions + 1):
# if i < num_actions:
# img = action_viz(rgbs[i], pick_pixels[i], place_pixels[i])
# else:
# img = rgbs[i]
# imageio.imwrite(os.path.join(save_folder_viz, str(i) + ".png"), img)
if __name__ == "__main__":
main()