Create README.md
Browse files
README.md
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
language:
|
| 4 |
+
- en
|
| 5 |
+
pipeline_tag: video-text-to-text
|
| 6 |
+
tags:
|
| 7 |
+
- video
|
| 8 |
+
- video-understanding
|
| 9 |
+
- vision
|
| 10 |
+
- multimodal
|
| 11 |
+
- conversational
|
| 12 |
+
- custom_code
|
| 13 |
+
- instruction-tuning
|
| 14 |
+
library_name: transformers
|
| 15 |
+
---
|
| 16 |
+
|
| 17 |
+
# Apollo: An Exploration of Video Understanding in Large Multimodal Models
|
| 18 |
+
|
| 19 |
+
Apollo is a family of Large Multimodal Models (LMMs) that push the state-of-the-art in video understanding. It supports tasks including:
|
| 20 |
+
- Long-form video comprehension
|
| 21 |
+
- Temporal reasoning
|
| 22 |
+
- Complex video question-answering
|
| 23 |
+
- Multi-turn conversations grounded in video content
|
| 24 |
+
|
| 25 |
+
Apollo models excel at handling hour-long videos, balancing speed and accuracy through strategic design decisions. Our models outperform most 7B competitors at just 3B parameters and even rival 30B-scale models.
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
**Key Highlights:**
|
| 30 |
+
- **7B model varient**
|
| 31 |
+
- **32 tokens/frame**
|
| 32 |
+
|
| 33 |
+
## Quick Start
|
| 34 |
+
|
| 35 |
+
**Installation:**
|
| 36 |
+
```bash
|
| 37 |
+
pip install -e .
|
| 38 |
+
pip install flash-attn --no-build-isolation
|
| 39 |
+
```
|
| 40 |
+
|
| 41 |
+
**Inference Example:**
|
| 42 |
+
```python
|
| 43 |
+
import torch
|
| 44 |
+
from transformers import AutoModelForCausalLM
|
| 45 |
+
from apollo.mm_utils import (
|
| 46 |
+
KeywordsStoppingCriteria,
|
| 47 |
+
tokenizer_mm_token,
|
| 48 |
+
ApolloMMLoader
|
| 49 |
+
)
|
| 50 |
+
from apollo.conversations import conv_templates, SeparatorStyle
|
| 51 |
+
from huggingface_hub import snapshot_download
|
| 52 |
+
|
| 53 |
+
model_url = "Apollo-LMMs/Apollo-3B-t32"
|
| 54 |
+
model_path = snapshot_download(model_url, repo_type="model")
|
| 55 |
+
|
| 56 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 57 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 58 |
+
model_path,
|
| 59 |
+
trust_remote_code=True,
|
| 60 |
+
low_cpu_mem_usage=True
|
| 61 |
+
).to(device=device, dtype=torch.bfloat16)
|
| 62 |
+
|
| 63 |
+
tokenizer = model.tokenizer
|
| 64 |
+
vision_processors = model.vision_tower.vision_processor
|
| 65 |
+
config = model.config
|
| 66 |
+
num_repeat_token = config.mm_connector_cfg['num_output_tokens']
|
| 67 |
+
mm_processor = ApolloMMLoader(
|
| 68 |
+
vision_processors,
|
| 69 |
+
config.clip_duration,
|
| 70 |
+
frames_per_clip=4,
|
| 71 |
+
clip_sampling_ratio=0.65,
|
| 72 |
+
model_max_length=config.model_max_length,
|
| 73 |
+
device=device,
|
| 74 |
+
num_repeat_token=num_repeat_token
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
video_path = "path/to/video.mp4"
|
| 78 |
+
question = "Describe this video in detail"
|
| 79 |
+
mm_data, replace_string = mm_processor.load_video(video_path)
|
| 80 |
+
|
| 81 |
+
conv = conv_templates["qwen_2"].copy()
|
| 82 |
+
conv.append_message(conv.roles[0], replace_string + "\n\n" + question)
|
| 83 |
+
conv.append_message(conv.roles[1], None)
|
| 84 |
+
|
| 85 |
+
prompt = conv.get_prompt()
|
| 86 |
+
input_ids = tokenizer_mm_token(prompt, tokenizer, return_tensors="pt").unsqueeze(0).to(device)
|
| 87 |
+
|
| 88 |
+
stop_str = conv.sep if conv.sep_style != SeparatorStyle.TWO else conv.sep2
|
| 89 |
+
stopping_criteria = KeywordsStoppingCriteria([stop_str], tokenizer, input_ids)
|
| 90 |
+
|
| 91 |
+
with torch.inference_mode():
|
| 92 |
+
output_ids = model.generate(
|
| 93 |
+
input_ids,
|
| 94 |
+
vision_input=[mm_data],
|
| 95 |
+
data_types=['video'],
|
| 96 |
+
do_sample=True,
|
| 97 |
+
temperature=0.4,
|
| 98 |
+
max_new_tokens=256,
|
| 99 |
+
top_p=0.7,
|
| 100 |
+
use_cache=True,
|
| 101 |
+
num_beams=1,
|
| 102 |
+
stopping_criteria=[stopping_criteria]
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
pred = tokenizer.batch_decode(output_ids, skip_special_tokens=True)[0].strip()
|
| 106 |
+
print(pred)
|
| 107 |
+
```
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
For more details, visit the [project website](https://apollo-lmms.github.io) or check out the [paper](https://arxiv.org/abs/2412.10360).
|