infinitejoy commited on
Commit
cb5fb7f
1 Parent(s): 9e332cb

eval results

Browse files
eval.py ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ return text
64
+
65
+
66
+ def main(args):
67
+ # load dataset
68
+ dataset = load_dataset(args.dataset, args.config, split=args.split, use_auth_token=True)
69
+
70
+ # for testing: only process the first two examples as a test
71
+ dataset = dataset.select(range(10))
72
+
73
+ # load processor
74
+ feature_extractor = AutoFeatureExtractor.from_pretrained(args.model_id)
75
+ sampling_rate = feature_extractor.sampling_rate
76
+
77
+ # resample audio
78
+ dataset = dataset.cast_column("audio", Audio(sampling_rate=sampling_rate))
79
+
80
+ # load eval pipeline
81
+ asr = pipeline("automatic-speech-recognition", model=args.model_id)
82
+
83
+ # map function to decode audio
84
+ def map_to_pred(batch):
85
+ prediction = asr(
86
+ batch["audio"]["array"], chunk_length_s=args.chunk_length_s, stride_length_s=args.stride_length_s
87
+ )
88
+
89
+ batch["prediction"] = prediction["text"]
90
+ batch["target"] = normalize_text(batch["sentence"])
91
+ return batch
92
+
93
+ # run inference on all examples
94
+ result = dataset.map(map_to_pred, remove_columns=dataset.column_names)
95
+
96
+ # compute and log_results
97
+ # do not change function below
98
+ log_results(result, args)
99
+
100
+
101
+ if __name__ == "__main__":
102
+ parser = argparse.ArgumentParser()
103
+
104
+ parser.add_argument(
105
+ "--model_id", type=str, required=True, help="Model identifier. Should be loadable with 🤗 Transformers"
106
+ )
107
+ parser.add_argument(
108
+ "--dataset",
109
+ type=str,
110
+ required=True,
111
+ help="Dataset name to evaluate the `model_id`. Should be loadable with 🤗 Datasets",
112
+ )
113
+ parser.add_argument(
114
+ "--config", type=str, required=True, help="Config of the dataset. *E.g.* `'en'` for Common Voice"
115
+ )
116
+ parser.add_argument("--split", type=str, required=True, help="Split of the dataset. *E.g.* `'test'`")
117
+ parser.add_argument(
118
+ "--chunk_length_s", type=float, default=None, help="Chunk length in seconds. Defaults to 5 seconds."
119
+ )
120
+ parser.add_argument(
121
+ "--stride_length_s", type=float, default=None, help="Stride of the audio chunks. Defaults to 1 second."
122
+ )
123
+ parser.add_argument(
124
+ "--log_outputs", action="store_true", help="If defined, write outputs to log file for analysis."
125
+ )
126
+ args = parser.parse_args()
127
+
128
+ main(args)
log_mozilla-foundation_common_voice_7_0_or_test_predictions.txt ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 0
2
+ ପ<s>ୋ<s>ର<s>ରେ<s> <s>ବ<s>ା<s>ଲ<s>ା<s> ଗ<s>ସ୍<s>ତ<s>ି<s> ଭ<s>ା<s>ଣ୍ଡ<s>ୀ<s> <s>ଗ<s>ୋ<s>ପ<s>ା<s>ଳ<s> ପ<s>ରୋ<s>ଠ<s>ା<s>ରୁ<s> <s>ଦ<s>େ<s>ଢ଼<s> କ<s>ୋ<s>ସ<s> <s>ଦ<s>ୁ<s>ର<s> ।
3
+ 1
4
+ ଆ<s>ଜ<s>ି<s>ମ<s>ଳ<s>ତ<s>ୀ<s>ର<s> <s>ଆ<s>ଦ<s>ର<s> <s>ଦ<s>େ<s>ଖ<s>େ<s> <s>କ<s>ି<s>ଏ
5
+ 2
6
+ ହ<s>ୁ<s>ଏ<s> <s>ତ<s> <s>ମ<s>ଙ୍<s>ଗ<s>ରା<s>ଜ<s>େ<s> ବ<s>ୁ<s>ଜ<s>ି<s>ଚ<s>ନ୍<s>ତ<s>ି<s> <s>ହ<s>ଲ<s>ଗ<s>ୁ<s> <s>ଚ<s>ି<s>ର<s>କ<s>ା<s>ଳ<s> ନ<s>େ<s>ମ<s>ନ୍<s>ତ<s>େ<s> ବ<s>ି<s>ଦ<s>ା<s>ୟ<s> <s>ଗ<s>ହ<s>ଣ<s> <s>କ<s>ଲ<s>ା<s> ।
7
+ 3
8
+ ତ<s>ା<s>ହ<s>ା<s>ଲ<s>େ ଆ<s>ପ<s>ଣ<s> ଘ<s>ର<s> ପ<s>ା<s>ଣ<s>ି<s> ମ<s>ା<s>ଠ<s>ି<s>ଆା<s>ରୁ<s> <s>ତ<s> <s>ମ<s>ା<s>ଚ<s> ଭା<s>ହା<s>ର<s>ଥ<s>ାନ୍<s>ଦ<s>େ<s> ।
9
+ 4
10
+ ତ<s>ହ<s>ି<s>ଁ <s>ଆ<s>ର<s> ଦ<s>ି<s>ନ<s> <s>ଠ<s>ା<s>ର<s>ୁ<s> <s>ଚ<s>ା<s>ର<s>ି<s>ଦ<s>ି<s>ନ<s> <s>ଜ<s>ା<s>ଏ<s> ଭ<s>ଗ<s>ୁ<s>ମ<s>ା<s>ନ<s>ଙ<s>କ<s> <s>ଗ<s>ା<s> ମ<s>ଦ<s>୍<s>ଆ<s>ର<s>େ<s> କ<s>େ<s>ହ<s>ି<s> <s>ଦ<s>େ<s>ଖ<s>ି<s> ନ<s>ା<s>ହ<s>ି<s>ଁ ।
11
+ 5
12
+ ତ<s>େ<s>ବ<s>େ<s> ଆ<s>ଜ<s>ି<s> ଏ<s>ତ<s>େ<s> <s>ବ<s>ା<s>କ<s>ୁ<s>ଳ<s> <s>କ<s>ି<s> ପ<s>ା<s>ହ<s>ଁ
13
+ 6
14
+ ବ<s>ା<s>ପ<s> <s>ଛ<s>େ<s>ଉ<s>ଉ<s>ଁ <s>ଡ<s>ଟ<s>ି<s> ଲ<s>ା<s>ଗ<s> <s>କ<s>ି<s>ଏ ଧ<s>ା<s>ଇଁ<s>ବ<s> ।
15
+ 7
16
+ ଆ<s>ଉ<s> <s>ମ<s>ୁ<s>ଁ<s> ବ<s>ୋ<s>ଲ<s>ି<s> <s>ସ<s>ି<s>ନ<s>ା<s> <s>ତ<s>ୋ<s>ର<s> ମ<s>ା<s>ଣ<s> ପ<s>ା<s>ଙ୍<s>ଚ<s>ଟ<s>ଙ୍<s>କ<s>ା<s>ରେ<s> ନ<s>େ<s>ଲ<s>ି<s>
17
+ 8
18
+ ବ<s>ା<s> <s>ହ<s>ାନ<s>ତ<s> <s>ପ<s>ା<s>କୁ<s>ଲ<s>ି<s>ର<s>େ<s> <s>ମ<s>ୁ<s>ହ<s>ଁ<s>ର<s>ୁ<s> <s>ପ<s>ୋ<s>ସ<s>ପ<s>ସ<s> ଜ<s>ା<s>ଳ<s> ପ<s>ୋ<s>ଛ<s>ି<s> ପ<s>କ<s>ା<s>ଉ<s>ଛ<s>ନ୍<s>ତ<s>ି<s> ।
19
+ 9
20
+ ଏ<s>ଣ<s>ି<s>ଚ<s> <s>ପ<s>ି<s>ଚ୍<s>ୁ<s> ଲ<s>ୋ<s>କ<s> <s>ପ<s>ା<s>ଣ<s>ି<s> <s>ମ<s>ୁ<s>ଠ<s>ା<s>ଏ<s> ପ<s>ା<s>ଇ<s>ବ<s>ା<s>ର<s> <s>ବ<s>ା<s>ଟ<s> <s>କ<s>ର<s> ।
log_mozilla-foundation_common_voice_7_0_or_test_targets.txt ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 0
2
+ ମକ୍ରାମପୁରରେ ବାଲାଗସ୍ତି ଫାଣ୍ତି ଗୋପାଳପୁରଠାରୁ ଦେଢ଼କୋଶ ଦୂର ।
3
+ 1
4
+ ଆଜି ମଳତୀର ଆଦର ଦେଖେ କିଏ
5
+ 2
6
+ ହୁଏତ ମଙ୍ଗରାଜେ ବୁଝିଛନ୍ତି ଫଲ୍ଗୁ ଚିରକାଳ ନିମନ୍ତେ ବିଦାୟ ଗ୍ରହଣ କଲା ।
7
+ 3
8
+ ତାହେଲେ ଆପଣଙ୍କ ଘର ପାଣିମାଠିଆରୁ ତ ମାଛ ବାହାରୁଥାନ୍ତେ
9
+ 4
10
+ ତହିଁଆର ଦିନଠାରୁ ଚାରିଦିନ ଯାଏ ଭଗବାନକୁ ଗାଁ ମଧ୍ୟରେ କେହି ଦେଖିନାହିଁ ।
11
+ 5
12
+ ତେବେ ଆଜି ଏତେ ବ୍ୟାକୁଳ କି ପାଇଁ
13
+ 6
14
+ ବାପ ଛେଉଣ୍ଡଟି ଲାଗି କିଏ ଧାଇଁବ
15
+ 7
16
+ ଆଉ ମୁଁ ବୋଲି ସିନା ତୋର ମାଣ ପାଞ୍ଚ ଟଙ୍କାରେ ନେଲି
17
+ 8
18
+ ବାଁ ହାତ ପାପୁଲିରେ ମୁହଁରୁ ପୋଷ ପୋଷ ଝାଳ ପୋଛିପକାଉଅଛନ୍ତି ।
19
+ 9
20
+ ଏଣିକି ପିତୃଲୋକ ପାଣି ମୁଠାଏ ପାଇବାର ବାଟ କର ।
mozilla-foundation_common_voice_7_0_or_test_eval_results.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ WER: 1.0921052631578947
2
+ CER: 2.5547945205479454