• Stars
    star
    656
  • Rank 68,675 (Top 2 %)
  • Language
    Python
  • License
    Apache License 2.0
  • Created about 1 year ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

LLaMA-VID: An Image is Worth 2 Tokens in Large Language Models (ECCV 2024)

LLaMA-VID: An Image is Worth 2 Tokens in Large Language Models

LLaMA-VID empowers existing frameworks to support hour-long videos and pushes their upper limit with an extra context token. We build this repo based on LLaVA.

Release

  • [12/05] 🔥 We release the full training and evalution model, data, and scripts to support movie chating!
  • [11/29] 🔥 LLaMA-VID is comming! We release the paper, code, data, models, and demo for LLaMA-VID!

Contents

Demo

We provide some selected examples in this section. More examples can be found in our project page. Feel free to try our online demo!

Install

Please follow the instructions below to install the required packages.

  1. Clone this repository
git clone https://github.com/dvlab-research/LLaMA-VID.git
  1. Install Package
conda create -n llamavid python=3.10 -y
conda activate llamavid
cd LLaMA-VID
pip install --upgrade pip  # enable PEP 660 support
pip install -e .
  1. Install additional packages for training cases
pip install ninja
pip install flash-attn --no-build-isolation

Model

LLaMA-VID simply contains three parts: encoder and decoder are adopted to produce visual embedding and text-guided features, respectively; context token and content token are transformed with the tailored token generation strategy; instruction tuning is designed to unleash the potential of LLMs for image and video.

We provide all our fully finetuned models on Stage 1 and 2 data (Long Video + Stage 3) for LLaMA-VID:

Type Image Size Max Token Base LLM Vision Encoder Finetuning Data Finetuning schedule Download
Image only 224 4K Vicuna-7B-v1.5 EVA-G LLaVA1.5-Instruct full_ft-1e ckpt
Image only 336 4K Vicuna-7B-v1.5 EVA-G LLaVA1.5-Instruct full_ft-1e ckpt
Image only 336 4K Vicuna-13B-v1.5 EVA-G LLaVA1.5-Instruct full_ft-1e ckpt
Short video 224 4K Vicuna-7B-v1.5 EVA-G LLaVA1.5-VideoChatGPT-Instruct full_ft-1e ckpt
Short video 224 4K Vicuna-13B-v1.5 EVA-G LLaVA1.5-VideoChatGPT-Instruct full_ft-1e ckpt
Long video 224 64K Vicuna-7B-v1.5 EVA-G LLaVA1.5-VideoChatGPT-Instruct + LongVideoQA full_ft-1e ckpt

Here are the pretrained weights (text decoder + context attention + projector) on Stage 1 data only:

Type Image Size Max Token Base LLM Vision Encoder Pretrain Data Pretraining schedule Download
Image only 224 4K Vicuna-7B-v1.5 EVA-G LCS-558K 1e ckpt
Image only 336 4K Vicuna-7B-v1.5 EVA-G LCS-558K 1e ckpt
Image only 336 4K Vicuna-13B-v1.5 EVA-G LCS-558K 1e ckpt
Short video 224 4K Vicuna-7B-v1.5 EVA-G LCS-558K-WebVid-232K 1e ckpt
Short video 224 4K Vicuna-13B-v1.5 EVA-G LCS-558K-WebVid-232K 1e ckpt

Preparation

Dataset

We provide the processed image-based data for LLaMA-VID training. We organize the data in the format of LLaVA, please organize the training image-based data following this and evaluation image-based data following this. Please put the pretrained data, finetuned data, and eval data in LLaMA-VID-Pretrain, LLaMA-VID-Finetune, and LLaMA-VID-Eval subset following Structure.

For video-based dataset, please download the 2.5M subset from WebVid and ActivityNet dataset from official website or video-chatgpt. If you want to perform evaluation, please also download corresponding files from here. You can download MSVD-QA from here and MSRVTT-QA from here.

As for long video tuning, please download the long video data from MovieNet, shot detection results from here and our construced long video QA pairs from here. Place shot detection results under LLaMA-VID-Finetune/movienet/files before preprocessing.

