Rethinking the Up-Sampling Operations in CNN-based Generative Network for Generalizable Deepfake Detection

August 15, 2024 · View on GitHub


Beijing Jiaotong University, YanShan University, A*Star

overall pipeline

Reference github repository for the paper Rethinking the Up-Sampling Operations in CNN-based Generative Network for Generalizable Deepfake Detection.

@misc{tan2023rethinking,
      title={Rethinking the Up-Sampling Operations in CNN-based Generative Network for Generalizable Deepfake Detection}, 
      author={Chuangchuang Tan and Huan Liu and Yao Zhao and Shikui Wei and Guanghua Gu and Ping Liu and Yunchao Wei},
      year={2023},
      eprint={2312.10461},
      archivePrefix={arXiv},
      primaryClass={cs.CV}
}

News 🆕

  • 2024/02: NPR is accepted by CVPR 2024! Congratulations and thanks to my all co-authors!
  • 2024/05: 🤗Online Demo

Environment setup

Classification environment: We recommend installing the required packages by running the command:

pip install -r requirements.txt

In order to ensure the reproducibility of the results, we provide the following suggestions:

Getting the data

paperUrl
Train setCNNDetection CVPR2020Baidudrive
Val setCNNDetection CVPR2020Baidudrive
Table1 TestCNNDetection CVPR2020Baidudrive
Table2 TestFreqNet AAAI2024googledrive
Table3 TestDIRE ICCV2023googledrive
Table4 TestUniversalFakeDetect CVPR2023googledrive
Table5 TestDiffusion1kStepgoogledrive
pip install gdown==4.7.1

chmod 777 ./download_dataset.sh

./download_dataset.sh

Directory structure

