### Install Muse MaskGit PyTorch Source: https://github.com/lucidrains/muse-maskgit-pytorch/blob/main/README.md Install the package via pip. ```bash $ pip install muse-maskgit-pytorch ``` -------------------------------- ### Get T5 Model and Tokenizer Source: https://context7.com/lucidrains/muse-maskgit-pytorch/llms.txt Retrieve and cache the T5 model and its corresponding tokenizer for direct use. This is efficient for repeated access. ```python # Get model and tokenizer directly (cached for reuse) model, tokenizer = get_model_and_tokenizer('t5-small') ``` -------------------------------- ### Get T5 Encoded Dimension Source: https://context7.com/lucidrains/muse-maskgit-pytorch/llms.txt Retrieve the embedding dimension for various T5 model sizes without loading the model. Supports 't5-small', 't5-base', 't5-large', and a default T5 name. ```python import torch from muse_maskgit_pytorch.t5 import ( t5_encode_text, get_encoded_dim, get_model_and_tokenizer, DEFAULT_T5_NAME ) # Get the embedding dimension for a T5 model (without loading the model) dim_small = get_encoded_dim('t5-small') # 512 dim_base = get_encoded_dim('t5-base') # 768 dim_large = get_encoded_dim('t5-large') # 1024 dim_default = get_encoded_dim(DEFAULT_T5_NAME) # google/t5-v1_1-base = 768 ``` -------------------------------- ### Initialize MaskGit Model Source: https://context7.com/lucidrains/muse-maskgit-pytorch/llms.txt Set up the complete MaskGit text-to-image generation model by combining a VAE and a MaskGitTransformer. Configure parameters like image size, conditioning dropout, and self-conditioning probability. ```python import torch from muse_maskgit_pytorch import VQGanVAE, MaskGit, MaskGitTransformer # Initialize VAE and load pretrained weights vae = VQGanVAE(dim=256, codebook_size=65536).cuda() vae.load('./pretrained_vae.pt') # Create transformer for base generation transformer = MaskGitTransformer( num_tokens=65536, seq_len=256, # 16x16 feature map dim=512, depth=8, dim_head=64, heads=8, ff_mult=4, t5_name='t5-small' ) # Create MaskGit model maskgit = MaskGit( vae=vae, transformer=transformer, image_size=256, # Output image size cond_drop_prob=0.25, # Classifier-free guidance dropout self_cond_prob=0.9, # Self-conditioning probability during training no_mask_token_prob=0.0, # Probability of keeping tokens unmasked critic_loss_weight=1.0 # Weight for token critic loss (if using) ).cuda() ``` -------------------------------- ### Initialize and Run VQGanVAETrainer Source: https://context7.com/lucidrains/muse-maskgit-pytorch/llms.txt Initializes the VQGanVAETrainer for managing the training loop of the VQGanVAE. This trainer integrates with Accelerate for distributed training and handles checkpointing and validation. ```python import torch from muse_maskgit_pytorch import VQGanVAE, VQGanVAETrainer # Create the VAE model vae = VQGanVAE( dim=256, codebook_size=65536, use_vgg_and_gan=True ) # Initialize the trainer with training configuration trainer = VQGanVAETrainer( vae=vae, folder='/path/to/image/dataset', # Folder containing training images image_size=128, # Training image size (can curriculum learn to larger sizes) batch_size=4, # Batch size per GPU grad_accum_every=8, # Gradient accumulation steps num_train_steps=50000, # Total training steps lr=3e-4, # Learning rate for both VAE and discriminator valid_frac=0.05, # Fraction for validation split save_results_every=100, # Save reconstruction samples every N steps save_model_every=1000, # Save model checkpoint every N steps results_folder='./vae_results', # Output directory use_ema=True, # Use exponential moving average ema_beta=0.995, # EMA decay rate max_grad_norm=1.0, # Gradient clipping for VAE discr_max_grad_norm=1.0, # Gradient clipping for discriminator apply_grad_penalty_every=4, # Apply gradient penalty every N steps accelerate_kwargs=dict() # Additional Accelerate configuration ).cuda() # Run training - automatically handles distributed training if launched with accelerate trainer.train() # Training loop with custom logging def custom_log(logs): print(f"VAE Loss: {logs.get('loss', 'N/A')}, Discr Loss: {logs.get('discr_loss', 'N/A')}") trainer.train(log_fn=custom_log) ``` -------------------------------- ### Train VQGanVAE Source: https://github.com/lucidrains/muse-maskgit-pytorch/blob/main/README.md Instantiate and train a VQGanVAE on a folder of images. ```python import torch from muse_maskgit_pytorch import VQGanVAE, VQGanVAETrainer vae = VQGanVAE( dim = 256, codebook_size = 65536 ) # train on folder of images, as many images as possible trainer = VQGanVAETrainer( vae = vae, image_size = 128, # you may want to start with small images, and then curriculum learn to larger ones, but because the vae is all convolution, it should generalize to 512 (as in paper) without training on it folder = '/path/to/images', batch_size = 4, grad_accum_every = 8, num_train_steps = 50000 ).cuda() trainer.train() ``` -------------------------------- ### Initialize and Use VQGanVAE Source: https://context7.com/lucidrains/muse-maskgit-pytorch/llms.txt Initializes the VQGanVAE model for learning discrete image representations. Use this for encoding images to tokens, decoding tokens back to images, and computing reconstruction and GAN losses. ```python import torch from muse_maskgit_pytorch import VQGanVAE # Initialize the VQGanVAE with standard configuration vae = VQGanVAE( dim=256, # Base model dimension channels=3, # RGB images layers=4, # Number of encoder/decoder layers codebook_size=65536, # Size of the discrete codebook lookup_free_quantization=True, # Use LFQ for improved codebook usage use_vgg_and_gan=True, # Enable perceptual and GAN losses l2_recon_loss=False, # Use L1 reconstruction loss discr_layers=4 # Discriminator depth ).cuda() # Encode images to discrete tokens images = torch.randn(4, 3, 256, 256).cuda() encoded_fmap, token_ids, vq_loss = vae.encode(images) # token_ids shape: (4, 16, 16) - discrete token indices # Decode from token IDs back to images reconstructed = vae.decode_from_ids(token_ids) # reconstructed shape: (4, 3, 256, 256) # Forward pass with loss computation for training loss = vae(images, return_loss=True) loss.backward() # Get discriminator loss separately discr_loss = vae(images, return_discr_loss=True, add_gradient_penalty=True) discr_loss.backward() # Save and load model weights vae.save('./vae_checkpoint.pt') vae.load('./vae_checkpoint.pt') # Get feature map size for a given image size fmap_size = vae.get_encoded_fmap_size(256) # Returns 16 for 256x256 images with 4 layers ``` -------------------------------- ### Initialize and Use Unified Muse Model Source: https://github.com/lucidrains/muse-maskgit-pytorch/blob/main/README.md Load trained base and super-resolution MaskGit models. Initialize the unified Muse model by passing these trained models. Generate images by providing a list of text prompts. ```python from muse_maskgit_pytorch import Muse base_maskgit.load('./path/to/base.pt') superres_maskgit.load('./path/to/superres.pt') # pass in the trained base_maskgit and superres_maskgit from above muse = Muse( base = base_maskgit, superres = superres_maskgit ) images = muse([ 'a whale breaching from afar', 'young girl blowing out candles on her birthday cake', 'fireworks with blue and green sparkles', 'waking up to a psychedelic landscape' ]) images # List[PIL.Image.Image] ``` -------------------------------- ### Initialize and Train Super-Resolution MaskGit Source: https://context7.com/lucidrains/muse-maskgit-pytorch/llms.txt Configures a MaskGit model for super-resolution tasks and demonstrates training with conditioning images. ```python # Create super-resolution MaskGit superres_maskgit = MaskGit( vae=vae, transformer=superres_transformer, image_size=512, # Output image size (larger than base) cond_image_size=256, # Conditioning image size (from base model) cond_drop_prob=0.25 ).cuda() # Training with conditioning images high_res_images = torch.randn(4, 3, 512, 512).cuda() texts = ['a detailed landscape painting'] * 4 loss = superres_maskgit(high_res_images, texts=texts) loss.backward() # Generate super-resolution images from low-res conditioning low_res_images = torch.randn(4, 3, 256, 256).cuda() # From base model cond_images = F.interpolate(low_res_images, 256, mode='bilinear') superres_images = superres_maskgit.generate( texts=[ 'a whale breaching from afar', 'young girl blowing out candles', 'fireworks with sparkles', 'psychedelic landscape' ], cond_images=cond_images, # Low-res conditioning images cond_scale=3.0, timesteps=18 ) # superres_images shape: (4, 3, 512, 512) ``` -------------------------------- ### Train Base MaskGit Model Source: https://github.com/lucidrains/muse-maskgit-pytorch/blob/main/README.md Prepare training data with text descriptions and corresponding images. Feed them into the MaskGit instance with return_loss set to True for training. Backpropagate the loss and repeat for extended training. ```python texts = [ 'a child screaming at finding a worm within a half-eaten apple', 'lizard running across the desert on two feet', 'waking up to a psychedelic landscape', 'seashells sparkling in the shallow waters' ] images = torch.randn(4, 3, 512, 512).cuda() # feed it into your maskgit instance, with return_loss set to True loss = superres_maskgit( images, texts = texts ) loss.backward() ``` -------------------------------- ### Initialize MaskGitTransformer Source: https://context7.com/lucidrains/muse-maskgit-pytorch/llms.txt Instantiate the MaskGitTransformer for masked image token prediction. Ensure num_tokens matches the VAE codebook size and seq_len corresponds to the feature map size squared. Supports T5 text conditioning and self-conditioning. ```python import torch from muse_maskgit_pytorch import MaskGitTransformer # Create the MaskGit transformer transformer = MaskGitTransformer( num_tokens=65536, # Must match VAE codebook size seq_len=256, # Sequence length = fmap_size^2 (16x16 = 256) dim=512, # Model dimension depth=8, # Number of transformer layers dim_head=64, # Attention head dimension heads=8, # Number of attention heads ff_mult=4, # Feedforward expansion factor t5_name='t5-small', # T5 model for text encoding ('t5-small', 't5-base', 't5-large') self_cond=True # Enable self-conditioning for improved quality ).cuda() ``` -------------------------------- ### Implement End-to-End Muse Pipeline Source: https://context7.com/lucidrains/muse-maskgit-pytorch/llms.txt Combines base and super-resolution models into a single Muse pipeline for high-resolution text-to-image generation. ```python import torch from muse_maskgit_pytorch import VQGanVAE, MaskGit, MaskGitTransformer, Muse # Setup base model vae = VQGanVAE(dim=256, codebook_size=65536).cuda() vae.load('./pretrained_vae.pt') base_transformer = MaskGitTransformer( num_tokens=65536, seq_len=256, dim=512, depth=8, dim_head=64, heads=8, t5_name='t5-small' ) base_maskgit = MaskGit( vae=vae, transformer=base_transformer, image_size=256, cond_drop_prob=0.25 ).cuda() base_maskgit.load('./base_maskgit.pt') # Setup super-resolution model superres_transformer = MaskGitTransformer( num_tokens=65536, seq_len=1024, dim=512, depth=8, dim_head=64, heads=8, t5_name='t5-small' ) superres_maskgit = MaskGit( vae=vae, transformer=superres_transformer, image_size=512, cond_image_size=256, cond_drop_prob=0.25 ).cuda() superres_maskgit.load('./superres_maskgit.pt') # Create end-to-end Muse model muse = Muse( base=base_maskgit, superres=superres_maskgit ) # Generate high-resolution images from text images = muse( texts=[ 'a whale breaching from afar', 'young girl blowing out candles on her birthday cake', 'fireworks with blue and green sparkles', 'waking up to a psychedelic landscape' ], cond_scale=3.0, # Classifier-free guidance scale temperature=1.0, # Sampling temperature timesteps=18, # Base model timesteps superres_timesteps=18, # Super-resolution timesteps return_pil_images=True, # Return PIL Images instead of tensors return_lowres=False # Only return final high-res images ) # images: List[PIL.Image.Image] of 512x512 images # Get both low-res and high-res outputs superres_images, lowres_images = muse( texts=['beautiful mountain landscape'], return_lowres=True, return_pil_images=True ) # superres_images: List[PIL.Image.Image] at 512x512 # lowres_images: List[PIL.Image.Image] at 256x256 ``` -------------------------------- ### Initialize MaskGitTransformer for Super-Resolution Source: https://context7.com/lucidrains/muse-maskgit-pytorch/llms.txt Configure the MaskGitTransformer for super-resolution tasks by setting a larger sequence length to handle higher resolution feature maps. Ensure VAE is also initialized appropriately. ```python import torch import torch.nn.functional as F from muse_maskgit_pytorch import VQGanVAE, MaskGit, MaskGitTransformer # Initialize VAE for super-resolution vae = VQGanVAE(dim=256, codebook_size=65536).cuda() vae.load('./pretrained_vae.pt') # Create transformer for super-resolution (larger sequence length) superres_transformer = MaskGitTransformer( num_tokens=65536, seq_len=1024, # 32x32 feature map for 512px output dim=512, depth=8, dim_head=64, heads=8, ff_mult=4, t5_name='t5-small' ) ``` -------------------------------- ### Generate Images with Super-Resolution MaskGit Source: https://github.com/lucidrains/muse-maskgit-pytorch/blob/main/README.md Generate images using the trained super-resolution MaskGit model. Conditioning images are required and must be passed in, interpolated to the appropriate size. Adjust cond_scale for generation. ```python images = superres_maskgit.generate( texts = [ 'a whale breaching from afar', 'young girl blowing out candles on her birthday cake', 'fireworks with blue and green sparkles', 'waking up to a psychedelic landscape' ], cond_images = F.interpolate(images, 256), # conditioning images must be passed in for generating from superres cond_scale = 3. ) images.shape # (4, 3, 512, 512) ``` -------------------------------- ### Train MaskGit Transformer Source: https://github.com/lucidrains/muse-maskgit-pytorch/blob/main/README.md Load a trained VAE and train a MaskGit transformer for text-to-image generation. ```python import torch from muse_maskgit_pytorch import VQGanVAE, MaskGit, MaskGitTransformer # first instantiate your vae vae = VQGanVAE( dim = 256, codebook_size = 65536 ).cuda() vae.load('/path/to/vae.pt') # you will want to load the exponentially moving averaged VAE # then you plug the vae and transformer into your MaskGit as so # (1) create your transformer / attention network transformer = MaskGitTransformer( num_tokens = 65536, # must be same as codebook size above seq_len = 256, # must be equivalent to fmap_size ** 2 in vae dim = 512, # model dimension depth = 8, # depth dim_head = 64, # attention head dimension heads = 8, # attention heads, ff_mult = 4, # feedforward expansion factor t5_name = 't5-small', # name of your T5 ) # (2) pass your trained VAE and the base transformer to MaskGit base_maskgit = MaskGit( vae = vae, # vqgan vae transformer = transformer, # transformer image_size = 256, # image size cond_drop_prob = 0.25, # conditional dropout, for classifier free guidance ).cuda() # ready your training text and images texts = [ 'a child screaming at finding a worm within a half-eaten apple', 'lizard running across the desert on two feet', 'waking up to a psychedelic landscape', 'seashells sparkling in the shallow waters' ] images = torch.randn(4, 3, 256, 256).cuda() # feed it into your maskgit instance, with return_loss set to True loss = base_maskgit( images, texts = texts ) loss.backward() # do this for a long time on much data # then... images = base_maskgit.generate(texts = [ 'a whale breaching from afar', 'young girl blowing out candles on her birthday cake', 'fireworks with blue and green sparkles' ], cond_scale = 3.) # conditioning scale for classifier free guidance images.shape # (3, 3, 256, 256) ``` -------------------------------- ### MaskGitTransformer Inference with Classifier-Free Guidance Source: https://context7.com/lucidrains/muse-maskgit-pytorch/llms.txt Perform inference with the MaskGitTransformer using classifier-free guidance for stronger text conditioning. Adjust cond_scale to control the guidance strength. ```python # Forward with classifier-free guidance at inference scaled_logits = transformer.forward_with_cond_scale( token_ids, texts=texts, cond_scale=3.0 # Guidance scale (higher = stronger text conditioning) ) ``` -------------------------------- ### Configure TokenCritic for Generation Quality Source: https://context7.com/lucidrains/muse-maskgit-pytorch/llms.txt Integrates a TokenCritic to predict token correctness, improving iterative generation quality. ```python import torch from muse_maskgit_pytorch import VQGanVAE, MaskGit, MaskGitTransformer, TokenCritic # Create token critic with same architecture as main transformer token_critic = TokenCritic( num_tokens=65536, seq_len=256, dim=512, depth=4, # Can be shallower than main transformer dim_head=64, heads=8, ff_mult=4, t5_name='t5-small' ).cuda() # Create MaskGit with token critic vae = VQGanVAE(dim=256, codebook_size=65536).cuda() transformer = MaskGitTransformer( num_tokens=65536, seq_len=256, dim=512, depth=8, dim_head=64, heads=8, t5_name='t5-small' ) maskgit_with_critic = MaskGit( vae=vae, transformer=transformer, token_critic=token_critic, # Attach token critic image_size=256, cond_drop_prob=0.25, critic_loss_weight=1.0 # Weight for critic loss ).cuda() # Training includes both generation and critic losses images = torch.randn(4, 3, 256, 256).cuda() texts = ['sample text'] * 4 # Combined loss (generation + critic) total_loss = maskgit_with_critic(images, texts=texts) total_loss.backward() # Train only the generator (freeze critic) gen_loss = maskgit_with_critic(images, texts=texts, train_only_generator=True) # Alternative: Use self token critic (shares weights with transformer) maskgit_self_critic = MaskGit( vae=vae, transformer=transformer, self_token_critic=True, # Use transformer embeddings for critic image_size=256, cond_drop_prob=0.25 ).cuda() ``` -------------------------------- ### MaskGitTransformer Forward Pass with Text Conditioning Source: https://context7.com/lucidrains/muse-maskgit-pytorch/llms.txt Perform a forward pass with the MaskGitTransformer using token IDs and text descriptions for conditioning. Classifier-free guidance can be applied during training using cond_drop_prob. ```python # Forward pass with text conditioning token_ids = torch.randint(0, 65536, (4, 256)).cuda() # Batch of token sequences texts = [ 'a beautiful sunset over mountains', 'a cat sitting on a windowsill', 'abstract colorful painting', 'futuristic city skyline' ] # Get logits for next token prediction logits = transformer( token_ids, texts=texts, cond_drop_prob=0.1 # Dropout probability for classifier-free guidance training ) # logits shape: (4, 256, 65536) ``` -------------------------------- ### MaskGit Image Generation Source: https://context7.com/lucidrains/muse-maskgit-pytorch/llms.txt Generate images from text descriptions using the MaskGit model. Control generation quality and style with parameters like cond_scale, temperature, timesteps, and topk_filter_thres. ```python # Generate images from text descriptions generated_images = maskgit.generate( texts=[ 'a whale breaching from afar', 'young girl blowing out candles on her birthday cake', 'fireworks with blue and green sparkles' ], cond_scale=3.0, # Classifier-free guidance scale temperature=1.0, # Sampling temperature timesteps=18, # Number of iterative denoising steps topk_filter_thres=0.9, # Top-k filtering threshold can_remask_prev_masked=False # Whether to remask previously masked tokens ) # generated_images shape: (3, 3, 256, 256) ``` -------------------------------- ### Save and Load MaskGit Model Source: https://context7.com/lucidrains/muse-maskgit-pytorch/llms.txt Save the trained MaskGit model to a file for later use and load it back into memory. This is essential for resuming training or deploying the model. ```python # Save and load model maskgit.save('./maskgit_base.pt') maskgit.load('./maskgit_base.pt') ``` -------------------------------- ### MaskGit Training Forward Pass Source: https://context7.com/lucidrains/muse-maskgit-pytorch/llms.txt Perform a training forward pass with the MaskGit model, providing images and corresponding text descriptions. The model returns a loss value that can be backpropagated. ```python # Training forward pass images = torch.randn(4, 3, 256, 256).cuda() texts = [ 'a child screaming at finding a worm within a half-eaten apple', 'lizard running across the desert on two feet', 'waking up to a psychedelic landscape', 'seashells sparkling in the shallow waters' ] loss = maskgit(images, texts=texts) loss.backward() ``` -------------------------------- ### Train Super-Resolution MaskGit Source: https://github.com/lucidrains/muse-maskgit-pytorch/blob/main/README.md Configure MaskGit for super-resolution by specifying cond_image_size. ```python import torch import torch.nn.functional as F from muse_maskgit_pytorch import VQGanVAE, MaskGit, MaskGitTransformer # first instantiate your ViT VQGan VAE # a VQGan VAE made of transformers vae = VQGanVAE( dim = 256, codebook_size = 65536 ).cuda() vae.load('./path/to/vae.pt') # you will want to load the exponentially moving averaged VAE # then you plug the VqGan VAE into your MaskGit as so # (1) create your transformer / attention network transformer = MaskGitTransformer( num_tokens = 65536, # must be same as codebook size above seq_len = 1024, # must be equivalent to fmap_size ** 2 in vae dim = 512, # model dimension depth = 2, # depth dim_head = 64, # attention head dimension heads = 8, # attention heads, ff_mult = 4, # feedforward expansion factor t5_name = 't5-small', # name of your T5 ) # (2) pass your trained VAE and the base transformer to MaskGit superres_maskgit = MaskGit( vae = vae, transformer = transformer, cond_drop_prob = 0.25, image_size = 512, # larger image size cond_image_size = 256, # conditioning image size <- this must be set ).cuda() ``` -------------------------------- ### MaskGitTransformer with Pre-computed Text Embeddings Source: https://context7.com/lucidrains/muse-maskgit-pytorch/llms.txt Optimize MaskGitTransformer forward pass by pre-computing text embeddings using the `encode_text` method, then passing these embeddings to the transformer. ```python # Use pre-computed text embeddings for efficiency text_embeds = transformer.encode_text(texts) # Pre-encode texts logits = transformer(token_ids, text_embeds=text_embeds) ``` -------------------------------- ### Manual T5 Tokenization Source: https://context7.com/lucidrains/muse-maskgit-pytorch/llms.txt Perform manual tokenization of text using the T5 tokenizer. This allows for custom processing of input IDs and attention masks, with options for padding, truncation, and tensor format. ```python # Manual tokenization for custom processing encoded = tokenizer.batch_encode_plus( texts, return_tensors='pt', padding='longest', max_length=256, truncation=True ) input_ids = encoded.input_ids attention_mask = encoded.attention_mask ``` -------------------------------- ### Encode Batch of Texts with T5 Source: https://context7.com/lucidrains/muse-maskgit-pytorch/llms.txt Encode a list of texts into their T5 embeddings. Allows specifying the output device for the embeddings. ```python # Encode batch of texts texts = [ 'a majestic lion in the savanna', 'underwater coral reef with colorful fish', 'futuristic cityscape at night', 'cozy cabin in snowy mountains' ] text_embeddings = t5_encode_text( texts, name='t5-base', output_device='cuda' # Move output to specific device ) # text_embeddings shape: (4, seq_len, 768) ``` -------------------------------- ### Encode Single Text with T5 Source: https://context7.com/lucidrains/muse-maskgit-pytorch/llms.txt Encode a single piece of text into its T5 embedding representation using a specified T5 model size. ```python # Encode single text text_embedding = t5_encode_text( 'a beautiful sunset over the ocean', name='t5-small' ) # text_embedding shape: (1, seq_len, 512) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.