For meta info, please download the following files and organize them as in Structure.

Data file name Size
blip_laion_cc_sbu_558k.json 181M
llava_v1_5_mix665k.json 1.03G
llava_558k_with_webvid.json 254 MB
llava_v1_5_mix665k_with_video_chatgpt.json 860 MB
llava_v1_5_mix665k_with_video_chatgpt_maxtime_5min.json 860 MB
long_videoqa.json 260MB

Pretrained Weights

We recommend users to download the pretrained weights from the following link Vicuna-7b-v1.5, Vicuna-13b-v1.5, EVA-ViT-G, QFormer-7b, QFormer-13b and put them in model_zoo following Structure.

Structure

The folder structure should be organized as follows before training.

LLaMA-VID
├── llamavid
├── scripts
├── work_dirs
│   ├── llama-vid
│   │   ├── llama-vid-13b-full-336
│   │   ├── ...
├── model_zoo
│   ├── LLM
│   │   ├── vicuna
│   │   │   ├── 7B-V1.5
│   │   │   ├── 13B-V1.5
│   ├── LAVIS
│   │   ├── eva_vit_g.pth
│   │   ├── instruct_blip_vicuna7b_trimmed.pth
│   │   ├── instruct_blip_vicuna13b_trimmed.pth
├── data
│   ├── LLaMA-VID-Pretrain
│   │   ├── blip_laion_cc_sbu_558k.json
│   │   ├── llava_558k_with_webvid.json
│   │   ├── images
│   │   ├── videos
│   ├── LLaMA-VID-Finetune
│   │   ├── llava_v1_5_mix665k.json
│   │   ├── llava_v1_5_mix665k_maxround_6_total_921k.json
│   │   ├── llava_v1_5_mix665k_maxround_12_total_714k.json
│   │   ├── llava_v1_5_mix665k_with_video_chatgpt.json
│   │   ├── llava_v1_5_mix665k_with_video_chatgpt_maxtime_5min.json
│   │   ├── long_videoqa.json
│   │   ├── movienet
│   │   ├── activitynet
│   │   ├── coco
│   │   ├── gqa
│   │   ├── ocr_vqa
│   │   ├── textvqa
│   │   ├── vg
│   ├── LLaMA-VID-Eval
│   │   ├── gqa
│   │   ├── ...

Train

LLaMA-VID training consists of three stages: (1) feature alignment stage: bridge the vision and language tokens; (2) instruction tuning stage: teach the model to follow multimodal instructions; (3) long video tuning stage: extend the position embedding and teach the model to follow hour-long video instructions.

LLaMA-VID is trained on 8 A100 GPUs with 80GB memory. To train on fewer GPUs, you can reduce the per_device_train_batch_size and increase the gradient_accumulation_steps accordingly. Always keep the global batch size the same: per_device_train_batch_size x gradient_accumulation_steps x num_gpus.

Please make sure you download and organize the data following Preparation before training.

Image Only

If you only want to train and finetune LLaMA-VID on image-based data, please run the following command for Vicuna-7B with image size 336:

bash scripts/image_only/train/stage_1_2_full_v7b_336.sh

or for Vicuna-13B with image size 336:

bash scripts/image_only/train/stage_1_2_full_v13b_336.sh

You can also try that with a smaller image size 224 and less visual tokens:

bash scripts/image_only/train/stage_1_2_full_v7b_224_grid_4.sh

Please find more training scripts in scripts/image_only/train.

Short Video

If you are interested in training and finetuning LLaMA-VID on short video-based data, please run the following command for Vicuna-7B with image size 224:

bash scripts/video/train/stage_1_2_full_v7b_224_fps_1.sh

or for Vicuna-13B with image size 224:

bash scripts/video/train/stage_1_2_full_v13b_224_fps_1.sh

Please find more training scripts in scripts/video/train.

Long Video

We provide dataset and scripts for long video-based training. Please download the long video-based data following Preparation and organize them as in Structure. In the training stage, we first extract all the frames from the long video and save the visual features local for efficient training.

