add audio task
Browse files- tasks/audio.py +32 -14
tasks/audio.py
CHANGED
@@ -2,25 +2,34 @@ from fastapi import APIRouter
|
|
2 |
from datetime import datetime
|
3 |
from datasets import load_dataset
|
4 |
from sklearn.metrics import accuracy_score
|
|
|
|
|
|
|
5 |
import random
|
6 |
import os
|
|
|
7 |
|
8 |
from .utils.evaluation import AudioEvaluationRequest
|
9 |
from .utils.emissions import tracker, clean_emissions_data, get_space_info
|
|
|
|
|
|
|
10 |
|
|
|
11 |
from dotenv import load_dotenv
|
12 |
load_dotenv()
|
13 |
|
|
|
14 |
router = APIRouter()
|
15 |
|
16 |
-
DESCRIPTION = "
|
17 |
ROUTE = "/audio"
|
18 |
-
|
19 |
|
20 |
|
21 |
@router.post(ROUTE, tags=["Audio Task"],
|
22 |
description=DESCRIPTION)
|
23 |
-
async def evaluate_audio(request: AudioEvaluationRequest):
|
24 |
"""
|
25 |
Evaluate audio classification for rainforest sound detection.
|
26 |
|
@@ -38,34 +47,44 @@ async def evaluate_audio(request: AudioEvaluationRequest):
|
|
38 |
}
|
39 |
# Load and prepare the dataset
|
40 |
# Because the dataset is gated, we need to use the HF_TOKEN environment variable to authenticate
|
41 |
-
|
|
|
|
|
42 |
|
43 |
-
|
44 |
-
train_test = dataset["train"]
|
45 |
test_dataset = dataset["test"]
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
# Start tracking emissions
|
48 |
tracker.start()
|
49 |
tracker.start_task("inference")
|
50 |
-
|
51 |
#--------------------------------------------------------------------------------------------
|
52 |
# YOUR MODEL INFERENCE CODE HERE
|
53 |
# Update the code below to replace the random baseline by your model inference within the inference pass where the energy consumption and emissions are tracked.
|
54 |
#--------------------------------------------------------------------------------------------
|
|
|
55 |
|
56 |
-
|
57 |
-
|
58 |
-
predictions =
|
59 |
|
60 |
#--------------------------------------------------------------------------------------------
|
61 |
# YOUR MODEL INFERENCE STOPS HERE
|
62 |
#--------------------------------------------------------------------------------------------
|
63 |
-
|
64 |
# Stop tracking emissions
|
65 |
emissions_data = tracker.stop_task()
|
66 |
|
67 |
# Calculate accuracy
|
68 |
accuracy = accuracy_score(true_labels, predictions)
|
|
|
69 |
|
70 |
# Prepare results dictionary
|
71 |
results = {
|
@@ -84,5 +103,4 @@ async def evaluate_audio(request: AudioEvaluationRequest):
|
|
84 |
"test_seed": request.test_seed
|
85 |
}
|
86 |
}
|
87 |
-
|
88 |
-
return results
|
|
|
2 |
from datetime import datetime
|
3 |
from datasets import load_dataset
|
4 |
from sklearn.metrics import accuracy_score
|
5 |
+
from accelerate import Accelerator
|
6 |
+
from tqdm import tqdm
|
7 |
+
from torch.amp import autocast
|
8 |
import random
|
9 |
import os
|
10 |
+
import torch
|
11 |
|
12 |
from .utils.evaluation import AudioEvaluationRequest
|
13 |
from .utils.emissions import tracker, clean_emissions_data, get_space_info
|
14 |
+
from audio_utils import AudioClassifier, AudioDataset, Config, collate_fn, Evaluator
|
15 |
+
from transformers import AutoModelForImageClassification
|
16 |
+
from torch.utils.data import DataLoader
|
17 |
|
18 |
+
from loguru import logger
|
19 |
from dotenv import load_dotenv
|
20 |
load_dotenv()
|
21 |
|
22 |
+
|
23 |
router = APIRouter()
|
24 |
|
25 |
+
DESCRIPTION = "Audio pipeline to classify sounds."
|
26 |
ROUTE = "/audio"
|
27 |
+
device = "cuda"
|
28 |
|
29 |
|
30 |
@router.post(ROUTE, tags=["Audio Task"],
|
31 |
description=DESCRIPTION)
|
32 |
+
async def evaluate_audio(request: AudioEvaluationRequest): #, model_path: str):
|
33 |
"""
|
34 |
Evaluate audio classification for rainforest sound detection.
|
35 |
|
|
|
47 |
}
|
48 |
# Load and prepare the dataset
|
49 |
# Because the dataset is gated, we need to use the HF_TOKEN environment variable to authenticate
|
50 |
+
config = Config()
|
51 |
+
accelerator = Accelerator()
|
52 |
+
device = accelerator.device
|
53 |
|
54 |
+
dataset = load_dataset(request.dataset_name,token=os.getenv("HF_TOKEN"))
|
|
|
55 |
test_dataset = dataset["test"]
|
56 |
+
test_dataset = test_dataset.filter(lambda x: len(x["audio"]["array"]) > 0)
|
57 |
+
true_labels = test_dataset["label"]
|
58 |
+
|
59 |
+
test_dataset = AudioDataset(test_dataset)
|
60 |
+
test_loader = DataLoader(test_dataset, batch_size=2 * config.BATCH_SIZE, shuffle=False, collate_fn=collate_fn, num_workers=config.NUM_WORKERS, pin_memory=True)
|
61 |
+
|
62 |
+
model = AudioClassifier(config.MODEL_NAME, config.MODEL_PATH)
|
63 |
+
model, test_loader = accelerator.prepare(model, test_loader)
|
64 |
+
|
65 |
# Start tracking emissions
|
66 |
tracker.start()
|
67 |
tracker.start_task("inference")
|
68 |
+
|
69 |
#--------------------------------------------------------------------------------------------
|
70 |
# YOUR MODEL INFERENCE CODE HERE
|
71 |
# Update the code below to replace the random baseline by your model inference within the inference pass where the energy consumption and emissions are tracked.
|
72 |
#--------------------------------------------------------------------------------------------
|
73 |
+
predictions = []
|
74 |
|
75 |
+
logger.info("Running inference ...")
|
76 |
+
evaluator = Evaluator(model, test_loader, device)
|
77 |
+
predictions = evaluator.evaluate()
|
78 |
|
79 |
#--------------------------------------------------------------------------------------------
|
80 |
# YOUR MODEL INFERENCE STOPS HERE
|
81 |
#--------------------------------------------------------------------------------------------
|
|
|
82 |
# Stop tracking emissions
|
83 |
emissions_data = tracker.stop_task()
|
84 |
|
85 |
# Calculate accuracy
|
86 |
accuracy = accuracy_score(true_labels, predictions)
|
87 |
+
print("accuracy", accuracy)
|
88 |
|
89 |
# Prepare results dictionary
|
90 |
results = {
|
|
|
103 |
"test_seed": request.test_seed
|
104 |
}
|
105 |
}
|
106 |
+
return results
|
|