gigant commited on
Commit
6f56cc5
1 Parent(s): 7c3ada2

Upload eval.py

Browse files
Files changed (1) hide show
  1. eval.py +140 -0
eval.py ADDED
@@ -0,0 +1,140 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ import argparse
3
+ import re
4
+ from typing import Dict
5
+
6
+ from datasets import Audio, Dataset, load_dataset, load_metric
7
+
8
+ from transformers import AutoFeatureExtractor, pipeline
9
+
10
+
11
+ def log_results(result: Dataset, args: Dict[str, str]):
12
+ """DO NOT CHANGE. This function computes and logs the result metrics."""
13
+
14
+ log_outputs = args.log_outputs
15
+ dataset_id = "_".join(args.dataset.split("/") + [args.config, args.split])
16
+
17
+ # load metric
18
+ wer = load_metric("wer")
19
+ cer = load_metric("cer")
20
+
21
+ # compute metrics
22
+ wer_result = wer.compute(references=result["target"], predictions=result["prediction"])
23
+ cer_result = cer.compute(references=result["target"], predictions=result["prediction"])
24
+
25
+ # print & log results
26
+ result_str = f"WER: {wer_result}\n" f"CER: {cer_result}"
27
+ print(result_str)
28
+
29
+ with open(f"{dataset_id}_eval_results.txt", "w") as f:
30
+ f.write(result_str)
31
+
32
+ # log all results in text file. Possibly interesting for analysis
33
+ if log_outputs is not None:
34
+ pred_file = f"log_{dataset_id}_predictions.txt"
35
+ target_file = f"log_{dataset_id}_targets.txt"
36
+
37
+ with open(pred_file, "w") as p, open(target_file, "w") as t:
38
+
39
+ # mapping function to write output
40
+ def write_to_file(batch, i):
41
+ p.write(f"{i}" + "\n")
42
+ p.write(batch["prediction"] + "\n")
43
+ t.write(f"{i}" + "\n")
44
+ t.write(batch["target"] + "\n")
45
+
46
+ result.map(write_to_file, with_indices=True)
47
+
48
+
49
+ def normalize_text(text: str) -> str:
50
+ """DO ADAPT FOR YOUR USE CASE. this function normalizes the target text."""
51
+
52
+ chars_to_ignore_regex = '[,?.!\-\;\:"“%‘”�—’…–„]' # noqa: W605 IMPORTANT: this should correspond to the chars that were ignored during training
53
+
54
+ text = re.sub(chars_to_ignore_regex, "", text.lower())
55
+
56
+ # In addition, we can normalize the target text, e.g. removing new lines characters etc...
57
+ # note that order is important here!
58
+ token_sequences_to_ignore = ["\n\n", "\n", " ", " "]
59
+
60
+ for t in token_sequences_to_ignore:
61
+ text = " ".join(text.split(t))
62
+
63
+ #Replace some characters that are not in the Romanian alphabet but are found in the dataset
64
+ text = re.sub('[á]', 'a', text)
65
+ text = re.sub('[é]', 'e', text)
66
+ text = re.sub('[í]', 'i', text)
67
+ text = re.sub('[ò]', 'o', text)
68
+ text = re.sub('[ü]', 'u', text)
69
+ text = re.sub('[ć]', 'c', text)
70
+ text = re.sub('[đ]', 'd', text)
71
+ text = re.sub('[č]', 'c', text)
72
+ text = re.sub('[š]', 's', text)
73
+ text = re.sub('[ş]', 'ș', text) #Replace the s-cedilla that is sometimes (wrongly) used instead of the s-comma
74
+ text = re.sub('[ţ]', 'ț', text) #Replace the t-cedilla that is sometimes (wrongly) used instead of the t-comma
75
+ return text
76
+
77
+
78
+ def main(args):
79
+ # load dataset
80
+ dataset = load_dataset(args.dataset, args.config, split=args.split, use_auth_token=True)
81
+
82
+ # for testing: only process the first two examples as a test
83
+ # dataset = dataset.select(range(10))
84
+
85
+ # load processor
86
+ feature_extractor = AutoFeatureExtractor.from_pretrained(args.model_id)
87
+ sampling_rate = feature_extractor.sampling_rate
88
+
89
+ # resample audio
90
+ dataset = dataset.cast_column("audio", Audio(sampling_rate=sampling_rate))
91
+
92
+ # load eval pipeline
93
+ asr = pipeline("automatic-speech-recognition", model=args.model_id)
94
+
95
+ # map function to decode audio
96
+ def map_to_pred(batch):
97
+ prediction = asr(
98
+ batch["audio"]["array"], chunk_length_s=args.chunk_length_s, stride_length_s=args.stride_length_s
99
+ )
100
+
101
+ batch["prediction"] = prediction["text"]
102
+ batch["target"] = normalize_text(batch["sentence"])
103
+ return batch
104
+
105
+ # run inference on all examples
106
+ result = dataset.map(map_to_pred, remove_columns=dataset.column_names)
107
+
108
+ # compute and log_results
109
+ # do not change function below
110
+ log_results(result, args)
111
+
112
+
113
+ if __name__ == "__main__":
114
+ parser = argparse.ArgumentParser()
115
+
116
+ parser.add_argument(
117
+ "--model_id", type=str, required=True, help="Model identifier. Should be loadable with 🤗 Transformers"
118
+ )
119
+ parser.add_argument(
120
+ "--dataset",
121
+ type=str,
122
+ required=True,
123
+ help="Dataset name to evaluate the `model_id`. Should be loadable with 🤗 Datasets",
124
+ )
125
+ parser.add_argument(
126
+ "--config", type=str, required=True, help="Config of the dataset. *E.g.* `'en'` for Common Voice"
127
+ )
128
+ parser.add_argument("--split", type=str, required=True, help="Split of the dataset. *E.g.* `'test'`")
129
+ parser.add_argument(
130
+ "--chunk_length_s", type=float, default=None, help="Chunk length in seconds. Defaults to 5 seconds."
131
+ )
132
+ parser.add_argument(
133
+ "--stride_length_s", type=float, default=None, help="Stride of the audio chunks. Defaults to 1 second."
134
+ )
135
+ parser.add_argument(
136
+ "--log_outputs", action="store_true", help="If defined, write outputs to log file for analysis."
137
+ )
138
+ args = parser.parse_args()
139
+
140
+ main(args)