python scripts/extra_tool/extract_movienet_features.py \
    --video_dir <path to movienet video> \
    --files_dir <path to movienet files> \ # files in downladed MovieNet.tar.gz
    --feat_dir <path to output features>

And run the following command for Vicuna-7B with image size 224:

bash scripts/video/train/stage_3_full_v7b_224_longvid.sh

Evaluation

We perform evaluation on both image-based and video-based benchmarks. Please download the evaluation data following Preparation and organize them as in Structure.

Image Only

LLM Res. Model GQA MMB MME POPE SEED SQA-Image VizWiz VQA v2
Vicuna-7B 224 ckpt 63.0 65.3 1405.6 86.6 59.7 67.7 52.5 78.3
Vicuna-7B 336 ckpt 64.3 65.1 1521.4 86.0 59.9 68.3 54.2 79.3
Vicuna-13B 336 ckpt 65.0 66.6 1542.3 86.0 62.3 70.0 54.3 80.0

If you want to evaluate the model on image-based benchmarks, please use the scripts in scripts/image_only/eval. For example, run the following command for GQA evaluation:

bash scripts/image_only/eval/gqa.sh

Please find more evaluation scripts in scripts/image_only/eval.

Video

LLM Res. Model MSVD-QA MSRVTT-QA ActivityNet-QA Correctness Detail Context Temporal Consistency
Vicuna-7B 224 ckpt 69.7 57.7 47.4 2.96 3.00 3.53 2.46 2.51
Vicuna-13B 224 ckpt 70.0 58.9 47.5 3.07 3.05 3.60 2.58 2.63

If you want to evaluate the model on video-based benchmarks, please use the scripts in scripts/video/eval. For example, run the following command for MSVD-QA evaluation:

bash scripts/video/eval/msvd_eval.sh

Please find more evaluation scripts in scripts/video/eval.

CLI Inference

Chat with images and videos using LLaMA-VID without the need of Gradio interface. It also supports multiple GPUs, 4-bit and 8-bit quantized inference. With 4-bit quantization. Please try this for image or video inference:

python -m llamavid.serve.cli \
    --model-path work_dirs/llama-vid/llama-vid-7b-full-336 \
    --image-file <path to your image>

or try this for video inference:

python -m llamavid.serve.cli \
    --model-path work_dirs/llama-vid/llama-vid-7b-full-224-video-fps-1 \
    --image-file <path to your video> \
    --temperature 0.5

You can also try 4bit or 8bit for efficient inference

python -m llamavid.serve.cli \
    --model-path work_dirs/llama-vid/llama-vid-7b-full-224-video-fps-1 \
    --image-file <path to your video>
    --temperature 0.5 \
    --load-4bit

Long Video Inference

For long video, if you want to inference on videos in movienet, please first process the video data and subtitles like this:

python scripts/extra_tool/extract_movienet_features.py \
    --video_dir <path to movienet video> \
    --files_dir <path to movienet files> \ # files in downladed MovieNet.tar.gz
    --feat_dir <path to output features>

If you want to inference with your customized video, please first process the video data and subtitles like this:

python scripts/extra_tool/extract_video_features_subtitles.py \
    --video_file <path to customized video> \
    --feat_dir <path to output features>

Then, please try this for long video inference:

python llamavid/serve/run_llamavid_movie.py \
    --model-path work_dirs/llama-vid/llama-vid-7b-full-224-long-video \
    --video-file <path to your processed video file> \
    --load-4bit

Gradio Web UI

Here, we adopt the Gradio UI similar to that in LLaVA to provide a user-friendly interface for LLaMA-VID. To launch a Gradio demo locally, please run the following commands one by one. If you plan to launch multiple model workers to compare between different checkpoints, you only need to launch the controller and the web server ONCE.

Launch a controller

python -m llamavid.serve.controller --host 0.0.0.0 --port 10000

Launch a gradio web server.

python -m llamavid.serve.gradio_web_server --controller http://localhost:10000 --model-list-mode reload

You just launched the Gradio web interface. Now, you can open the web interface with the URL printed on the screen. You may notice that there is no model in the model list. Do not worry, as we have not launched any model worker yet. It will be automatically updated when you launch a model worker.

