-
Notifications
You must be signed in to change notification settings - Fork 268
/
Copy pathutils.py
317 lines (290 loc) · 15.9 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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import importlib
import pickle
from os import listdir
from os.path import join, exists
from typing import List
import numpy as np
from PIL import Image
from natsort import natsorted
from pyrep.objects import VisionSensor
from rlbench.backend.const import *
from rlbench.backend.utils import image_to_float_array, rgb_handles_to_mask
from rlbench.demo import Demo
from rlbench.observation_config import ObservationConfig
class InvalidTaskName(Exception):
pass
def name_to_task_class(task_file: str):
name = task_file.replace('.py', '')
class_name = ''.join([w[0].upper() + w[1:] for w in name.split('_')])
try:
mod = importlib.import_module("rlbench.tasks.%s" % name)
mod = importlib.reload(mod)
except ModuleNotFoundError as e:
raise InvalidTaskName(
"The task file '%s' does not exist or cannot be compiled."
% name) from e
try:
task_class = getattr(mod, class_name)
except AttributeError as e:
raise InvalidTaskName(
"Cannot find the class name '%s' in the file '%s'."
% (class_name, name)) from e
return task_class
def get_stored_demos(amount: int, image_paths: bool, dataset_root: str,
variation_number: int, task_name: str,
obs_config: ObservationConfig,
random_selection: bool = True,
from_episode_number: int = 0) -> List[Demo]:
task_root = join(dataset_root, task_name)
if not exists(task_root):
raise RuntimeError("Can't find the demos for %s at: %s" % (
task_name, task_root))
# Sample an amount of examples for the variation of this task
examples_path = join(
task_root, VARIATIONS_FOLDER % variation_number,
EPISODES_FOLDER)
examples = listdir(examples_path)
if amount == -1:
amount = len(examples)
if amount > len(examples):
raise RuntimeError(
'You asked for %d examples, but only %d were available.' % (
amount, len(examples)))
if random_selection:
selected_examples = np.random.choice(examples, amount, replace=False)
else:
selected_examples = natsorted(
examples)[from_episode_number:from_episode_number+amount]
# Process these examples (e.g. loading observations)
demos = []
for example in selected_examples:
example_path = join(examples_path, example)
with open(join(example_path, LOW_DIM_PICKLE), 'rb') as f:
obs = pickle.load(f)
l_sh_rgb_f = join(example_path, LEFT_SHOULDER_RGB_FOLDER)
l_sh_depth_f = join(example_path, LEFT_SHOULDER_DEPTH_FOLDER)
l_sh_mask_f = join(example_path, LEFT_SHOULDER_MASK_FOLDER)
r_sh_rgb_f = join(example_path, RIGHT_SHOULDER_RGB_FOLDER)
r_sh_depth_f = join(example_path, RIGHT_SHOULDER_DEPTH_FOLDER)
r_sh_mask_f = join(example_path, RIGHT_SHOULDER_MASK_FOLDER)
oh_rgb_f = join(example_path, OVERHEAD_RGB_FOLDER)
oh_depth_f = join(example_path, OVERHEAD_DEPTH_FOLDER)
oh_mask_f = join(example_path, OVERHEAD_MASK_FOLDER)
wrist_rgb_f = join(example_path, WRIST_RGB_FOLDER)
wrist_depth_f = join(example_path, WRIST_DEPTH_FOLDER)
wrist_mask_f = join(example_path, WRIST_MASK_FOLDER)
front_rgb_f = join(example_path, FRONT_RGB_FOLDER)
front_depth_f = join(example_path, FRONT_DEPTH_FOLDER)
front_mask_f = join(example_path, FRONT_MASK_FOLDER)
num_steps = len(obs)
if not (num_steps == len(listdir(l_sh_rgb_f)) == len(
listdir(l_sh_depth_f)) == len(listdir(r_sh_rgb_f)) == len(
listdir(r_sh_depth_f)) == len(listdir(oh_rgb_f)) == len(
listdir(oh_depth_f)) == len(listdir(wrist_rgb_f)) == len(
listdir(wrist_depth_f)) == len(listdir(front_rgb_f)) == len(
listdir(front_depth_f))):
raise RuntimeError('Broken dataset assumption')
for i in range(num_steps):
si = IMAGE_FORMAT % i
if obs_config.left_shoulder_camera.rgb:
obs[i].left_shoulder_rgb = join(l_sh_rgb_f, si)
if obs_config.left_shoulder_camera.depth or obs_config.left_shoulder_camera.point_cloud:
obs[i].left_shoulder_depth = join(l_sh_depth_f, si)
if obs_config.left_shoulder_camera.mask:
obs[i].left_shoulder_mask = join(l_sh_mask_f, si)
if obs_config.right_shoulder_camera.rgb:
obs[i].right_shoulder_rgb = join(r_sh_rgb_f, si)
if obs_config.right_shoulder_camera.depth or obs_config.right_shoulder_camera.point_cloud:
obs[i].right_shoulder_depth = join(r_sh_depth_f, si)
if obs_config.right_shoulder_camera.mask:
obs[i].right_shoulder_mask = join(r_sh_mask_f, si)
if obs_config.overhead_camera.rgb:
obs[i].overhead_rgb = join(oh_rgb_f, si)
if obs_config.overhead_camera.depth or obs_config.overhead_camera.point_cloud:
obs[i].overhead_depth = join(oh_depth_f, si)
if obs_config.overhead_camera.mask:
obs[i].overhead_mask = join(oh_mask_f, si)
if obs_config.wrist_camera.rgb:
obs[i].wrist_rgb = join(wrist_rgb_f, si)
if obs_config.wrist_camera.depth or obs_config.wrist_camera.point_cloud:
obs[i].wrist_depth = join(wrist_depth_f, si)
if obs_config.wrist_camera.mask:
obs[i].wrist_mask = join(wrist_mask_f, si)
if obs_config.front_camera.rgb:
obs[i].front_rgb = join(front_rgb_f, si)
if obs_config.front_camera.depth or obs_config.front_camera.point_cloud:
obs[i].front_depth = join(front_depth_f, si)
if obs_config.front_camera.mask:
obs[i].front_mask = join(front_mask_f, si)
# Remove low dim info if necessary
if not obs_config.joint_velocities:
obs[i].joint_velocities = None
if not obs_config.joint_positions:
obs[i].joint_positions = None
if not obs_config.joint_forces:
obs[i].joint_forces = None
if not obs_config.gripper_open:
obs[i].gripper_open = None
if not obs_config.gripper_pose:
obs[i].gripper_pose = None
if not obs_config.gripper_joint_positions:
obs[i].gripper_joint_positions = None
if not obs_config.gripper_touch_forces:
obs[i].gripper_touch_forces = None
if not obs_config.task_low_dim_state:
obs[i].task_low_dim_state = None
if not image_paths:
for i in range(num_steps):
if obs_config.left_shoulder_camera.rgb:
obs[i].left_shoulder_rgb = np.array(
_resize_if_needed(
Image.open(obs[i].left_shoulder_rgb),
obs_config.left_shoulder_camera.image_size))
if obs_config.right_shoulder_camera.rgb:
obs[i].right_shoulder_rgb = np.array(
_resize_if_needed(Image.open(
obs[i].right_shoulder_rgb),
obs_config.right_shoulder_camera.image_size))
if obs_config.overhead_camera.rgb:
obs[i].overhead_rgb = np.array(
_resize_if_needed(Image.open(
obs[i].overhead_rgb),
obs_config.overhead_camera.image_size))
if obs_config.wrist_camera.rgb:
obs[i].wrist_rgb = np.array(
_resize_if_needed(
Image.open(obs[i].wrist_rgb),
obs_config.wrist_camera.image_size))
if obs_config.front_camera.rgb:
obs[i].front_rgb = np.array(
_resize_if_needed(
Image.open(obs[i].front_rgb),
obs_config.front_camera.image_size))
if obs_config.left_shoulder_camera.depth or obs_config.left_shoulder_camera.point_cloud:
l_sh_depth = image_to_float_array(
_resize_if_needed(
Image.open(obs[i].left_shoulder_depth),
obs_config.left_shoulder_camera.image_size),
DEPTH_SCALE)
near = obs[i].misc['left_shoulder_camera_near']
far = obs[i].misc['left_shoulder_camera_far']
l_sh_depth_m = near + l_sh_depth * (far - near)
if obs_config.left_shoulder_camera.depth:
d = l_sh_depth_m if obs_config.left_shoulder_camera.depth_in_meters else l_sh_depth
obs[i].left_shoulder_depth = obs_config.left_shoulder_camera.depth_noise.apply(d)
else:
obs[i].left_shoulder_depth = None
if obs_config.right_shoulder_camera.depth or obs_config.right_shoulder_camera.point_cloud:
r_sh_depth = image_to_float_array(
_resize_if_needed(
Image.open(obs[i].right_shoulder_depth),
obs_config.right_shoulder_camera.image_size),
DEPTH_SCALE)
near = obs[i].misc['right_shoulder_camera_near']
far = obs[i].misc['right_shoulder_camera_far']
r_sh_depth_m = near + r_sh_depth * (far - near)
if obs_config.right_shoulder_camera.depth:
d = r_sh_depth_m if obs_config.right_shoulder_camera.depth_in_meters else r_sh_depth
obs[i].right_shoulder_depth = obs_config.right_shoulder_camera.depth_noise.apply(d)
else:
obs[i].right_shoulder_depth = None
if obs_config.overhead_camera.depth or obs_config.overhead_camera.point_cloud:
oh_depth = image_to_float_array(
_resize_if_needed(
Image.open(obs[i].overhead_depth),
obs_config.overhead_camera.image_size),
DEPTH_SCALE)
near = obs[i].misc['overhead_camera_near']
far = obs[i].misc['overhead_camera_far']
oh_depth_m = near + oh_depth * (far - near)
if obs_config.overhead_camera.depth:
d = oh_depth_m if obs_config.overhead_camera.depth_in_meters else oh_depth
obs[i].overhead_depth = obs_config.overhead_camera.depth_noise.apply(d)
else:
obs[i].overhead_depth = None
if obs_config.wrist_camera.depth or obs_config.wrist_camera.point_cloud:
wrist_depth = image_to_float_array(
_resize_if_needed(
Image.open(obs[i].wrist_depth),
obs_config.wrist_camera.image_size),
DEPTH_SCALE)
near = obs[i].misc['wrist_camera_near']
far = obs[i].misc['wrist_camera_far']
wrist_depth_m = near + wrist_depth * (far - near)
if obs_config.wrist_camera.depth:
d = wrist_depth_m if obs_config.wrist_camera.depth_in_meters else wrist_depth
obs[i].wrist_depth = obs_config.wrist_camera.depth_noise.apply(d)
else:
obs[i].wrist_depth = None
if obs_config.front_camera.depth or obs_config.front_camera.point_cloud:
front_depth = image_to_float_array(
_resize_if_needed(
Image.open(obs[i].front_depth),
obs_config.front_camera.image_size),
DEPTH_SCALE)
near = obs[i].misc['front_camera_near']
far = obs[i].misc['front_camera_far']
front_depth_m = near + front_depth * (far - near)
if obs_config.front_camera.depth:
d = front_depth_m if obs_config.front_camera.depth_in_meters else front_depth
obs[i].front_depth = obs_config.front_camera.depth_noise.apply(d)
else:
obs[i].front_depth = None
if obs_config.left_shoulder_camera.point_cloud:
obs[i].left_shoulder_point_cloud = VisionSensor.pointcloud_from_depth_and_camera_params(
l_sh_depth_m,
obs[i].misc['left_shoulder_camera_extrinsics'],
obs[i].misc['left_shoulder_camera_intrinsics'])
if obs_config.right_shoulder_camera.point_cloud:
obs[i].right_shoulder_point_cloud = VisionSensor.pointcloud_from_depth_and_camera_params(
r_sh_depth_m,
obs[i].misc['right_shoulder_camera_extrinsics'],
obs[i].misc['right_shoulder_camera_intrinsics'])
if obs_config.overhead_camera.point_cloud:
obs[i].overhead_point_cloud = VisionSensor.pointcloud_from_depth_and_camera_params(
oh_depth_m,
obs[i].misc['overhead_camera_extrinsics'],
obs[i].misc['overhead_camera_intrinsics'])
if obs_config.wrist_camera.point_cloud:
obs[i].wrist_point_cloud = VisionSensor.pointcloud_from_depth_and_camera_params(
wrist_depth_m,
obs[i].misc['wrist_camera_extrinsics'],
obs[i].misc['wrist_camera_intrinsics'])
if obs_config.front_camera.point_cloud:
obs[i].front_point_cloud = VisionSensor.pointcloud_from_depth_and_camera_params(
front_depth_m,
obs[i].misc['front_camera_extrinsics'],
obs[i].misc['front_camera_intrinsics'])
# Masks are stored as coded RGB images.
# Here we transform them into 1 channel handles.
if obs_config.left_shoulder_camera.mask:
obs[i].left_shoulder_mask = rgb_handles_to_mask(
np.array(_resize_if_needed(Image.open(
obs[i].left_shoulder_mask),
obs_config.left_shoulder_camera.image_size)))
if obs_config.right_shoulder_camera.mask:
obs[i].right_shoulder_mask = rgb_handles_to_mask(
np.array(_resize_if_needed(Image.open(
obs[i].right_shoulder_mask),
obs_config.right_shoulder_camera.image_size)))
if obs_config.overhead_camera.mask:
obs[i].overhead_mask = rgb_handles_to_mask(
np.array(_resize_if_needed(Image.open(
obs[i].overhead_mask),
obs_config.overhead_camera.image_size)))
if obs_config.wrist_camera.mask:
obs[i].wrist_mask = rgb_handles_to_mask(np.array(
_resize_if_needed(Image.open(
obs[i].wrist_mask),
obs_config.wrist_camera.image_size)))
if obs_config.front_camera.mask:
obs[i].front_mask = rgb_handles_to_mask(np.array(
_resize_if_needed(Image.open(
obs[i].front_mask),
obs_config.front_camera.image_size)))
demos.append(obs)
return demos
def _resize_if_needed(image, size):
if image.size[0] != size[0] or image.size[1] != size[1]:
image = image.resize(size)
return image