The AntiDeepfake project provides a series of powerful foundation models post-trained for deepfake detection. The AntiDeepfake model can be used for feature extraction for deepfake detection in a zero-shot manner, or it may be further fine-tuned and optimized for a specific database or deepfake-related task.
### New conda environments ###
conda create --name antideepfake python==3.9.0
conda activate antideepfake
conda install pip==24.0
### Install Fariseq ###
# fairseq 0.10.2 on pip does not work
git clone https://github.com/pytorch/fairseq
cd fairseq
# checkout this specific commit. Latest commit does not work
git checkout 862efab86f649c04ea31545ce28d13c59560113d
pip install --editable .
### Install other packages ###
pip install huggingface-hub==0.31.1 safetensors==0.5.3 soundfile==0.13.1 numpy==1.21.2
Additionally, you need to update line 315 in /where/you/clone/fairseq/fairseq/checkpoint_utils.py to:
state = torch.load(f, map_location=torch.device("cpu"), weights_only=False)
🚀 Inference:
python
1import os
2import urllib.request
3import torch
4import torchaudio
5import fairseq
6from huggingface_hub import PyTorchModelHubMixin
78# This is the only part of the script you need to modify.9# Set this to the path where your audio files are stored.10folder_path ="/path/to/folder/contains/wavs/"11audio_formats =(".mp3",".wav",".flac",".m4a")1213# === Set device (use GPU if available) ===14device = torch.device("cuda"if torch.cuda.is_available()else"cpu")15print(f"Using device: {device}")1617# === Download Fairseq checkpoint if not present ===18# The downloaded checkpoint is used for building front-end architecture,19# its weights will be replaced by the model.safetensors file in this repo.20ssl_path ="hubert_xtralarge_ll60k.pt"21ssl_url ="https://dl.fbaipublicfiles.com/hubert/hubert_xtralarge_ll60k.pt"2223ifnot os.path.exists(ssl_path):24print(f"Downloading checkpoint to {ssl_path}...")25 urllib.request.urlretrieve(ssl_url, ssl_path)26print("Download complete.")27else:28print(f"{ssl_path} already exists. Skipping download.")2930# === Wrapper for the SSL model ===31classSSLModel(torch.nn.Module):32def__init__(self):33super().__init__()34# The downloaded .pt file is used here35 model, _, _ = fairseq.checkpoint_utils.load_model_ensemble_and_task([ssl_path])36 self.model = model[0].to(device)3738defextract_feat(self, input_data):39# If input has shape (B, T, 1), squeeze the last dim40if input_data.ndim ==3:41 input_data = input_data[:,:,0]42# Extract features43with torch.no_grad():44 features = self.model(input_data.to(device), mask=False, features_only=True)['x']45return features
4647# === Function for reading and pre-processing waveforms ===48defload_wav_and_preprocess(wav_path, target_sr=16000):49# Load audio file50 wav, sr = torchaudio.load(wav_path)51# Convert to mono if stereo52 wav = wav.mean(dim=0)53# Resample to target sampling rate54 wav = torchaudio.functional.resample(wav, sr, new_freq=target_sr)55# Normalize waveform56with torch.no_grad():57 wav = torch.nn.functional.layer_norm(wav, wav.shape)58# Add batch dimension and return59return wav.unsqueeze(0).to(device)6061# === The actual deepfake detection model using SSL frontend + FC backend ===62classDeepfakeDetector(torch.nn.Module, PyTorchModelHubMixin):63def__init__(self):64super().__init__()65 self.ssl_orig_output_dim =128066 self.num_classes =267# Frontend: SSL model68 self.m_ssl = SSLModel()69# Backend: Pooling + Classification70 self.adap_pool1d = torch.nn.AdaptiveAvgPool1d(output_size=1)71 self.proj_fc = torch.nn.Linear(72 in_features=self.ssl_orig_output_dim,73 out_features=self.num_classes,74)7576defforward(self, wav):77 emb = self.m_ssl.extract_feat(wav)# [B, T, D]78 emb = emb.transpose(1,2)# [B, D, T]79 pooled_emb = self.adap_pool1d(emb)# [B, D, 1]80 pooled_emb = pooled_emb.squeeze(-1)# [B, D]81 logits = self.proj_fc(pooled_emb)# [B, 2]82return logits
8384# === Load AntiDeepfake model from Hugging Face===85model = DeepfakeDetector.from_pretrained("nii-yamagishilab/hubert-xlarge-anti-deepfake")86model.to(device)87model.eval()8889# === Inference on a folder of audio files ===90results =[]91for root, _, files in os.walk(folder_path):92forfilein files:93iffile.lower().endswith(audio_formats):94 input_path = os.path.join(root,file)95with torch.no_grad():96 wav = load_wav_and_preprocess(input_path)97 logits = model(wav)98 probs = torch.nn.functional.softmax(logits, dim=1)99 results.append((file, probs.cpu().numpy()[0]))100101# Sort results alphabetically by filename102results.sort(key=lambda x: x[0])103104# Print formatted results105print("\n=== Deepfake Detection Results ===")106for file_name, prob in results:107print(f"{file_name}: real prob = {prob[1]:.3f}, fake prob = {prob[0]:.3f}")
📊 Performance Metrics
Results shown below can be reproduced using scripts provided in our GitHub repository.
Test Database
ROC AUC
Accuracy
Precision
Recall
F1-score
FPR
FNR
EER (%) @ Threshold
ADD2023
0.918
0.802
0.926
0.796
0.856
0.180
0.204
18.91 @ 0.4211
DeepVoice
0.988
0.808
0.389
0.985
0.558
0.216
0.015
5.68 @ 0.9906
FakeOrReal
0.996
0.968
0.984
0.950
0.967
0.014
0.050
2.48 @ 0.2221
FakeOrReal-norm
0.995
0.930
0.878
0.996
0.933
0.132
0.004
3.18 @ 0.9578
In-the-Wild
0.992
0.898
0.994
0.843
0.912
0.009
0.157
5.23 @ 0.0098
Deepfake-Eval-2024
0.723
0.705
0.718
0.906
0.801
0.674
0.094
34.10 @ 0.9986
You can also fine-tune this model on a specific database, the corresponding code is provided in our GitHub repository. Fine-tuning will follow a similar process to training a new model, except that model weights will be initialized as AntiDeepfake checkpoints.
Below are the evaluation results of this model fine-tuned on the Deepfake-Eval-2024 training set and tested on its corresponding test set (as shown in the previous table):
Test Input Length
ROC AUC
Accuracy
Precision
Recall
F1-score
FPR
FNR
EER (%) @ Threshold
4s
0.5936
0.6541
0.6545
0.9980
0.7905
0.9959
0.0020
42.81 @ 0.5322
10s
0.6071
0.6534
0.6531
0.9989
0.7899
0.9944
0.0011
42.18 @ 0.5292
13s
0.6079
0.6524
0.6526
0.9976
0.7890
0.9934
0.0024
42.20 @ 0.5285
30s
0.6300
0.6553
0.6529
0.9971
0.7891
0.9702
0.0029
40.38 @ 0.5253
50s
0.6408
0.6409
0.6393
0.9991
0.7797
0.9846
0.0009
40.86 @ 0.5285
Training Set
Below is a breakdown of the training set used for post-training of speech SSL models.
📚 Database
🌍 Language
✅ Genuine (hrs)
❌ Fake (hrs)
AISHELL3
zh
85.62
0
ASVspoof2019-LA
en
11.85
97.80
ASVspoof2021-LA
en
16.40
116.10
ASVspoof2021-DF
en
20.73
487.00
ASVspoof5
en
413.49
1808.48
CFAD
zh
171.25
224.55
CNCeleb2
zh
1084.34
0
Codecfake
en, zh
129.66
808.32
CodecFake
en
0
660.92
CVoiceFake
en, fr, de, it, zh
315.14
1561.16
DECRO
en, zh
35.18
102.44
DFADD
en
41.62
66.01
Diffuse or Confuse
en
0
231.66
DiffSSD
en
0
139.73
DSD
en, ja, ko
100.98
60.23
FLEURS
102 languages
1388.97
0
FLEURS-R
102 languages
0
1238.83
HABLA
es
35.56
87.83
LibriTTS
en
585.83
0
LibriTTS-R
en
0
583.15
LibriTTS-Vocoded
en
0
2345.14
LJSpeech
en
23.92
0
MLAAD
38 languages
0
377.96
MLS
8 languages
50558.11
0
SpoofCeleb
Multilingual
173.00
1916.20
VoiceMOS
en
0
448.44
VoxCeleb2
Multilingual
1179.62
0
VoxCeleb2-Vocoded
Multilingual
0
4721.46
WaveFake
en, ja
0
198.65
Train Set
Over 100 languages
56370.00
18280.00
Attribution
All AntiDeepfake models were developed by Yamagishi Lab at the National Institute of Informatics (NII), Japan.
All model weights are the intellectual property of NII and are made available for research and educational purposes under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) license.
Acknowledgments
This project is based on results obtained from project JPNP22007, commissioned by the New Energy and Industrial Technology Development Organization (NEDO).
It is also partially supported by the following grants from the Japan Science and Technology Agency (JST):
AIP Acceleration Research (Grant No. JPMJCR24U3)
PRESTO (Grant No. JPMJPR23P9)
This study was carried out using the TSUBAME4.0 supercomputer at Institute of Science Tokyo.