shuohann commited on
Commit
e4dd4b2
·
verified ·
1 Parent(s): e1e20a1

trim redundant training/doc files from deepfake-whisper-features

Browse files
deepfake-whisper-features/README DELETED
@@ -1,6 +0,0 @@
1
- 1. Prepare metadata.csv file in the same way we prepare one for df_arena toolkit
2
- 2. Go inside src/datasets/generic_deep_faket_dataset.py
3
- 3. Change filepath in line 31 to path of desired metadata file.
4
- self.samples=pd.read_csv('/data/code/df_arena_stuff/df_arena_metadata/asvspoof_2024_eval.csv', names=['file', 'label' ], header=0)
5
- 4. Change name of output score file inside evaluate_models_2019.py on line 162 and 165 to desired name
6
- 5. Do inference using python evaluate_models_2019.py --config /mesonet_whisper_mfcc_finetuned/config.yaml
 
 
 
 
 
 
 
deepfake-whisper-features/configs/finetuning/whisper_frontend_mesonet.yaml DELETED
@@ -1,16 +0,0 @@
1
- data:
2
- seed: 42
3
-
4
- checkpoint:
5
- path: "trained_models/whisper_frontend_mesonet/ckpt.pth"
6
-
7
- model:
8
- name: "whisper_frontend_mesonet"
9
- parameters:
10
- freeze_encoder: false
11
- input_channels: 2
12
- fc1_dim: 1024
13
- frontend_algorithm: ["lfcc"]
14
- optimizer:
15
- lr: 1.0e-06
16
- weight_decay: 0.0001
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
deepfake-whisper-features/configs/training/lcnn.yaml DELETED
@@ -1,14 +0,0 @@
1
- data:
2
- seed: 42
3
-
4
- checkpoint:
5
- path: ""
6
-
7
- model:
8
- name: "lcnn"
9
- parameters:
10
- input_channels: 1
11
- frontend_algorithm: ["mfcc"]
12
- optimizer:
13
- lr: 0.0001
14
- weight_decay: 0.0001
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
deepfake-whisper-features/configs/training/mesonet.yaml DELETED
@@ -1,15 +0,0 @@
1
- data:
2
- seed: 42
3
-
4
- checkpoint:
5
- path: ""
6
-
7
- model:
8
- name: "mesonet"
9
- parameters:
10
- input_channels: 1
11
- fc1_dim: 1024
12
- frontend_algorithm: ["lfcc"]
13
- optimizer:
14
- lr: 0.0001
15
- weight_decay: 0.0001
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
deepfake-whisper-features/configs/training/rawnet3.yaml DELETED
@@ -1,13 +0,0 @@
1
- data:
2
- seed: 42
3
-
4
- checkpoint:
5
- path: ""
6
-
7
- model:
8
- name: "rawnet3"
9
- parameters: {}
10
- optimizer:
11
- lr: 0.001
12
- weight_decay: 0.00005 # 5e-5
13
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
deepfake-whisper-features/configs/training/specrnet.yaml DELETED
@@ -1,14 +0,0 @@
1
- data:
2
- seed: 42
3
-
4
- checkpoint:
5
- path: ""
6
-
7
- model:
8
- name: "specrnet"
9
- parameters:
10
- input_channels: 1
11
- frontend_algorithm: ["lfcc"]
12
- optimizer:
13
- lr: 0.0001
14
- weight_decay: 0.0001
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
deepfake-whisper-features/configs/training/whisper_frontend_lcnn.yaml DELETED
@@ -1,16 +0,0 @@
1
- data:
2
- seed: 42
3
-
4
- checkpoint:
5
- path: ""
6
-
7
- model:
8
- name: "whisper_frontend_lcnn"
9
- parameters:
10
- freeze_encoder: True
11
- input_channels: 2
12
- frontend_algorithm: ["lfcc"]
13
- optimizer:
14
- lr: 0.0001
15
- weight_decay: 0.0001
16
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
deepfake-whisper-features/configs/training/whisper_frontend_lcnn_mfcc.yaml DELETED
@@ -1,15 +0,0 @@
1
- data:
2
- seed: 42
3
-
4
- checkpoint:
5
- path: ""
6
-
7
- model:
8
- name: "whisper_frontend_lcnn"
9
- parameters:
10
- freeze_encoder: True
11
- input_channels: 2
12
- frontend_algorithm: ["mfcc"]
13
- optimizer:
14
- lr: 0.0001
15
- weight_decay: 0.0001
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
deepfake-whisper-features/configs/training/whisper_frontend_mesonet.yaml DELETED
@@ -1,16 +0,0 @@
1
- data:
2
- seed: 42
3
-
4
- checkpoint:
5
- path: ""
6
-
7
- model:
8
- name: "whisper_frontend_mesonet"
9
- parameters:
10
- freeze_encoder: True
11
- input_channels: 2
12
- fc1_dim: 1024
13
- frontend_algorithm: ["lfcc"]
14
- optimizer:
15
- lr: 0.0001
16
- weight_decay: 0.0001
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
deepfake-whisper-features/configs/training/whisper_frontend_mesonet_mfcc.yaml DELETED
@@ -1,17 +0,0 @@
1
- data:
2
- seed: 42
3
-
4
- checkpoint:
5
- path: ""
6
-
7
- model:
8
- name: "whisper_frontend_mesonet"
9
- parameters:
10
- freeze_encoder: True
11
- input_channels: 2
12
- fc1_dim: 1024
13
- frontend_algorithm: ["mfcc"]
14
- optimizer:
15
- lr: 0.0001
16
- weight_decay: 0.0001
17
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
deepfake-whisper-features/configs/training/whisper_frontend_specrnet.yaml DELETED
@@ -1,15 +0,0 @@
1
- data:
2
- seed: 42
3
-
4
- checkpoint:
5
- path: ""
6
-
7
- model:
8
- name: "whisper_frontend_specrnet"
9
- parameters:
10
- freeze_encoder: True
11
- input_channels: 2
12
- frontend_algorithm: ["lfcc"]
13
- optimizer:
14
- lr: 0.0001
15
- weight_decay: 0.0001
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
deepfake-whisper-features/configs/training/whisper_frontend_specrnet_mfcc.yaml DELETED
@@ -1,16 +0,0 @@
1
- data:
2
- seed: 42
3
-
4
- checkpoint:
5
- path: ""
6
-
7
- model:
8
- name: "whisper_frontend_specrnet"
9
- parameters:
10
- freeze_encoder: True
11
- input_channels: 2
12
- frontend_algorithm: ["mfcc"]
13
- optimizer:
14
- lr: 0.0001
15
- weight_decay: 0.0001
16
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
deepfake-whisper-features/configs/training/whisper_lcnn.yaml DELETED
@@ -1,15 +0,0 @@
1
- data:
2
- seed: 42
3
-
4
- checkpoint:
5
- path: ""
6
-
7
- model:
8
- name: "whisper_lcnn"
9
- parameters:
10
- freeze_encoder: True
11
- input_channels: 1
12
- frontend_algorithm: ["lfcc"]
13
- optimizer:
14
- lr: 0.0001
15
- weight_decay: 0.0001
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
deepfake-whisper-features/configs/training/whisper_mesonet.yaml DELETED
@@ -1,16 +0,0 @@
1
- data:
2
- seed: 42
3
-
4
- checkpoint:
5
- path: ""
6
-
7
- model:
8
- name: "whisper_mesonet"
9
- parameters:
10
- freeze_encoder: True
11
- input_channels: 1
12
- fc1_dim: 1024
13
- frontend_algorithm: []
14
- optimizer:
15
- lr: 0.0001
16
- weight_decay: 0.0001
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
deepfake-whisper-features/configs/training/whisper_specrnet.yaml DELETED
@@ -1,15 +0,0 @@
1
- data:
2
- seed: 42
3
-
4
- checkpoint:
5
- path: ""
6
-
7
- model:
8
- name: "whisper_specrnet"
9
- parameters:
10
- freeze_encoder: True
11
- input_channels: 1
12
- frontend_algorithm: ["lfcc"]
13
- optimizer:
14
- lr: 0.0001
15
- weight_decay: 0.0001
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
deepfake-whisper-features/download_whisper.py DELETED
@@ -1,29 +0,0 @@
1
- # pip install git+https://github.com/openai/whisper.git
2
- from collections import OrderedDict
3
- import whisper
4
- import torch
5
-
6
- from src.commons import WHISPER_MODEL_WEIGHTS_PATH
7
-
8
- def download_whisper():
9
- model = whisper.load_model("tiny.en")
10
- return model
11
-
12
-
13
- def extract_and_save_encoder(model):
14
- model_ckpt = OrderedDict()
15
-
16
- model_ckpt['model_state_dict'] = OrderedDict()
17
-
18
- for key, value in model.encoder.state_dict().items():
19
- model_ckpt['model_state_dict'][f'encoder.{key}'] = value
20
-
21
- model_ckpt['dims'] = model.dims
22
- torch.save(model_ckpt, WHISPER_MODEL_WEIGHTS_PATH)
23
-
24
-
25
- if __name__ == "__main__":
26
- model = download_whisper()
27
- print("Downloaded Whisper model!")
28
- extract_and_save_encoder(model)
29
- print(f"Saved encoder at '{WHISPER_MODEL_WEIGHTS_PATH}'")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
deepfake-whisper-features/evaluate_models_2019.py DELETED
@@ -1,232 +0,0 @@
1
- import argparse
2
- import logging
3
- from pathlib import Path
4
- from typing import Dict, List, Optional, Union
5
- import sys
6
-
7
- import torch
8
- import torchaudio
9
- import yaml
10
- from sklearn.metrics import precision_recall_fscore_support, roc_auc_score
11
- from torch.utils.data import DataLoader, Dataset
12
-
13
- from src import metrics, commons
14
- from src.models import models
15
- from src.datasets.base_dataset import SimpleAudioFakeDataset
16
- from src.datasets.generic_deep_fake_dataset import GenericDeepFakeDataset
17
-
18
-
19
- def get_dataset(
20
- datasets_paths: List[Union[Path, str]],
21
- amount_to_use: Optional[int],
22
- ) -> SimpleAudioFakeDataset:
23
- data_val = GenericDeepFakeDataset(
24
- subset="test",
25
- path=datasets_paths[0],
26
- )
27
- # data_val = ASVspoof2019Dataset(
28
- # subset="foo",
29
- # path=datasets_paths[0],
30
- # )
31
- return data_val
32
-
33
- def genSpoof_list(dir_meta, is_train=False, is_eval=False):
34
- d_meta = {}
35
- file_list = []
36
-
37
- with open(dir_meta, "r") as f:
38
- for line in f:
39
- if is_eval:
40
- key = line.strip()
41
- file_list.append(key)
42
- else:
43
- _, key, _, _, label = line.strip().split()
44
- file_list.append(key)
45
- d_meta[key] = 1 if label == "bonafide" else 0
46
-
47
- return (d_meta, file_list) if not is_eval else file_list
48
-
49
- class Dataset_ASVspoof2019_eval(Dataset):
50
- def __init__(self, list_IDs, base_dir):
51
- self.list_IDs = list_IDs
52
- self.base_dir = base_dir
53
-
54
- def __len__(self):
55
- return len(self.list_IDs)
56
-
57
- def __getitem__(self, index):
58
- utt_id = self.list_IDs[index]
59
- X, _ = torchaudio.load(self.base_dir + "/" + utt_id + ".flac")
60
- return X, utt_id
61
-
62
- def collate_fn_eval(batch):
63
- x, utt_id = zip(*batch)
64
- x = torch.nn.utils.rnn.pad_sequence(
65
- [tensor.squeeze() for tensor in x], batch_first=True, padding_value=0.0
66
- )
67
- return x, utt_id
68
-
69
- def evaluate_nn(
70
- model_paths: List[Path],
71
- datasets_paths: List[Union[Path, str]],
72
- model_config: Dict,
73
- device: str,
74
- amount_to_use: Optional[int] = None,
75
- batch_size: int = 32,
76
- ):
77
- logging.info("Loading data...")
78
- model_name, model_parameters = model_config["name"], model_config["parameters"]
79
-
80
- # Load model architecture
81
- model = models.get_model(
82
- model_name=model_name,
83
- config=model_parameters,
84
- device=device,
85
- )
86
- # If provided weights, apply corresponding ones (from an appropriate fold)
87
- if len(model_paths):
88
- model.load_state_dict(torch.load(model_paths))
89
- model = model.to(device)
90
-
91
- data_val = get_dataset(
92
- datasets_paths=datasets_paths,
93
- amount_to_use=amount_to_use,
94
- )
95
-
96
- logging.info(
97
- f"Testing '{model_name}' model, weights path: '{model_paths}', on {len(data_val)} audio files."
98
- )
99
- test_loader = DataLoader(
100
- data_val,
101
- batch_size=batch_size,
102
- shuffle=False,
103
- drop_last=False,
104
- num_workers=3,
105
- )
106
-
107
- # d_label_eval, file_eval = genSpoof_list(
108
- # dir_meta="/home/tran/dev/dataset/LA/ASVspoof2019_LA_cm_protocols/ASVspoof2019.LA.cm.eval.trl.txt",
109
- # is_eval=False, # TODO problem in implementing
110
- # )
111
- # eval = Dataset_ASVspoof2019_eval(
112
- # list_IDs=file_eval,
113
- # base_dir="/home/tran/dev/dataset/LA/ASVspoof2019_LA_eval/flac",
114
- # )
115
-
116
- # test_loader = DataLoader(
117
- # dataset=eval,
118
- # batch_size=batch_size,
119
- # shuffle=False,
120
- # collate_fn=collate_fn_eval,
121
- # )
122
-
123
- # logging.info(
124
- # f"Testing '{model_name}' model, weights path: '{model_paths}', on {len(data_val)} audio files."
125
- # )
126
- # test_loader = DataLoader(
127
- # data_val,
128
- # batch_size=batch_size,
129
- # shuffle=True,
130
- # drop_last=True,
131
- # num_workers=3,
132
- # )
133
-
134
- batches_number = len(data_val) // batch_size
135
- num_correct = 0.0
136
- num_total = 0.0
137
-
138
- y_pred = torch.Tensor([]).to(device)
139
- y = torch.Tensor([]).to(device)
140
- y_pred_label = torch.Tensor([]).to(device)
141
- fname_list = []
142
- score_list = []
143
-
144
- for i, (batch_x, sr, sample_name, batch_y) in enumerate(test_loader):
145
-
146
- model.eval()
147
- if i % 10 == 0:
148
- print(f"Batch [{i}/{batches_number}]")
149
-
150
- with torch.no_grad():
151
- batch_x = batch_x.to(device)
152
- batch_y = batch_y.to(device)
153
- num_total += batch_x.size(0)
154
-
155
- batch_pred = model(batch_x).squeeze(1)
156
- batch_score = batch_pred.data.cpu().numpy().ravel()
157
- batch_pred = torch.sigmoid(batch_pred)
158
- batch_pred_label = (batch_pred + 0.5).int()
159
- fname_list.extend(sample_name)
160
- score_list.extend(batch_score.tolist())
161
-
162
- with open("./in_the_wild.txt", 'w') as fh:
163
- for f, cm in zip(fname_list, score_list):
164
- fh.write('{} {}\n'.format(f, cm))
165
- print('Result saved to {}'.format("./in_the_wild.txt"))
166
-
167
- def main(args):
168
- LOGGER = logging.getLogger()
169
- LOGGER.setLevel(logging.INFO)
170
-
171
- ch = logging.StreamHandler()
172
- formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
173
- ch.setFormatter(formatter)
174
- LOGGER.addHandler(ch)
175
- logging.basicConfig(stream=sys.stdout, level=logging.INFO)
176
-
177
- if not args.cpu and torch.cuda.is_available():
178
- device = "cuda"
179
- else:
180
- device = "cpu"
181
-
182
- with open(args.config, "r") as f:
183
- config = yaml.safe_load(f)
184
-
185
- seed = config["data"].get("seed", 42)
186
- # fix all seeds - this should not actually change anything
187
- commons.set_seed(seed)
188
- evaluate_nn(
189
- model_paths=config["checkpoint"].get("path", []),
190
- datasets_paths=[
191
- "/data/code/SSL_antispoof_work/asvspoof5/flac_D/",
192
- ],
193
- model_config=config["model"],
194
- amount_to_use=args.amount,
195
- device=device,
196
- )
197
-
198
-
199
- def parse_args():
200
- parser = argparse.ArgumentParser()
201
-
202
- # If assigned as None, then it won't be taken into account
203
- IN_THE_WILD_DATASET_PATH = "/data/code/SSL_antispoof_work/asvspoof5/flac_D/"
204
-
205
- parser.add_argument(
206
- "--in_the_wild_path", type=str, default=IN_THE_WILD_DATASET_PATH
207
- )
208
-
209
- default_model_config = "config.yaml"
210
- parser.add_argument(
211
- "--config",
212
- help="Model config file path (default: config.yaml)",
213
- type=str,
214
- default=default_model_config,
215
- )
216
-
217
- default_amount = None
218
- parser.add_argument(
219
- "--amount",
220
- "-a",
221
- help=f"Amount of files to load from each directory (default: {default_amount} - use all).",
222
- type=int,
223
- default=default_amount,
224
- )
225
-
226
- parser.add_argument("--cpu", "-c", help="Force using cpu", action="store_true")
227
-
228
- return parser.parse_args()
229
-
230
-
231
- if __name__ == "__main__":
232
- main(parse_args())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
deepfake-whisper-features/install.sh DELETED
@@ -1,6 +0,0 @@
1
- conda install pytorch==1.11.0 torchvision==0.12.0 torchaudio==0.11.0 cudatoolkit=11.3 -c pytorch -y
2
-
3
- pip install asteroid-filterbanks==0.4.0
4
- pip install librosa==0.9.2
5
- pip install git+https://github.com/openai/whisper.git@7858aa9c08d98f75575035ecd6481f462d66ca27
6
- pip install pandas==2.0.2
 
 
 
 
 
 
 
