status: not-started difficulty: โญโญโญโญโญ time_to_learn: 6-7 hours date_created: 2026-03-07 date_started: date_completed: tags:
- ai
- multimodal
- vision
- audio
- advanced progress: 0 prerequisites:
- "06 Large Language Models"
- "04 Transformer" next_concepts:
- "13 Agents"
- "17 Small Language Models"
๐บ๏ธ Concept Mind Map
MULTI-MODAL MODELS
|
+---------------+---------------+
| | |
MODALITIES ARCHITECTURES APPLICATIONS
| | |
| | |
Text + Image CLIP Image Understanding
- GPT-4V - Contrastive - Medical diagnosis
- Gemini learning - Visual QA
- Claude 3 - Text-Image - Scene analysis
- LLaVA alignment - OCR
| | |
| | |
Text + Audio Flamingo Content Generation
- Whisper - Few-shot - DALL-E (textโimage)
- AudioLM learning - Stable Diffusion
- MusicGen - Interleaved - Midjourney
- Bark data - Imagen
| | |
| | |
Text + Video GPT-4V Accessibility
- Gemini - Vision - Image description
- Video-LLaMA encoder - Audio transcription
- VideoChat - Language - Sign language
- Flamingo decoder - Real-time captions
| | |
| | |
All Modalities ImageBind Robotics
- ImageBind - Unified - Visual navigation
- Meta-Transformer embedding - Object manipulation
- Unified-IO - 6 modalities - Scene understanding
- Gato - Cross-modal - Task planning
Modality Combinations:
INPUT โ OUTPUT MODEL USE CASE
Text โ Image DALL-E Art generation
Image โ Text GPT-4V Image captioning
Audio โ Text Whisper Transcription
Text โ Audio Bark Text-to-speech
Video โ Text Gemini Video analysis
Text โ Video Gen-2 Video generation
Image+Text โ Text LLaVA Visual QA
Architecture Pattern:
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ MULTI-MODAL MODEL โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Vision Encoder (Images/Video) โ
โ - ViT, CLIP, ResNet โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Audio Encoder (Sound/Speech) โ
โ - Whisper, Wav2Vec โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Unified Embedding Space โ
โ - All modalities โ same space โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Language Model (Transformer) โ
โ - GPT, LLaMA, T5 โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Output Decoder โ
โ - Text, Image, Audio, Video โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Key Advantages:
- Richer Understanding: See + Hear + Read = Better context
- Accessibility: Help blind/deaf users
- Efficiency: One model for multiple tasks
- Natural Interaction: Communicate like humans do
๐ Start With a Real-World Scenario
You show GPT-4V a photo of your fridge:
Regular ChatGPT (Text-only):
You: "What can I cook with these ingredients?"
ChatGPT: "I can't see images. Please list the ingredients."
โ Useless! You have to type everything!
GPT-4V (Multi-Modal):
You: [Upload fridge photo]
"What can I cook with these ingredients?"
GPT-4V: "I can see:
- Eggs (6)
- Milk
- Cheese
- Tomatoes
- Onions
- Bread
You can make:
1. Cheese omelet with tomatoes
2. French toast
3. Grilled cheese sandwich
4. Scrambled eggs with veggies
Would you like a recipe for any of these?"
โ Sees the image AND suggests recipes!
The Power of Multi-Modal AI:
DALL-E (Text โ Image):
You: "A cat wearing a space suit on Mars"
DALL-E: [Generates perfect image in 10 seconds]
Whisper (Audio โ Text):
You: [Upload 1-hour meeting recording]
Whisper: [Transcribes entire meeting with 95% accuracy]
GPT-4V (Image โ Text):
You: [Upload medical X-ray]
GPT-4V: "This appears to show a fracture in the left radius..."
Gemini (Video โ Text):
You: [Upload cooking video]
Gemini: "Step-by-step recipe:
1. Heat oil (0:15)
2. Add onions (0:45)
3. Add spices (1:20)..."
One model, multiple formats! ๐คฏ
Real Applications:
1. Medical Diagnosis
- Upload X-ray โ AI analyzes โ Suggests diagnosis
- 95% accuracy, faster than humans!
2. Accessibility
- Blind users: AI describes images
- Deaf users: AI transcribes audio
- Everyone benefits!
3. Content Creation
- Text prompt โ Image (DALL-E)
- Image โ Description โ New image
- Infinite creativity!
4. Education
- Upload homework photo โ AI explains solution
- Upload diagram โ AI explains concept
- Learning made visual!
By the end of this guide, you'll understand:
- How multi-modal models work
- Text-to-image generation (DALL-E, Midjourney)
- Image understanding (GPT-4V, Gemini)
- Audio processing (Whisper, ElevenLabs)
- Video analysis
- Cross-modal reasoning
The revolution: AI is no longer text-only! It can see, hear, and create across all formats! ๐จ๐ต๐ธ
Let's dive in! ๐
๐จ Multi-Modal Models
Beyond Text: AI That Sees, Hears, and Understands Multiple Formats
๐ ๏ธ Local Setup (20 Minutes)
Set up multi-modal environment:
Step 1: Install Dependencies
pip install openai==1.3.0 \
pillow==10.1.0 \
requests==2.31.0 \
transformers==4.35.0 \
torch==2.1.0 \
torchvision==0.16.0
Step 2: Install Multi-Modal Libraries
pip install clip-by-openai \
whisper \
diffusers==0.24.0
Step 3: Test Vision
from openai import OpenAI
import base64
client = OpenAI()
# Test GPT-4V
response = client.chat.completions.create(
model="gpt-4-vision-preview",
messages=[{
"role": "user",
"content": [
{"type": "text", "text": "What's in this image?"},
{"type": "image_url", "image_url": {"url": "https://example.com/image.jpg"}}
]
}]
)
print(response.choices[0].message.content)
print("โ
Vision working!")
Step 4: Test Audio
import whisper
# Load Whisper model
model = whisper.load_model("base")
# Transcribe audio
result = model.transcribe("audio.mp3")
print(result["text"])
print("โ
Audio working!")
๐ง Core Concept
The Problem: Text-Only Limitation
Traditional LLMs only understand text:
# User: "What's in this image?" [shows cat picture]
# Text-only LLM: "I cannot see images" โ
The Solution: Multi-Modal Understanding
Multi-modal models process multiple formats:
# User: "What's in this image?" [shows cat picture]
# GPT-4V: "A gray tabby cat sitting on a windowsill" โ
How Multi-Modal Models Work
1. Unified Representation:
Text โ Encoder โ Shared Space
Image โ Encoder โ Shared Space
Audio โ Encoder โ Shared Space
2. Cross-Modal Attention:
Text tokens โ Image patches โ Audio frames
3. Modality-Specific Decoders:
Shared Space โ Text Decoder โ Text
Shared Space โ Image Decoder โ Image
Shared Space โ Audio Decoder โ Audio
Key Architectures
| Model | Input | Output | Use Case |
|---|---|---|---|
| GPT-4V | Text + Image | Text | Image understanding |
| DALL-E | Text | Image | Image generation |
| Whisper | Audio | Text | Transcription |
| CLIP | Text + Image | Similarity | Image search |
| Gemini | Text + Image + Video | Text | Multi-modal chat |
๐ป Code Examples
Example 1: Image Understanding with GPT-4V
from openai import OpenAI
import base64
from pathlib import Path
client = OpenAI()
def analyze_image(image_path: str, question: str = "What's in this image?"):
"""
Analyze image using GPT-4V.
"""
# Read and encode image
with open(image_path, "rb") as image_file:
image_data = base64.b64encode(image_file.read()).decode('utf-8')
# Call GPT-4V
response = client.chat.completions.create(
model="gpt-4-vision-preview",
messages=[{
"role": "user",
"content": [
{"type": "text", "text": question},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{image_data}",
"detail": "high" # or "low" for faster/cheaper
}
}
]
}],
max_tokens=500
)
return response.choices[0].message.content
# Example usage
description = analyze_image(
"photo.jpg",
"Describe this image in detail, including colors, objects, and mood"
)
print(f"Description: {description}")
# Multiple images
def compare_images(image1: str, image2: str):
"""Compare two images."""
with open(image1, "rb") as f1, open(image2, "rb") as f2:
img1_data = base64.b64encode(f1.read()).decode('utf-8')
img2_data = base64.b64encode(f2.read()).decode('utf-8')
response = client.chat.completions.create(
model="gpt-4-vision-preview",
messages=[{
"role": "user",
"content": [
{"type": "text", "text": "Compare these two images. What are the similarities and differences?"},
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{img1_data}"}},
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{img2_data}"}}
]
}]
)
return response.choices[0].message.content
comparison = compare_images("before.jpg", "after.jpg")
print(f"Comparison: {comparison}")
Example 2: Image Generation with DALL-E
from openai import OpenAI
import requests
from PIL import Image
from io import BytesIO
client = OpenAI()
def generate_image(prompt: str, size: str = "1024x1024", quality: str = "standard"):
"""
Generate image using DALL-E 3.
"""
print(f"Generating: {prompt}")
response = client.images.generate(
model="dall-e-3",
prompt=prompt,
size=size,
quality=quality, # "standard" or "hd"
n=1
)
image_url = response.data[0].url
# Download image
image_response = requests.get(image_url)
image = Image.open(BytesIO(image_response.content))
# Save
filename = f"generated_{prompt[:30].replace(' ', '_')}.png"
image.save(filename)
print(f"Saved to: {filename}")
return image, image_url
# Example usage
image, url = generate_image(
"A serene Japanese garden with cherry blossoms, koi pond, and stone lanterns, digital art style",
size="1024x1024",
quality="hd"
)
# Edit existing image
def edit_image(original_image: str, mask_image: str, prompt: str):
"""
Edit image using DALL-E.
"""
response = client.images.edit(
image=open(original_image, "rb"),
mask=open(mask_image, "rb"),
prompt=prompt,
n=1,
size="1024x1024"
)
return response.data[0].url
# Create variations
def create_variations(image_path: str, n: int = 3):
"""
Create variations of an image.
"""
response = client.images.create_variation(
image=open(image_path, "rb"),
n=n,
size="1024x1024"
)
return [img.url for img in response.data]
variations = create_variations("original.png", n=3)
print(f"Created {len(variations)} variations")
Example 3: Audio Transcription with Whisper
import whisper
import torch
from pathlib import Path
class AudioTranscriber:
"""
Transcribe audio using Whisper.
"""
def __init__(self, model_size: str = "base"):
"""
Initialize Whisper model.
model_size: tiny, base, small, medium, large
"""
print(f"Loading Whisper {model_size} model...")
self.model = whisper.load_model(model_size)
print("Model loaded!")
def transcribe(self, audio_path: str, language: str = None):
"""
Transcribe audio file.
"""
print(f"Transcribing: {audio_path}")
result = self.model.transcribe(
audio_path,
language=language, # None for auto-detect
task="transcribe", # or "translate" for English translation
verbose=False
)
return {
"text": result["text"],
"language": result["language"],
"segments": result["segments"]
}
def transcribe_with_timestamps(self, audio_path: str):
"""
Transcribe with word-level timestamps.
"""
result = self.model.transcribe(
audio_path,
word_timestamps=True
)
# Format with timestamps
formatted = []
for segment in result["segments"]:
start = segment["start"]
end = segment["end"]
text = segment["text"]
formatted.append(f"[{start:.2f}s - {end:.2f}s] {text}")
return "\n".join(formatted)
def translate_to_english(self, audio_path: str):
"""
Translate any language to English.
"""
result = self.model.transcribe(
audio_path,
task="translate"
)
return result["text"]
# Example usage
transcriber = AudioTranscriber(model_size="base")
# Basic transcription
result = transcriber.transcribe("podcast.mp3")
print(f"Language: {result['language']}")
print(f"Text: {result['text']}")
# With timestamps
timestamped = transcriber.transcribe_with_timestamps("interview.mp3")
print(timestamped)
# Translation
english_text = transcriber.translate_to_english("spanish_audio.mp3")
print(f"English translation: {english_text}")
Example 4: Image-Text Matching with CLIP
import torch
import clip
from PIL import Image
import numpy as np
class CLIPMatcher:
"""
Match images to text using CLIP.
"""
def __init__(self, model_name: str = "ViT-B/32"):
"""
Initialize CLIP model.
"""
self.device = "cuda" if torch.cuda.is_available() else "cpu"
self.model, self.preprocess = clip.load(model_name, device=self.device)
print(f"CLIP loaded on {self.device}")
def find_best_match(self, image_path: str, text_options: list):
"""
Find which text best describes the image.
"""
# Load and preprocess image
image = self.preprocess(Image.open(image_path)).unsqueeze(0).to(self.device)
# Tokenize text
text = clip.tokenize(text_options).to(self.device)
# Get features
with torch.no_grad():
image_features = self.model.encode_image(image)
text_features = self.model.encode_text(text)
# Calculate similarity
similarity = (100.0 * image_features @ text_features.T).softmax(dim=-1)
# Get results
values, indices = similarity[0].topk(len(text_options))
results = []
for value, index in zip(values, indices):
results.append({
"text": text_options[index],
"probability": value.item()
})
return results
def search_images(self, query: str, image_paths: list):
"""
Search images using text query.
"""
# Tokenize query
text = clip.tokenize([query]).to(self.device)
# Process all images
images = []
for path in image_paths:
image = self.preprocess(Image.open(path)).unsqueeze(0)
images.append(image)
images = torch.cat(images).to(self.device)
# Get features
with torch.no_grad():
image_features = self.model.encode_image(images)
text_features = self.model.encode_text(text)
# Calculate similarity
similarity = (100.0 * image_features @ text_features.T).squeeze()
# Sort by similarity
sorted_indices = similarity.argsort(descending=True)
results = []
for idx in sorted_indices:
results.append({
"image": image_paths[idx],
"score": similarity[idx].item()
})
return results
# Example usage
matcher = CLIPMatcher()
# Find best description
descriptions = [
"a photo of a cat",
"a photo of a dog",
"a photo of a bird",
"a photo of a car"
]
matches = matcher.find_best_match("photo.jpg", descriptions)
print("Best matches:")
for match in matches:
print(f" {match['text']}: {match['probability']:.2%}")
# Search images
image_files = ["img1.jpg", "img2.jpg", "img3.jpg"]
results = matcher.search_images("sunset over ocean", image_files)
print("\nSearch results:")
for result in results:
print(f" {result['image']}: {result['score']:.2f}")
Example 5: Multi-Modal RAG System
from openai import OpenAI
import base64
from typing import List, Dict
client = OpenAI()
class MultiModalRAG:
"""
RAG system that works with text and images.
"""
def __init__(self):
self.knowledge_base = []
def add_document(self, text: str, image_path: str = None):
"""
Add document to knowledge base.
"""
doc = {"text": text}
if image_path:
with open(image_path, "rb") as f:
doc["image"] = base64.b64encode(f.read()).decode('utf-8')
self.knowledge_base.append(doc)
def query(self, question: str, include_images: bool = True):
"""
Query the knowledge base.
"""
# Build context from knowledge base
context_parts = []
for i, doc in enumerate(self.knowledge_base):
context_parts.append(f"Document {i+1}: {doc['text']}")
context = "\n\n".join(context_parts)
# Build messages
messages = [{
"role": "system",
"content": f"Answer questions based on this context:\n{context}"
}]
# Add images if requested
if include_images:
content = [{"type": "text", "text": question}]
for doc in self.knowledge_base:
if "image" in doc:
content.append({
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{doc['image']}"
}
})
messages.append({"role": "user", "content": content})
model = "gpt-4-vision-preview"
else:
messages.append({"role": "user", "content": question})
model = "gpt-4"
# Get answer
response = client.chat.completions.create(
model=model,
messages=messages
)
return response.choices[0].message.content
# Example usage
rag = MultiModalRAG()
# Add documents
rag.add_document(
"The Eiffel Tower is 330 meters tall and located in Paris, France.",
"eiffel_tower.jpg"
)
rag.add_document(
"The Statue of Liberty is 93 meters tall and located in New York, USA.",
"statue_of_liberty.jpg"
)
# Query
answer = rag.query("Which landmark is taller?", include_images=True)
print(f"Answer: {answer}")
๐ Assignments & Practice Problems
Assignment 1: Vision App โญโญ
Difficulty: Medium | Time: 90 minutes
Build image understanding app.
Requirements:
- Upload images
- Ask questions about images
- Display answers
- Support multiple images
Assignment 2: Multi-Modal RAG โญโญโญ
Difficulty: Hard | Time: 180 minutes
Build complete multi-modal RAG.
Requirements:
- Text + image documents
- Cross-modal search
- Answer with context
- Cite sources
โ Bad: Send full resolution
Expensive and slow
โ Good: Resize images
from PIL import Image img = Image.open("large.jpg") img.thumbnail((1024, 1024))
### Pitfall 2: Wrong Format
```python
# โ Bad: Wrong image encoding
image_url = "file://local/path.jpg"
# โ
Good: Base64 encoding
with open("path.jpg", "rb") as f:
data = base64.b64encode(f.read()).decode()
url = f"data:image/jpeg;base64,{data}"
๐ Related Concepts
- Previous: Reasoning Models - Advanced thinking
- Next: Small Language Models - Efficient models
- Related: AI Agents - Multi-modal agents
- Foundation: Transformer - Base architecture
