-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update dependencies in poetry and add accelerate package * Add metrics in properly. --------- Co-authored-by: Ben Foley <[email protected]>
- Loading branch information
1 parent
3fb405b
commit cff43a0
Showing
6 changed files
with
2,016 additions
and
302 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from typing import Callable, Dict, Optional, Sequence | ||
|
||
import evaluate | ||
import numpy as np | ||
from loguru import logger | ||
from transformers import EvalPrediction, Wav2Vec2Processor | ||
|
||
|
||
def create_metrics( | ||
metric_names: Sequence[str], processor: Wav2Vec2Processor | ||
) -> Optional[Callable[[EvalPrediction], Dict]]: | ||
# Handle metrics | ||
if len(metric_names) == 0: | ||
return | ||
|
||
# Note: was using evaluate.combine but was having many unexpected errors. | ||
metrics = {name: evaluate.load(name) for name in metric_names} | ||
|
||
def compute_metrics(pred: EvalPrediction) -> Dict: | ||
# taken from https://huggingface.co/blog/fine-tune-xlsr-wav2vec2 | ||
pred_logits = pred.predictions | ||
|
||
pred.label_ids[pred.label_ids == -100] = processor.tokenizer.pad_token_id # type: ignore | ||
|
||
# Taken from: https://discuss.huggingface.co/t/code-review-compute-metrics-for-wer-with-wav2vec2processorwithlm/16841/3 | ||
if type(processor).__name__ == "Wav2Vec2ProcessorWithLM": | ||
pred_str = processor.batch_decode(pred_logits).text | ||
else: | ||
pred_ids = np.argmax(pred_logits, axis=-1) | ||
pred_str = processor.batch_decode(pred_ids) | ||
|
||
# We do not want to group tokens when computing the metrics | ||
label_str = processor.batch_decode(pred.label_ids, group_tokens=False) | ||
|
||
logger.debug(f"METRICS->pred: {pred_str} label:{label_str}") | ||
|
||
result = { | ||
name: metric.compute(predictions=pred_str, references=label_str) | ||
for name, metric in metrics.items() | ||
} | ||
logger.debug(f"Metrics Result: {result}") | ||
return result | ||
|
||
return compute_metrics |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.