Click to expand the folder tree structure.
datasets
|-- ForenSynths_train_val
|   |-- train
|   |   |-- car
|   |   |-- cat
|   |   |-- chair
|   |   `-- horse
|   `-- val
|   |   |-- car
|   |   |-- cat
|   |   |-- chair
|   |   `-- horse
|   |-- test
|       |-- biggan
|       |-- cyclegan
|       |-- deepfake
|       |-- gaugan
|       |-- progan
|       |-- stargan
|       |-- stylegan
|       `-- stylegan2
`-- Generalization_Test
    |-- ForenSynths_test       # Table1
    |   |-- biggan
    |   |-- cyclegan
    |   |-- deepfake
    |   |-- gaugan
    |   |-- progan
    |   |-- stargan
    |   |-- stylegan
    |   `-- stylegan2
    |-- GANGen-Detection     # Table2
    |   |-- AttGAN
    |   |-- BEGAN
    |   |-- CramerGAN
    |   |-- InfoMaxGAN
    |   |-- MMDGAN
    |   |-- RelGAN
    |   |-- S3GAN
    |   |-- SNGAN
    |   `-- STGAN
    |-- DiffusionForensics  # Table3
    |   |-- adm
    |   |-- ddpm
    |   |-- iddpm
    |   |-- ldm
    |   |-- pndm
    |   |-- sdv1_new
    |   |-- sdv2
    |   `-- vqdiffusion
    `-- UniversalFakeDetect # Table4
    |   |-- dalle
    |   |-- glide_100_10
    |   |-- glide_100_27
    |   |-- glide_50_27
    |   |-- guided          # Also known as ADM.
    |   |-- ldm_100
    |   |-- ldm_200
    |   `-- ldm_200_cfg
    |-- Diffusion1kStep     # Table5
        |-- DALLE
        |-- ddpm
        |-- guided-diffusion    # Also known as ADM.
        |-- improved-diffusion  # Also known as IDDPM.
        `-- midjourney


Training the model

CUDA_VISIBLE_DEVICES=0 ./pytorch18/bin/python train.py --name 4class-resnet-car-cat-chair-horse --dataroot ./datasets/ForenSynths_train_val --classes car,cat,chair,horse --batch_size 32 --delr_freq 10 --lr 0.0002 --niter 50

Testing the detector

Modify the dataroot in test.py.

CUDA_VISIBLE_DEVICES=0 ./pytorch18/bin/python test.py --model_path ./NPR.pth  --batch_size {BS}

Detection Results

AIGCDetectBenchmark using ProGAN-4class checkpoint

When testing on AIGCDetectBenchmark, set no_resize and no_crop to True, and set batch_size to 1. To deal with images of odd sizes, add the following code in network/resnet.py.

n,c,w,h = x.shape
if w%2 == 1 : x = x[:,:,:-1,:]
if h%2 == 1 : x = x[:,:,:,:-1]
GeneratorCNNSpotFreDectFusingGramNetLNPLGradDIRE-GDIRE-DUnivFDRPTConNPR
ProGAN100.0099.36100.0099.9999.6799.8395.1952.7599.81100.0099.9
StyleGan90.1778.0285.2087.0591.7591.0883.0351.3184.9392.7796.1
BigGAN71.1781.9777.4067.3377.7585.6270.1249.7095.0895.8087.3
CycleGAN87.6278.7787.0086.0784.1086.9474.1949.5898.3370.1790.3
StarGAN94.6094.6297.0095.0599.9299.2795.4746.7295.7599.9799.6
GauGAN81.4280.5777.0069.3575.3978.4667.7951.2399.4771.5885.4
Stylegan286.9166.1983.3087.2894.6485.3275.3151.7274.9689.5598.1
WFIR91.6550.7566.8086.8070.8555.7058.0553.3086.9085.8060.7
ADM60.3963.4249.0058.6184.7367.1575.7898.2566.8782.1784.9
Glide58.0754.1357.2054.5080.5266.1171.7592.4262.4683.7996.7
Midjourney51.3945.8752.2050.0265.5565.3558.0189.4556.1390.1292.6
SDv1.450.5738.7951.0051.7085.5563.0249.7491.2463.6695.3897.4
SDv1.550.5339.2151.4052.1685.6763.6749.8391.6363.4995.3097.5
VQDM56.4677.8055.1052.8674.4672.9953.6891.9085.3188.9190.1
Wukong51.0340.3051.7050.7682.0659.5554.4690.9070.9391.0791.7
DALLE250.4534.7052.8049.2588.7565.4566.4892.4550.7596.6099.6
Average70.7864.0368.3868.6783.8475.3468.6871.5378.4389.3191.7

GenImage

(1) Change "resize" to "translate and duplicate". (2) Set random seed to 70. (3) During testing, set no_crop to False.

(1)

dset = datasets.ImageFolder(
    root,
    transforms.Compose([
        # rz_func,
	transforms.Lambda(lambda img: translate_duplicate(img, opt.cropSize)),
	crop_func,
	flip_func,
	transforms.ToTensor(),
	transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
    ]))

import math
def translate_duplicate(img, cropSize):
    if min(img.size) < cropSize:
        width, height = img.size
        
        new_width = width * math.ceil(cropSize/width)
        new_height = height * math.ceil(cropSize/height)
        
        new_img = Image.new('RGB', (new_width, new_height))
        for i in range(0, new_width, width):
            for j in range(0, new_height, height):
                new_img.paste(img, (i, j))
        return new_img
    else:
        return img

(2) Set random seed to 70.

(3) During testing, set no_crop to False. And set test config

vals =       ['ADM', 'biggan', 'glide', 'midjourney', 'sdv5', 'vqdm', 'wukong']
multiclass = [ 0,     0,        0,       0,            0,      0,      0      ]
./pytorch18/bin/python  train.py --dataroot {GenImage Path} --name sdv4_bs32_ --batch_size 32 --lr 0.0002 --niter 1  --cropSize 224 --classes sdv4 

Train with sdv4 as the training set, using a random seed of 70. Pretrained checkpoint.

GeneratorAcc.A.P.
ADM87.896.0
biggan80.789.8
glide93.299.1
midjourney91.797.9
sdv594.499.9
vqdm88.796.1
wukong94.099.7
Mean90.196.9

Acknowledgments

This repository borrows partially from the CNNDetection.