Delete functions.py
Browse files- functions.py +0 -66
functions.py
DELETED
|
@@ -1,66 +0,0 @@
|
|
| 1 |
-
from typing import Tuple, List, Dict, Any
|
| 2 |
-
|
| 3 |
-
import evaluate
|
| 4 |
-
import numpy as np
|
| 5 |
-
import pandas as pd
|
| 6 |
-
from transformers import ASTForAudioClassification, Trainer, TrainingArguments
|
| 7 |
-
|
| 8 |
-
from dataset import load_dataset_esc50
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
def squeeze_func(batch: Dict[str, Any]) -> Dict[str, Any]:
|
| 12 |
-
"""
|
| 13 |
-
Remove the extra dimension from input_values.
|
| 14 |
-
"""
|
| 15 |
-
batch['input_values'] = np.asarray(batch['input_values']).squeeze(0)
|
| 16 |
-
return batch
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
def prepare_datasets(apply_augmentation: bool) -> Tuple[Any, List[int]]:
|
| 20 |
-
"""
|
| 21 |
-
Load and preprocess the ESC-50 dataset.
|
| 22 |
-
"""
|
| 23 |
-
dataset, folds = load_dataset_esc50(apply_augmentation=apply_augmentation)
|
| 24 |
-
return dataset.map(squeeze_func), folds
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
def get_model(model_name: str) -> ASTForAudioClassification:
|
| 28 |
-
"""
|
| 29 |
-
Load the AST audio classification model with SDPA.
|
| 30 |
-
"""
|
| 31 |
-
return ASTForAudioClassification.from_pretrained(
|
| 32 |
-
model_name,
|
| 33 |
-
attn_implementation="sdpa"
|
| 34 |
-
)
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
def make_trainer(
|
| 38 |
-
model: ASTForAudioClassification,
|
| 39 |
-
train_dataset: Any,
|
| 40 |
-
eval_dataset: Any,
|
| 41 |
-
args: TrainingArguments
|
| 42 |
-
) -> Trainer:
|
| 43 |
-
"""
|
| 44 |
-
Build and return a HuggingFace Trainer instance.
|
| 45 |
-
"""
|
| 46 |
-
metric = evaluate.load('accuracy')
|
| 47 |
-
|
| 48 |
-
def compute_metrics(pred):
|
| 49 |
-
logits, labels = pred
|
| 50 |
-
return metric.compute(predictions=np.argmax(logits, axis=-1), references=labels)
|
| 51 |
-
|
| 52 |
-
return Trainer(
|
| 53 |
-
model=model,
|
| 54 |
-
args=args,
|
| 55 |
-
train_dataset=train_dataset,
|
| 56 |
-
eval_dataset=eval_dataset,
|
| 57 |
-
compute_metrics=compute_metrics,
|
| 58 |
-
)
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
def save_results(results: List[Dict[str, Any]], csv_path: str) -> None:
|
| 62 |
-
"""
|
| 63 |
-
Save the experiment results to a CSV file.
|
| 64 |
-
"""
|
| 65 |
-
pd.DataFrame(results).to_csv(csv_path, index=False)
|
| 66 |
-
print(f"Results saved to {csv_path}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|