Update README.md
Browse files
README.md
CHANGED
|
@@ -62,16 +62,8 @@ conda create --name antideepfake python==3.9.0
|
|
| 62 |
conda activate antideepfake
|
| 63 |
conda install pip==24.0
|
| 64 |
|
| 65 |
-
### Install
|
| 66 |
-
|
| 67 |
-
git clone https://github.com/pytorch/fairseq
|
| 68 |
-
cd fairseq
|
| 69 |
-
# checkout this specific commit. Latest commit does not work
|
| 70 |
-
git checkout 862efab86f649c04ea31545ce28d13c59560113d
|
| 71 |
-
pip install --editable .
|
| 72 |
-
|
| 73 |
-
### Install other packages ###
|
| 74 |
-
pip install huggingface-hub==0.31.1 safetensors==0.5.3 soundfile==0.13.1 numpy==1.21.2
|
| 75 |
```
|
| 76 |
|
| 77 |
🚀 Inference:
|
|
@@ -80,7 +72,7 @@ import os
|
|
| 80 |
import urllib.request
|
| 81 |
import torch
|
| 82 |
import torchaudio
|
| 83 |
-
|
| 84 |
from huggingface_hub import PyTorchModelHubMixin
|
| 85 |
|
| 86 |
# This is the only part of the script you need to modify.
|
|
@@ -92,26 +84,31 @@ audio_formats = (".mp3", ".wav", ".flac", ".m4a")
|
|
| 92 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 93 |
print(f"Using device: {device}")
|
| 94 |
|
| 95 |
-
# === Download Fairseq checkpoint if not present ===
|
| 96 |
-
# The downloaded checkpoint is used for building front-end architecture,
|
| 97 |
-
# its weights will be replaced by the model.safetensors file in this repo.
|
| 98 |
-
ssl_path = "xlsr2_2B_1000k.pt"
|
| 99 |
-
ssl_url = "https://dl.fbaipublicfiles.com/fairseq/wav2vec/xlsr2_2B_1000k.pt"
|
| 100 |
-
|
| 101 |
-
if not os.path.exists(ssl_path):
|
| 102 |
-
print(f"Downloading checkpoint to {ssl_path}...")
|
| 103 |
-
urllib.request.urlretrieve(ssl_url, ssl_path)
|
| 104 |
-
print("Download complete.")
|
| 105 |
-
else:
|
| 106 |
-
print(f"{ssl_path} already exists. Skipping download.")
|
| 107 |
-
|
| 108 |
# === Wrapper for the SSL model ===
|
| 109 |
class SSLModel(torch.nn.Module):
|
| 110 |
def __init__(self):
|
| 111 |
super().__init__()
|
| 112 |
-
#
|
| 113 |
-
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
def extract_feat(self, input_data):
|
| 117 |
# If input has shape (B, T, 1), squeeze the last dim
|
|
|
|
| 62 |
conda activate antideepfake
|
| 63 |
conda install pip==24.0
|
| 64 |
|
| 65 |
+
### Install packages ###
|
| 66 |
+
pip install huggingface-hub==0.31.1 fairseq==0.12.2 safetensors==0.5.3 soundfile==0.13.1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
```
|
| 68 |
|
| 69 |
🚀 Inference:
|
|
|
|
| 72 |
import urllib.request
|
| 73 |
import torch
|
| 74 |
import torchaudio
|
| 75 |
+
from fairseq.models.wav2vec import Wav2Vec2Model, Wav2Vec2Config
|
| 76 |
from huggingface_hub import PyTorchModelHubMixin
|
| 77 |
|
| 78 |
# This is the only part of the script you need to modify.
|
|
|
|
| 84 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 85 |
print(f"Using device: {device}")
|
| 86 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
# === Wrapper for the SSL model ===
|
| 88 |
class SSLModel(torch.nn.Module):
|
| 89 |
def __init__(self):
|
| 90 |
super().__init__()
|
| 91 |
+
# Model config used to build SSL architecture
|
| 92 |
+
cfg = Wav2Vec2Config(
|
| 93 |
+
quantize_targets=True,
|
| 94 |
+
extractor_mode="layer_norm",
|
| 95 |
+
layer_norm_first=True,
|
| 96 |
+
final_dim=1024,
|
| 97 |
+
latent_temp=(2.0, 0.1, 0.999995),
|
| 98 |
+
encoder_layerdrop=0.0,
|
| 99 |
+
dropout_input=0.0,
|
| 100 |
+
dropout_features=0.0,
|
| 101 |
+
dropout=0.0,
|
| 102 |
+
attention_dropout=0.0,
|
| 103 |
+
conv_bias=True,
|
| 104 |
+
encoder_layers=48,
|
| 105 |
+
encoder_embed_dim=1920,
|
| 106 |
+
encoder_ffn_embed_dim=7680,
|
| 107 |
+
encoder_attention_heads=16,
|
| 108 |
+
feature_grad_mult=1.0,
|
| 109 |
+
)
|
| 110 |
+
# Initialize SSL model with random weights
|
| 111 |
+
self.model = Wav2Vec2Model(cfg)
|
| 112 |
|
| 113 |
def extract_feat(self, input_data):
|
| 114 |
# If input has shape (B, T, 1), squeeze the last dim
|