MCPcopy Create free account
hub / github.com/SpatialVLA/SpatialVLA / apply_frame_transforms

Function apply_frame_transforms

data/rlds.py:364–423  ·  view source on GitHub ↗

Applies common transforms that happen at a frame level. These transforms are usually more CPU-intensive, (e.g., decoding or resizing images). Args: train (bool): Whether the dataset is for training (affects image augmentation). dataset (dl.DLataset): The dataset to tran

(
    dataset: dl.DLataset,
    *,
    train: bool,
    image_augment_kwargs: Union[Dict, Dict[str, Dict]] = {},
    resize_size: Union[Tuple[int, int], Dict[str, Tuple[int, int]]] = {},
    depth_resize_size: Union[Tuple[int, int], Dict[str, Tuple[int, int]]] = {},
    num_parallel_calls: int = tf.data.AUTOTUNE,
)

Source from the content-addressed store, hash-verified

362 return dataset
363
364def apply_frame_transforms(
365 dataset: dl.DLataset,
366 *,
367 train: bool,
368 image_augment_kwargs: Union[Dict, Dict[str, Dict]] = {},
369 resize_size: Union[Tuple[int, int], Dict[str, Tuple[int, int]]] = {},
370 depth_resize_size: Union[Tuple[int, int], Dict[str, Tuple[int, int]]] = {},
371 num_parallel_calls: int = tf.data.AUTOTUNE,
372) -> dl.DLataset:
373 """
374 Applies common transforms that happen at a frame level. These transforms are usually more CPU-intensive, (e.g.,
375 decoding or resizing images).
376
377 Args:
378 train (bool): Whether the dataset is for training (affects image augmentation).
379 dataset (dl.DLataset): The dataset to transform.
380 image_augment_kwargs (dict|Mapping[str, dict]): Keyword arguments to pass to the image augmentation
381 function. See `dlimp.transforms.augment_image` for documentation of these kwargs. If a dict of
382 dicts is provided, then key "k" will be used for "image_{k}" (names determined by `image_obs_keys`
383 in `make_dataset_from_rlds`). Augmentation will be skipped for missing keys (so pass an empty dict
384 to skip augmentation for all images).
385 resize_size (Tuple[int, int]|Mapping[str, Tuple[int, int]]): If provided, images will be resized to
386 this size. If a dict of tuples is provided, then key "k" will be used for "image_{k}" (names
387 determined by `image_obs_keys` in `make_dataset_from_rlds`). Resizing will be skipped for missing
388 keys (so pass an empty dict to skip resizing for all images).
389 depth_resize_size (Tuple[int, int]|Mapping[str, Tuple[int, int]]): Same as resize_size, but for depth
390 images.
391 num_parallel_calls (int): number of parallel calls for frame_map operations. Default to AUTOTUNE.
392 """
393
394 # Convenience wrapper that takes a function that operates on a non-chunked "observation" dict and applies
395 # it to the chunked "observation" dict as well as the non-chunked "task" dict
396 def apply_obs_transform(fn: Callable[[Dict], Dict], frame: Dict) -> Dict:
397 frame["task"] = fn(frame["task"])
398 frame["observation"] = dl.vmap(fn)(frame["observation"])
399 return frame
400
401 # Decode + resize images (and depth images)
402 dataset = dataset.frame_map(
403 partial(
404 apply_obs_transform,
405 partial(
406 obs_transforms.decode_and_resize,
407 resize_size=resize_size,
408 depth_resize_size=depth_resize_size,
409 ),
410 ),
411 num_parallel_calls,
412 )
413
414 if train:
415 # Augment all images with the same seed, skipping padding images
416 def aug(frame: dict):
417 seed = tf.random.uniform([2], maxval=tf.dtypes.int32.max, dtype=tf.int32)
418 aug_fn = partial(obs_transforms.augment, seed=seed, augment_kwargs=image_augment_kwargs)
419 return apply_obs_transform(aug_fn, frame)
420
421 dataset = dataset.frame_map(aug, num_parallel_calls)

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected