carlosdanielhernandezmena commited on
Commit
71b1f48
1 Parent(s): 8126b89

Adding code for evaluation

Browse files
Files changed (1) hide show
  1. README.md +59 -0
README.md CHANGED
@@ -50,6 +50,65 @@ The specific dataset used to create the model is called "Ravnursson Faroese Spee
50
 
51
  The fine-tuning process was perform during November (2022) in the servers of the Language and Voice Lab (https://lvl.ru.is/) at Reykjavík University (Iceland) by Carlos Daniel Hernández Mena.
52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  # BibTeX entry and citation info
54
  *When publishing results based on these models please refer to:*
55
  ```bibtex
 
50
 
51
  The fine-tuning process was perform during November (2022) in the servers of the Language and Voice Lab (https://lvl.ru.is/) at Reykjavík University (Iceland) by Carlos Daniel Hernández Mena.
52
 
53
+ # Evaluation
54
+ ```python
55
+ import torch
56
+ from transformers import Wav2Vec2Processor
57
+ from transformers import Wav2Vec2ForCTC
58
+
59
+ #Load the processor and model.
60
+ MODEL_NAME="carlosdanielhernandezmena/wav2vec2-large-xlsr-53-faroese-100h"
61
+ processor = Wav2Vec2Processor.from_pretrained(MODEL_NAME)
62
+ model = Wav2Vec2ForCTC.from_pretrained(MODEL_NAME)
63
+
64
+ #Load the dataset
65
+ from datasets import load_dataset, load_metric, Audio
66
+ ds=load_dataset("carlosdanielhernandezmena/ravnursson_asr",split='test')
67
+
68
+ #Downsample to 16kHz
69
+ ds = ds.cast_column("audio", Audio(sampling_rate=16_000))
70
+
71
+ #Process the dataset
72
+ def prepare_dataset(batch):
73
+ audio = batch["audio"]
74
+ #Batched output is "un-batched" to ensure mapping is correct
75
+ batch["input_values"] = processor(audio["array"], sampling_rate=audio["sampling_rate"]).input_values[0]
76
+ with processor.as_target_processor():
77
+ batch["labels"] = processor(batch["normalized_text"]).input_ids
78
+ return batch
79
+ ds = ds.map(prepare_dataset, remove_columns=ds.column_names,num_proc=1)
80
+
81
+ #Define the evaluation metric
82
+ import numpy as np
83
+ wer_metric = load_metric("wer")
84
+ def compute_metrics(pred):
85
+ pred_logits = pred.predictions
86
+ pred_ids = np.argmax(pred_logits, axis=-1)
87
+ pred.label_ids[pred.label_ids == -100] = processor.tokenizer.pad_token_id
88
+ pred_str = processor.batch_decode(pred_ids)
89
+ #We do not want to group tokens when computing the metrics
90
+ label_str = processor.batch_decode(pred.label_ids, group_tokens=False)
91
+ wer = wer_metric.compute(predictions=pred_str, references=label_str)
92
+ return {"wer": wer}
93
+
94
+ #Do the evaluation (with batch_size=1)
95
+ model = model.to(torch.device("cuda"))
96
+ def map_to_result(batch):
97
+ with torch.no_grad():
98
+ input_values = torch.tensor(batch["input_values"], device="cuda").unsqueeze(0)
99
+ logits = model(input_values).logits
100
+ pred_ids = torch.argmax(logits, dim=-1)
101
+ batch["pred_str"] = processor.batch_decode(pred_ids)[0]
102
+ batch["sentence"] = processor.decode(batch["labels"], group_tokens=False)
103
+ return batch
104
+ results = ds.map(map_to_result,remove_columns=ds.column_names)
105
+
106
+ #Compute the overall WER now.
107
+ print("Test WER: {:.3f}".format(wer_metric.compute(predictions=results["pred_str"], references=results["sentence"])))
108
+
109
+ ```
110
+ **Test Result**: 0.076
111
+
112
  # BibTeX entry and citation info
113
  *When publishing results based on these models please refer to:*
114
  ```bibtex