Skip to content
jiamingzhang94Public

About

No description, website, or topics provided.

Resources

Stars

7 stars

Watchers

0 watching

Forks

Repository files navigation

CAVALRY-V: A Large-Scale Generator Framework for Adversarial Attacks on Video MLLMs

arXiv License: MIT Python 3.8+ PyTorch

Official implementation of "CAVALRY-V: A Large-Scale Generator Framework for Adversarial Attacks on Video MLLMs"

πŸš€ Overview

CAVALRY-V is a comprehensive framework for generating adversarial attacks against Video Multimodal Large Language Models (MLLMs). Our approach introduces a novel generator-based method that can efficiently produce adversarial perturbations for both image and video inputs, enabling systematic evaluation of video MLLM robustness.

Key Features

  • 🎯 Universal Generator: Single framework for both image and video adversarial attacks
  • ⚑ Efficient Generation: Open-source large-scale generator
  • πŸ“Š Comprehensive Evaluation: Support for MMBench-Video, MME, and custom datasets
  • πŸ› οΈ Easy Integration: Compatible with VLMEvalKit for standardized evaluation

πŸ› οΈ Installation

Prerequisites

  • Python 3.8+
  • PyTorch 1.12+
  • CUDA 11.0+ (recommended)

Quick Setup

# Clone the repository
git clone https://github.com/jiamingzhang94/CAVALRY-V.git
cd CAVALRY-V

# Install dependencies
pip install -r requirements.txt

# Install VLMEvalKit for evaluation
git clone https://github.com/open-compass/VLMEvalKit.git
cd VLMEvalKit
pip install -e .

πŸ“Š Data Preparation

Required Datasets

Dataset Purpose Download Link
MMBench-Video Video evaluation HuggingFace
MME Image evaluation Auto-downloaded via HuggingFace

Optional Datasets (for training from scratch)

Dataset Purpose Download Link
LAION-400M Generator pre-training WebDataset
LLaVA-Instruct-150K Visual instruction tuning Auto-downloaded via HuggingFace
Video-MME Video finetuning Auto-downloaded via HuggingFace

πŸƒβ€β™‚οΈ Quick Start

1. Download Pre-trained Generator

Download our pre-trained generator weights:

2. Generate Adversarial Videos

python generate_video.py \
    --generator_checkpoint /path/to/generator/weights \
    --video_input_dir /path/to/MMBench-Video \
    --video_output_dir /path/to/output/adversarial/videos \
    --qa_json dataset/mmbench-video-qa.json

Note: Generation on MMBench-Video takes several hours on a single GPU. For faster processing:

  • You can run multiple instances of the generation command with different GPU assignments
  • Each instance will automatically skip already processed videos (supported via lines 157-183 in generate_video.py)
  • Remove any .inprogress files after completion

3. Generate Adversarial Images

python generate_img.py \
    --generator_checkpoint /path/to/generator/weights \
    --input_dir /path/to/MME \
    --output_dir /path/to/output/adversarial/images \
    --raw_tsv /path/to/MME.tsv \
    --output_tsv /path/to/output/adversarial/MME.tsv

Download MME.tsv from: OpenCompass

πŸ’‘ Tips for Image Generation
  • Image generation is much faster than video generation
  • --output_dir is where results are saved - you can easily modify this script to adapt to your own datasets
  • --output_tsv generates the TSV file needed for VLMEval evaluation - this is optional if you don't plan to use VLMEval

πŸ“ˆ Evaluation

Setup VLMEvalKit

