### Setup FLAVA Repository and Install Dependencies Source: https://github.com/facebookresearch/multimodal/blob/main/examples/flava/README.md This snippet outlines the steps to clone the FLAVA repository, install the multimodal library, and set up project-specific requirements using pip. It assumes a conda environment is already set up. ```bash git clone https://github.com/facebookresearch/multimodal.git cd multimodal pip install -e . cd examples pip install -r flava/requirements.txt ``` -------------------------------- ### Install PyTorch and Multimodal Package Source: https://github.com/facebookresearch/multimodal/blob/main/examples/omnivore/README.md Installs necessary PyTorch versions and the multimodal package from source. Also installs specific versions of scipy and av libraries required for certain functionalities. ```bash conda install pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch-nightly cd pip install -e . pip install scipy==1.8.1 av==9.2.0 ``` -------------------------------- ### Install Pre-commit Hooks (Shell) Source: https://github.com/facebookresearch/multimodal/blob/main/CONTRIBUTING.md Installs pre-commit hooks to maintain code style consistency and prevent common errors before each commit. This is a one-time setup for the development environment. ```shell pre-commit install ``` -------------------------------- ### Run Omnivore Training Command Source: https://github.com/facebookresearch/multimodal/blob/main/examples/omnivore/README.md Example command to initiate the training process for the Omnivore model across image, video, and RGB-D modalities. It specifies distributed training parameters, data paths, learning rates, epochs, and various augmentation techniques. ```bash torchrun --nproc_per_node=8 --nnodes=8 -m omnivore.train \ --batch-size=128 --workers=6 --extra-video-dataloader-workers=6 \ --cache-video-dataset --eval-every-num-epoch=10 --model="omnivore_swin_t" \ --lr=0.002 --lr-warmup-epochs=25 --lr-warmup-method=linear \ --lr-scheduler="cosineannealinglr" --lr-min=0.0000001 \ --epochs=500 --weight-decay=0.05 \ --label-smoothing=0.1 --mixup-alpha=0.8 --cutmix-alpha=1.0 \ --train-crop-size=224 --val-resize-size=224 \ --opt="adamw" --random-erase=0.25 \ --color-jitter-factor 0.4 0.4 0.4 0.4 \ --model-ema --model-ema-steps=1 --model-ema-decay=0.99998 \ --video-grad-accum-iter=32 \ --modalities image video rgbd \ --val-data-sampling-factor 1 1 1 \ --train-data-sampling-factor 1 1 10 \ --imagenet-data-path="${IMAGENET_PATH}" \ --kinetics-data-path="${KINETICS_PATH}" \ --sunrgbd-data-path="${SUNRGBD_PATH}" \ ``` -------------------------------- ### Install MUGEN Dependencies Source: https://github.com/facebookresearch/multimodal/blob/main/examples/mugen/README.md Installs the necessary PyTorch and project-specific dependencies required to run MUGEN multimodal models. This assumes a Conda environment is already active. ```bash conda install -c pytorch torchtext pip install -r examples/mugen/requirements.txt ``` -------------------------------- ### Setup MDETR Repository Source: https://github.com/facebookresearch/multimodal/blob/main/examples/mdetr/LoadAndComparePretrainedWeights.ipynb Clones the official MDETR GitHub repository into a specified directory. This is necessary to access the MDETR classes and functions required for model loading. ```bash # Install MDETR repo !git clone https://github.com/ashkamath/mdetr.git $repo_dir ``` -------------------------------- ### Run FLAVA Training on a Single Node with Torchrun Source: https://github.com/facebookresearch/multimodal/blob/main/examples/flava/native/README.md Provides the command to initiate FLAVA model training on a single compute node using torchrun. This example specifies the number of GPUs to use per node and points to the main training script and configuration file. ```bash NUM_GPUS=8; torchrun --nproc_per_node=$NUM_GPUS -m flava.native.train config=flava/native/configs/pretrain_debug.yaml ``` -------------------------------- ### Run Linters and Formatters (Python/Shell) Source: https://github.com/facebookresearch/multimodal/blob/main/CONTRIBUTING.md Installs development requirements and runs flake8 for linting and ufmt for code formatting on specified directories. It also shows how to run these tools on modified files only. ```python pip install -r dev-requirements.txt flake8 (examples|test|torchmultimodal) ufmt format (examples|test|torchmultimodal) ``` ```shell flake8 `git diff main --name-only` ufmt format `git diff main --name-only` ``` -------------------------------- ### Download and Setup Flickr30k Dataset for MDETR Source: https://github.com/facebookresearch/multimodal/blob/main/examples/mdetr/README.md This snippet outlines the steps to download and prepare the Flickr30k dataset, including images, annotations, and split mappings, required for MDETR phrase grounding tasks. It also shows how to create symbolic links for dataset splits and configure paths in a JSON file. ```bash tar -xvzf flickr30k-images.tar.gz # Note that MDETR will expect separate directories for each dataset split, but you can just create symlinks. ln -s flickr30k-images flickr30k-images/train ln -s flickr30k-images flickr30k-images/val ln -s flickr30k-images flickr30k-images/test wget https://github.com/BryanPlummer/flickr30k_entities/blob/master/annotations.zip unzip annotations.zip wget https://github.com/BryanPlummer/flickr30k_entities/blob/master/train.txt wget https://github.com/BryanPlummer/flickr30k_entities/blob/master/val.txt wget https://github.com/BryanPlummer/flickr30k_entities/blob/master/test.txt wget https://zenodo.org/record/4729015/files/mdetr_annotations.tar.gz?download=1 tar -xvzf 'mdetr_annotations.tar.gz?download=1' ``` ```json { "combine_datasets": ["flickr"], "combine_datasets_val": ["flickr"], "GT_type" : "separate", "flickr_img_path" : "/data/flickr30k-images", "flickr_dataset_path" : "/data/flickr30k/", "flickr_ann_path" : "/data/OpenSource" } ``` -------------------------------- ### Install Development Dependencies (Python) Source: https://github.com/facebookresearch/multimodal/blob/main/CONTRIBUTING.md Installs the necessary development dependencies for the TorchMultimodal project using pip. This command ensures all required packages for development are available. ```python pip install -e ".[dev]" ``` -------------------------------- ### GET /models/mdetr/phrase-grounding Source: https://github.com/facebookresearch/multimodal/blob/main/examples/mdetr/MDETRTutorial.ipynb Initializes the MDETR model for phrase grounding tasks and loads pretrained weights from a remote checkpoint. ```APIDOC ## GET /models/mdetr/phrase-grounding ### Description Initializes the MDETR model specifically configured for phrase grounding and loads the pretrained ResNet101 weights. ### Method GET ### Endpoint /models/mdetr/phrase-grounding ### Parameters #### Query Parameters - **checkpoint_url** (string) - Required - The URL to the pretrained model weights (e.g., https://pytorch.s3.amazonaws.com/models/multimodal/mdetr/pretrained_resnet101_checkpoint.pth) ### Request Example { "checkpoint_url": "https://pytorch.s3.amazonaws.com/models/multimodal/mdetr/pretrained_resnet101_checkpoint.pth" } ### Response #### Success Response (200) - **model** (object) - The initialized MDETR model instance loaded with weights. #### Response Example { "status": "success", "message": "Model loaded successfully" } ``` -------------------------------- ### Override FLAVA Pretraining Configuration Source: https://github.com/facebookresearch/multimodal/blob/main/examples/flava/README.md Example of how to override specific parameters in the FLAVA pretraining configuration using dot notation. This demonstrates changing the maximum training steps. ```python python -m flava.train config=flava/configs/pretraining/debug.yaml training.lightning.max_steps=1000 ``` -------------------------------- ### Masked Autoencoder (MAE) Initialization and Usage (Python) Source: https://context7.com/facebookresearch/multimodal/llms.txt Illustrates the use of Masked Autoencoders (MAE) for vision transformers, including both image and audio variants. The code shows how to initialize an image MAE model, calculate reconstruction loss, and use a pretrained encoder for downstream tasks. It also includes an example for the audio MAE. ```python import torch from torchmultimodal.models.masked_auto_encoder.model import ( vit_l_16_image_mae, vit_b_16_image_mae_encoder, vit_b_16_audio_mae ) from torchmultimodal.modules.losses.reconstruction_loss import ReconstructionLoss # Create Image MAE model mae_model = vit_l_16_image_mae() mae_loss = ReconstructionLoss(normalize_target=True) # Training mode - masks 75% of patches mae_model.train() images = torch.randn(8, 3, 224, 224) output = mae_model(images) # output.encoder_output: encoder representations # output.decoder_pred: reconstructed patches # output.label_patches: original patchified image # output.mask: which patches were masked (1=masked, 0=visible) loss = mae_loss(output.decoder_pred, output.label_patches, output.mask) print(f"Reconstruction loss: {loss.item()}") # Use pretrained encoder for downstream tasks encoder = vit_b_16_image_mae_encoder(pretrained=True) encoder.eval() with torch.no_grad(): features = encoder(images) # features.last_hidden_state: (batch, num_patches+1, hidden_dim) cls_token = features.last_hidden_state[:, 0] # CLS token for classification # Audio MAE for spectrograms audio_mae = vit_b_16_audio_mae() spectrograms = torch.randn(4, 1, 1024, 128) # (batch, channels, time, freq) audio_output = audio_mae(spectrograms) ``` -------------------------------- ### Initialize ALBEF Model and Environment Source: https://github.com/facebookresearch/multimodal/blob/main/examples/albef/retrieval_with_albef.ipynb Sets up the necessary environment, downloads retrieval data, and initializes the ALBEF model using a configuration file and pre-trained checkpoints. ```python import json import torch from PIL import Image from ruamel import yaml from examples.albef.model import albef_model_for_retrieval from examples.albef.data.transforms import ALBEFTextTransform, testing_image_transform data_dir = "./retrieval_data/" !wget https://download.pytorch.org/torchmultimodal/examples/albef/assets/retrieval_data.zip !unzip retrieval_data.zip config = yaml.load(open("./configs/retrieval.yaml", "r"), Loader=yaml.Loader) model = albef_model_for_retrieval(config) checkpoint_url = "https://download.pytorch.org/models/multimodal/albef/finetuned_retrieval_checkpoint.pt" checkpoint = torch.hub.load_state_dict_from_url(checkpoint_url, map_location="cpu") model.load_state_dict(checkpoint) ``` -------------------------------- ### Initialize and Configure MDETR Model Source: https://github.com/facebookresearch/multimodal/blob/main/examples/mdetr/MDETRTutorial.ipynb Demonstrates how to swap components like the text encoder and class embedding, and how to instantiate the MDETR model with custom parameters. ```python from transformers import DistilBertModel distilbert_text_encoder = DistilBertModel.from_pretrained("distilbert-base-uncased") mlp_class_embed = MLP(transformer_dim, num_classes + 1, [transformer_dim] * 2, activation=nn.GELU, dropout=0.1) modified_mdetr = MDETR( image_backbone = image_backbone, text_encoder = distilbert_text_encoder, transformer = transformer, pos_embed = pos_embed, text_projection = text_projection, image_projection = image_projection, query_embed = query_embed, bbox_embed = bbox_embed, class_embed = class_embed, ) ``` -------------------------------- ### Launch FLAVA Finetuning Source: https://github.com/facebookresearch/multimodal/blob/main/examples/flava/README.md Command to launch the finetuning process for the FLAVA model using a specified configuration file and enabling pretrained weights. ```python python -m flava.finetune config=flava/configs/finetuning/qnli.yaml model.pretrained=True ``` -------------------------------- ### Initialize ALBEF VQA Environment Source: https://github.com/facebookresearch/multimodal/blob/main/examples/albef/vqa_with_albef.ipynb Imports necessary libraries and downloads the required dataset for the VQA task. ```python import json import torch from PIL import Image from ruamel import yaml from examples.albef.model import albef_model_for_vqa from examples.albef.data.transforms import ALBEFTextTransform, testing_image_transform data_dir = "./vqa_data/" !wget https://download.pytorch.org/torchmultimodal/examples/albef/assets/vqa_data.zip !unzip vqa_data.zip ``` -------------------------------- ### Custom Question Inference Example Source: https://github.com/facebookresearch/multimodal/blob/main/examples/mdetr/MDETRTutorial.ipynb This Python example shows how to perform VQA inference with a custom-generated question. It calls `plot_qa_inference_results` with a specific question about an animal in a backpack and provides the answer ID for 'dog'. This demonstrates the flexibility of the function in handling user-defined queries. ```python plot_qa_inference_results(img, "What animal is inside the backpack?", torch.tensor(dataset.answer2id["dog"]))) ``` -------------------------------- ### Generate video from text using MUGEN Source: https://github.com/facebookresearch/multimodal/blob/main/examples/mugen/generation/README.md This snippet demonstrates how to initialize a pre-trained MUGEN model and use the GenerationUtil class to sample video sequences from text prompts. It requires a configured model and device context to perform auto-regressive generation. ```python from torchmultimodal.utils.generate import GenerationUtil from examples.mugen.generation.text_video_gpt import text_video_gpt model = text_video_gpt(video_seq_len=32, pretrained_text_video_gpt_model_key="mugen_L32") generator = GenerationUtil(model) output = generator.sample( ['Mugen moves left to right on a cliff and picks up a gem.'], max_seq_len=512, use_cache=True, causal=True, device=, ) samples = output.decoded ``` -------------------------------- ### VQA Inference Example with Dataset Source: https://github.com/facebookresearch/multimodal/blob/main/examples/mdetr/MDETRTutorial.ipynb This Python code snippet demonstrates how to use the `plot_qa_inference_results` function with a dataset. It iterates through the dataset, extracts an image, question, and answer, and then calls the plotting function to display the inference results. This is a basic usage example for performing VQA on sample data. ```python i = 0 img = dataset[i][0] question = dataset[i][1]["caption"] answer = dataset[i][1]["answer"] plot_qa_inference_results(img, question, answer) ``` -------------------------------- ### Instantiate and Load VideoCLIP Model Source: https://github.com/facebookresearch/multimodal/blob/main/examples/mugen/retrieval/tutorial.ipynb Instantiates the VideoCLIP model and loads pre-trained weights specifically finetuned on the MUGEN dataset from a provided URL. The `text_pretrained` and `video_pretrained` flags are set to False as they are intended for generic pretraining, not finetuning. ```python # Instantiate the VideoCLIP model and load the weights from URL model = videoclip(text_pretrained=False, video_pretrained=False) load_module_from_url(model, "https://pytorch.s3.amazonaws.com/models/multimodal/mugen/videoclip_mugen.pt") ``` -------------------------------- ### Vision Transformer Implementation and Configuration Source: https://context7.com/facebookresearch/multimodal/llms.txt Demonstrates how to instantiate pre-built Vision Transformer models like ViT-B/16 and configure custom ViT architectures. It also shows how to apply global average pooling and handle patch masking for MAE-style training. ```python import torch from torchmultimodal.modules.encoders.vision_transformer import ( vision_transformer, vit_b_16, GlobalAveragePooler ) pooler = GlobalAveragePooler(input_dim=768, output_dim=1000) vit = vit_b_16(pooler=pooler) images = torch.randn(4, 3, 224, 224) output = vit(images) custom_vit = vision_transformer( patch_size=16, hidden_dim=512, n_layer=8, n_head=8, image_size=256 ) mask = torch.randint(0, 2, (4, 256)) output_masked = custom_vit(images, image_patches_mask=mask) ``` -------------------------------- ### Setup Diffusion Hybrid Loss Source: https://github.com/facebookresearch/multimodal/blob/main/torchmultimodal/diffusion_labs/mnist_training.ipynb Initializes the DiffusionHybridLoss function based on a predefined schedule to measure model performance during training. ```python h_loss = DiffusionHybridLoss(schedule) ``` -------------------------------- ### Load MUGEN Checkpoint - Python Source: https://github.com/facebookresearch/multimodal/blob/main/examples/mugen/generation/LoadAndComparePretrainedVQVAE.ipynb Loads a pre-trained Video VQVAE checkpoint from MUGEN into a PyTorch model. It sets up the system path and uses torch.load to load the checkpoint, mapping it to the CPU. ```python import sys import os sys.path.append(home_dir) import torch from torch import nn import mugen ckpt = torch.load( os.path.join(checkpoint_dir, 'generation/video_vqvae/L32/epoch=54-step=599999.ckpt'), map_location=torch.device('cpu') ) ``` -------------------------------- ### Execute MDETR Validation Test Source: https://github.com/facebookresearch/multimodal/blob/main/examples/mdetr/LoadAndComparePretrainedWeights.ipynb A script to instantiate the test class and run the full validation pipeline including setup, inference, and result comparison. ```python tester = TestMDETR() tester.setUp() tester.run_mdetr() tester.run_mm_mdetr() tester.compare_results() ``` -------------------------------- ### Create TorchMultimodal Video VQVAE - Python Source: https://github.com/facebookresearch/multimodal/blob/main/examples/mugen/generation/LoadAndComparePretrainedVQVAE.ipynb Initializes an instance of TorchMultimodal's Video VQVAE model. The 'pretrained_model_key' is set to None, indicating a fresh model instance. ```python from examples.mugen.generation.video_vqvae import video_vqvae_mugen vv_torchmm = video_vqvae_mugen(pretrained_model_key=None) ``` -------------------------------- ### Apply Transforms and Get Model Output Source: https://github.com/facebookresearch/multimodal/blob/main/examples/mugen/retrieval/tutorial.ipynb Applies text and video transforms to the data batch and passes it through the VideoCLIP model in evaluation mode. This step generates embeddings for both text and video modalities. ```python text_transform = BertTextTransform() video_transform = VideoTransform() model.eval() with torch.no_grad(): output = model( text_transform(sample_batch['text']), video_transform(sample_batch['video']) ) ``` -------------------------------- ### CoCa Model Initialization and Forward Pass (Python) Source: https://context7.com/facebookresearch/multimodal/llms.txt Shows how to set up the CoCa (Contrastive Captioner) model for pre-training, combining contrastive learning and generative captioning. It includes creating the base model, wrapping it for pre-training, and performing a forward pass to obtain contrastive and captioning losses. Inference usage is also demonstrated. ```python import torch from torchmultimodal.models.coca.coca_model import coca_vit_b_32, CoCaForPretraining # Create CoCa model model = coca_vit_b_32() # For pretraining with contrastive and captioning losses pretrain_model = CoCaForPretraining(model, pad_idx=0) # Prepare inputs images = torch.randn(4, 3, 224, 224) texts = torch.randint(0, 49408, (4, 77)) # CLIP vocab size text_padding_mask = torch.ones(4, 77, dtype=torch.bool) # Forward pass losses = pretrain_model(images, texts, text_padding_mask) print(f"Contrastive loss: {losses['contrastive'].item()}") print(f"Captioning loss: {losses['captioning'].item()}") # For inference, use the base model model.eval() with torch.no_grad(): output = model(images, texts, text_padding_mask) # output.image_pooled_output: contrastive image embeddings # output.text_pooled_output: contrastive text embeddings # output.multimodal_embeddings: captioning logits ``` -------------------------------- ### Train Masked Autoencoder (MAE) Source: https://github.com/facebookresearch/multimodal/blob/main/README.md Shows the basic training loop for a Masked Autoencoder model. It covers model instantiation, loss function setup, optimizer configuration, and the training iteration process. ```python import torch from torch.utils.data import DataLoader from torchmultimodal.models.masked_auto_encoder.model import vit_l_16_image_mae from torchmultimodal.models.masked_auto_encoder.utils import CosineWithWarmupAndLRScaling from torchmultimodal.modules.losses.reconstruction_loss import ReconstructionLoss from torchmultimodal.transforms.mae_transform import ImagePretrainTransform mae_transform = ImagePretrainTransform() dataset = MyDatasetClass(transforms=mae_transform) dataloader = DataLoader(dataset, batch_size=8) mae_model = vit_l_16_image_mae() mae_loss = ReconstructionLoss() optimizer = torch.optim.AdamW(mae_model.parameters()) lr_scheduler = CosineWithWarmupAndLRScaling(optimizer, max_iters=1000, warmup_iters=100) for batch in dataloader: model_out = mae_model(batch["images"]) loss = mae_loss(model_out.decoder_pred, model_out.label_patches, model_out.mask) loss.backward() optimizer.step() lr_scheduler.step() ``` -------------------------------- ### BLIP-2 Model Initialization and Forward Pass (Python) Source: https://context7.com/facebookresearch/multimodal/llms.txt Demonstrates how to initialize the BLIP-2 model, which uses a Querying Transformer (Q-Former) to connect frozen image encoders and language models. It includes setting up the vision encoder, Q-former, and the main BLIP-2 model, followed by a forward pass with dummy image and text inputs. ```python import torch from torchmultimodal.models.blip2.blip2 import BLIP2 from torchmultimodal.models.blip2.qformer_model import QformerForCLM from torchmultimodal.modules.encoders.vision_transformer import vit_b_16 # Build BLIP-2 components vision_encoder = vit_b_16() qformer = QformerForCLM( dim_q=768, dim_kv=768, dim_feedforward=3072, num_heads=12, num_layers=12, vocab_size=30522, cross_attention_freq=2, ) # Initialize BLIP-2 model model = BLIP2( qformer=qformer, vision_encoder=vision_encoder, dim_q=768, image_encoder_embedding_dim=768, freeze_vision_encoder=True, embedding_dim=256, num_query_token=32 ) # Forward pass with image and text images = torch.randn(2, 3, 224, 224) input_ids = torch.randint(0, 30522, (2, 32)) attention_mask = torch.ones(2, 32) output = model( image=images, input_ids=input_ids, attention_mask=attention_mask ) print(f"Image embeddings: {output.image_embeddings.shape}") # (2, 197, 768) print(f"Image features: {output.image_features.shape}") # (2, 32, 256) print(f"Text features: {output.text_features.shape}") # (2, 256) ``` -------------------------------- ### Prepare Dataset and Tokenization Source: https://github.com/facebookresearch/multimodal/blob/main/examples/mdetr/MDETRTutorial.ipynb Configures the tokenizer and image transformations, then initializes the ModulatedDetection dataset for phrase grounding tasks. ```python from examples.mdetr.data.dataset import ModulatedDetection import torchvision.transforms as T from transformers import RobertaTokenizerFast tokenizer = RobertaTokenizerFast.from_pretrained("roberta-base") img_transform = T.Compose([ T.Resize(800), T.ToTensor(), T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) ]) dataset = ModulatedDetection( img_dir, os.path.join(asset_dir, "flickr30k_sample_annotations.json"), transforms=None, return_tokens=True, tokenizer=tokenizer, is_train=False, ) ``` -------------------------------- ### Get Top 5 Predictions - Python Source: https://github.com/facebookresearch/multimodal/blob/main/examples/omnivore/omnivore_inference_demo.ipynb This snippet takes a model's output and retrieves the top 5 predictions. It uses the model to predict based on the prepared input and then extracts the values and indices of the top predictions. It also maps these indices to human-readable labels using a predefined class list. ```python # Assuming model, sunrgbd_classes, and input_depth are defined preds = model(input_depth, "rgbd") top5_values, top5_indices = preds[0].topk(5) top5_labels = [sunrgbd_classes[index] for index in top5_indices.tolist()] # The correct label is kitchen ``` -------------------------------- ### Launch FLAVA Pretraining with Pretrained Model Source: https://github.com/facebookresearch/multimodal/blob/main/examples/flava/README.md Command to launch FLAVA pretraining, enabling the use of a pretrained model by setting `model.pretrained=True` in the configuration. ```python python -m flava.train config=flava/configs/pretraining/debug.yaml model.pretrained=True ``` -------------------------------- ### Download MDETR Checkpoint Source: https://github.com/facebookresearch/multimodal/blob/main/examples/mdetr/LoadAndComparePretrainedWeights.ipynb Downloads the pretrained ResNet101 checkpoint file from a given URL and saves it to a specified directory. This is a prerequisite for loading the model weights. ```python download_dir = "/data/home/ebs/data/mdetr" repo_dir = "/data/home/ebs" !wget https://zenodo.org/record/4721981/files/pretrained_resnet101_checkpoint.pth?download=1 -P $download_dir ``` -------------------------------- ### Configure Optimizer and Mixed Precision Scaler Source: https://github.com/facebookresearch/multimodal/blob/main/torchmultimodal/diffusion_labs/mnist_training.ipynb Moves models to the target device, initializes the AdamW optimizer, and sets up a GradScaler for mixed-precision training. ```python encoder.to(device) decoder.to(device) optimizer = torch.optim.AdamW([{"params": encoder.parameters()}, {"params": decoder.parameters()}], lr=0.0001) scaler = torch.cuda.amp.GradScaler() ``` -------------------------------- ### Configure MDETR for VQA Tasks Source: https://github.com/facebookresearch/multimodal/blob/main/examples/mdetr/MDETRTutorial.ipynb Demonstrates how to initialize an MDETR model for VQA using the TorchMultimodal builder, including loading pretrained weights and customizing classification heads for specific datasets. ```python from torchmultimodal.models.mdetr.model import mdetr_for_vqa import torch import torch.nn as nn mdetr_vqa = mdetr_for_vqa() other_vqa_heads = nn.ModuleDict({"head1": nn.Linear(256, 3), "head2": nn.Linear(256,12)}) mdetr_other_vqa_dataset = mdetr_for_vqa(vqa_heads=other_vqa_heads) checkpoint = torch.hub.load_state_dict_from_url("https://pytorch.s3.amazonaws.com/models/multimodal/mdetr/gqa_resnet101_checkpoint.pth", map_location="cpu", check_hash=True) mdetr_vqa.load_state_dict(checkpoint["model_ema"], strict=False) mdetr_vqa.eval() ``` -------------------------------- ### Set Local Directories - Python Source: https://github.com/facebookresearch/multimodal/blob/main/examples/mugen/generation/LoadAndComparePretrainedVQVAE.ipynb Defines local directory paths for checkpoints, the MUGEN repository, and the home directory. Users should replace these with their specific paths. ```python checkpoint_dir = '/Users/rafiayub/checkpoints/' repo_dir = '/Users/rafiayub/mugen/' home_dir = '/Users/rafiayub/' ``` -------------------------------- ### Load MUGEN Dataset and DataModule Source: https://github.com/facebookresearch/multimodal/blob/main/examples/mugen/retrieval/tutorial.ipynb Initializes the MUGENDataset and MUGENDataModule for loading and batching data from the MUGEN dataset. It configures dataset arguments such as text description retrieval and game frame inclusion, and sets the batch size for data loading. ```python mugen_args = MUGENDatasetArgs( get_text_desc=True, get_game_frame=True, get_audio=False, get_seg_map=False, use_manual_annotation=True, use_auto_annotation=False, ) datamodule = MUGENDataModule( mugen_args, batch_size=8, shuffle=False, ) ``` -------------------------------- ### Launch FLAVA Debug Pretraining (GPU) Source: https://github.com/facebookresearch/multimodal/blob/main/examples/flava/README.md Command to launch a debug pretraining job for the FLAVA model on a GPU. This uses a specified configuration file and defaults to GPU acceleration. ```python python -m flava.train config=flava/configs/pretraining/debug.yaml ``` -------------------------------- ### Initialize DDPModule Sampler Source: https://github.com/facebookresearch/multimodal/blob/main/torchmultimodal/diffusion_labs/mnist_training.ipynb Initializes the DDPModule sampler, which orchestrates the denoising process. It uses the defined decoder, schedule, predictor, and a set of evaluation steps to generate a denoised image. ```python eval_steps = torch.linspace(0, 999, 250, dtype=torch.int) decoder = DDPModule(decoder, schedule, predictor, eval_steps) ``` -------------------------------- ### Override Training Configuration via Command Line Source: https://github.com/facebookresearch/multimodal/blob/main/examples/flava/native/README.md Demonstrates how to override default configuration settings for FLAVA model training directly from the command line. This allows for dynamic adjustment of parameters like batch size, activation checkpointing, and distributed training strategy. ```bash python -m flava.native.train config=flava/native/configs/pretrain_debug.yaml training.batch_size=8 training.enable_amp=True training.activation_checkpointing=True training.strategy=fsdp ``` -------------------------------- ### Retrieve and Display Video from Text Query Source: https://github.com/facebookresearch/multimodal/blob/main/examples/mugen/retrieval/tutorial.ipynb This sequence demonstrates how to select a text query from a batch, identify the most similar video using a precomputed similarity matrix, and display the result by saving it as a temporary file. It assumes the existence of helper functions like retrieve_video_from_text and save_and_display_video. ```python text_idx = 6 text = sample_batch['text'][text_idx] video_idx = retrieve_video_from_text(similarity_matrix, text_idx) video = sample_batch['video'][video_idx] video_filename = f"video_{video_idx}.mp4" save_and_display_video(video_filename, video) !rm "{video_filename}" ``` -------------------------------- ### Run FLAVA Training on Multiple Nodes using SLURM Source: https://github.com/facebookresearch/multimodal/blob/main/examples/flava/native/README.md Illustrates how to configure and run FLAVA model training across multiple compute nodes using SLURM job scheduler. It includes a sample SLURM script (`run.slurm`) that sets up distributed training parameters and the command to submit the job. ```bash RDZV_ENDPOINT=$(scontrol show hostnames $SLURM_JOB_NODELIST | head -n 1) srun torchrun --nnodes=$SLURM_NNODES --nproc_per_node=$SLURM_GPUS_PER_TASK --rdzv_id=$SLURM_JOB_ID --rdzv_backend=c10d --rdzv_endpoint=$RDZV_ENDPOINT --max_restarts 0 -m flava.native.train config=flava/native/configs/pretrain_debug.yaml $@ ``` ```bash sbatch --partition=[PARTITION] --nodes=[NUM_NODES] --gpus-per-task=[NUM_GPUS_PER_NODE] run.slurm ``` -------------------------------- ### Download VQA Sample Assets Source: https://github.com/facebookresearch/multimodal/blob/main/examples/mdetr/MDETRTutorial.ipynb Downloads sample images and JSON annotation files from a remote S3 bucket into a local directory structure. It uses the wget utility to ensure files are retrieved and organized for dataset processing. ```python import os asset_dir = "assets/vqa" img_dir = os.path.join(asset_dir, "images") os.makedirs(img_dir, exist_ok=True) !wget -nc "https://pytorch.s3.amazonaws.com/torchmultimodal/examples/mdetr/assets/gqa/images/n216553.jpg" -P $img_dir !wget -nc "https://pytorch.s3.amazonaws.com/torchmultimodal/examples/mdetr/assets/gqa/images/n411121.jpg" -P $img_dir !wget -nc "https://pytorch.s3.amazonaws.com/torchmultimodal/examples/mdetr/assets/gqa/gqa_sample_annotations.json" -P $asset_dir !wget -nc "https://pytorch.s3.amazonaws.com/torchmultimodal/examples/mdetr/assets/gqa/gqa_answer2id.json" -P $asset_dir !wget -nc "https://pytorch.s3.amazonaws.com/torchmultimodal/examples/mdetr/assets/gqa/gqa_answer2id_by_type.json" -P $asset_dir ``` -------------------------------- ### Initialize GQA Dataset Source: https://github.com/facebookresearch/multimodal/blob/main/examples/mdetr/MDETRTutorial.ipynb Constructs an instance of the GQADataset class for multimodal inference. It requires paths to image directories and annotation files, and supports optional tokenization and custom transforms. ```python from examples.mdetr.data.dataset import GQADataset from pathlib import Path dataset = GQADataset( img_dir, os.path.join(asset_dir, "gqa_sample_annotations.json"), transforms=None, return_tokens=True, tokenizer=tokenizer, ann_folder=Path(asset_dir), ) ``` -------------------------------- ### Build MDETR Model with Builder Function Source: https://github.com/facebookresearch/multimodal/blob/main/examples/mdetr/MDETRTutorial.ipynb This code shows the simplest way to instantiate an MDETR model using the `mdetr_resnet101` builder function. It allows for easy creation of a prebuilt model with a ResNet101 backbone and provides options to modify default parameters like the number of object queries and transformer feed-forward network dimensions. ```python # Simplest entry point: model builder function. # Provides prebuilt model from the paper with ResNet101 backbone. from torchmultimodal.models.mdetr.model import mdetr_resnet101 mdetr_rn101 = mdetr_resnet101() # The builder function contains sensible default values but can also be easily modified. # E.g. we can modify the number of object queries and change the transformer FFN dimension modified_mdetr_rn101 = mdetr_resnet101(num_queries=100, transformer_dim_feedforward=1024) ``` -------------------------------- ### Launch FLAVA Debug Pretraining (CPU) Source: https://github.com/facebookresearch/multimodal/blob/main/examples/flava/README.md Command to launch a debug pretraining job for the FLAVA model on a CPU. This explicitly sets the accelerator to CPU and disables GPU usage. ```python python -m flava.train config=flava/configs/pretraining/debug.yaml training.lightning.accelerator=cpu training.lightning.gpus=0 training.lightning.strategy=null ``` -------------------------------- ### Initialize and Use CLIP Model for Zero-Shot Classification Source: https://context7.com/facebookresearch/multimodal/llms.txt This snippet demonstrates how to load a pretrained CLIP model, apply necessary image and text transforms, and compute similarity scores between image and text embeddings to perform zero-shot classification. ```python import torch from PIL import Image from torchmultimodal.models.clip.model import clip_vit_b16, clip_vit_l14 from torchmultimodal.transforms.clip_transform import CLIPTransform # Initialize CLIP model with pretrained weights model = clip_vit_b16(pretrained=True) model.eval() # Create transforms for image and text transform = CLIPTransform(image_size=224, is_train=False) # Load and transform an image image = Image.open("cat.jpg") text_labels = ["a photo of a cat", "a photo of a dog", "a photo of a bird"] # Apply transforms image_tensor, text_tensor = transform(image, text_labels) # Get embeddings with torch.no_grad(): output = model(image_tensor.unsqueeze(0), text_tensor) # Calculate similarity scores similarity = output.embeddings_a @ output.embeddings_b.T probs = torch.softmax(similarity * 100, dim=-1) print(f"Predicted label: {text_labels[probs.argmax()]}") ``` -------------------------------- ### Load ALBEF Model and Checkpoint Source: https://github.com/facebookresearch/multimodal/blob/main/examples/albef/vqa_with_albef.ipynb Loads the model configuration from a YAML file and initializes the model with a pre-trained checkpoint. ```python config = yaml.load(open("./configs/vqa.yaml", "r"), Loader=yaml.Loader) model = albef_model_for_vqa(config) checkpoint_url = "https://download.pytorch.org/models/multimodal/albef/finetuned_vqa_checkpoint.pt" checkpoint = torch.hub.load_state_dict_from_url(checkpoint_url, map_location='cpu') model.load_state_dict(checkpoint) ``` -------------------------------- ### Load Pretrained MDETR for Phrase Grounding Source: https://github.com/facebookresearch/multimodal/blob/main/examples/mdetr/MDETRTutorial.ipynb Shows how to load a task-specific MDETR model for phrase grounding and initialize it with pretrained weights from a URL. ```python import torch from torchmultimodal.models.mdetr.model import mdetr_for_phrase_grounding mdetr_pg = mdetr_for_phrase_grounding() checkpoint = torch.hub.load_state_dict_from_url("https://pytorch.s3.amazonaws.com/models/multimodal/mdetr/pretrained_resnet101_checkpoint.pth") mdetr_pg.load_state_dict(checkpoint["model_ema"]) mdetr_pg.eval() ``` -------------------------------- ### ALBEF Model Initialization (Python) Source: https://context7.com/facebookresearch/multimodal/llms.txt Shows the initialization of the ALBEF (Align Before Fuse) model, which aligns image and text representations before fusing them. The code outlines the construction of the vision encoder, text encoder, multimodal encoder, and the final ALBEF model, preparing for a forward pass with sample inputs. ```python import torch from torchmultimodal.models.albef.model import ALBEFModel, ALBEFModelWithSimilarity from torchmultimodal.models.albef.image_encoder import albef_image_encoder from torchmultimodal.models.albef.multimodal_encoder import albef_multimodal_encoder from torchmultimodal.modules.encoders.bert_text_encoder import bert_text_encoder # Build ALBEF components vision_encoder = albef_image_encoder() text_encoder = bert_text_encoder(hidden_size=768, num_hidden_layers=6) multimodal_encoder = albef_multimodal_encoder() # Create ALBEF model model = ALBEFModel( vision_encoder=vision_encoder, text_encoder=text_encoder, multimodal_encoder=multimodal_encoder, momentum=0.995 ) # Prepare inputs images = torch.randn(4, 3, 384, 384) text_ids = torch.randint(0, 30522, (4, 64)) text_atts = torch.ones(4, 64) ``` -------------------------------- ### Load State Dict into MUGEN Model - Python Source: https://github.com/facebookresearch/multimodal/blob/main/examples/mugen/generation/LoadAndComparePretrainedVQVAE.ipynb Loads the state dictionary from the checkpoint into the initialized MUGEN VQVAE model. This step ensures the model has the pre-trained weights. ```python vv_mugen.load_state_dict(ckpt['state_dict']) ``` -------------------------------- ### Instantiate MDETR Model from Components Source: https://github.com/facebookresearch/multimodal/blob/main/examples/mdetr/MDETRTutorial.ipynb This snippet illustrates how to construct an MDETR model by assembling its modular components. It details the core parts like the image backbone, text encoder, and multimodal transformer, along with auxiliary components such as positional embeddings, projection layers, and prediction heads. This approach offers flexibility for customization and experimentation. ```python # mdetr_resnet101 is an instantiation of the MDETR model class. # This class is composed of modular components which can be swapped out for easy experimentation. from torchmultimodal.models.mdetr.model import MDETR # Other imports we will use import math from torch import nn from torchmultimodal.modules.layers.mlp import MLP from torchmultimodal.models.mdetr.model import FeatureResizer from torchmultimodal.models.mdetr.image_encoder import mdetr_resnet101_backbone, MaskedIntermediateLayer, PositionEmbedding2D from torchmultimodal.models.mdetr.text_encoder import mdetr_roberta_text_encoder from torchmultimodal.models.mdetr.transformer import mdetr_transformer # Set some dimensions as constant image_embedding_dim = 2048 text_embedding_dim = 768 transformer_dim = 256 num_queries = 100 num_classes = 255 # The core components of the MDETR model class are: # - image backbone # - text encoder # - multimodal transformer # Encoder and transformer components also have builder functions. image_backbone = mdetr_resnet101_backbone() text_encoder = mdetr_roberta_text_encoder() transformer = mdetr_transformer() # Other components include # - positional embeddings: 2D embeddings added to image backbone outputs # - text/image projections (between unimodal encoders and multimodal transformer) # - query embeddings: learned object query embeddings, used in multimodal transformer decoder # - bounding box embeddings: map multimodal transformer outputs to bounding box coordinates # - class embeddings: map multimodal transformer outputs to class logits pos_embed = PositionEmbedding2D(num_pos_feats=128, scale=2 * math.pi) text_projection = FeatureResizer(text_embedding_dim, transformer_dim) image_projection = nn.Conv2d(image_embedding_dim, transformer_dim, kernel_size=1) query_embed = nn.Embedding(num_queries, transformer_dim) bbox_embed = MLP(transformer_dim, 4, [transformer_dim] * 2, dropout=0.0) class_embed = nn.Linear(transformer_dim, num_classes + 1) # This will give us the original ResNet101 MDETR model mdetr = MDETR( image_backbone = image_backbone, text_encoder = text_encoder, transformer = transformer, pos_embed = pos_embed, text_projection = text_projection, image_projection = image_projection, query_embed = query_embed, bbox_embed = bbox_embed, class_embed = class_embed, ) ``` -------------------------------- ### Initialize Omnivore Model Source: https://github.com/facebookresearch/multimodal/blob/main/examples/omnivore/omnivore_inference_demo.ipynb Loads the pretrained Omnivore Swin Transformer model and sets it to evaluation mode for inference. ```python import torchmultimodal.models.omnivore as omnivore model = omnivore.omnivore_swin_t(pretrained=True) model = model.eval() ``` -------------------------------- ### Import Diffusion Libraries Source: https://github.com/facebookresearch/multimodal/blob/main/torchmultimodal/diffusion_labs/mnist_training.ipynb Imports necessary libraries for building and running diffusion models, including PyTorch, torchvision, and specific modules from torchmultimodal for U-Net, adapters, losses, samplers, predictors, schedules, and transforms. ```python import torch import torchvision import torchvision.transforms.functional as F from torch import nn from tqdm import tqdm from torchmultimodal.diffusion_labs.models.adm_unet.adm import adm_unet from torchmultimodal.diffusion_labs.modules.adapters.cfguidance import CFGuidance from torchmultimodal.diffusion_labs.modules.losses.diffusion_hybrid_loss import DiffusionHybridLoss from torchmultimodal.diffusion_labs.samplers.ddpm import DDPModule from torchmultimodal.diffusion_labs.predictors.noise_predictor import NoisePredictor from torchmultimodal.diffusion_labs.schedules.discrete_gaussian_schedule import linear_beta_schedule, DiscreteGaussianSchedule from torchmultimodal.diffusion_labs.transforms.diffusion_transform import RandomDiffusionSteps device = "cuda" ``` -------------------------------- ### Define MUGEN VQVAE Arguments - Python Source: https://github.com/facebookresearch/multimodal/blob/main/examples/mugen/generation/LoadAndComparePretrainedVQVAE.ipynb Defines the arguments for MUGEN's Video VQVAE model using a Namespace class. These arguments are derived from MUGEN's training scripts. ```python class Namespace: def __init__(self, **kwargs): self.__dict__.update(kwargs) vqvae_args=Namespace( embedding_dim=256, n_codes=2048, n_hiddens=240, n_res_layers=4, lr=0.0003, downsample=(4, 32, 32), kernel_size=3, sequence_length=16, resolution=256, ) vv_mugen = mugen.VQVAE(vqvae_args) ``` -------------------------------- ### Apply CFGuidance Adapter Source: https://github.com/facebookresearch/multimodal/blob/main/torchmultimodal/diffusion_labs/mnist_training.ipynb Applies the CFGuidance adapter to the U-Net model. This technique enhances image-prompt alignment in conditional generative models by adjusting the guidance strength. It takes the base model and a dictionary of conditional inputs with their sizes. ```python decoder = CFGuidance(unet, {"digit": 768}, guidance=2.0) ``` -------------------------------- ### Initialize and Use FLAVA Model for Zero-Shot Classification Source: https://context7.com/facebookresearch/multimodal/llms.txt This snippet shows how to initialize the FLAVA model, process inputs using specific image and text transforms, and perform zero-shot classification by comparing projected image and text features. ```python import torch from PIL import Image from torchmultimodal.models.flava.model import flava_model from torchmultimodal.transforms.flava_transform import FLAVAImageTransform from torchmultimodal.transforms.bert_text_transform import BertTextTransform # Initialize FLAVA model with pretrained weights model = flava_model(pretrained=True) model.eval() # Create transforms image_transform = FLAVAImageTransform(is_train=False) text_transform = BertTextTransform() # Process image and text image = Image.open("sample.jpg") image_input = image_transform(image)["image"].unsqueeze(0) text_input = text_transform(["a dog playing in the park"]) # Zero-shot classification labels = ["dog", "cat", "bird", "car"] text_tokens = text_transform(labels) with torch.no_grad(): _, image_features = model.encode_image(image_input, projection=True) _, text_features = model.encode_text(text_tokens, projection=True) # Calculate similarity scores = image_features @ text_features.t() probs = torch.nn.Softmax(dim=-1)(scores) predicted_label = labels[torch.argmax(probs)] print(f"Predicted: {predicted_label}, Probabilities: {probs}") ``` -------------------------------- ### Apply Transforms and Prepare Input - Python Source: https://github.com/facebookresearch/multimodal/blob/main/examples/omnivore/omnivore_inference_demo.ipynb This code applies a predefined transformation preset for depth classification evaluation to an RGB-D tensor. It resizes and crops the input to a specified size and adds a batch dimension, preparing it for model inference. ```python # Assuming presets and tensor_rgbd are defined depth_val_presets = presets.DepthClassificationPresetEval(crop_size=224, resize_size=224) input_depth = depth_val_presets(tensor_rgbd) # Add batch dimension input_depth = input_depth.unsqueeze(0) ``` -------------------------------- ### Download and Unzip Checkpoints - Shell Source: https://github.com/facebookresearch/multimodal/blob/main/examples/mugen/generation/LoadAndComparePretrainedVQVAE.ipynb Downloads the MUGEN checkpoint zip file from a provided URL and saves it to the specified checkpoint directory. It then unzips the downloaded file. ```shell !wget https://dl.noahmt.com/creativity/data/MUGEN_release/checkpoints.zip -P $checkpoint_dir ``` ```shell import os # Unzip checkpoints zip_location = os.path.join(checkpoint_dir, 'checkpoints.zip') !unzip $zip_location -d $checkpoint_dir ``` -------------------------------- ### Initialize MNIST DataLoader Source: https://github.com/facebookresearch/multimodal/blob/main/torchmultimodal/diffusion_labs/mnist_training.ipynb Loads the MNIST dataset and configures a DataLoader with specific batching, shuffling, and worker settings for efficient training. ```python from torchvision.datasets import MNIST from torch.utils.data import DataLoader train_dataset = MNIST("mnist", train=True, download=True, transform=transform) train_dataloader = DataLoader(train_dataset, batch_size=192, shuffle=True, num_workers=2, pin_memory=True) ``` -------------------------------- ### Prepare Data and Perform Inference Source: https://github.com/facebookresearch/multimodal/blob/main/examples/albef/vqa_with_albef.ipynb Transforms input images and text, then runs the model to predict answer IDs for the given questions. ```python image_transform = testing_image_transform() question_transform = ALBEFTextTransform(add_end_token=False) answer_transform = ALBEFTextTransform(do_pre_process=False) vqa_data = json.load(open(data_dir + "vqa_data.json", "r")) answer_list = json.load(open(data_dir + "answer_list.json", "r")) image_paths = [sample["image"] for sample in vqa_data] questions = [sample["question"] for sample in vqa_data] images = [Image.open(data_dir + path).convert("RGB") for path in image_paths] image_input = [image_transform(image) for image in images] image_input = torch.stack(image_input, dim=0) question_input = question_transform(questions) question_atts = (question_input != 0).type(torch.long) answer_input = answer_transform(answer_list) answer_atts = (answer_input != 0).type(torch.long) answer_ids, _ = model( image_input, question_input, question_atts, answer_input, answer_atts, k=1, is_train=False, ) ``` -------------------------------- ### Importing Libraries for Video-Text Retrieval Source: https://github.com/facebookresearch/multimodal/blob/main/examples/mugen/retrieval/tutorial.ipynb Imports necessary libraries from PyTorch, torchvision, and the multimodal library for video processing, model loading, and data handling. These are essential for setting up the video-text retrieval pipeline. ```python import torch from torchvision.io import write_video from IPython.core.display import Video from examples.mugen.retrieval.video_clip import videoclip from examples.mugen.data.mugen_dataset import MUGENDatasetArgs from examples.mugen.data.mugen_datamodules import MUGENDataModule from torchmultimodal.utils.common import load_module_from_url from examples.mugen.data.bert_text_transform import BertTextTransform from torchmultimodal.transforms.video_transform import VideoTransform ```