论文"Chapter-LLaMA: 利用 LLM 在长达一小时的视频中进行高效分章"的官方 PyTorch 实现
This repository contains the code for the paper "Chapter-Llama: Efficient Chaptering in Hour-Long Videos with LLMs" (CVPR 2025).
Please visit our webpage for more details.
Project Structure
…Create environment
conda create python=3.12 --name chapter-llama -y
conda activate chapter-llamaTo install the necessary packages for training and testing, you can use the provided requirements.txt file:
python -m pip install -r requirements.txtor
python -m pip install -e .The code was tested on Python 3.12.9 and PyTorch 2.6.0
For inference on videos that are not in the VidChapters-7M dataset, you will need to install the following dependencies to extract ASR and captions from the video (not required for training):
python -m pip install -e ".[inference]"Download models
The Llama-3.1-8B-Instruct model will be downloaded automatically from Hugging Face,
make sure you agree to the license terms here.
If you already have it downloaded, please check the llama3.1_8B.yaml config file to specify the checkpoint path.
We provide 3 LoRA parameter sets for Llama-3.1-8B-Instruct:
asr-10k: Model trained with ASR from 10k videos of the VidChapters-7M dataset. Used for our Speech-based frame selector.captions_asr-10k: Model trained with Captions+ASR from 10k videos of the VidChapters-7M dataset. Used for most of our experiments.captions_asr-1k: Model trained with Captions+ASR from 1k videos of the VidChapters-7M dataset. Used for the full test set.To download the LoRA parameter sets, run:
python tools/download/models.py "asr-1k" --local_dir "."
python tools/download/models.py "asr-10k" --local_dir "."
python tools/download/models.py "captions_asr-1k" --local_dir "."
python tools/download/models.py "captions_asr-10k" --local_dir "."Download captions
First, create a directory to store the data and create a symlink to it from the VidChapters directory:
mkdir path/to/VidChapters/
ln -s path/to/VidChapters/ dataset/The dataset/captions/ directory contains the video captions organized by the captioning model used (HwwwH_MiniCPM-V-2) and the sampling method.
To download the captions for our sampling method (asr_s10k-2_train_preds+no-asr-10s), run:
bash tools/download/captions.sh asr_s10k-2_train_preds+no-asr-10sThis method uses predictions from an ASR model trained on the s10k-2 subset when ASR is available for the video,
and falls back to sampling frames every 10 seconds when ASR is not available.
The other sampling methods available are:
bash tools/download/captions.sh 10s
bash tools/download/captions.sh 100f
bash tools/download/captions.sh shot_boundariesPlease refer to the how_to_extract_captions.md documentation for more details.
Download docs
The dataset/docs/ directory contains the ASR and chapter data for the VidChapters-7M dataset. We provide:
To download a specific subset's data (which includes video ids, ASR and chapter information), run:
bash tools/download/docs.sh subset_nameWhere subset_name can be full for the complete dataset or one of the following:
Training sets:
sml1k_train: Training set with 1k videos (short+medium+long), used for ablation studies.sml10k_train: Training set with 10k videos (short+medium+long), used for Table 1 and the data scaling experiment (Figure 4).s10k-2_train: Training set with 10k videos (short), used for our Speech-based frame selector.Validation sets:
sml300_val: Validation set with 300 videos (short+medium+long)s100_val: Validation set with 100 videos (short)m100_val: Validation set with 100 videos (medium)l100_val: Validation set with 100 videos (long)Test sets:
s_test: Short videos (Download videos
Note: Downloading videos is not necessary for training or testing since we provide the extracted captions and ASR data via Hugging Face (see commands above). This step is only required if you want to process new videos from different subsets or extract additional captions.
To download videos, install the yt-dlp library and run:
python tools/download/videos.py dataset/docs/subset_data/subset_name.jsonThe videos will be downloaded to dataset/videos/.
If you encounter any issues with yt-dlp, you can find the video subsets sml1k_train, sml300_val, and test on Hugging Face.
Access them here: Hugging Face Datasets - lucas-ventura/chapter-llama.
Dataset structure Here's how the dataset folder should be structured:
…Training and testing
The command to launch a training experiment is the following:
python train.py [OPTIONS]or to run both train.py and test.py with the same options:
bash train.sh [OPTIONS]For only testing, run:
python test.py [OPTIONS]Note: You might need to run the test script with a single GPU (CUDA_VISIBLE_DEVICES=0).
Configuration
The project uses Hydra for configuration management. Key configuration options:
data: asr, captions, captions_asr, captions_asr_given_times, captions_asr_given_titles, captions_asr_windowsubset_train: Training dataset subset (default: "s1k_train")paths: To change default paths, create a default.yaml file in configs/local/ and modify it as in configs/local/example.yamlmodel: llama3.1_8B (default), zero-shot, llama3.2_3B, etc.For example, to run training with the sml1k_train subset with ASR only, run:
bash train.sh data=asr subset_train=sml1k_train subset_test=sml300_valResults
To get results from a single test experiment, run:
python tools/results/evaluate_results.py path/to/experiment/test_dir --subset subset_nameFor example:
python tools/results/evaluate_results.py outputs/chapterize/Meta-Llama-3.1-8B-Instruct/captions_asr/asr_s10k-2_train_preds+no-asr-10s/sml1k_train/default/test/Additionally, you can use the tools/results/evaluate_results.ipynb notebook to compare results from different video chapter generation experiments.
Single Video Chaptering
If you just want to generate chapters for a single video:
# Clone the repository
git clone https://github.com/lucas-ventura/chapter-llama.git
cd chapter-llama
# Create a conda environment (optional but recommended)
conda create python=3.12 --name chapter-llama -y
conda activate chapter-llama
# Install the package with inference dependencies
python -m pip install -e ".[inference]"
# Run the chaptering command
python inference.py /path/to/your/video.mp4Currently, the command only uses the audio (via ASR extraction) to generate chapters. Support for automatic visual caption extraction will be added soon.
Chapters and the full output text will be saved in outputs/inference//.
Interactive Demo
We also provide an interactive demo where you can upload videos and visualize the generated chapters:
# Clone the repository
git clone https://github.com/lucas-ventura/chapter-llama.git
cd chapter-llama
# Install demo dependencies
python -m pip install -e ".[demo]"
# Launch the demo
python demo.pyThe demo will start a local web server that you can access in your browser. You can upload videos, generate chapters, and see them visualized on the video timeline.
If you use this code in your work, please cite our paper:
@InProceedings{ventura25chapter,
title = {{Chapter-Llama}: Efficient Chaptering in Hour-Long Videos with {LLM}s},
author = {Lucas Ventura and Antoine Yang and Cordelia Schmid and G{"u}l Varol},
booktitle = {CVPR},
year = {2025}
}Based on llama-cookbook and lightning-hydra-template.
This code is distributed under an MIT License.
Our models are trained using the VidChapters-7M dataset, which has its own license that must be followed. Additionally, this project depends on several third-party libraries and resources, each with their own licenses: PyTorch, Hugging Face Transformers, Hydra, Lightning, Llama models.
暂无开放 Issues,或尚未同步最近议题。