### Install Multi-threaded Dataset Builder Source: https://github.com/kvablack/dlimp/blob/main/rlds_converters/README.md Installs the multithreaded dataset builder package. This command should be run from the root directory of the project. Additional dataset-specific requirements may be installed separately. ```bash pip install . ``` -------------------------------- ### Complete DLataset Pipeline Example in Python Source: https://context7.com/kvablack/dlimp/llms.txt An end-to-end example demonstrating the DLataset pipeline, including loading data from TFRecords, trajectory-level filtering and transformations (like goal relabeling), frame-level image processing (decoding, resizing, augmentation), and finally shuffling, batching, and preparing the dataset for training. It utilizes method chaining for a functional programming style. ```python from dlimp import DLataset from dlimp.transforms.frame_transforms import decode_images, resize_images, augment from dlimp.transforms import goal_relabeling import tensorflow as tf # Configuration augment_config = { 'augment_order': ['random_resized_crop', 'random_brightness', 'random_flip_left_right'], 'random_resized_crop': {'scale': [0.9, 1.0], 'ratio': [0.9, 1.1]}, 'random_brightness': [0.1] } # Build pipeline dataset = ( DLataset.from_tfrecords( "/path/to/robotics/dataset/", shuffle=True, num_parallel_reads=8 ) .with_ram_budget(gb=16) # Trajectory-level: filter short trajectories .filter(lambda traj: traj['_len'][0] >= 10) # Trajectory-level: add next_obs .traj_map(lambda traj: { **traj, 'next_obs': tf.nest.map_structure( lambda x: tf.concat([x[1:], x[-1:]], axis=0), traj['obs'] ) }) # Trajectory-level: goal relabeling .traj_map(lambda x: goal_relabeling.uniform(x, reached_proportion=0.2)) # Frame-level: decode and resize images .frame_map(decode_images) .frame_map(lambda x: resize_images(x, size=(224, 224))) # Frame-level: augmentation .frame_map(lambda x: augment(x, match='*image', augment_kwargs=augment_config)) # Convert to frame-level dataset .flatten() # Shuffle and batch .shuffle(buffer_size=50000, seed=42) .batch(256) .repeat() ) # Train model for epoch in range(100): for batch in dataset.iterator(prefetch=2): # batch['obs'] shape: [256, 224, 224, 3] # batch['action'] shape: [256, action_dim] # batch['goals'] shape: [256, 224, 224, 3] # batch['rewards'] shape: [256] train_step(batch) ``` -------------------------------- ### Apply Trajectory-Level Transformations with DLIMP Source: https://context7.com/kvablack/dlimp/llms.txt Applies transformations at the trajectory level using DLIMP's `traj_map` and `filter` methods. Examples include adding computed fields like 'next_observations', filtering trajectories based on length, and truncating trajectories to a fixed maximum length. ```python from dlimp import DLataset import tensorflow as tf dataset = DLataset.from_tfrecords("/path/to/dataset/") # Add next observations to trajectory def add_next_obs(traj): # Pad so last transition has next_obs == obs traj['next_observations'] = tf.nest.map_structure( lambda x: tf.concat([x[1:], x[-1:]], axis=0), traj['observations'] ) return traj dataset = dataset.traj_map(add_next_obs) # Filter trajectories by length def filter_short_trajs(traj): return traj['_len'][0] >= 10 dataset = dataset.filter(filter_short_trajs) # Truncate trajectories to fixed length def truncate_traj(traj, max_len=50): if traj['_len'][0] > max_len: return tf.nest.map_structure(lambda x: x[:max_len], traj) return traj dataset = dataset.traj_map(lambda x: truncate_traj(x, max_len=50)) ``` -------------------------------- ### Build RLDS Dataset using tfds build Source: https://github.com/kvablack/dlimp/blob/main/rlds_converters/README.md Builds a specific RLDS dataset using the `tfds build` command. Requires specifying the path to the raw data and potentially modifying builder settings like `NUM_WORKERS` and `CHUNKSIZE`. ```bash CUDA_VISIBLE_DEVICES="" tfds build --manual_dir ``` -------------------------------- ### Convert Kinetics Raw to TFRecord (Python) Source: https://github.com/kvablack/dlimp/blob/main/legacy_converters/kinetics/downloading_kinetics.txt This script converts raw Kinetics dataset files into the TFRecord format required by DLIMP. It takes the input path to the downloaded dataset and an output path for the TFRecord files. The 'aspect_ratio' flag should be set to True for proper data handling. Supports 400, 600, and 700 class splits. ```bash python3 -m scripts.kinetics.raw_to_tfrecord --input_path /path-to-kinetics-dataset/k{num} --output_path /path-to-output/ --aspect_ratio True ``` -------------------------------- ### Load Datasets from RLDS Format using DLIMP Source: https://context7.com/kvablack/dlimp/llms.txt Loads datasets using the TensorFlow Datasets (TFDS) RLDS format. It automatically handles metadata, moving trajectory-level information to the 'traj_metadata' key. Supports TFDS dataset builders and specifies splits for loading. ```python import tensorflow_datasets as tfds from dlimp import DLataset # Create TFDS dataset builder builder = tfds.builder('bridge_dataset/1.0.0', data_dir='/path/to/data') # Load dataset from RLDS format dataset = DLataset.from_rlds( builder=builder, split='train[:80%]', shuffle=True, num_parallel_reads=8 ) # RLDS metadata is automatically moved from nested structure to top level # with 'traj_metadata' key containing trajectory-level information for traj in dataset.iterator(): print(traj['traj_metadata']) # Original trajectory metadata print(traj['observation']) # Frame-level observations print(traj['action']) # Frame-level actions ``` -------------------------------- ### Load Trajectory Datasets from TFRecords using DLIMP Source: https://context7.com/kvablack/dlimp/llms.txt Loads trajectory datasets from TFRecord files. Supports loading from a directory or specific files, with options for shuffling and parallel reading. It allows setting a RAM budget and iterating through trajectories, automatically adding metadata like trajectory length and frame index. ```python from dlimp import DLataset # Load from directory containing .tfrecord files dataset = DLataset.from_tfrecords( "/path/to/dataset/", shuffle=True, num_parallel_reads=8 ) # Load from specific files dataset = DLataset.from_tfrecords( ["/path/to/file1.tfrecord", "/path/to/file2.tfrecord"], shuffle=True, num_parallel_reads=4 ) # Set RAM budget for dataset processing dataset = dataset.with_ram_budget(gb=8) # Iterate through trajectories for trajectory in dataset.iterator(prefetch=10): # Each trajectory is a dict with metadata added automatically print(trajectory['_len']) # Trajectory length print(trajectory['_traj_index']) # Trajectory index in dataset print(trajectory['_frame_index']) # Frame index within trajectory print(trajectory.keys()) # All available keys ``` -------------------------------- ### Python: Mixing Multiple Datasets with Dlimp Source: https://context7.com/kvablack/dlimp/llms.txt Combines multiple DLataset objects using different sampling strategies, such as uniform sampling, weighted sampling, deterministic choice, or zipping. This is useful for multi-task learning or curriculum learning scenarios. Dependencies include 'dlimp' and 'tensorflow'. ```python from dlimp import DLataset # Load multiple datasets dataset1 = DLataset.from_tfrecords("/path/to/dataset1/") dataset2 = DLataset.from_tfrecords("/path/to/dataset2/") dataset3 = DLataset.from_tfrecords("/path/to/dataset3/") # Sample from datasets with uniform weights mixed_dataset = DLataset.sample_from_datasets( datasets=[dataset1, dataset2, dataset3], weights=None, # Uniform sampling seed=42, stop_on_empty_dataset=False ) # Sample with custom weights (40%, 40%, 20%) mixed_dataset = DLataset.sample_from_datasets( datasets=[dataset1, dataset2, dataset3], weights=[0.4, 0.4, 0.2], seed=42 ) # Choose dataset deterministically import tensorflow as tf choice_dataset = tf.data.Dataset.from_tensor_slices([0, 1, 2, 0, 1]) mixed_dataset = DLataset.choose_from_datasets( datasets=[dataset1, dataset2, dataset3], choice_dataset=choice_dataset, stop_on_empty_dataset=True ) # Zip datasets together zipped_dataset = DLataset.zip(dataset1, dataset2) # Each element is tuple of (traj_from_dataset1, traj_from_dataset2) ``` -------------------------------- ### Python: Goal Relabeling for RL with Dlimp Source: https://context7.com/kvablack/dlimp/llms.txt Implements goal relabeling strategies for hindsight experience replay and goal-conditioned reinforcement learning. This involves adding a 'next_obs' field and then applying uniform, geometric, or last-state upweighted relabeling. Dependencies include 'dlimp', 'tensorflow', and its 'nest' module. ```python from dlimp import DLataset from dlimp.transforms import goal_relabeling import tensorflow as tf dataset = DLataset.from_tfrecords("/path/to/dataset/") # Add next_obs field required for goal relabeling def add_next_obs(traj): traj['next_obs'] = tf.nest.map_structure( lambda x: tf.concat([x[1:], x[-1:]], axis=0), traj['obs'] ) return traj dataset = dataset.traj_map(add_next_obs) # Uniform goal relabeling - samples goals uniformly from future dataset = dataset.traj_map( lambda x: goal_relabeling.uniform(x, reached_proportion=0.2) ) # Output: traj['goals'] and traj['rewards'] added # reached_proportion=0.2 means 20% of transitions have reward=0 # Geometric goal relabeling - samples with exponential decay dataset = dataset.traj_map( lambda x: goal_relabeling.geometric( x, reached_proportion=0.1, discount=0.95 ) ) # Goals sampled with probability decaying by discount factor # Last-state upweighted relabeling dataset = dataset.traj_map( lambda x: goal_relabeling.last_state_upweighted( x, reached_proportion=0.15 ) ) # Later transitions more likely to get final state as goal ``` -------------------------------- ### Python: Image Data Augmentation with Dlimp Source: https://context7.com/kvablack/dlimp/llms.txt Applies various deterministic or randomized augmentations to image data within a DLataset. It supports configurable augmentation pipelines and options for applying augmentations consistently across frames in a trajectory or independently. Dependencies include 'dlimp' and its transforms. ```python from dlimp import DLataset from dlimp.transforms.frame_transforms import decode_images, resize_images, augment dataset = DLataset.from_tfrecords("/path/to/dataset/") dataset = dataset.frame_map(decode_images) dataset = dataset.frame_map(lambda x: resize_images(x, size=(224, 224))) # Configure augmentation pipeline augment_config = { 'augment_order': [ 'random_resized_crop', 'random_brightness', 'random_contrast', 'random_saturation', 'random_flip_left_right' ], 'random_resized_crop': { 'scale': [0.8, 1.0], 'ratio': [3/4, 4/3] }, 'random_brightness': [0.2], 'random_contrast': [0.8, 1.2], 'random_saturation': [0.8, 1.2] } # Apply augmentations (same seed for all images in trajectory by default) dataset = dataset.frame_map( lambda x: augment( x, match='*image', # Matches keys ending in 'image' traj_identical=True, # Same augmentation for all frames in trajectory keys_identical=True, # Same augmentation for all matching keys augment_kwargs=augment_config ) ) # Different augmentations per frame dataset = dataset.frame_map( lambda x: augment( x, match='*image', traj_identical=False, keys_identical=True, augment_kwargs=augment_config ) ) ``` -------------------------------- ### Frame-Level Image Processing with DLIMP Transforms Source: https://context7.com/kvablack/dlimp/llms.txt Applies frame-level transformations for image processing using DLIMP's `frame_map` and specialized transforms. Includes decoding images, resizing images and depth images, and chaining multiple transformations for image augmentation and preparation. ```python from dlimp import DLataset from dlimp.transforms.frame_transforms import decode_images, resize_images, resize_depth_images dataset = DLataset.from_tfrecords("/path/to/dataset/") # Decode all image fields (searches for 'image' in key names) dataset = dataset.frame_map(decode_images) # Decode specific image fields dataset = dataset.frame_map( lambda x: decode_images(x, match=['rgb', 'camera']) ) # Resize images to specific dimensions dataset = dataset.frame_map( lambda x: resize_images(x, match='image', size=(128, 128)) ) # Resize depth images (handles float32 depth data) dataset = dataset.frame_map( lambda x: resize_depth_images(x, match='depth', size=(64, 64)) ) # Chain multiple transformations dataset = ( dataset .frame_map(decode_images) .frame_map(lambda x: resize_images(x, size=(256, 256))) .frame_map(lambda x: resize_depth_images(x, size=(128, 128))) ) ``` -------------------------------- ### Python: Flattening Trajectories to Frames with Dlimp Source: https://context7.com/kvablack/dlimp/llms.txt Converts a trajectory-level dataset into a frame-level dataset, suitable for training on individual frames. This involves applying any necessary trajectory-level transformations before flattening and then performing frame-level transformations and batching. Only 'frame_map' is allowed after flattening. ```python from dlimp import DLataset dataset = DLataset.from_tfrecords("/path/to/dataset/") # Apply trajectory-level transformations first dataset = dataset.traj_map(lambda x: x) # Any traj transformations # Flatten to frame-level dataset dataset = dataset.flatten(num_parallel_calls=8) # After flattening, only frame_map is allowed (not traj_map) dataset = dataset.frame_map(lambda x: x) # Frame transformations # Batch frames for training dataset = dataset.batch(32) # Shuffle at frame level dataset = dataset.shuffle(buffer_size=10000) # Iterate through frames for batch in dataset.iterator(prefetch=10): # batch is dict with shape [batch_size, ...] for each field print(batch['observation'].shape) print(batch['action'].shape) ``` -------------------------------- ### Custom Frame Transformations with Parallel Processing in Python Source: https://context7.com/kvablack/dlimp/llms.txt Applies custom transformations to dataset frames using DLataset's frame_map, vmap, and parallel_vmap utilities. `normalize_action` is applied frame-wise, while `complex_frame_transform` demonstrates more involved processing suitable for parallel execution. `parallel_vmap` is used for efficient parallel processing, and `vmap` for sequential but fused processing. ```python from dlimp import DLataset, vmap, parallel_vmap import tensorflow as tf dataset = DLataset.from_tfrecords("/path/to/dataset/") # Custom transformation function for a single frame def normalize_action(frame): frame['action'] = (frame['action'] - tf.constant([0.5, 0.5])) / tf.constant([0.2, 0.2]) return frame # Apply to all frames using frame_map dataset = dataset.frame_map(normalize_action) # For more complex transformations, use vmap utilities def complex_frame_transform(frame): # Some expensive computation frame['processed'] = tf.nn.relu(frame['observation']) return frame # Use parallel_vmap for parallelized frame processing dataset = dataset.traj_map( parallel_vmap(complex_frame_transform, num_parallel_calls=8) ) # Use vmap for sequential but fused frame processing dataset = dataset.traj_map( vmap(complex_frame_transform) ) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.