### Install magvit2-pytorch Source: https://github.com/lucidrains/magvit2-pytorch/blob/main/README.md Install the magvit2-pytorch library using pip. ```bash pip install magvit2-pytorch ``` -------------------------------- ### Train the VideoTokenizer Source: https://github.com/lucidrains/magvit2-pytorch/blob/main/README.md Start the training process for the VideoTokenizer using the configured trainer. ```python trainer.train() ``` -------------------------------- ### Initialize and Run VideoTokenizerTrainer Source: https://context7.com/lucidrains/magvit2-pytorch/llms.txt Configure the trainer with dataset paths and hyperparameters, then execute the training loop and perform inference using the EMA model. ```python trainer = VideoTokenizerTrainer( tokenizer, dataset_folder = '/path/to/videos', # Folder containing videos dataset_type = 'videos', # 'videos' or 'images' batch_size = 4, grad_accum_every = 8, # Effective batch size = 32 learning_rate = 2e-5, num_train_steps = 1_000_000, num_frames = 17, # Frames per video clip validate_every_step = 100, checkpoint_every_step = 1000, checkpoints_folder = './checkpoints', results_folder = './results', valid_frac = 0.05, # 5% validation split max_grad_norm = 1.0, # Gradient clipping warmup_steps = 1000, discr_start_after_step = 10000, # Start adversarial training after warmup ) # Start training trainer.train() # After training, use the EMA model for inference ema_tokenizer = trainer.ema_tokenizer # Tokenize new videos with the trained model video = torch.randn(1, 3, 17, 128, 128).to(trainer.device) codes = ema_tokenizer.tokenize(video) decoded = ema_tokenizer.decode_from_code_indices(codes) ``` -------------------------------- ### Initialize and Use VideoTokenizer Source: https://context7.com/lucidrains/magvit2-pytorch/llms.txt Initializes the VideoTokenizer with custom architecture and parameters for encoding and decoding videos. Demonstrates a forward pass for reconstruction and training with loss calculation. ```python import torch from magvit2_pytorch import VideoTokenizer # Initialize the video tokenizer with a custom architecture tokenizer = VideoTokenizer( image_size = 128, # Input video spatial resolution init_dim = 64, # Initial feature dimension max_dim = 512, # Maximum feature dimension codebook_size = 1024, # Size of the discrete codebook channels = 3, # Number of input channels (RGB) num_codebooks = 1, # Number of separate codebooks layers = ( 'residual', # Residual block 'compress_space', # 2x spatial downsampling ('consecutive_residual', 2), # 2 consecutive residual blocks 'compress_space', # 2x spatial downsampling ('consecutive_residual', 2), # 2 consecutive residual blocks 'linear_attend_space', # Linear attention over space 'compress_space', # 2x spatial downsampling ('consecutive_residual', 2), # 2 consecutive residual blocks 'attend_space', # Full attention over space 'compress_time', # 2x temporal downsampling ('consecutive_residual', 2), # 2 consecutive residual blocks 'compress_time', # 2x temporal downsampling ('consecutive_residual', 2), # 2 consecutive residual blocks 'attend_time', # Causal attention over time ), # LFQ parameters lfq_entropy_loss_weight = 0.1, lfq_commitment_loss_weight = 1., lfq_diversity_gamma = 2.5, # Adversarial training parameters use_gan = True, adversarial_loss_weight = 1., perceptual_loss_weight = 0.1, flash_attn = True # Use flash attention for efficiency ) # Create a mock video tensor: (batch, channels, frames, height, width) video = torch.randn(1, 3, 17, 128, 128) # Forward pass returns reconstructed video recon_video = tokenizer(video) print(f"Reconstructed video shape: {recon_video.shape}") # Output: Reconstructed video shape: torch.Size([1, 3, 17, 128, 128]) # Forward pass with loss for training total_loss, loss_breakdown = tokenizer(video, return_loss=True) print(f"Total loss: {total_loss.item():.4f}") print(f"Recon loss: {loss_breakdown.recon_loss.item():.4f}") print(f"Perceptual loss: {loss_breakdown.perceptual_loss.item():.4f}") print(f"Adversarial gen loss: {loss_breakdown.adversarial_gen_loss.item():.4f}") ``` -------------------------------- ### Initialize VideoTokenizer and VideoTokenizerTrainer Source: https://github.com/lucidrains/magvit2-pytorch/blob/main/README.md Initialize the VideoTokenizer with specified parameters and the VideoTokenizerTrainer for training. The trainer requires a dataset path and type, batch size, and learning rate. ```python from magvit2_pytorch import ( VideoTokenizer, VideoTokenizerTrainer ) tokenizer = VideoTokenizer( image_size = 128, init_dim = 64, max_dim = 512, codebook_size = 1024, layers = ( 'residual', 'compress_space', ('consecutive_residual', 2), 'compress_space', ('consecutive_residual', 2), 'linear_attend_space', 'compress_space', ('consecutive_residual', 2), 'attend_space', 'compress_time', ('consecutive_residual', 2), 'compress_time', ('consecutive_residual', 2), 'attend_time', ) ) trainer = VideoTokenizerTrainer( tokenizer, dataset_folder = '/path/to/a/lot/of/media', # folder of either videos or images, depending on setting below dataset_type = 'videos', # 'videos' or 'images', prior papers have shown pretraining on images to be effective for video synthesis batch_size = 4, grad_accum_every = 8, learning_rate = 2e-5, num_train_steps = 1_000_000 ) ``` -------------------------------- ### Initialize VideoTokenizerTrainer Source: https://context7.com/lucidrains/magvit2-pytorch/llms.txt Initializes the `VideoTokenizerTrainer` which provides a complete training loop. It supports gradient accumulation, EMA, multi-GPU via Accelerate, validation, checkpointing, and optional Weights & Biases tracking. ```python import torch from magvit2_pytorch import VideoTokenizer, VideoTokenizerTrainer # Initialize the tokenizer model tokenizer = VideoTokenizer( image_size = 128, init_dim = 64, max_dim = 512, codebook_size = 1024, layers = ( 'residual', 'compress_space', ('consecutive_residual', 2), 'compress_space', ('consecutive_residual', 2), 'linear_attend_space', 'compress_space', ('consecutive_residual', 2), 'attend_space', 'compress_time', ('consecutive_residual', 2), 'compress_time', ('consecutive_residual', 2), 'attend_time', ) ) ``` -------------------------------- ### Enable Weights & Biases Tracking Source: https://context7.com/lucidrains/magvit2-pytorch/llms.txt Integrate W&B for monitoring training metrics by setting use_wandb_tracking to True and using the trackers context manager. ```python from magvit2_pytorch import VideoTokenizer, VideoTokenizerTrainer tokenizer = VideoTokenizer( image_size = 128, init_dim = 64, max_dim = 512, codebook_size = 1024, layers = ( 'residual', 'compress_space', ('consecutive_residual', 2), 'compress_space', ('consecutive_residual', 2), 'compress_space', ('consecutive_residual', 2), 'compress_time', ('consecutive_residual', 2), 'compress_time', ('consecutive_residual', 2), ) ) # Enable W&B tracking trainer = VideoTokenizerTrainer( tokenizer, dataset_folder = '/path/to/videos', dataset_type = 'videos', batch_size = 4, grad_accum_every = 8, learning_rate = 2e-5, num_train_steps = 1_000_000, use_wandb_tracking = True, # Enable Weights & Biases ) # Use the trackers context manager for proper initialization with trainer.trackers( project_name = 'magvit2-video-tokenizer', run_name = 'experiment-001' ): trainer.train() ``` -------------------------------- ### Configure Finite Scalar Quantization (FSQ) Source: https://context7.com/lucidrains/magvit2-pytorch/llms.txt Use FSQ as an alternative to LFQ by setting use_fsq to True and defining the quantization levels. ```python import torch from magvit2_pytorch import VideoTokenizer # Use FSQ instead of LFQ tokenizer = VideoTokenizer( image_size = 128, init_dim = 64, max_dim = 512, use_fsq = True, # Enable Finite Scalar Quantization fsq_levels = [8, 6, 5, 5, 5], # FSQ levels per dimension # Effective codebook size = 8 * 6 * 5 * 5 * 5 = 6000 layers = ( 'residual', 'compress_space', ('consecutive_residual', 2), 'compress_space', ('consecutive_residual', 2), 'compress_space', ('consecutive_residual', 2), 'compress_time', ('consecutive_residual', 2), 'compress_time', ('consecutive_residual', 2), ) ) video = torch.randn(1, 3, 17, 128, 128) # Training with FSQ loss, loss_breakdown = tokenizer(video, return_loss=True) print(f"Total loss: {loss.item():.4f}") ``` -------------------------------- ### Tokenize and Decode Video Source: https://github.com/lucidrains/magvit2-pytorch/blob/main/README.md After training, use the EMA (Exponential Moving Average) of the tokenizer to tokenize a mock video into discrete codes and then decode it back. Asserts that the decoded video closely matches the reconstruction from the tokenizer. ```python # after a lot of training ... # can use the EMA of the tokenizer ema_tokenizer = trainer.ema_tokenizer # mock video video = torch.randn(1, 3, 17, 128, 128) # tokenizing video to discrete codes codes = ema_tokenizer.tokenize(video) # (1, 9, 16, 16) <- in this example, time downsampled by 4x and space downsampled by 8x. flatten token ids for (non)-autoregressive training # sanity check decoded_video = ema_tokenizer.decode_from_code_indices(codes) assert torch.allclose( decoded_video, ema_tokenizer(video, return_recon = True) ) ``` -------------------------------- ### Load Video and Image Datasets Source: https://context7.com/lucidrains/magvit2-pytorch/llms.txt Utilize built-in dataset classes and utility functions for processing video and image files for training. ```python from magvit2_pytorch.data import ( VideoDataset, ImageDataset, DataLoader, video_tensor_to_gif, gif_to_tensor, video_to_tensor, tensor_to_video ) import torch # Video dataset for training on video files video_dataset = VideoDataset( folder = '/path/to/videos', image_size = 128, # Resize to this resolution channels = 3, # RGB num_frames = 17, # Sample this many frames force_num_frames = True, # Pad/truncate to exact frame count exts = ['gif', 'mp4'] # Supported extensions ) # Image dataset for pretraining on images image_dataset = ImageDataset( folder = '/path/to/images', image_size = 128, channels = 3, exts = ['jpg', 'jpeg', 'png'] ) # Create dataloaders video_loader = DataLoader(video_dataset, batch_size=4, shuffle=True, drop_last=True) image_loader = DataLoader(image_dataset, batch_size=8, shuffle=True, drop_last=True) # Load a single video file video_tensor = video_to_tensor('/path/to/video.mp4', num_frames=17) print(f"Video tensor shape: {video_tensor.shape}") # Output: Video tensor shape: torch.Size([3, 17, H, W]) # Load a GIF file gif_tensor = gif_to_tensor('/path/to/animation.gif', channels=3) print(f"GIF tensor shape: {gif_tensor.shape}") # Save tensor as video tensor_to_video(video_tensor, '/path/to/output.mp4', fps=25) # Save tensor as GIF video_tensor_to_gif(video_tensor, '/path/to/output.gif', duration=120) ``` -------------------------------- ### Configure Multi-Scale Discriminators Source: https://context7.com/lucidrains/magvit2-pytorch/llms.txt Set up multiple discriminators at different temporal scales to improve adversarial training performance. ```python import torch from magvit2_pytorch import VideoTokenizer from magvit2_pytorch.magvit2_pytorch import Discriminator # Create custom multi-scale discriminators discr_4frames = Discriminator( dim = 64, image_size = 128, channels = 3, max_dim = 512 ) discr_8frames = Discriminator( dim = 64, image_size = 128, channels = 3, max_dim = 512 ) # Tokenizer with multi-scale discriminators tokenizer = VideoTokenizer( image_size = 128, init_dim = 64, max_dim = 512, codebook_size = 1024, layers = ( 'residual', 'compress_space', ('consecutive_residual', 2), 'compress_space', ('consecutive_residual', 2), 'compress_space', ('consecutive_residual', 2), 'compress_time', ('consecutive_residual', 2), 'compress_time', ('consecutive_residual', 2), ), use_gan = True, adversarial_loss_weight = 1., multiscale_discrs = (discr_4frames, discr_8frames), multiscale_adversarial_loss_weight = 0.5, grad_penalty_loss_weight = 10. ) video = torch.randn(1, 3, 17, 128, 128) # Generator loss (for training the main model) gen_loss, gen_breakdown = tokenizer(video, return_loss=True) print(f"Generator loss: {gen_loss.item():.4f}") print(f"Multiscale gen losses: {[l.item() for l in gen_breakdown.multiscale_gen_losses]}") ``` -------------------------------- ### Enable Weights & Biases Tracking Source: https://github.com/lucidrains/magvit2-pytorch/blob/main/README.md Configure the VideoTokenizerTrainer to use Weights & Biases for experiment tracking by setting `use_wandb_tracking` to True. Training can then be performed within a context manager to log runs. ```python trainer = VideoTokenizerTrainer( use_wandb_tracking = True, ... ) with trainer.trackers(project_name = 'magvit2', run_name = 'baseline'): trainer.train() ``` -------------------------------- ### VideoTokenizerTrainer Source: https://context7.com/lucidrains/magvit2-pytorch/llms.txt Provides a complete training loop with gradient accumulation, EMA, multi-GPU support via Accelerate, validation, checkpointing, and optional Weights & Biases tracking. ```APIDOC ## VideoTokenizerTrainer - Training Pipeline ### Description The `VideoTokenizerTrainer` provides a complete training loop with gradient accumulation, EMA, multi-GPU support via Accelerate, validation, checkpointing, and optional Weights & Biases tracking. ### Method POST (or similar, depending on implementation) ### Endpoint `/api/train/video` (example endpoint) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **tokenizer_config** (dict) - Required - Configuration for the VideoTokenizer. - **training_data** (DataLoader) - Required - DataLoader for training data. - **validation_data** (DataLoader) - Optional - DataLoader for validation data. - **optimizer_config** (dict) - Required - Configuration for the optimizer. - **scheduler_config** (dict) - Optional - Configuration for the learning rate scheduler. - **accelerate_config** (dict) - Required - Configuration for Hugging Face Accelerate. - **wandb_config** (dict) - Optional - Configuration for Weights & Biases logging. ### Request Example ```json { "tokenizer_config": { ... }, "training_data": "[dataloader object]", "optimizer_config": { ... }, "accelerate_config": { ... } } ``` ### Response #### Success Response (200) - **message** (string) - Confirmation message indicating training has started or completed. #### Response Example ```json { "message": "Training process initiated successfully." } ``` ``` -------------------------------- ### Tokenize Video and Flatten Codes Source: https://context7.com/lucidrains/magvit2-pytorch/llms.txt Demonstrates tokenizing a video into discrete codes and then flattening these codes for autoregressive training. Ensure the video is preprocessed before tokenization. ```python codes = tokenizer.tokenize(video) print(f"Token codes shape: {codes.shape}") flattened_codes = codes.flatten(start_dim=1) print(f"Flattened codes shape: {flattened_codes.shape}") ``` -------------------------------- ### Save and Load Tokenizer Models Source: https://context7.com/lucidrains/magvit2-pytorch/llms.txt Persist trained models to disk and reload them for inference or further training. ```python import torch from magvit2_pytorch import VideoTokenizer # Create and train a tokenizer tokenizer = VideoTokenizer( image_size = 128, init_dim = 64, max_dim = 512, codebook_size = 1024, layers = ( 'residual', 'compress_space', ('consecutive_residual', 2), 'compress_space', ('consecutive_residual', 2), 'compress_space', ('consecutive_residual', 2), 'compress_time', ('consecutive_residual', 2), 'compress_time', ('consecutive_residual', 2), ) ) # Save the model (includes config for reconstruction) tokenizer.save('./trained_tokenizer.pt') # Load the model weights into an existing instance tokenizer.load('./trained_tokenizer.pt') # Or initialize and load from checkpoint in one step loaded_tokenizer = VideoTokenizer.init_and_load_from('./trained_tokenizer.pt') # Use for inference video = torch.randn(1, 3, 17, 128, 128) codes = loaded_tokenizer.tokenize(video) reconstructed = loaded_tokenizer.decode_from_code_indices(codes) print(f"Reconstruction successful: {reconstructed.shape}") ``` -------------------------------- ### Conditional Video Tokenization Source: https://context7.com/lucidrains/magvit2-pytorch/llms.txt Configure a VideoTokenizer with conditioning signals and perform training or inference. Note that the tokenize method does not accept conditioning signals. ```python import torch from magvit2_pytorch import VideoTokenizer # Conditional tokenizer with conditionable layers tokenizer = VideoTokenizer( image_size = 128, init_dim = 64, max_dim = 512, codebook_size = 1024, dim_cond = 256, # Conditioning dimension dim_cond_expansion_factor = 4., # Expand cond before use layers = ( 'residual', 'compress_space', 'cond_residual', # Conditioned residual block 'compress_space', 'cond_residual', 'cond_linear_attend_space', # Conditioned linear attention 'compress_space', 'cond_residual', 'cond_attend_space', # Conditioned full attention 'compress_time', 'cond_residual', 'compress_time', 'cond_residual', 'cond_attend_time', # Conditioned temporal attention ) ) # Create video and conditioning tensor video = torch.randn(1, 3, 17, 128, 128) cond = torch.randn(1, 256) # Conditioning signal (e.g., text embedding) # Forward pass with conditioning recon = tokenizer(video, cond=cond) print(f"Conditional reconstruction shape: {recon.shape}") # Training with conditioning loss, breakdown = tokenizer(video, cond=cond, return_loss=True) # Encoding/decoding with conditioning codes = tokenizer.tokenize(video) # Note: tokenize doesn't use cond decoded = tokenizer.decode_from_code_indices(codes, cond=cond) ``` -------------------------------- ### Tokenize and Decode Video Source: https://context7.com/lucidrains/magvit2-pytorch/llms.txt Basic usage for tokenizing a video tensor and decoding it back from indices. ```python codes = tokenizer.tokenize(video) decoded = tokenizer.decode_from_code_indices(codes) ``` -------------------------------- ### Tokenize Video to Discrete Codes Source: https://context7.com/lucidrains/magvit2-pytorch/llms.txt Encodes a video tensor into discrete token indices using the `tokenize` method. This is essential for processing videos with language models. ```python import torch from magvit2_pytorch import VideoTokenizer tokenizer = VideoTokenizer( image_size = 128, init_dim = 64, max_dim = 512, codebook_size = 1024, layers = ( 'residual', 'compress_space', ('consecutive_residual', 2), 'compress_space', ('consecutive_residual', 2), 'compress_space', ('consecutive_residual', 2), 'compress_time', ('consecutive_residual', 2), 'compress_time', ('consecutive_residual', 2), ) ) # Input video: (batch, channels, frames, height, width) video = torch.randn(1, 3, 17, 128, 128) # Tokenize video to discrete codes # With 3x spatial compression (8x) and 2x temporal compression (4x): # - Spatial: 128 -> 16 ``` -------------------------------- ### Encode Video to Latent Space Source: https://context7.com/lucidrains/magvit2-pytorch/llms.txt Encodes video into continuous latent representations before quantization using the `encode` method. This is useful for analysis or custom quantization. The method can also return quantized latents, codes, and auxiliary loss when `quantize=True`. ```python import torch from magvit2_pytorch import VideoTokenizer tokenizer = VideoTokenizer( image_size = 128, init_dim = 64, max_dim = 512, codebook_size = 1024, layers = ( 'residual', 'compress_space', ('consecutive_residual', 2), 'compress_space', ('consecutive_residual', 2), 'compress_space', ('consecutive_residual', 2), 'compress_time', ('consecutive_residual', 2), 'compress_time', ('consecutive_residual', 2), ) ) video = torch.randn(1, 3, 17, 128, 128) # Encode to continuous latent space (before quantization) latent = tokenizer.encode(video, quantize=False) print(f"Continuous latent shape: {latent.shape}") # Encode with quantization applied quantized, codes, aux_loss = tokenizer.encode(video, quantize=True) print(f"Quantized latent shape: {quantized.shape}") print(f"Codes shape: {codes.shape}") print(f"Auxiliary loss: {aux_loss.item():.4f}") ``` -------------------------------- ### Decode Video from Code Indices Source: https://context7.com/lucidrains/magvit2-pytorch/llms.txt Reconstructs a video from discrete token codes using the `decode_from_code_indices` method. This is useful for converting language model outputs back into video sequences. Flattened codes can also be used for decoding. ```python import torch from magvit2_pytorch import VideoTokenizer tokenizer = VideoTokenizer( image_size = 128, init_dim = 64, max_dim = 512, codebook_size = 1024, layers = ( 'residual', 'compress_space', ('consecutive_residual', 2), 'compress_space', ('consecutive_residual', 2), 'compress_space', ('consecutive_residual', 2), 'compress_time', ('consecutive_residual', 2), 'compress_time', ('consecutive_residual', 2), ) ) video = torch.randn(1, 3, 17, 128, 128) # First tokenize the video codes = tokenizer.tokenize(video) print(f"Codes shape: {codes.shape}") # Decode from code indices back to video decoded_video = tokenizer.decode_from_code_indices(codes) print(f"Decoded video shape: {decoded_video.shape}") # Verify reconstruction matches direct forward pass direct_recon = tokenizer(video, return_recon=True) codes_with_recon, recon_from_forward = tokenizer(video, return_codes=True, return_recon=True) print(f"Codes match: {torch.allclose(codes, codes_with_recon)}") # Flattened codes also work flattened_codes = codes.flatten(start_dim=1) # Shape: (1, 1280) decoded_from_flat = tokenizer.decode_from_code_indices(flattened_codes) print(f"Decoded from flat shape: {decoded_from_flat.shape}") ``` -------------------------------- ### Calculate Discriminator Loss Source: https://context7.com/lucidrains/magvit2-pytorch/llms.txt Computes the discriminator loss, multiscale losses, and gradient penalty for a given video input. ```python discr_loss, discr_breakdown = tokenizer( video, return_discr_loss=True, apply_gradient_penalty=True ) print(f"Discriminator loss: {discr_loss.item():.4f}") print(f"Multiscale discr losses: {[l.item() for l in discr_breakdown.multiscale_discr_losses]}") print(f"Gradient penalty: {discr_breakdown.gradient_penalty.item():.4f}") ``` -------------------------------- ### VideoTokenizer.decode_from_code_indices Source: https://context7.com/lucidrains/magvit2-pytorch/llms.txt Reconstructs a video from discrete token codes. This is used during generation to convert language model outputs back to video. ```APIDOC ## VideoTokenizer.decode_from_code_indices - Reconstruct Video from Codes ### Description The `decode_from_code_indices` method reconstructs a video from discrete token codes. This is used during generation to convert language model outputs back to video. ### Method POST (or similar, depending on implementation) ### Endpoint `/api/video/decode` (example endpoint) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **codes** (torch.Tensor) - Required - The discrete token codes representing the video. - **flattened_codes** (torch.Tensor) - Optional - Flattened token codes if not in the standard shape. ### Request Example ```json { "codes": "[tensor data]" } ``` ### Response #### Success Response (200) - **decoded_video** (torch.Tensor) - The reconstructed video tensor. #### Response Example ```json { "decoded_video": "[tensor data]" } ``` ``` -------------------------------- ### VideoTokenizer.encode Source: https://context7.com/lucidrains/magvit2-pytorch/llms.txt Produces continuous latent representations before quantization. Useful for analysis or custom quantization schemes. ```APIDOC ## VideoTokenizer.encode - Encode Video to Latent Space ### Description The `encode` method produces continuous latent representations before quantization. Useful for analysis or custom quantization schemes. ### Method POST (or similar, depending on implementation) ### Endpoint `/api/video/encode` (example endpoint) ### Parameters #### Path Parameters None #### Query Parameters - **quantize** (boolean) - Optional - Whether to apply quantization to the latent representation. Defaults to False. #### Request Body - **video** (torch.Tensor) - Required - The input video tensor. ### Request Example ```json { "video": "[tensor data]" } ``` ### Response #### Success Response (200) - **latent** (torch.Tensor) - The continuous latent representation (if quantize=False). - **quantized** (torch.Tensor) - The quantized latent representation (if quantize=True). - **codes** (torch.Tensor) - The discrete token codes (if quantize=True). - **aux_loss** (float) - Auxiliary loss value (if quantize=True). #### Response Example ```json { "quantized": "[tensor data]", "codes": "[tensor data]", "aux_loss": 0.1234 } ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.