deepfake-whisper-features/train_and_test.py DELETED
@@ -1,147 +0,0 @@
1
- import argparse
2
- import logging
3
- from pathlib import Path
4
-
5
- import torch
6
- import yaml
7
-
8
- import train_models
9
- import evaluate_models
10
- from src.commons import set_seed
11
-
12
-
13
- LOGGER = logging.getLogger()
14
- LOGGER.setLevel(logging.INFO)
15
-
16
- ch = logging.StreamHandler()
17
- formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
18
- ch.setFormatter(formatter)
19
- LOGGER.addHandler(ch)
20
-
21
-
22
- def parse_args():
23
- parser = argparse.ArgumentParser()
24
-
25
- ASVSPOOF_DATASET_PATH = "../datasets/ASVspoof2021/DF"
26
- IN_THE_WILD_DATASET_PATH = "../datasets/release_in_the_wild"
27
-
28
- parser.add_argument(
29
- "--asv_path",
30
- type=str,
31
- default=ASVSPOOF_DATASET_PATH,
32
- help="Path to ASVspoof2021 dataset directory",
33
- )
34
- parser.add_argument(
35
- "--in_the_wild_path",
36
- type=str,
37
- default=IN_THE_WILD_DATASET_PATH,
38
- help="Path to In The Wild dataset directory",
39
- )
40
- default_model_config = "config.yaml"
41
- parser.add_argument(
42
- "--config",
43
- help="Model config file path (default: config.yaml)",
44
- type=str,
45
- default=default_model_config,
46
- )
47
-
48
- default_train_amount = None
49
- parser.add_argument(
50
- "--train_amount",
51
- "-a",
52
- help=f"Amount of files to load for training.",
53
- type=int,
54
- default=default_train_amount,
55
- )
56
-
57
- default_valid_amount = None
58
- parser.add_argument(
59
- "--valid_amount",
60
- "-va",
61
- help=f"Amount of files to load for testing.",
62
- type=int,
63
- default=default_valid_amount,
64
- )
65
-
66
- default_test_amount = None
67
- parser.add_argument(
68
- "--test_amount",
69
- "-ta",
70
- help=f"Amount of files to load for testing.",
71
- type=int,
72
- default=default_test_amount,
73
- )
74
-
75
- default_batch_size = 8
76
- parser.add_argument(
77
- "--batch_size",
78
- "-b",
79
- help=f"Batch size (default: {default_batch_size}).",
80
- type=int,
81
- default=default_batch_size,
82
- )
83
-
84
- default_epochs = 10 # it was 5 originally
85
- parser.add_argument(
86
- "--epochs",
87
- "-e",
88
- help=f"Epochs (default: {default_epochs}).",
89
- type=int,
90
- default=default_epochs,
91
- )
92
-
93
- default_model_dir = "trained_models"
94
- parser.add_argument(
95
- "--ckpt",
96
- help=f"Checkpoint directory (default: {default_model_dir}).",
97
- type=str,
98
- default=default_model_dir,
99
- )
100
-
101
- parser.add_argument("--cpu", "-c", help="Force using cpu?", action="store_true")
102
-
103
- return parser.parse_args()
104
-
105
-
106
- if __name__ == "__main__":
107
- args = parse_args()
108
- # TRAIN MODEL
109
-
110
- with open(args.config, "r") as f:
111
- config = yaml.safe_load(f)
112
-
113
- seed = config["data"].get("seed", 42)
114
- # fix all seeds
115
- set_seed(seed)
116
-
117
- if not args.cpu and torch.cuda.is_available():
118
- device = "cuda"
119
- else:
120
- device = "cpu"
121
-
122
- model_dir = Path(args.ckpt)
123
- model_dir.mkdir(parents=True, exist_ok=True)
124
-
125
- # evaluation_config_path, model_path = train_models.train_nn(
126
- # datasets_paths=[
127
- # args.asv_path,
128
- # ],
129
- # device=device,
130
- # amount_to_use=(args.train_amount, args.valid_amount),
131
- # batch_size=args.batch_size,
132
- # epochs=args.epochs,
133
- # model_dir=model_dir,
134
- # config=config,
135
- # )
136
-
137
- with open(evaluation_config_path, "r") as f:
138
- config = yaml.safe_load(f)
139
-
140
- evaluate_models.evaluate_nn(
141
- model_paths=config["checkpoint"].get("path", []),
142
- batch_size=args.batch_size,
143
- datasets_paths=[args.in_the_wild_path],
144
- model_config=config["model"],
145
- amount_to_use=args.test_amount,
146
- device=device,
147
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
deepfake-whisper-features/train_models.py DELETED
@@ -1,235 +0,0 @@
1
- import argparse
2
- import logging
3
- import sys
4
- import time
5
- from pathlib import Path
6
- from typing import Dict, List, Optional, Tuple, Union
7
-
8
- import torch
9
- import yaml
10
-
11
- from src.datasets.detection_dataset import DetectionDataset
12
- from src.models import models
13
- from src.trainer import GDTrainer
14
- from src.commons import set_seed
15
-
16
-
17
- def save_model(
18
- model: torch.nn.Module,
19
- model_dir: Union[Path, str],
20
- name: str,
21
- ) -> None:
22
- full_model_dir = Path(f"{model_dir}/{name}")
23
- full_model_dir.mkdir(parents=True, exist_ok=True)
24
- torch.save(model.state_dict(), f"{full_model_dir}/ckpt.pth")
25
-
26
-
27
- def get_datasets(
28
- datasets_paths: List[Union[Path, str]],
29
- amount_to_use: Tuple[Optional[int], Optional[int]],
30
- ) -> Tuple[DetectionDataset, DetectionDataset]:
31
- data_train = DetectionDataset(
32
- asvspoof_path=datasets_paths[0],
33
- subset="train",
34
- reduced_number=amount_to_use[0],
35
- oversample=True,
36
- )
37
- data_test = DetectionDataset(
38
- asvspoof_path=datasets_paths[0],
39
- subset="test",
40
- reduced_number=amount_to_use[1],
41
- oversample=True,
42
- )
43
-
44
- return data_train, data_test
45
-
46
-
47
- def train_nn(
48
- datasets_paths: List[Union[Path, str]],
49
- batch_size: int,
50
- epochs: int,
51
- device: str,
52
- config: Dict,
53
- model_dir: Optional[Path] = None,
54
- amount_to_use: Tuple[Optional[int], Optional[int]] = (None, None),
55
- config_save_path: str = "configs",
56
- ) -> Tuple[str, str]:
57
- logging.info("Loading data...")
58
- model_config = config["model"]
59
- model_name, model_parameters = model_config["name"], model_config["parameters"]
60
- optimizer_config = model_config["optimizer"]
61
-
62
- timestamp = time.time()
63
- checkpoint_path = ""
64
-
65
- data_train, data_test = get_datasets(
66
- datasets_paths=datasets_paths,
67
- amount_to_use=amount_to_use,
68
- )
69
-
70
- current_model = models.get_model(
71
- model_name=model_name,
72
- config=model_parameters,
73
- device=device,
74
- )
75
-
76
- # If provided weights, apply corresponding ones (from an appropriate fold)
77
- model_path = config["checkpoint"]["path"]
78
- if model_path:
79
- current_model.load_state_dict(torch.load(model_path))
80
- logging.info(
81
- f"Finetuning '{model_name}' model, weights path: '{model_path}', on {len(data_train)} audio files."
82
- )
83
- if config["model"]["parameters"].get("freeze_encoder"):
84
- for param in current_model.whisper_model.parameters():
85
- param.requires_grad = False
86
- else:
87
- logging.info(f"Training '{model_name}' model on {len(data_train)} audio files.")
88
- current_model = current_model.to(device)
89
-
90
- use_scheduler = "rawnet3" in model_name.lower()
91
-
92
- current_model = GDTrainer(
93
- device=device,
94
- batch_size=batch_size,
95
- epochs=epochs,
96
- optimizer_kwargs=optimizer_config,
97
- use_scheduler=use_scheduler,
98
- ).train(
99
- dataset=data_train,
100
- model=current_model,
101
- test_dataset=data_test,
102
- )
103
-
104
- if model_dir is not None:
105
- save_name = f"model__{model_name}__{timestamp}"
106
- save_model(
107
- model=current_model,
108
- model_dir=model_dir,
109
- name=save_name,
110
- )
111
- checkpoint_path = str(model_dir.resolve() / save_name / "ckpt.pth")
112
-
113
- # Save config for testing
114
- if model_dir is not None:
115
- config["checkpoint"] = {"path": checkpoint_path}
116
- config_name = f"model__{model_name}__{timestamp}.yaml"
117
- config_save_path = str(Path(config_save_path) / config_name)
118
- with open(config_save_path, "w") as f:
119
- yaml.dump(config, f)
120
- logging.info("Test config saved at location '{}'!".format(config_save_path))
121
- return config_save_path, checkpoint_path
122
-
123
-
124
- def main(args):
125
- LOGGER = logging.getLogger()
126
- LOGGER.setLevel(logging.INFO)
127
-
128
- ch = logging.StreamHandler()
129
- formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
130
- ch.setFormatter(formatter)
131
- LOGGER.addHandler(ch)
132
- logging.basicConfig(stream=sys.stdout, level=logging.INFO)
133
-
134
- with open(args.config, "r") as f:
135
- config = yaml.safe_load(f)
136
-
137
- seed = config["data"].get("seed", 42)
138
- # fix all seeds
139
- set_seed(seed)
140
-
141
- if not args.cpu and torch.cuda.is_available():
142
- device = "cuda"
143
- else:
144
- device = "cpu"
145
-
146
- model_dir = Path(args.ckpt)
147
- model_dir.mkdir(parents=True, exist_ok=True)
148
-
149
- train_nn(
150
- datasets_paths=[
151
- args.asv_path,
152
- args.wavefake_path,
153
- args.celeb_path,
154
- args.asv19_path,
155
- ],
156
- device=device,
157
- amount_to_use=(args.train_amount, args.test_amount),
158
- batch_size=args.batch_size,
159
- epochs=args.epochs,
160
- model_dir=model_dir,
161
- config=config,
162
- )
163
-
164
-
165
- def parse_args():
166
- parser = argparse.ArgumentParser()
167
-
168
- ASVSPOOF_DATASET_PATH = "../datasets/ASVspoof2021/DF"
169
-
170
- parser.add_argument(
171
- "--asv_path",
172
- type=str,
173
- default=ASVSPOOF_DATASET_PATH,
174
- help="Path to ASVspoof2021 dataset directory",
175
- )
176
-
177
- default_model_config = "config.yaml"
178
- parser.add_argument(
179
- "--config",
180
- help="Model config file path (default: config.yaml)",
181
- type=str,
182
- default=default_model_config,
183
- )
184
-
185
- default_train_amount = None
186
- parser.add_argument(
187
- "--train_amount",
188
- "-a",
189
- help=f"Amount of files to load for training.",
190
- type=int,
191
- default=default_train_amount,
192
- )
193
-
194
- default_test_amount = None
195
- parser.add_argument(
196
- "--test_amount",
197
- "-ta",
198
- help=f"Amount of files to load for testing.",
199
- type=int,
200
- default=default_test_amount,
201
- )
202
-
203
- default_batch_size = 8
204
- parser.add_argument(
205
- "--batch_size",
206
- "-b",
207
- help=f"Batch size (default: {default_batch_size}).",
208
- type=int,
209
- default=default_batch_size,
210
- )
211
-
212
- default_epochs = 10
213
- parser.add_argument(
214
- "--epochs",
215
- "-e",
216
- help=f"Epochs (default: {default_epochs}).",
217
- type=int,
218
- default=default_epochs,
219
- )
220
-
221
- default_model_dir = "trained_models"
222
- parser.add_argument(
223
- "--ckpt",
224
- help=f"Checkpoint directory (default: {default_model_dir}).",
225
- type=str,
226
- default=default_model_dir,
227
- )
228
-
229
- parser.add_argument("--cpu", "-c", help="Force using cpu?", action="store_true")
230
-
231
- return parser.parse_args()
232
-
233
-
234
- if __name__ == "__main__":
235
- main(parse_args())