EC2 Default User commited on
Commit
ddc02d0
1 Parent(s): bef1005

robust-speech-event only tokenization

Browse files
eval.py ADDED
@@ -0,0 +1,154 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ from pythainlp.tokenize import word_tokenize, syllable_tokenize
11
+ from deepcut import tokenize as deepcut_word_tokenize
12
+ from functools import partial
13
+
14
+
15
+ def log_results(result: Dataset, args: Dict[str, str]):
16
+ """DO NOT CHANGE. This function computes and logs the result metrics."""
17
+
18
+ log_outputs = args.log_outputs
19
+ dataset_id = "_".join(args.dataset.split("/") + [args.config, args.split])
20
+
21
+ # load metric
22
+ wer = load_metric("wer")
23
+ cer = load_metric("cer")
24
+
25
+ # compute metrics
26
+ wer_result = wer.compute(references=result["target"], predictions=result["prediction"])
27
+ cer_result = cer.compute(references=result["target"], predictions=result["prediction"])
28
+
29
+ # print & log results
30
+ result_str = f"WER: {wer_result}\n" f"CER: {cer_result}"
31
+ print(result_str)
32
+
33
+ with open(f"robust-speech-event/{dataset_id}_eval_results_{args.thai_tokenizer}.txt", "w") as f:
34
+ f.write(result_str)
35
+
36
+ # log all results in text file. Possibly interesting for analysis
37
+ if log_outputs is not None:
38
+ pred_file = f"robust-speech-event/log_{dataset_id}_predictions_{args.thai_tokenizer}.txt"
39
+ target_file = f"robust-speech-event/log_{dataset_id}_targets_{args.thai_tokenizer}.txt"
40
+
41
+ with open(pred_file, "w") as p, open(target_file, "w") as t:
42
+
43
+ # mapping function to write output
44
+ def write_to_file(batch, i):
45
+ p.write(f"{i}" + "\n")
46
+ p.write(batch["prediction"] + "\n")
47
+ t.write(f"{i}" + "\n")
48
+ t.write(batch["target"] + "\n")
49
+
50
+ result.map(write_to_file, with_indices=True)
51
+
52
+
53
+ def normalize_text(text: str, tok_func) -> str:
54
+ """DO ADAPT FOR YOUR USE CASE. this function normalizes the target text."""
55
+
56
+ chars_to_ignore_regex = '[,?.!\-\;\:"“%‘”�—’…–]' # noqa: W605 IMPORTANT: this should correspond to the chars that were ignored during training
57
+
58
+ text = re.sub(chars_to_ignore_regex, "", text.lower())
59
+
60
+ # In addition, we can normalize the target text, e.g. removing new lines characters etc...
61
+ # note that order is important here!
62
+ token_sequences_to_ignore = ["\n\n", "\n", " ", " "]
63
+
64
+ for t in token_sequences_to_ignore:
65
+ text = " ".join(text.split(t))
66
+
67
+ #thai tokenize
68
+ text = " ".join(tok_func(text))
69
+
70
+ return text
71
+
72
+ def retokenize(text:str, tok_func) -> str:
73
+ """tokenize and rejoin prediction outputs without cleaning"""
74
+ return " ".join(tok_func("".join(text.split())))
75
+
76
+
77
+ def main(args):
78
+ # load dataset
79
+ dataset = load_dataset(args.dataset, args.config, split=args.split, use_auth_token=True)
80
+
81
+ # for testing: only process the first two examples as a test
82
+ dataset = dataset.select(range(10))
83
+
84
+ # load processor
85
+ feature_extractor = AutoFeatureExtractor.from_pretrained(args.model_id)
86
+ sampling_rate = feature_extractor.sampling_rate
87
+
88
+ # resample audio
89
+ dataset = dataset.cast_column("audio", Audio(sampling_rate=sampling_rate))
90
+
91
+ # load eval pipeline
92
+ asr = pipeline("automatic-speech-recognition", model=args.model_id)
93
+
94
+ #select tokenizer
95
+ if args.thai_tokenizer=='deepcut':
96
+ tok_func = deepcut_word_tokenize
97
+ elif args.thai_tokenizer=='newmm':
98
+ tok_func = word_tokenize
99
+ elif args.thai_tokenizer=='syllable':
100
+ tok_func = syllable_tokenize
101
+ else:
102
+ raise('No Thai tokenizer chosen')
103
+
104
+ # map function to decode audio
105
+ def map_to_pred(batch, tok_func):
106
+ prediction = asr(
107
+ batch["audio"]["array"], chunk_length_s=args.chunk_length_s, stride_length_s=args.stride_length_s
108
+ )
109
+
110
+ batch["prediction"] = retokenize(prediction["text"], tok_func)
111
+ batch["target"] = normalize_text(batch["sentence"], tok_func)
112
+ return batch
113
+
114
+ # run inference on all examples
115
+ result = dataset.map(partial(map_to_pred, tok_func=tok_func),
116
+ remove_columns=dataset.column_names)
117
+
118
+ # compute and log_results
119
+ # do not change function below
120
+ log_results(result, args)
121
+
122
+
123
+ if __name__ == "__main__":
124
+ parser = argparse.ArgumentParser()
125
+
126
+ parser.add_argument(
127
+ "--model_id", type=str, required=True, help="Model identifier. Should be loadable with 🤗 Transformers"
128
+ )
129
+ parser.add_argument(
130
+ "--thai_tokenizer", type=str, default="newmm",
131
+ required=True, help="newmm, syllable, or deepcut"
132
+ )
133
+ parser.add_argument(
134
+ "--dataset",
135
+ type=str,
136
+ required=True,
137
+ help="Dataset name to evaluate the `model_id`. Should be loadable with 🤗 Datasets",
138
+ )
139
+ parser.add_argument(
140
+ "--config", type=str, required=True, help="Config of the dataset. *E.g.* `'en'` for Common Voice"
141
+ )
142
+ parser.add_argument("--split", type=str, required=True, help="Split of the dataset. *E.g.* `'test'`")
143
+ parser.add_argument(
144
+ "--chunk_length_s", type=float, default=None, help="Chunk length in seconds. Defaults to 5 seconds."
145
+ )
146
+ parser.add_argument(
147
+ "--stride_length_s", type=float, default=None, help="Stride of the audio chunks. Defaults to 1 second."
148
+ )
149
+ parser.add_argument(
150
+ "--log_outputs", action="store_true", help="If defined, write outputs to log file for analysis."
151
+ )
152
+ args = parser.parse_args()
153
+
154
+ main(args)
robust-speech-event/log_mozilla-foundation_common_voice_7_0_th_test_predictions_deepcut.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
+ วิธี การ สร้าง ช่อ ดอก ไม้ ที่ กิน ได้
robust-speech-event/log_mozilla-foundation_common_voice_7_0_th_test_predictions_newmm.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
+ วิธีการ สร้าง ช่อดอกไม้ ที่ กิน ได้
robust-speech-event/log_mozilla-foundation_common_voice_7_0_th_test_predictions_syllable.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
+ วิ ธี การ สร้าง ช่อ ดอก ไม้ ที่ กิน ได้
robust-speech-event/log_mozilla-foundation_common_voice_7_0_th_test_targets_deepcut.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
+ วิธี การ สร้าง ช่อ ดอก ไม้ ที่ กิน ได้
robust-speech-event/log_mozilla-foundation_common_voice_7_0_th_test_targets_newmm.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
+ วิธีการ สร้าง ช่อดอกไม้ ที่ กิน ได้
robust-speech-event/log_mozilla-foundation_common_voice_7_0_th_test_targets_syllable.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
+ วิ ธี การ สร้าง ช่อ ดอก ไม้ ที่ กิน ได้
robust-speech-event/mozilla-foundation_common_voice_7_0_th_test_eval_results_deepcut.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ WER: 0.012345679012345678
2
+ CER: 0.0026041666666666665
robust-speech-event/mozilla-foundation_common_voice_7_0_th_test_eval_results_newmm.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ WER: 0.02531645569620253
2
+ CER: 0.005235602094240838
robust-speech-event/mozilla-foundation_common_voice_7_0_th_test_eval_results_syllable.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ WER: 0.009523809523809525
2
+ CER: 0.0024509803921568627