-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransforms.py
More file actions
executable file
·550 lines (419 loc) · 18.8 KB
/
Copy pathtransforms.py
File metadata and controls
executable file
·550 lines (419 loc) · 18.8 KB
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
from collections.abc import Callable, Mapping, Sequence
import dataclasses
import re
from typing import Protocol, TypeAlias, TypeVar, runtime_checkable
import torch
import flax.traverse_util as traverse_util
import jax
import numpy as np
from PIL import Image
import torchvision
from openpi_client import image_tools
from openpi.models import tokenizer as _tokenizer
from openpi.shared import array_typing as at
from openpi.shared import normalize as _normalize
DataDict: TypeAlias = at.PyTree
NormStats: TypeAlias = _normalize.NormStats
T = TypeVar("T")
S = TypeVar("S")
@runtime_checkable
class DataTransformFn(Protocol):
def __call__(self, data: DataDict) -> DataDict:
"""Apply transformation to the data.
Args:
data: The data to apply the transform to. This is a possibly nested dictionary that contains
unbatched data elements. Each leaf is expected to be a numpy array. Using JAX arrays is allowed
but not recommended since it may result in extra GPU memory usage inside data loader worker
processes.
Returns:
The transformed data. Could be the input `data` that was modified in place, or a new data structure.
"""
@dataclasses.dataclass(frozen=True)
class Group:
"""A group of transforms."""
# Transforms that are applied to the model input data.
inputs: Sequence[DataTransformFn] = ()
# Transforms that are applied to the model output data.
outputs: Sequence[DataTransformFn] = ()
def push(self, *, inputs: Sequence[DataTransformFn] = (), outputs: Sequence[DataTransformFn] = ()) -> "Group":
"""Append transforms to the group and return a new group.
Args:
inputs: Appended to the *end* of the current input transforms.
outputs: Appended to the *beginning* of the current output transforms.
Returns:
A new group with the appended transforms.
"""
return Group(inputs=(*self.inputs, *inputs), outputs=(*outputs, *self.outputs))
@dataclasses.dataclass(frozen=True)
class CompositeTransform(DataTransformFn):
"""A composite transform that applies a sequence of transforms in order."""
transforms: Sequence[DataTransformFn]
def __call__(self, data: DataDict) -> DataDict:
for transform in self.transforms:
data = transform(data)
return data
def compose(transforms: Sequence[DataTransformFn]) -> DataTransformFn:
"""Compose a sequence of transforms into a single transform."""
return CompositeTransform(transforms)
@dataclasses.dataclass(frozen=True)
class RepackTransform(DataTransformFn):
"""Repacks an input dictionary into a new dictionary.
Repacking is defined using a dictionary where the keys are the new keys and the values
are the flattened paths to the old keys. We use '/' as the separator during flattening.
Example:
{
"images": {
"cam_high": "observation.images.top",
"cam_low": "observation.images.bottom",
},
"state": "observation.state",
"actions": "action",
}
"""
structure: at.PyTree[str]
def __call__(self, data: DataDict) -> DataDict:
flat_item = flatten_dict(data)
return jax.tree.map(lambda k: flat_item[k], self.structure)
@dataclasses.dataclass(frozen=True)
class InjectDefaultPrompt(DataTransformFn):
prompt: str | None
def __call__(self, data: DataDict) -> DataDict:
if self.prompt is not None and "prompt" not in data:
data["prompt"] = np.asarray(self.prompt)
return data
@dataclasses.dataclass(frozen=True)
class Normalize(DataTransformFn):
norm_stats: at.PyTree[NormStats] | None
# If true, will use quantile normalization. Otherwise, normal z-score normalization will be used.
use_quantiles: bool = False
# If true, will raise an error if any of the keys in the norm stats are not present in the data.
strict: bool = False
def __post_init__(self):
if self.norm_stats is not None and self.use_quantiles:
_assert_quantile_stats(self.norm_stats)
def __call__(self, data: DataDict) -> DataDict:
if self.norm_stats is None:
return data
return apply_tree(
data,
self.norm_stats,
self._normalize_quantile if self.use_quantiles else self._normalize,
strict=self.strict,
)
def _normalize(self, x, stats: NormStats):
mean, std = stats.mean[..., : x.shape[-1]], stats.std[..., : x.shape[-1]]
return (x - mean) / (std + 1e-6)
def _normalize_quantile(self, x, stats: NormStats):
assert stats.q01 is not None
assert stats.q99 is not None
q01, q99 = stats.q01[..., : x.shape[-1]], stats.q99[..., : x.shape[-1]]
return (x - q01) / (q99 - q01 + 1e-6) * 2.0 - 1.0
@dataclasses.dataclass(frozen=True)
class Unnormalize(DataTransformFn):
norm_stats: at.PyTree[NormStats] | None
# If true, will use quantile normalization. Otherwise, normal z-score normalization will be used.
use_quantiles: bool = False
def __post_init__(self):
if self.norm_stats is not None and self.use_quantiles:
_assert_quantile_stats(self.norm_stats)
def __call__(self, data: DataDict) -> DataDict:
if self.norm_stats is None:
return data
# Make sure that all the keys in the norm stats are present in the data.
return apply_tree(
data,
self.norm_stats,
self._unnormalize_quantile if self.use_quantiles else self._unnormalize,
strict=False,
)
def _unnormalize(self, x, stats: NormStats):
mean = pad_to_dim(stats.mean, x.shape[-1], axis=-1, value=0.0)
std = pad_to_dim(stats.std, x.shape[-1], axis=-1, value=1.0)
return x * (std + 1e-6) + mean
def _unnormalize_quantile(self, x, stats: NormStats):
assert stats.q01 is not None
assert stats.q99 is not None
q01, q99 = stats.q01, stats.q99
if (dim := q01.shape[-1]) < x.shape[-1]:
return np.concatenate([(x[..., :dim] + 1.0) / 2.0 * (q99 - q01 + 1e-6) + q01, x[..., dim:]], axis=-1)
return (x + 1.0) / 2.0 * (q99 - q01 + 1e-6) + q01
@dataclasses.dataclass(frozen=True)
class ResizeImages(DataTransformFn):
height: int
width: int
key: str = "image"
def to_numpy(self, array_like):
if hasattr(array_like, "cpu"):
return array_like.detach().cpu().numpy()
if hasattr(array_like, "__array__"):
return np.array(array_like)
return array_like
def __call__(self, data: DataDict) -> DataDict:
data[self.key] = {k: image_tools.resize_with_pad(self.to_numpy(v), self.height, self.width) for k, v in data[self.key].items()}
return data
@dataclasses.dataclass(frozen=True)
class SubsampleActions(DataTransformFn):
stride: int
def __call__(self, data: DataDict) -> DataDict:
data["actions"] = data["actions"][:: self.stride]
return data
@dataclasses.dataclass(frozen=True)
class DeltaActions(DataTransformFn):
"""Repacks absolute actions into delta action space."""
# Boolean mask for the action dimensions to be repacked into delta action space. Length
# can be smaller than the actual number of dimensions. If None, this transform is a no-op.
# See `make_bool_mask` for more details.
mask: Sequence[bool] | None
def __call__(self, data: DataDict) -> DataDict:
if "actions" not in data or self.mask is None:
return data
state, actions = data["state"], data["actions"]
mask = np.asarray(self.mask)
dims = mask.shape[-1]
actions[..., :dims] -= np.expand_dims(np.where(mask, state[..., :dims], 0), axis=-2)
data["actions"] = actions
return data
@dataclasses.dataclass(frozen=True)
class AbsoluteActions(DataTransformFn):
"""Repacks delta actions into absolute action space."""
# Boolean mask for the action dimensions to be repacked into absolute action space. Length
# can be smaller than the actual number of dimensions. If None, this transform is a no-op.
# See `make_bool_mask` for more details.
mask: Sequence[bool] | None
def __call__(self, data: DataDict) -> DataDict:
if "actions" not in data or self.mask is None:
return data
state, actions = data["state"], data["actions"]
mask = np.asarray(self.mask)
dims = mask.shape[-1]
actions[..., :dims] += np.expand_dims(np.where(mask, state[..., :dims], 0), axis=-2)
data["actions"] = actions
return data
@dataclasses.dataclass(frozen=True)
class ACOTDeltaActions(DataTransformFn):
"""Repacks absolute actions into delta action space."""
mask: Sequence[bool] | None
use_delta_joint_actions: Sequence[bool]
def __call__(self, data: DataDict) -> DataDict:
keys = ["coarse_actions", "actions"]
state = data["state"]
mask = np.asarray(self.mask)
dims = mask.shape[-1]
for idx, key in enumerate(keys):
if self.use_delta_joint_actions[idx] and key in data:
actions = data[key]
actions[..., :dims] -= np.expand_dims(np.where(mask, state[..., :dims], 0), axis=-2)
data[key] = actions
return data
@dataclasses.dataclass(frozen=True)
class ACOTAbsoluteActions(DataTransformFn):
"""Repacks delta actions into absolute action space."""
mask: Sequence[bool] | None
use_delta_joint_actions: Sequence[bool]
def __call__(self, data: DataDict) -> DataDict:
keys = ["coarse_actions", "actions"]
state = data["state"]
mask = np.asarray(self.mask)
dims = mask.shape[-1]
for idx, key in enumerate(keys):
if self.use_delta_joint_actions[idx] and key in data:
actions = data[key]
actions[..., :dims] += np.expand_dims(np.where(mask, state[..., :dims], 0), axis=-2)
data[key] = actions
return data
@dataclasses.dataclass(frozen=True)
class TokenizePrompt(DataTransformFn):
tokenizer: _tokenizer.PaligemmaTokenizer
discrete_state_input: bool = False
def __call__(self, data: DataDict) -> DataDict:
if (prompt := data.pop("prompt", None)) is None:
raise ValueError("Prompt is required")
# print(f"Prompt: {prompt}")
if self.discrete_state_input:
if (state := data.get("state", None)) is None:
raise ValueError("State is required.")
else:
state = None
if not isinstance(prompt, str):
prompt = prompt.item()
tokens, token_masks = self.tokenizer.tokenize(prompt, state)
return {**data, "tokenized_prompt": tokens, "tokenized_prompt_mask": token_masks}
@dataclasses.dataclass(frozen=True)
class TokenizeFASTInputs(DataTransformFn):
tokenizer: _tokenizer.FASTTokenizer
def __call__(self, data: DataDict) -> DataDict:
if (prompt := data.pop("prompt", None)) is None:
raise ValueError("Prompt is required")
if not isinstance(prompt, str):
prompt = prompt.item()
state, actions = data["state"], data.get("actions")
tokens, token_mask, ar_mask, loss_mask = self.tokenizer.tokenize(prompt, state, actions)
return {
**data,
"tokenized_prompt": tokens,
"tokenized_prompt_mask": token_mask,
"token_ar_mask": ar_mask,
"token_loss_mask": loss_mask,
}
@dataclasses.dataclass(frozen=True)
class ExtractFASTActions(DataTransformFn):
tokenizer: _tokenizer.FASTTokenizer
action_horizon: int
action_dim: int
def __call__(self, data: DataDict) -> DataDict:
if "actions" not in data:
return data
# Model outputs are saved in "actions", but for FAST models they represent tokens.
tokens = data.pop("actions")
actions = self.tokenizer.extract_actions(tokens.astype(np.int32), self.action_horizon, self.action_dim)
return {
**data,
"actions": actions,
}
@dataclasses.dataclass(frozen=True)
class PromptFromLeRobotTask(DataTransformFn):
"""Extracts a prompt from the current LeRobot dataset task."""
# Contains the LeRobot dataset tasks (dataset.meta.tasks).
tasks: dict[int, str]
def __call__(self, data: DataDict) -> DataDict:
if "task_index" not in data:
raise ValueError('Cannot extract prompt without "task_index"')
task_index = int(data["task_index"])
if (prompt := self.tasks.get(task_index)) is None:
raise ValueError(f"{task_index=} not found in task mapping: {self.tasks}")
return {**data, "prompt": prompt}
@dataclasses.dataclass(frozen=True)
class PromptFromHighlevelInstruction(DataTransformFn):
"""Extracts a prompt from the current LeRobot dataset task."""
# Contains the LeRobot dataset tasks (dataset.meta.tasks).
instruction_segments: dict
def __call__(self, data: DataDict) -> DataDict:
if "episode_index" not in data:
raise ValueError('Cannot extract prompt without "task_index"')
episode_index = int(data["episode_index"])
frame_index = int(data["frame_index"])
segments = self.instruction_segments.get(str(episode_index))
segment_id = len(segments) - 1
segments[0]['start_frame_index'] = 0
for i, segment in enumerate(segments):
if frame_index >= segment['start_frame_index'] and frame_index < segment['end_frame_index']:
segment_id = i
break
if segment_id is not None:
segment = segments[segment_id]
instruction = segment['instruction']
else:
raise ValueError(f"No segment found for episode {episode_index} and frame {frame_index}")
return {**data, "prompt": instruction}
@dataclasses.dataclass(frozen=True)
class PadStatesAndActions(DataTransformFn):
"""Zero-pads states and actions to the model action dimension."""
model_action_dim: int
def __call__(self, data: DataDict) -> DataDict:
data["state"] = pad_to_dim(data["state"], self.model_action_dim, axis=-1)
if "actions" in data:
data["actions"] = pad_to_dim(data["actions"], self.model_action_dim, axis=-1)
return data
@dataclasses.dataclass(frozen=True)
class ACOTPadStatesAndActions(DataTransformFn):
"""Zero-pads states and actions to the model action dimension."""
model_action_dim: int
def __call__(self, data: DataDict) -> DataDict:
data["state"] = pad_to_dim(data["state"], self.model_action_dim, axis=-1)
keys = ["coarse_actions", "actions"]
for key in keys:
if key in data:
data[key] = pad_to_dim(data[key], self.model_action_dim, axis=-1)
return data
def flatten_dict(tree: at.PyTree) -> dict:
"""Flatten a nested dictionary. Uses '/' as the separator."""
return traverse_util.flatten_dict(tree, sep="/")
def unflatten_dict(tree: dict) -> at.PyTree:
"""Unflatten a flattened dictionary. Assumes that '/' was used as a separator."""
return traverse_util.unflatten_dict(tree, sep="/")
def transform_dict(patterns: Mapping[str, str | None], tree: at.PyTree) -> at.PyTree:
"""Transform the structure of a nested dictionary using a set of patterns.
The transformation is defined using the `patterns` dictionary. The keys are the
input keys that should be matched and the values are the new names inside the output
dictionary. If the value is None, the input key is removed.
Both keys and values should represent flattened paths using '/' as the separator.
Keys can be regular expressions and values can include backreferences to the
matched groups (see `re.sub` for more details). Note that the regular expression
must match the entire key.
The order inside the `patterns` dictionary is important. Only the first pattern that
matches the input key will be used.
See unit tests for more examples.
Args:
patterns: A mapping from old keys to new keys.
tree: The nested dictionary to transform.
Returns:
The transformed nested dictionary.
"""
data = flatten_dict(tree)
# Compile the patterns.
compiled = {re.compile(k): v for k, v in patterns.items()}
output = {}
for k in data:
for pattern, repl in compiled.items():
if pattern.fullmatch(k):
new_k = pattern.sub(repl, k, count=1) if repl is not None else None
break
else:
# Use the original key if no match is found.
new_k = k
if new_k is not None:
if new_k in output:
raise ValueError(f"Key '{new_k}' already exists in output")
output[new_k] = data[k]
# Validate the output structure to make sure that it can be unflattened.
names = sorted(output)
for i in range(len(names) - 1):
name, next_name = names[i : i + 2]
if next_name.startswith(name + "/"):
raise ValueError(f"Leaf '{name}' aliases a node of '{next_name}'")
return unflatten_dict(output)
def apply_tree(
tree: at.PyTree[T], selector: at.PyTree[S], fn: Callable[[T, S], T], *, strict: bool = False
) -> at.PyTree[T]:
tree = flatten_dict(tree)
selector = flatten_dict(selector)
def transform(k: str, v: T) -> T:
if k in selector:
return fn(v, selector[k])
return v
if strict:
for k in selector:
if k not in tree:
raise ValueError(f"Selector key {k} not found in tree")
return unflatten_dict({k: transform(k, v) for k, v in tree.items()})
def pad_to_dim(x: np.ndarray, target_dim: int, axis: int = -1, value: float = 0.0) -> np.ndarray:
"""Pad an array to the target dimension with zeros along the specified axis."""
current_dim = x.shape[axis]
if current_dim < target_dim:
pad_width = [(0, 0)] * len(x.shape)
pad_width[axis] = (0, target_dim - current_dim)
return np.pad(x, pad_width, constant_values=value)
return x
def make_bool_mask(*dims: int) -> tuple[bool, ...]:
"""Make a boolean mask for the given dimensions.
Example:
make_bool_mask(2, -2, 2) == (True, True, False, False, True, True)
make_bool_mask(2, 0, 2) == (True, True, True, True)
Args:
dims: The dimensions to make the mask for.
Returns:
A tuple of booleans.
"""
result = []
for dim in dims:
if dim > 0:
result.extend([True] * (dim))
else:
result.extend([False] * (-dim))
return tuple(result)
def _assert_quantile_stats(norm_stats: at.PyTree[NormStats]) -> None:
for k, v in flatten_dict(norm_stats).items():
if v.q01 is None or v.q99 is None:
raise ValueError(
f"quantile stats must be provided if use_quantile_norm is True. Key {k} is missing q01 or q99."
)