Home

Awesome

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

<p align="center"> <br> Beijing Jiaotong University, YanShan University, A*Star </p> <img src="./NPR.png" width="100%" alt="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 🆕

<a href="https://huggingface.co/spaces/tancc/Generalizable_Deepfake_Detection-NPR-CVPR2024"><img src="assets/demo_detection.gif" width="70%"></a>

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

<!-- Download dataset from [CNNDetection CVPR2020 (Table1 results)](https://github.com/peterwang512/CNNDetection), [GANGen-Detection (Table2 results)](https://github.com/chuangchuangtan/GANGen-Detection) ([googledrive](https://drive.google.com/drive/folders/11E0Knf9J1qlv2UuTnJSOFUjIIi90czSj?usp=sharing)), [UniversalFakeDetect CVPR2023](https://github.com/Yuheng-Li/UniversalFakeDetect) ([googledrive](https://drive.google.com/drive/folders/1nkCXClC7kFM01_fqmLrVNtnOYEFPtWO-?usp=drive_link)), [DIRE 2023ICCV](https://github.com/ZhendongWang6/DIRE) ([googledrive](https://drive.google.com/drive/folders/1jZE4hg6SxRvKaPYO_yyMeJN_DOcqGMEf?usp=sharing)), Diffusion1kStep [googledrive](https://drive.google.com/drive/folders/14f0vApTLiukiPvIHukHDzLujrvJpDpRq?usp=sharing). -->
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

<details> <summary> Click to expand the folder tree structure. </summary>
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


</details>

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

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

(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      ]
</details>
./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
<!-- | <font size=2>Method</font>|<font size=2>ProGAN</font> | |<font size=2>StyleGAN</font>| |<font size=2>StyleGAN2</font>| |<font size=2>BigGAN</font>| |<font size=2>CycleGAN</font> | |<font size=2>StarGAN</font>| |<font size=2>GauGAN</font> | |<font size=2>Deepfake</font>| | <font size=2>Mean</font> | | |:----------------------:|:-----:|:-----:|:------:|:---:|:-------:|:--:|:----:|:-----:|:-------:|:----:|:----: |:-----:|:---: |:-----:|:----:|:----:|:----:|:----:| | | Acc. | A.P. | Acc. | A.P.| Acc. | A.P. | Acc.| A.P. | Acc. | A.P. | Acc. | A.P. | Acc. | A.P. | Acc. | A.P. | Acc. | A.P. | | CNNDetection | 91.4 | 99.4 | 63.8 | 91.4| 76.4 | 97.5 | 52.9| 73.3 | 72.7 | 88.6 | 63.8 | 90.8 | 63.9 | 92.2 | 51.7 | 62.3 | 67.1 | 86.9 | | Frank | 90.3 | 85.2 | 74.5 | 72.0| 73.1 | 71.4 | 88.7| 86.0 | 75.5 | 71.2 | 99.5 | 99.5 | 69.2 | 77.4 | 60.7 | 49.1 | 78.9 | 76.5 | | Durall | 81.1 | 74.4 | 54.4 | 52.6| 66.8 | 62.0 | 60.1| 56.3 | 69.0 | 64.0 | 98.1 | 98.1 | 61.9 | 57.4 | 50.2 | 50.0 | 67.7 | 64.4 | | Patchfor | 97.8 | 100.0 | 82.6 | 93.1| 83.6 | 98.5 | 64.7| 69.5 | 74.5 | 87.2 | 100.0 | 100.0 | 57.2 | 55.4 | 85.0 | 93.2 | 80.7 | 87.1 | | F3Net | 99.4 | 100.0 | 92.6 | 99.7| 88.0 | 99.8 | 65.3| 69.9 | 76.4 | 84.3 | 100.0 | 100.0 | 58.1 | 56.7 | 63.5 | 78.8 | 80.4 | 86.2 | | SelfBland | 58.8 | 65.2 | 50.1 | 47.7| 48.6 | 47.4 | 51.1| 51.9 | 59.2 | 65.3 | 74.5 | 89.2 | 59.2 | 65.5 | 93.8 | 99.3 | 61.9 | 66.4 | | GANDetection | 82.7 | 95.1 | 74.4 | 92.9| 69.9 | 87.9 | 76.3| 89.9 | 85.2 | 95.5 | 68.8 | 99.7 | 61.4 | 75.8 | 60.0 | 83.9 | 72.3 | 90.1 | | BiHPF | 90.7 | 86.2 | 76.9 | 75.1| 76.2 | 74.7 | 84.9| 81.7 | 81.9 | 78.9 | 94.4 | 94.4 | 69.5 | 78.1 | 54.4 | 54.6 | 78.6 | 77.9 | | FrePGAN | 99.0 | 99.9 | 80.7 | 89.6| 84.1 | 98.6 | 69.2| 71.1 | 71.1 | 74.4 | 99.9 | 100.0 | 60.3 | 71.7 | 70.9 | 91.9 | 79.4 | 87.2 | | LGrad | 99.9 | 100.0 | 94.8 | 99.9| 96.0 | 99.9 | 82.9| 90.7 | 85.3 | 94.0 | 99.6 | 100.0 | 72.4 | 79.3 | 58.0 | 67.9 | 86.1 | 91.5 | | Ojha | 99.7 | 100.0 | 89.0 | 98.7| 83.9 | 98.4 | 90.5| 99.1 | 87.9 | 99.8 | 91.4 | 100.0 | 89.9 | 100.0 | 80.2 | 90.2 | 89.1 | 98.3 | | NPR(our) | 99.8 | 100.0 | 96.3 | 99.8| 97.3 | 100.0| 87.5| 94.5 | 95.0 | 99.5 | 99.7 | 100.0 | 86.6 | 88.8 | 77.4 | 86.2 | 92.5 | 96.1 | -->

Acknowledgments

This repository borrows partially from the CNNDetection.