Multimodal Embeddings

Modern AI systems increasingly work across multiple types of data: text, images, videos, audio, etc…

Multimodal embeddings allow these systems to understand and relate data from different modalities by mapping them to a shared embedding space.

In this lesson, we’ll explore how multimodal embeddings work, focusing on text-image embeddings, cross-modal retrieval, joint embedding spaces, and fusion strategies.

This Lesson Will Cover
  • 🖼️ Text-Image Embeddings: Understanding how to represent text and images in a shared space
  • 🤖 CLIP Architecture: Exploring OpenAI’s groundbreaking multimodal model
  • 🔗 Joint Embedding Spaces: Learning how different modalities can be unified
  • ⚠️ Challenges: Key difficulties in working with multimodal embeddings

What Are Multimodal Embeddings?

Multimodal embeddings are representations that integrate multiple types of data into a unified vector space. The goal is to enable meaningful comparisons and computations across different modalities.

Text-Image Embeddings

Text-image embeddings align textual descriptions with visual content, enabling powerful AI capabilities like text-to-image search or image captioning.

For example, using a pretrained embedding model, a sentence describing a sunset can be embedded into the same space as an image of a sunset, producing embeddings that are relatively close to each other.

Joint Embedding Spaces

Multimodal embeddings exist in a high-dimensional joint embedding space that enables different types of data to be compared and processed together.

Typically, this is done by using separate encoders to process each type of data (like text and images), while a shared objective function (such as maximizing similarity between paired inputs) ensures the outputs are properly aligned.

A prime example of this approach is CLIP, which combines a text encoder (based on a Transformer architecture) with an image encoder (using a Vision Transformer) to create a unified embedding space.

CLIP (Contrastive Language–Image Pretraining)

CLIP was developed by OpenAI in 2021 to learn visual concepts from natural language supervision. It is a neural network trained on a variety of image-text pairs from the internet and can be applied to any visual classification task.

As mentioned above, both text and images are encoded into the same vector space using separate encoders. During training, CLIP was exposed to 400 million image-text pairs collected from the internet. The model learns through contrastive learning - it’s trained to maximize the similarity between actual image-text pairs while minimizing similarity between mismatched pairs.

The training process works by showing CLIP a batch of image-text pairs and having it predict which text goes with which image. For each image in a batch, CLIP computes the similarity between that image’s embedding and all text embeddings in the batch. It’s trained to give the highest similarity score to the correct image-text pair while giving lower scores to incorrect pairings.

Example: Text-Image Similarity with CLIP

Kale
Kale image used in the example below. Sourced from wikipedia commons.
import torch
from PIL import Image
from transformers import CLIPProcessor, CLIPModel
import requests
from io import BytesIO

# Load a pre-trained CLIP model
model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")

kale_image_url = "https://upload.wikimedia.org/wikipedia/commons/2/20/Boerenkool.jpg"
kale_image_resp = requests.get(kale_image_url, headers={"User-Agent": "Mozilla/5.0"})
kale_image_resp.raise_for_status()
kale_image = Image.open(BytesIO(kale_image_resp.content))
caption = "a photo of a kale bush"

# Input text and image
inputs = processor(text=[caption], images=[kale_image], return_tensors="pt", padding=True)

# Get embeddings
outputs = model(**inputs)
text_embeddings = outputs.text_embeds
image_embeddings = outputs.image_embeds

# Compute similarity
similarity = torch.cosine_similarity(text_embeddings, image_embeddings)
print(f"Text-Image Similarity: {similarity.item():.2f}")

# Output: Text-Image Similarity: 0.35
CLIP Cosine Similarity for a photo of kale with various captions
CLIP cosine similarity for various captions, including the caption above: "a photo of a kale bush".
3D PCA projection of CLIP embeddings for kale image captions
3D PCA projection of CLIP embeddings for the kale image captions above. It's interesting to note how the edible-food captions ("fresh broccoli", "green salad", etc...) are clustered more closely together.

Challenges in Multimodal Embeddings

Working with multimodal embeddings presents several difficult challenges:

  • Cross-Modal Alignment: Ensuring semantic consistency between modalities is extremely difficult. For example, the same concept may be represented very differently in images versus text, making it challenging to create truly aligned embeddings.

  • Training Data Quality: Finding high-quality paired data is a major bottleneck. Web-scraped image-text pairs often contain noise, irrelevant captions, or misleading associations that can degrade model performance.

  • Embedding Space Collapse: Models can suffer from “modality collapse,” where embeddings from different modalities cluster separately instead of forming a truly unified space.

  • Evaluation Complexity: Measuring the quality of multimodal embeddings is non-trivial since there’s no single metric that captures all aspects of cross-modal performance.

  • Domain Adaptation: Multimodal models trained on general web data often perform poorly on specialized domains (e.g., medical imaging, technical diagrams) where the relationship between modalities follows different patterns.

  • Computational Requirements: Training and deploying multimodal models at scale requires significant computational resources, often making them impractical for many real-world applications.

Conclusion

Multimodal was a huge topic in AI in 2024. We saw models go from text-only to multimodal: generating responses from a mix of text, image and audio inputs.

Working with multimodal embeddings is a difficult problem, but novel solutions will unlock a ton of value in the coming years.

Key Takeaways

🌐 Multimodal embeddings enable AI systems to work with multiple types of data by mapping them to a shared vector space.

🖼️ CLIP is a powerful model that learns visual concepts from natural language by aligning text and image embeddings.

🔄 Joint embedding spaces allow different modalities to be compared and processed together using separate encoders.

⚠️ Key challenges include cross-modal alignment, data quality, modality collapse, and computational requirements.

🔍 Text-image embeddings enable powerful capabilities like cross-modal search and image captioning.

Next: RAG

Up next, we’ll use what we’ve learned about embeddings to power advanced retrieval systems with RAG.

Comments (0)

No comments yet.