-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrun_eval.py
215 lines (183 loc) · 6.5 KB
/
run_eval.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
import argparse
import time
from time import strftime, localtime
import os
import evaluate
from tqdm import tqdm
import torch
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor
from datasets import load_dataset
import librosa
import numpy as np
from src.utils.logging import logger
import math
# Follow open_asr_leaderboard
# https://github.com/huggingface/open_asr_leaderboard/blob/main/speechbrain/run_eval.py
def get_whisper_model(model_name_or_path: str, device: str):
"""Load a pretrained Whisper model.
Arguments:
---------
model_name_or_path : str
Name or path of the Whisper model. E.g., 'openai/whisper-small' or path to a custom model.
device : str
Device to run the model on. E.g., 'cpu' or 'cuda:0'.
Returns:
-------
tuple
Containing the loaded model and processor.
"""
processor = AutoProcessor.from_pretrained(model_name_or_path)
model = AutoModelForSpeechSeq2Seq.from_pretrained(model_name_or_path).to(device)
return model, processor
def transcribe_batch(model, processor, batch, device, language):
"""Transcribe a batch of audio.
Arguments:
---------
model : WhisperForConditionalGeneration
Whisper model.
processor : WhisperProcessor
Whisper processor.
batch : dict
Batch containing audio data.
device : str
Device to run the model on.
Returns:
-------
dict
Dictionary containing transcription results and timing.
"""
# Load audio inputs
audio_inputs = batch["audio"]
input_features = processor(
audio_inputs, sampling_rate=16000, return_tensors="pt"
).input_features.to(device)
# Start timing
start_time = time.time()
# Generate transcriptions
with torch.no_grad():
generated_ids = model.generate(input_features, language=language)
transcriptions = processor.batch_decode(generated_ids, skip_special_tokens=True)
# End timing
runtime = time.time() - start_time
logger.info(f"Run Time: {runtime}")
# Normalize transcriptions
normalized_transcriptions = [normalize_text(trans) for trans in transcriptions]
return {
"transcription_time_s": [runtime / len(audio_inputs)] * len(audio_inputs),
"predictions": normalized_transcriptions,
}
def normalize_text(text):
"""Perform simple text normalization."""
return text.lower().strip()
def main(args):
device = "cuda" if torch.cuda.is_available() else "cpu"
model, processor = get_whisper_model(args.model_name_or_path, device)
# Load dataset
dataset = load_dataset(
args.dataset_path, args.language, split=args.split, streaming=args.streaming, trust_remote_code=True
)
if args.max_eval_samples:
dataset = dataset.select(range(min(args.max_eval_samples, len(dataset))))
# Prepare dataset
def prepare_dataset(batch):
audio = batch["audio"]
batch["audio"] = librosa.resample(
np.array(audio["array"]), orig_sr=audio["sampling_rate"], target_sr=16000
)
batch["audio_length_s"] = len(batch["audio"]) / 16000
batch["norm_text"] = normalize_text(batch["sentence"])
return batch
remove_columns = [
"client_id",
"path",
"up_votes",
"down_votes",
"age",
"gender",
"accent",
"locale",
"segment",
"variant",
]
dataset = dataset.map(prepare_dataset, remove_columns=remove_columns)
# Evaluate
all_results = {
"audio_length_s": [],
"transcription_time_s": [],
"predictions": [],
"references": [],
}
# Calculate total number of batches if possible
if not args.streaming and hasattr(dataset, '__len__'):
total_batches = math.ceil(len(dataset) / args.batch_size)
else:
total_batches = None # Unknown size for streaming datasets
data_iterator = dataset.iter(batch_size=args.batch_size)
progress_bar = tqdm(data_iterator, total=total_batches, desc="Evaluating", dynamic_ncols=True)
for batch in progress_bar:
language = "chinese" if "zh" in args.language else args.language
results = transcribe_batch(model, processor, batch, device, language)
all_results["predictions"].extend(results["predictions"])
all_results["transcription_time_s"].extend(results["transcription_time_s"])
all_results["references"].extend(batch["norm_text"])
all_results["audio_length_s"].extend(batch["audio_length_s"])
# Calculate WER
wer_metric = evaluate.load("wer")
wer = wer_metric.compute(
references=all_results["references"], predictions=all_results["predictions"]
)
wer = round(100 * wer, 2)
# Calculate RTFx
rtfx = round(
sum(all_results["audio_length_s"]) / sum(all_results["transcription_time_s"]), 2
)
print(f"WER: {wer}%, RTFx: {rtfx}")
# Save results
output_dir = "evaluation_results"
os.makedirs(output_dir, exist_ok=True)
run_name = f"{strftime('%Y-%m-%d', localtime(time.time()))}"
output_file = os.path.join(output_dir, f"{args.dataset_path.split('/')[-1]}_{args.split}_{run_name}_results.txt")
with open(output_file, "w") as f:
f.write(f"Model: {args.model_name_or_path}\n")
f.write(f"Split: {args.split}\n")
f.write(f"WER: {wer}%\n")
f.write(f"RTFx: {rtfx}\n")
print(f"Results saved to {output_file}")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--model_name_or_path",
type=str,
required=True,
help="Whisper model name or path",
)
parser.add_argument(
"--dataset_path",
type=str,
default="mozilla-foundation/common_voice_16_1",
help="Dataset path",
)
parser.add_argument("--language", type=str, required=True, help="language name")
parser.add_argument(
"--split", type=str, default="test", help="Dataset split to evaluate on"
)
parser.add_argument(
"--device",
type=int,
default=-1,
help="Device to run evaluation on (-1 for CPU, 0 or greater for GPU)",
)
parser.add_argument(
"--batch_size", type=int, default=32, help="Batch size for evaluation"
)
parser.add_argument(
"--max_eval_samples",
type=int,
default=None,
help="Maximum number of samples to evaluate",
)
parser.add_argument(
"--streaming", default=False, help="Whether to stream the dataset"
)
args = parser.parse_args()
main(args)