robotic-transformer-pytorch

repository·main·Indexed 19 days ago

https://github.com/lucidrains/robotic-transformer-pytorch

A PyTorch implementation of the RT-1 (Robotic Transformer) architecture for real-world robotic control using vision and language instructions. It features a MaxViT vision backbone to process video sequences and supports classifier-free guidance during inference via the cond_scale parameter.

Tokens
1.1K
Snippets
4
Records
4
Agent score
16%

What's inside robotic-transformer-pytorch

  1. Use RT1 and MaxViT for robotic control

    main

    This project provides a PyTorch implementation of the RT-1 (Robotic Transformer) architecture. The model typically uses a MaxViT vision backbone to process video sequences and outputs action logits based on text instructions.

    Training Mode

    When calling the model during training, pass the video tensor and a list of instructions. The output shape is (batch, frames, actions, bins).

    Inference with Classifier-Free Guidance

    For evaluation/inference, you can use classifier-free guidance by setting the cond_scale parameter. This allows you to control the strength of the conditional instruction during generation.

    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 the RT1 model
    model = RT1(
        vit = vit,
        num_actions = 11,
        depth = 6,
        heads = 8,
        dim_head = 64,
        cond_drop_prob = 0.2
    )
    
    # Prepare input data
    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'
    ]
    
    # Training forward pass
    train_logits = model(video, instructions) # Output shape: (2, 6, 11, 256)
    
    # Inference with classifier-free guidance
    model.eval()
    eval_logits = model(video, instructions, cond_scale = 3.)
  2. MaxViT class

    main

    A Multi-Axis Vision Transformer implementation used as a backbone for the RT1 model.

    Parameters:

    • num_classes: Number of classes for the vision task.
    • dim_conv_stem: Dimension of the convolutional stem.
    • dim: Embedding dimension.
    • dim_head: Dimension of each attention head.
    • depth: A tuple defining the depth of different stages.
    • window_size: Window size for local attention.
    • mbconv_expansion_rate: Expansion rate for MBConv blocks.
    • mbconv_shrinkage_rate: Shrinkage rate for MBConv blocks.
    • dropout: Dropout probability.
    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
    )
  3. RT1 class

    main

    The main Robotic Transformer (RT1) model class.

    Parameters:

    • vit: The vision transformer backbone (e.g., MaxViT).
    • num_actions: Number of possible actions.
    • depth: Number of transformer layers.
    • heads: Number of attention heads.
    • dim_head: Dimension of each attention head.
    • cond_drop_prob: Probability of dropping the conditioning (used for classifier-free guidance training).

    Methods:

    • __call__(video, instructions, cond_scale = None): Performs the forward pass. If cond_scale is provided, it applies classifier-free guidance during inference.
    model = RT1(
        vit = vit,
        num_actions = 11,
        depth = 6,
        heads = 8,
        dim_head = 64,
        cond_drop_prob = 0.2
    )