⚠️ Note: This step can be somewhat complex and requires careful environment management due to dependency conflicts between different models.

  1. Configure API Keys: Set up your evaluation model API keys following VLMEvalKit documentation

    πŸ’‘ Alternative Evaluation Options
    • VLMEvalKit also provides free open-source models as scorers, though they may be slightly less effective than GPT-based evaluators
  2. Deploy Target Models: Install the MLLMs you want to evaluate:

    • Qwen2.5-VL-7B-Instruct
    • llava_video_qwen2_7b
    • Aria
    • InternVL2_5-8B
    • MiniCPM-o-2_6
    πŸ”§ Environment Management Tips

    Transformer Version Conflicts: Different models have conflicting transformer library requirements. Based on our experience:

    • Qwen and InternVL can share the same conda environment
    • Other models (llava_video, Aria, MiniCPM-o) maybe should each have their own separate conda environment
  3. Apply Custom Modifications: Copy our custom evaluation files to override original VLMEval folder

  4. Update Configuration Paths:

    • Edit VLMEvalKit/vlmeval/dataset/video_dataset_config.py:11 β†’ Set your adversarial video output path
    • Edit VLMEvalKit/vlmeval/dataset/image_yorn.py:13 β†’ Set your adversarial image TSV path

Run Evaluation

cd VLMEvalKit
./run.sh
⚠️ Important Evaluation Guidelines

Best Practices:

  • One at a time: Evaluate only one dataset and one model per run to avoid conflicts
  • Cache management: After each evaluation run, manually delete cache files in:
    • output_${data} directory
    • LMUData directory
  • Why clean cache: This prevents the evaluator from reading stale cached data that could cause errors in subsequent evaluations

πŸ—οΈ Project Structure

CAVALRY-V/
β”œβ”€β”€ dataset/                    # Dataset configurations
β”‚   └── mmbench-video-qa.json
β”œβ”€β”€ VLMEvalKit/                # Custom evaluation scripts
β”‚   β”œβ”€β”€ run.sh
β”‚   └── vlmeval/
β”œβ”€β”€ attack_utils.py            # Adversarial attack utilities
β”œβ”€β”€ config.py                  # Configuration settings
β”œβ”€β”€ generate_img.py            # Image adversarial generation
β”œβ”€β”€ generate_video.py          # Video adversarial generation
β”œβ”€β”€ model_utils.py             # Model utilities
β”œβ”€β”€ pretrain.py                # Stage 1: Pre-training
β”œβ”€β”€ visual_finetuning.py       # Stage 2: Visual finetuning
β”œβ”€β”€ video_finetuning.py        # Stage 3: Video finetuning
└── requirements.txt           # Dependencies

πŸ”¬ Training from Scratch (Optional)

Stage 1: Pre-training on LAION-400M

torchrun --nproc_per_node=8 pretrain.py \
    --image_input_dir /path/to/laion400m/tars \
    --save_path ./weights/laion/

Stage 2: Visual Finetuning

torchrun --nproc_per_node=8 visual_finetuning.py \
    --generator_checkpoint ./weights/laion/checkpoint_latest.pth \
    --save_path ./weights/visual/

Stage 3: Video Finetuning

torchrun --nproc_per_node=8 video_finetuning.py \
    --generator_checkpoint ./weights/visual/checkpoint_latest.pth \
    --save_path ./weights/video/

πŸ“ Citation

If you find this work useful in your research, please consider citing:

@article{zhang2025cavalry,
  title={CAVALRY-V: A Large-Scale Generator Framework for Adversarial Attacks on Video MLLMs},
  author={Zhang, Jiaming and Hu, Rui and Guo, Qing and Lim, Wei Yang Bryan},
  journal={arXiv preprint arXiv:2507.00817},
  year={2025}
}

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ”§ Customization and Extensions

πŸ› οΈ Adapting the Generator to Your Own Datasets

For Custom Image Datasets:

  • The generate_img.py script can be easily modified to work with your own datasets
  • Simply adjust the input/output paths and modify the image loading logic if needed
  • The TSV output format follows VLMEval standards but can be customized for other evaluation frameworks

For Custom Video Datasets:

  • Modify the video loading and processing pipeline in generate_video.py
  • Ensure your video format is supported (MP4 recommended)
  • Adjust the QA JSON format to match your dataset structure

πŸ™ Acknowledgments


⭐ If you find this project helpful, please consider giving it a star! ⭐

About

No description, website, or topics provided.

Resources

Stars

7 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages