This repository contains the official implementation of the paper MambaGuard: A CLIP-Mamba Approach for OOD Generated Image Detection.
MCDF, short for MambaClip-DeFuseNet, is a generated image detector designed for out-of-distribution (OOD) generalization. The project combines CLIP-Mamba style visual backbones with spectral feature refinement and contrastive supervision for real/fake image classification.
The official paper implementation in this repository is centered on:
main.pyas the training and evaluation entry pointconfigs/config_mcdf.pyas the default experiment configurationmodels/MCDF.pyandmodels/Dual_MCDF.pyas the main detector variants
In the paper setting, MCDF uses:
- a CLIP-Mamba backbone for robust visual representation learning
- an Einstein FFT Disentangled Block for spectral feature refinement
- a hybrid spectro-contrastive objective for improved OOD generalization
- Official implementation of
MambaGuard: A CLIP-Mamba Approach for OOD Generated Image Detection - Support for multiple backbones, including CLIP-VMamba, CLIP-Simba, and CLIP baselines
- MCDF and DualBranchMCDF detector variants
- Spectral feature modeling with
EinFFT - Supervised contrastive training support
- Generator-wise evaluation on OOD test sets
MCDF/
|-- configs/
| |-- config_mcdf.py
| `-- config_setting.py
|-- data/
|-- datasets/
| `-- dataset.py
|-- kernels/
| `-- selective_scan/
|-- mamba/
|-- models/
| |-- MCDF.py
| |-- Dual_MCDF.py
| |-- Dual_MCDF_2d.py
| |-- PIMNet.py
| |-- gilm_det.py
| |-- glim_clip.py
| |-- vmamba/
| `-- clip/
|-- pre_trained_weights/
|-- results/
|-- main.py
|-- engine.py
|-- earlystop.py
`-- utils.py
mcdfdual_mcdfmamba_clipvmunet
CLIP_VMamba_SCLIP_VMamba_BCLIP_VMamba_T220CLIP_Simba_SCLIP_Simba_BCLIP_Simba_LRN50ViT-B/32ViT-B/16ViT-L/14
Install the dependencies required by the current codebase:
pip install torch torchvision
pip install timm tensorboardX scikit-learn opencv-python pillow scipy tqdm einops transformers
pip install matplotlib thop SimpleITK medpy h5pyThe repository includes selective scan / Mamba-related CUDA components. Depending on your environment, you may also need:
- CUDA Toolkit
ninja- a compatible C/C++ build toolchain
The default official configuration in configs/config_mcdf.py expects:
- CNNSpot-style training data
- FDMAS-style OOD test data
wang2020_data_path = "../../datasets/CNNSpot"
test_root = "../../datasets/fdmas"The default wang2020 mode recursively searches for samples under:
../../datasets/CNNSpot/
|-- train/
| `-- progan/
| `-- .../
| |-- 0_real/
| `-- 1_fake/
`-- test/
`-- progan/
`-- .../
|-- 0_real/
`-- 1_fake/
For custom generator-wise testing, the code expects one subdirectory per generator under test_root:
../../datasets/fdmas/
|-- generator_a/
| |-- 0_real/
| `-- 1_fake/
|-- generator_b/
| |-- 0_real/
| `-- 1_fake/
`-- ...
Nested generator layouts are also supported as long as the final folders are named 0_real and 1_fake.
Place the required pretrained weights in the paths referenced by the config files. The default MCDF setup uses:
models/clip/VMamba_B_clip.pt
pre_trained_weights/vmamba_small_e238_ema.pth
pre_trained_weights/vmamba_tiny_e292.pth
You can switch to other backbones by editing configs/config_mcdf.py.
Edit configs/config_mcdf.py and update the key options as needed:
network = "mcdf" # or "dual_mcdf"
clip_backbone_type = "CLIP_VMamba_B"
clip_backbone_path = "models/clip/VMamba_B_clip.pt"
wang2020_data_path = "../../datasets/CNNSpot"
test_root = "../../datasets/fdmas"
work_dir = "results/MCDF_blur_sig1/"python main.pyThe script will:
- build the selected backbone and detector
- train on the configured dataset
- validate during training
- save checkpoints and logs under
results/ - run generator-wise custom testing after training finishes
To run evaluation only, set the following in configs/config_mcdf.py:
only_test_and_save_figs = True
best_ckpt_path = "path/to/best_ap.pth"
resume_txt_path = "results/your_run/tested_generators.txt"Then run:
python main.pynetwork: detector type, such asmcdfordual_mcdfclip_backbone_type: visual backbone nameclip_backbone_path: checkpoint path for the selected backbonemodel_config: detector-specific options
wang2020_data_path: path to the CNNSpot-style datasettest_root: root directory for OOD generator-wise evaluationcropSize: training crop sizeloadSize: resize resolution
blur_probblur_sigjpg_probjpg_methodjpg_qualrz_interp
batch_sizeepochsoptlrweight_decaysch
use_supconsupcon_temperaturesupcon_weight
Training outputs are saved under the configured work_dir:
checkpoints/: model checkpointslog/: training logsoutputs/: saved outputssummary/: TensorBoard event files
tensorboard --logdir=results- Some backbones and kernels require a properly configured CUDA environment.
- The repository contains additional research modules from related experiments; the official paper path is
main.pywithconfigs/config_mcdf.py. - Make sure all pretrained checkpoints are available before launching training.