Yingxin Lai1, Zhiming Luo1, Zitong Yu1,2*
1 Xiamen University 2 Great Bay University
This repository is the official implementation of Detect Any Deepfakes (CCBR 2023).
We propose DADF, a novel framework that adapts the Segment Anything Model (SAM) for the task of face forgery detection and localization. By incorporating low-level forgery traces into the SAM architecture, our model achieves state-of-the-art performance in pixel-level forgery localization while maintaining the versatility to be applied to other segmentation tasks (e.g., camouflage, shadow detection).
To ensure reproducibility, we recommend using a clean Conda environment.
# Create environment
conda create -n dadf python=3.8 -y
conda activate dadf
# Install PyTorch (Adjust CUDA version based on your driver)
pip install torch==1.13.0+cu117 torchvision==0.14.0+cu117 --extra-index-url [https://download.pytorch.org/whl/cu117](https://download.pytorch.org/whl/cu117)
# Install Dependencies
pip install -r requirements.txt
Please download the pre-trained weights and put them into the pretrained/ directory.
- **
defaultorvit_h**: ViT-H SAM model. (Recommended) vit_l: ViT-L SAM model.vit_b: ViT-B SAM model.
Please organize your datasets under the ./load/ directory.
| Task | Dataset | Source Link |
|---|---|---|
| Deepfake | FaceForensics++ | Official Github |
| Detect & Locate | Official Github | |
| Camouflage | COD10K | Official Github |
| CAMO | Google Drive | |
| Shadow | ISTD | Official Github |
| Medical | Kvasir (Polyp) | Official Website |
- Download the dataset and put it in
./load. - Download the pre-trained SAM (Segment Anything) and put it in
./pretrained. - Training:
# run.sh
CUDA_VISIBLE_DEVICES=0,1,2,3 \
python -m torch.distributed.launch --nnodes=1 --nproc_per_node=4 --master_port=29501 \
train.py --config configs/demo.yaml
⚠️ Note: The SAM model consumes significant memory. We use 4 x A100 graphics cards for training. If you encounter memory issues, please try using graphics cards with larger VRAM or reduce the batch size.
We support distributed training (DDP). Adjust nproc_per_node based on your GPU availability.
# Example: Train on 4 GPUs
CUDA_VISIBLE_DEVICES=0,1,2,3 \
python -m torch.distributed.launch --nnodes=1 --nproc_per_node=4 --master_port=29500 \
train.py --config configs/dadf_vit_h.yaml
Note: If you encounter OOM (Out of Memory) errors with ViT-H, try reducing the batch size in the config or switching to ViT-B/L backbones.
Evaluate the model performance and generate localization maps.
python test.py --config configs/dadf_vit_h.yaml --model checkpoints/dadf_best.pth
If you find this code or paper useful, please cite our work:
@article{lai2023detect,
title={Detect Any Deepfakes: Segment Anything Meets Face Forgery Detection and Localization},
author={Lai, Yingxin and Luo, Zhiming and Yu, Zitong},
journal={Chinese Conference on Biometric Recognition (CCBR)},
year={2023}
}
This project is built upon SAM-Adapter. We thank the authors for their excellent codebase.