Launch a model worker

This is the actual worker that performs the inference on the GPU. Each worker is responsible for a single model specified in --model-path.

python -m llamavid.serve.model_worker --host 0.0.0.0 --controller http://localhost:10000 --port 40000 --worker http://localhost:40000 --model-path work_dirs/llama-vid/llama-vid-vicuna-7b-short

Wait until the process finishes loading the model and you see "Uvicorn running on ...". Now, refresh your Gradio web UI, and you will see the model you just launched in the model list.

You can launch as many workers as you want, and compare between different models in the same Gradio interface. For example, short video model here. Please keep the --controller the same, and modify the --port and --worker to a different port number for each worker.

python -m llamavid.serve.model_worker_short --host 0.0.0.0 --controller http://localhost:10000 --port <different from 40000, say 40001> --worker http://localhost:<change accordingly, i.e. 40001> --model-path work_dirs/llama-vid/llama-vid-7b-full-224-video-fps-1

If you are using an Apple device with an M1 or M2 chip, you can specify the mps device by using the --device flag: --device mps.

Launch a model worker (Multiple GPUs, when GPU VRAM <= 24GB)

If the VRAM of your GPU is less than 24GB (e.g., RTX 3090, RTX 4090, etc.), you may try running it with multiple GPUs. Our latest code base will automatically try to use multiple GPUs if you have more than one GPU. You can specify which GPUs to use with CUDA_VISIBLE_DEVICES. Below is an example of running with the first two GPUs.

CUDA_VISIBLE_DEVICES=0,1 python -m llamavid.serve.model_worker --host 0.0.0.0 --controller http://localhost:10000 --port 40000 --worker http://localhost:40000 --model-path work_dirs/llama-vid/llama-vid-7b-full-224-long-video

Launch a model worker (4-bit, 8-bit inference, quantized)

You can launch the model worker with quantized bits (4-bit, 8-bit), which allows you to run the inference with reduced GPU memory footprint. Note that inference with quantized bits may not be as accurate as the full-precision model. Simply append --load-4bit or --load-8bit to the model worker command that you are executing. Below is an example of running with 4-bit quantization.

python -m llamavid.serve.model_worker --host 0.0.0.0 --controller http://localhost:10000 --port 40000 --worker http://localhost:40000 --model-path work_dirs/llama-vid/llama-vid-7b-full-224-long-video --load-4bit

Examples

We provide some examples in this section. More examples can be found in our project page.

Citation

If you find this repo useful for your research, please consider citing the paper

@article{li2023llamavid,
  title={LLaMA-VID: An Image is Worth 2 Tokens in Large Language Models},
  author={Li, Yanwei and Wang, Chengyao and Jia, Jiaya},
  journal={arXiv preprint arXiv:2311.17043},
  year={2023}
}

Acknowledgement

We would like to thank the following repos for their great work:

  • This work is built upon the LLaVA.
  • This work utilizes LLMs from Vicuna.
  • This work utilizes pretrained weights from InstructBLIP.
  • We perform video-based evaluation from Video-ChatGPT.

License

Code License Data License Weight License

The data and checkpoint is intended and licensed for research use only. They are also restricted to uses that follow the license agreement of LLaVA, LLaMA, Vicuna and GPT-4. The dataset is CC BY NC 4.0 (allowing only non-commercial use) and models trained using the dataset should not be used outside of research purposes.

More Repositories

1

MGM

Official repo for "Mini-Gemini: Mining the Potential of Multi-modality Vision Language Models"
Python
3,111
star
2

LongLoRA

Code and documents of LongLoRA and LongAlpaca (ICLR 2024 Oral)
Python
2,563
star
3

LISA

Project Page for "LISA: Reasoning Segmentation via Large Language Model"
Python
1,680
star
4

VoxelNeXt

VoxelNeXt: Fully Sparse VoxelNet for 3D Object Detection and Tracking (CVPR 2023)
Python
692
star
5

DeepUPE

Underexposed Photo Enhancement Using Deep Illumination Estimation
Python
566
star
6

