### Install Robotic Transformer PyTorch Source: https://github.com/lucidrains/robotic-transformer-pytorch/blob/main/README.md Install the library using pip. This is the first step before using the RT1 model. ```bash pip install robotic-transformer-pytorch ``` -------------------------------- ### RT1 Training Example Source: https://context7.com/lucidrains/robotic-transformer-pytorch/llms.txt Demonstrates a typical training forward pass with video and text instructions. Ensure the model is initialized before this step. The output shape indicates batch, frames, number of actions, and action bins. ```python import torch from robotic_transformer_pytorch import MaxViT, RT1 # Initialize model (example) vit = MaxViT( num_classes=1000, dim_conv_stem=64, dim=96, dim_head=32, depth=(2, 2, 5, 2), window_size=7, mbconv_expansion_rate=4, mbconv_shrinkage_rate=0.25, dropout=0.1 ) model = RT1( vit=vit, num_actions=11, action_bins=256, depth=6, heads=8, dim_head=64, cond_drop_prob=0.2 ) video = torch.randn(2, 3, 6, 224, 224) # (batch, channels, frames, height, width) instructions = [ 'bring me that apple sitting on the table', 'please pass the butter' ] # Forward pass returns action logits train_logits = model(video, instructions) print(f"Training output shape: {train_logits.shape}") # (2, 6, 11, 256) # Output: (batch, frames, num_actions, action_bins) # Convert logits to actions actions = train_logits.argmax(dim=-1) # (2, 6, 11) print(f"Predicted actions shape: {actions.shape}") ``` -------------------------------- ### Initialize and Use MaxViT Vision Backbone Source: https://context7.com/lucidrains/robotic-transformer-pytorch/llms.txt Configure the MaxViT vision backbone for classification or feature extraction. Ensure input images match the expected dimensions. ```python import torch from robotic_transformer_pytorch import MaxViT # Initialize MaxViT vision backbone vit = MaxViT( num_classes=1000, # Number of output classes for classification dim_conv_stem=64, # Dimension of convolutional stem dim=96, # Base dimension for transformer stages dim_head=32, # Dimension per attention head depth=(2, 2, 5, 2), # Number of transformer blocks per stage window_size=7, # Window size for block/grid attention mbconv_expansion_rate=4, # MBConv expansion rate mbconv_shrinkage_rate=0.25, # Squeeze-excitation shrinkage rate dropout=0.1, # Dropout rate channels=3 # Number of input channels ) # Process a batch of images images = torch.randn(2, 3, 224, 224) # (batch, channels, height, width) # Get classification output class_logits = vit(images) print(f"Classification output shape: {class_logits.shape}") # (2, 1000) # Get embeddings for downstream tasks (used by RT1) embeddings = vit(images, return_embeddings=True) print(f"Embedding output shape: {embeddings.shape}") # (2, embed_dim, h, w) ``` -------------------------------- ### Initialize MaxViT Backbone and RT1 Model Source: https://github.com/lucidrains/robotic-transformer-pytorch/blob/main/README.md Set up the MaxViT vision transformer backbone and the RT1 model. Ensure all required parameters are correctly configured for your task. ```python import torch from robotic_transformer_pytorch import MaxViT, RT1 vit = MaxViT( num_classes = 1000, dim_conv_stem = 64, dim = 96, dim_head = 32, depth = (2, 2, 5, 2), window_size = 7, mbconv_expansion_rate = 4, mbconv_shrinkage_rate = 0.25, dropout = 0.1 ) model = RT1( vit = vit, num_actions = 11, depth = 6, heads = 8, dim_head = 64, cond_drop_prob = 0.2 ) ``` -------------------------------- ### RT1 Inference with Classifier-Free Guidance Source: https://context7.com/lucidrains/robotic-transformer-pytorch/llms.txt Performs inference using classifier-free guidance to enhance instruction following. Higher `cond_scale` values increase adherence to text instructions. The model should be in evaluation mode (`model.eval()`) and gradients disabled (`torch.no_grad()`). ```python import torch from robotic_transformer_pytorch import MaxViT, RT1 # Initialize model (same as training) vit = MaxViT( num_classes=1000, dim_conv_stem=64, dim=96, dim_head=32, depth=(2, 2, 5, 2), window_size=7, mbconv_expansion_rate=4, mbconv_shrinkage_rate=0.25, dropout=0.1 ) model = RT1( vit=vit, num_actions=11, action_bins=256, depth=6, heads=8, dim_head=64, cond_drop_prob=0.2 ) # Set to evaluation mode model.eval() # Video input video = torch.randn(1, 3, 6, 224, 224) instruction = ['pick up the red block and place it on the blue block'] # Inference with classifier-free guidance with torch.no_grad(): # cond_scale > 1.0 amplifies text conditioning eval_logits = model(video, instruction, cond_scale=3.0) print(f"Inference output shape: {eval_logits.shape}") # (1, 6, 11, 256) # Decode actions from logits predicted_actions = eval_logits.argmax(dim=-1) print(f"Predicted actions: {predicted_actions.shape}") # (1, 6, 11) # Different guidance scales for comparison for scale in [1.0, 2.0, 3.0, 5.0]: with torch.no_grad(): logits = model(video, instruction, cond_scale=scale) actions = logits.argmax(dim=-1) print(f"Scale {scale}: action variance = {actions.float().var().item():.4f}") ``` -------------------------------- ### Initialize RT1 Model Source: https://context7.com/lucidrains/robotic-transformer-pytorch/llms.txt Combine the vision backbone and transformer components to create the RT1 model. Requires a pre-configured MaxViT instance. ```python import torch from robotic_transformer_pytorch import MaxViT, RT1 # Initialize the vision backbone vit = MaxViT( num_classes=1000, dim_conv_stem=64, dim=96, dim_head=32, depth=(2, 2, 5, 2), window_size=7, mbconv_expansion_rate=4, mbconv_shrinkage_rate=0.25, dropout=0.1 ) # Initialize RT1 model model = RT1( vit=vit, # MaxViT vision backbone num_actions=11, # Number of action dimensions action_bins=256, # Discretization bins per action depth=6, # Transformer depth heads=8, # Number of attention heads dim_head=64, # Dimension per head token_learner_ff_mult=2, # TokenLearner feedforward multiplier token_learner_num_layers=2, # TokenLearner layers token_learner_num_output_tokens=8, # Tokens per frame cond_drop_prob=0.2, # Conditioning dropout for CFG training use_attn_conditioner=False # Use attention-based text conditioning ) ``` -------------------------------- ### RT1 Model Training and Evaluation Source: https://github.com/lucidrains/robotic-transformer-pytorch/blob/main/README.md Use the RT1 model for training by passing video and instruction data. For evaluation, enable eval mode and use conditional scaling for classifier-free guidance. ```python video = torch.randn(2, 3, 6, 224, 224) instructions = [ 'bring me that apple sitting on the table', 'please pass the butter' ] train_logits = model(video, instructions) # (2, 6, 11, 256) # (batch, frames, actions, bins) # after much training model.eval() eval_logits = model(video, instructions, cond_scale = 3.) # classifier free guidance with conditional scale of 3 ``` -------------------------------- ### Compress Visual Features with TokenLearner Source: https://context7.com/lucidrains/robotic-transformer-pytorch/llms.txt Use TokenLearner to aggregate spatial features into a fixed number of tokens. Supports both static images and video frame sequences. ```python import torch from robotic_transformer_pytorch import TokenLearner # Initialize TokenLearner token_learner = TokenLearner( dim=768, # Input feature dimension ff_mult=2, # Feedforward multiplier for attention MLP num_output_tokens=8, # Number of learned output tokens num_layers=2 # Number of layers in attention generation ) # Input feature maps from vision backbone (batch, channels, height, width) features = torch.randn(4, 768, 14, 14) # Compress to learned tokens learned_tokens = token_learner(features) print(f"Output shape: {learned_tokens.shape}") # (4, 768, 8) # Works with video frames (batch, frames, channels, height, width) video_features = torch.randn(2, 6, 768, 14, 14) video_tokens = token_learner(video_features) print(f"Video tokens shape: {video_tokens.shape}") # (2, 6, 768, 8) ``` -------------------------------- ### RT1 with Attention-based Text Conditioning Source: https://context7.com/lucidrains/robotic-transformer-pytorch/llms.txt Enables attention-based text conditioning by setting `use_attn_conditioner=True`. This method uses cross-attention for more expressive text-vision alignment compared to adaptive layer normalization. Additional arguments for the conditioner can be passed via `conditioner_kwargs`. ```python import torch from robotic_transformer_pytorch import MaxViT, RT1 # Initialize vision backbone vit = MaxViT( num_classes=1000, dim_conv_stem=64, dim=96, dim_head=32, depth=(2, 2, 5, 2), window_size=7, mbconv_expansion_rate=4, mbconv_shrinkage_rate=0.25, dropout=0.1 ) # Initialize RT1 with attention-based conditioning model = RT1( vit=vit, num_actions=11, action_bins=256, depth=6, heads=8, dim_head=64, cond_drop_prob=0.2, use_attn_conditioner=True, # Enable attention-based text conditioning conditioner_kwargs=dict( # Additional kwargs passed to AttentionTextConditioner ) ) # Training with attention conditioning video = torch.randn(2, 3, 6, 224, 224) instructions = [ 'carefully pick up the fragile glass', 'stack the blocks in order of size' ] train_logits = model(video, instructions) print(f"Output shape: {train_logits.shape}") # (2, 6, 11, 256) # Inference with classifier-free guidance model.eval() with torch.no_grad(): eval_logits = model(video, instructions, cond_scale=3.0) ``` -------------------------------- ### RT1 with Pre-computed Text Embeddings Source: https://context7.com/lucidrains/robotic-transformer-pytorch/llms.txt Optimizes inference by pre-computing text embeddings, useful when the same instruction is used multiple times. Pass the computed `text_embeds` directly to the model instead of raw instructions. ```python import torch from robotic_transformer_pytorch import MaxViT, RT1 # Initialize model vit = MaxViT( num_classes=1000, dim_conv_stem=64, dim=96, dim_head=32, depth=(2, 2, 5, 2), window_size=7, mbconv_expansion_rate=4, mbconv_shrinkage_rate=0.25, dropout=0.1 ) model = RT1( vit=vit, num_actions=11, action_bins=256, depth=6, heads=8, dim_head=64, cond_drop_prob=0.2 ) # Pre-compute text embeddings for a set of instructions instructions = [ 'move the gripper to the left', 'grasp the object firmly', 'lift the object upward' ] # Embed texts once text_embeds = model.embed_texts(instructions) print(f"Text embeddings shape: {text_embeds.shape}") # Use pre-computed embeddings for multiple inference calls model.eval() video = torch.randn(3, 3, 6, 224, 224) with torch.no_grad(): # Pass text_embeds instead of texts logits = model(video, text_embeds=text_embeds, cond_scale=3.0) print(f"Output shape: {logits.shape}") # (3, 6, 11, 256) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.