-
Notifications
You must be signed in to change notification settings - Fork 1
/
data2vec_finetune.py
407 lines (348 loc) · 14.5 KB
/
data2vec_finetune.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "3,4,5"
from dataclasses import dataclass
from glob import glob
from os import listdir, makedirs
from shutil import rmtree
from os.path import isfile, join
from typing import Dict, List, Optional, Tuple, Union
from sklearn.preprocessing import LabelEncoder
import sys
import librosa
import numpy as np
import pandas as pd
import datetime
import socket
import soundfile as sf
import torch
import torch.nn as nn
import torchaudio
import yaml
import evaluate
import torch.nn.functional as F
from datasets import Audio, Dataset, concatenate_datasets, load_dataset
from sklearn.metrics import accuracy_score, confusion_matrix, recall_score
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss
from transformers import (
AutoConfig,
AutoFeatureExtractor,
AutoModelForAudioClassification,
AutoTokenizer,
Trainer,
TrainingArguments,
# Wav2Vec2FeatureExtractor,
Wav2Vec2Processor,
pipeline,
)
from transformers.file_utils import ModelOutput
from transformers.models.data2vec.modeling_data2vec_audio import (
Data2VecAudioModel,
Data2VecAudioPreTrainedModel,
)
@dataclass
class DataCollatorCTCWithPadding:
"""
Data collator that will dynamically pad the inputs received.
Args:
processor (:class:`~transformers.Wav2Vec2Processor`)
The processor used for proccessing the data.
padding (:obj:`bool`, :obj:`str` or :class:`~transformers.tokenization_utils_base.PaddingStrategy`, `optional`, defaults to :obj:`True`):
Select a strategy to pad the returned sequences (according to the model's padding side and padding index)
among:
* :obj:`True` or :obj:`'longest'`: Pad to the longest sequence in the batch (or no padding if only a single
sequence if provided).
* :obj:`'max_length'`: Pad to a maximum length specified with the argument :obj:`max_length` or to the
maximum acceptable input length for the model if that argument is not provided.
* :obj:`False` or :obj:`'do_not_pad'` (default): No padding (i.e., can output a batch with sequences of
different lengths).
max_length (:obj:`int`, `optional`):
Maximum length of the ``input_values`` of the returned list and optionally padding length (see above).
max_length_labels (:obj:`int`, `optional`):
Maximum length of the ``labels`` returned list and optionally padding length (see above).
pad_to_multiple_of (:obj:`int`, `optional`):
If set will pad the sequence to a multiple of the provided value.
This is especially useful to enable the use of Tensor Cores on NVIDIA hardware with compute capability >=
7.5 (Volta).
"""
processor: Wav2Vec2Processor
padding: Union[bool, str] = True
max_length: Optional[int] = None
max_length_labels: Optional[int] = None
pad_to_multiple_of: Optional[int] = None
pad_to_multiple_of_labels: Optional[int] = None
def __call__(
self, features: List[Dict[str, Union[List[int], torch.Tensor]]]
) -> Dict[str, torch.Tensor]:
input_features = [
{"input_values": feature["input_values"]} for feature in features
]
# d_type = torch.long if isinstance(label_features[0], int) else torch.float
d_type = torch.long
batch = self.processor.pad(
input_features,
padding=self.padding,
max_length=self.max_length,
truncation=True,
pad_to_multiple_of=self.pad_to_multiple_of,
return_tensors="pt",
)
if "labels" in features[0]:
label_features = [feature["labels"] for feature in features]
batch["labels"] = torch.tensor(label_features, dtype=d_type)
return batch
def speech_file_to_array_fn(path):
speech_array, sampling_rate = torchaudio.load(path)
resampler = torchaudio.transforms.Resample(sampling_rate, target_sampling_rate)
speech = resampler(speech_array).squeeze().numpy()
return speech
def preprocess_function(examples):
speech_list = [speech_file_to_array_fn(path) for path in examples["path"]]
result = processor(speech_list, sampling_rate=target_sampling_rate)
if "label" in examples:
target_list = [
int(label) for label in examples["label"]
] # Do any preprocessing on your float/integer data
result["labels"] = list(target_list)
return result
@dataclass
class SpeechClassifierOutput(ModelOutput):
loss: Optional[torch.FloatTensor] = None
logits: torch.FloatTensor = None
hidden_states: Optional[Tuple[torch.FloatTensor]] = None
attentions: Optional[Tuple[torch.FloatTensor]] = None
class Data2VecClassificationHead(nn.Module):
"""Head for data2vec classification task."""
def __init__(self, config):
super().__init__()
self.dense = nn.Linear(config.hidden_size, config.hidden_size)
self.dropout = nn.Dropout(config.final_dropout)
self.out_proj = nn.Linear(config.hidden_size, config.num_labels)
def forward(self, features, **kwargs):
x = features
x = self.dropout(x)
x = self.dense(x)
x = torch.tanh(x)
x = self.dropout(x)
x = self.out_proj(x)
return x
class Data2VecForSpeechClassification(Data2VecAudioPreTrainedModel):
def __init__(self, config):
super().__init__(config)
self.num_labels = config.num_labels
self.pooling_mode = config.pooling_mode
self.config = config
self.data2vec = Data2VecAudioModel(config)
self.classifier = Data2VecClassificationHead(config)
self.init_weights()
def freeze_feature_extractor(self):
self.data2vec.feature_extractor._freeze_parameters()
def merged_strategy(self, hidden_states, mode="mean"):
if mode == "mean":
outputs = torch.mean(hidden_states, dim=1)
elif mode == "sum":
outputs = torch.sum(hidden_states, dim=1)
elif mode == "max":
outputs = torch.max(hidden_states, dim=1)[0]
else:
raise Exception(
"The pooling method hasn't been defined! Your pooling mode must be one of these ['mean', 'sum', 'max']"
)
return outputs
def forward(
self,
input_values,
attention_mask=None,
output_attentions=None,
output_hidden_states=None,
return_dict=None,
labels=None,
):
return_dict = (
return_dict if return_dict is not None else self.config.use_return_dict
)
outputs = self.data2vec(
input_values,
attention_mask=attention_mask,
output_attentions=output_attentions,
output_hidden_states=output_hidden_states,
return_dict=return_dict,
)
hidden_states = outputs[0]
hidden_states = self.merged_strategy(hidden_states, mode=self.pooling_mode)
logits = self.classifier(hidden_states)
loss = None
if labels is not None:
if self.config.problem_type is None:
if self.num_labels == 1:
self.config.problem_type = "regression"
elif self.num_labels > 1 and (
labels.dtype == torch.long or labels.dtype == torch.int
):
self.config.problem_type = "single_label_classification"
else:
self.config.problem_type = "multi_label_classification"
if self.config.problem_type == "regression":
loss_fct = F.mse_loss
loss = loss_fct(logits.view(-1, self.num_labels), labels.unsqueeze(-1))
elif self.config.problem_type == "single_label_classification":
loss_fct = CrossEntropyLoss()
loss = loss_fct(logits.view(-1, self.num_labels), labels.view(-1))
elif self.config.problem_type == "multi_label_classification":
loss_fct = BCEWithLogitsLoss()
loss = loss_fct(logits, labels)
# print("problem type====>", self.config.problem_type)
if not return_dict:
output = (logits,) + outputs[2:]
return ((loss,) + output) if loss is not None else output
return SpeechClassifierOutput(
loss=loss,
logits=logits,
hidden_states=outputs.hidden_states,
attentions=outputs.attentions,
)
def compute_metrics(eval_pred):
"""Computes spearman and pearson on predictions"""
recall = recall_metric.compute(
predictions=np.argmax(eval_pred.predictions.squeeze(), axis=-1),
references=eval_pred.label_ids,
average="macro",
)
f1 = f1_metric.compute(
predictions=np.argmax(eval_pred.predictions.squeeze(), axis=-1),
references=eval_pred.label_ids,
average="macro",
)
# f1 = f1_metric.compute(predictions=predictions, references=eval_pred.label_ids, average="macro")
return {**recall, **f1}
if __name__ == "__main__":
with open("config/params.yaml") as f:
_params = yaml.safe_load(f)
params = _params["wav2vec"]
target = _params["target"]
model_used = _params['wav2vec']["model"].split("/")[-1]
label_dir = "data/eating"
result_dir = "results/{}".format(model_used)
logging_dir = "tb_logs/{}".format(model_used)
model_checkpoint = params["model"]
batch_size = 16
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
target_sr = 16000
model_name = model_checkpoint.split("/")[-1]
all_files = list(glob("./data/wav/*.wav", recursive=True))
label_list = [0]
makedirs(result_dir, exist_ok=True)
makedirs(logging_dir, exist_ok=True)
recall_metric = evaluate.load("recall")
f1_metric = evaluate.load("f1")
df_train = pd.read_csv(f"{label_dir}/train.csv", encoding="utf-8")
df_dev = pd.read_csv(f"{label_dir}/dev.csv", encoding="utf-8")
df_test = pd.read_csv(f"{label_dir}/test.csv", encoding="utf-8")
# df_train["filename_full"] = "/srv/data/egasj/corpora/eating-wav-all/train/" + df_train["filename"] + ".wav"
df_train["filename_full"] = "/srv/data/egasj/corpora/eating-wav-all/" + df_train["filename"] + ".wav"
# df_train = df_train[df_train["filename_full"].isin(all_files)]
df_dev["filename_full"] = "/srv/data/egasj/corpora/eating-wav-all/" + df_dev["filename"] + ".wav"
# df_dev["filename_full"] = "/srv/data/egasj/corpora/eating-wav-all/dev/" + df_dev["filename"] + ".wav"
# df_dev = df_dev[df_dev["filename_full"].isin(all_files)]
# df_test["filename_full"] = "./data/wav/" + df_test["filename"]
df_test["filename_full"] = "/srv/data/egasj/corpora/eating-wav-all/" + df_test["filename"] + ".wav"
# df_test = df_test[df_test["filename_full"].isin(all_files)]
label_encoder = LabelEncoder()
df_train["label"] = label_encoder.fit_transform(df_train['label'])
df_dev["label"] = label_encoder.transform(df_dev['label'])
# if len(set(df_test[target])) > 1:
df_test["label"] = label_encoder.transform(df_test['label'])
train_dataset = Dataset.from_pandas(df_train)
dev_dataset = Dataset.from_pandas(df_dev)
test_dataset = Dataset.from_pandas(df_test)
config = AutoConfig.from_pretrained(
model_checkpoint,
num_labels=len(label_encoder.classes_),
problem_type=None,
)
setattr(config, "pooling_mode", params["pooling"])
processor = AutoFeatureExtractor.from_pretrained(model_checkpoint)
target_sampling_rate = processor.sampling_rate
data_collator = DataCollatorCTCWithPadding(
processor=processor, padding=True, max_length=10 * target_sampling_rate
)
train_dataset = train_dataset.map(
preprocess_function, batch_size=100, batched=True, num_proc=4
)
print("processed train")
dev_dataset = dev_dataset.map(
preprocess_function, batch_size=100, batched=True, num_proc=4
)
print("processed devel")
test_dataset = test_dataset.map(
preprocess_function, batch_size=32, batched=True, num_proc=4
)
print("processed test")
model = AutoModelForAudioClassification.from_pretrained(
model_checkpoint,
config=config,
)
if params["freezeExtractor"]:
model.freeze_feature_extractor()
if params["freezeTransformer"]:
model.freeze_transformer()
args = TrainingArguments(
result_dir,
evaluation_strategy="epoch",
save_strategy="epoch",
learning_rate=3e-4,
per_device_train_batch_size=batch_size,
gradient_accumulation_steps=1,
per_device_eval_batch_size=batch_size,
num_train_epochs=params["epochs"],
warmup_ratio=0.1,
logging_steps=10,
fp16=True,
load_best_model_at_end=True,
metric_for_best_model="recall",
push_to_hub=False,
gradient_checkpointing=True,
save_total_limit=2,
disable_tqdm=False,
logging_dir=f"{logging_dir}/{datetime.datetime.now().strftime('%h%d_%Y-%H-%M-%S')}_{socket.gethostname()}",
)
print(args.device)
trainer = Trainer(
model=model,
args=args,
data_collator=data_collator,
train_dataset=train_dataset,
eval_dataset=dev_dataset,
tokenizer=processor,
compute_metrics=compute_metrics,
)
trainer.train()
dev_predictions = trainer.predict(dev_dataset)
dev_df = pd.DataFrame(
{
"filename": dev_dataset["filename"],
"prediction": label_encoder.inverse_transform(
np.argmax(dev_predictions.predictions.squeeze(), axis=-1)
),
"true": label_encoder.inverse_transform(
dev_predictions.label_ids.squeeze()
),
}
)
dev_df.to_csv(join(result_dir, "predictions.devel.csv"), index=False)
print("DEV -->", compute_metrics(dev_predictions))
test_predictions = trainer.predict(test_dataset)
test_df = pd.DataFrame(
{
"filename": test_dataset["filename"],
"prediction": label_encoder.inverse_transform(
np.argmax(test_predictions.predictions.squeeze(), axis=-1)
),
"true": label_encoder.inverse_transform(
test_predictions.label_ids.squeeze()
)
# if len(set(test_predictions.label_ids)) > 1
# else ["?"] * test_predictions.predictions.shape[0],
}
)
test_df.to_csv(join(result_dir, "predictions.test.csv"), index=False)
print("TEST-->",compute_metrics(test_predictions))