3D-Box-Segment-Anything

We extend Segment Anything to 3D perception by combining it with VoxelNeXt.
Jupyter Notebook
524
star
7

LLMGA

This project is the official implementation of 'LLMGA: Multimodal Large Language Model based Generation Assistant', ECCV2024
Python
424
star
8

ControlNeXt

Controllable video and image Generation, SVD, Animate Anyone, ControlNet, LoRA
Python
417
star
9

PanopticFCN

Fully Convolutional Networks for Panoptic Segmentation (CVPR2021 Oral)
Python
391
star
10

PointGroup

PointGroup: Dual-Set Point Grouping for 3D Instance Segmentation
Python
376
star
11

3DSSD

3DSSD: Point-based 3D Single Stage Object Detector (CVPR 2020)
Python
375
star
12

Video-P2P

Video-P2P: Video Editing with Cross-attention Control
Python
365
star
13

FocalsConv

Focal Sparse Convolutional Networks for 3D Object Detection (CVPR 2022, Oral)
Python
364
star
14

Stratified-Transformer

Stratified Transformer for 3D Point Cloud Segmentation (CVPR 2022)
Python
362
star
15

DSGN

DSGN: Deep Stereo Geometry Network for 3D Object Detection (CVPR 2020)
Python
324
star
16

PFENet

PFENet: Prior Guided Feature Enrichment Network for Few-shot Segmentation (TPAMI).
Python
307
star
17

SphereFormer

The official implementation for "Spherical Transformer for LiDAR-based 3D Recognition" (CVPR 2023).
Python
300
star
18

GridMask

Python
281
star
19

ReviewKD

Distilling Knowledge via Knowledge Review, CVPR 2021
Python
249
star
20

Parametric-Contrastive-Learning

Parametric Contrastive Learning (ICCV2021) & GPaCo (TPAMI 2023)
Python
237
star
21

Step-DPO

Implementation for "Step-DPO: Step-wise Preference Optimization for Long-chain Reasoning of LLMs"
Python
232
star
22

Simple-SR

Include MuCAN, LAPAR, etc.
Python
224
star
23

UVTR

Unifying Voxel-based Representation with Transformer for 3D Object Detection (NeurIPS 2022)
Python
224
star
24

Facelet_Bank

Facelet-Bank for Fast Portrait Manipulation (pytorch)
Python
208
star
25

SA-AutoAug

Scale-aware Automatic Augmentation for Object Detection (CVPR 2021)
Python
196
star
26

LargeKernel3D

LargeKernel3D: Scaling up Kernels in 3D Sparse CNNs (CVPR 2023)
Python
189
star
27

SNR-Aware-Low-Light-Enhance

This is the official implementation for the paper "SNR-aware low-light image enhancement" in CVPR2022
Python
160
star
28

MASA-SR

MASA-SR: Matching Acceleration and Spatial Adaptation for Reference-Based Image Super-Resolution (CVPR2021)
Python
158
star
29

ECCV22-P3AFormer-Tracking-Objects-as-Pixel-wise-Distributions

The official code for our ECCV22 oral paper: tracking objects as pixel-wise distributions.
Python
158
star
30

Context-Aware-Consistency

Semi-supervised Semantic Segmentation with Directional Context-aware Consistency (CVPR 2021)
Python
155
star
31

SparseTransformer

A fast and memory-efficient libarary for sparse transformer with varying token numbers (e.g., 3D point cloud).
Python
153
star
32

spconv-plus

Python
152
star
33

EfficientNeRF

The official code for "Efficient Neural Radiance Fields" in CVPR2022.
Python
152
star
34

MiSLAS

Improving Calibration for Long-Tailed Recognition (CVPR2021)
Python
144
star
35

RIVAL

[NeurIPS 2023 Spotlight] Real-World Image Variation by Aligning Diffusion Inversion Chain
Python
143
star
36

MOOD

Official PyTorch implementation of MOOD series: (1) MOODv1: Rethinking Out-of-distributionDetection: Masked Image Modeling Is All You Need. (2) MOODv2: Masked Image Modeling for Out-of-Distribution Detection.
Python
133
star
37

outpainting_srn

Wide-Context Semantic Image Extrapolation, CVPR2019
Python
131
star
38

MSAD

Multi-Scale Aligned Distillation for Low-Resolution Detection (CVPR2021)
Python
127
star
39

DeepVision3D

DeepVision3D is an open source toolbox for point-cloud understanding.
Python
119
star
40

Ref-NPR

[CVPR 2023] Ref-NPR: Reference-Based Non-PhotoRealistic Radiance Fields
Python
119
star
41

VFIformer

Video Frame Interpolation with Transformer (CVPR2022)
Python
112
star
42

Prompt-Highlighter

[CVPR 2024] Prompt Highlighter: Interactive Control for Multi-Modal LLMs
Python
112
star
43

VFF

Voxel Field Fusion for 3D Object Detection (CVPR2022)
Python
95
star
44

SMR

Self-Supervised 3D Mesh Reconstruction from Single Images (CVPR2021)
Python
91
star
45

SCGAN

The implementation of 'Image synthesis via semantic composition', ICCV2021.
Python
81
star
46

Imbalanced-Learning

Imbalanced learning tool for imbalanced recognition and segmentation
Python
79
star
47

JigsawClustering

This is the code for CVPR 2021 oral paper: Jigsaw Clustering for Unsupervised Visual Representation Learning
Python
78
star
48

AttenNorm

Attentive Normalization for Conditional Image Generation
Python
71
star
49

GFS-Seg

The official implementation of Generalized Few-shot Semantic Segmentation (CVPR 2022)
Python
63
star
50

Mask-Attention-Free-Transformer

Official Implementation for "Mask-Attention-Free Transformer for 3D Instance Segmentation"
Python
59
star
51

MoTCoder

This is the official code repository of MoTCoder: Elevating Large Language Models with Modular of Thought for Challenging Programming Tasks.
Python
58
star
52

SDSD

Seeing Dynamic Scene in the Dark: High-Quality Video Dataset with Mechatronic Alignment (ICCV2021)
Python
48
star
53

GroupContrast

[CVPR 2024] GroupContrast: Semantic-aware Self-supervised Representation Learning for 3D Understanding
42
star
54

ProposeReduce

Video Instance Segmentation with a Propose-Reduce Paradigm (ICCV 2021)
Python
41
star
55

Robust-Semantic-Segmentation

Dynamic Divide-and-Conquer Adversarial Training for Robust Semantic Segmentation (ICCV2021)
Python
40
star
56

Mr-Ben

This is the repo for our paper "Mr-Ben: A Comprehensive Meta-Reasoning Benchmark for Large Language Models"
Python
38
star
57

BAL

BAL: Balancing Diversity and Novelty for Active Learning - Official Pytorch Implementation
Python
38
star
58

TriVol

The official code of TriVol in CVPR-2023
Python
37
star
59

MR-GSM8K

Challenge LLMs to Reason About Reasoning: A Benchmark to Unveil Cognitive Depth in LLMs
Python
37
star
60

DecoupleNet

Official implementation for our ECCV 2022 paper "DecoupleNet: Decoupled Network for Domain Adaptive Semantic Segmentation"
Python
36
star
61

Dsig

Deep Structured Instance Graph for Distilling Object Detectors (ICCV 2021)
Python
35
star
62

LBGAT

Learnable Boundary Guided Adversarial Training (ICCV2021)
Python
33
star
63

Q-LLM

This is the official repo of "QuickLLaMA: Query-aware Inference Acceleration for Large Language Models"
Python
31
star
64

AGSS-VOS

AGSS-VOS: Attention Guided Single-Shot Video Object Segmentation
Python
20
star
65

MAT

MAT: Mask-Aware Transformer for Large Hole Image Inpainting
Python
16
star
66

MSN

Memory Selection Network for Video Propagation (ECCV 2020)
Python
6
star
67

APD

Python
5
star
68

Point2Pix

The official code of Point2pix in CVPR-2023
2
star
69

TagCLIP

Python
2
star