anuragshas commited on
Commit
4fa60f6
β€’
1 Parent(s): 47dba72

Update eval.py

Browse files
Files changed (1) hide show
  1. eval.py +51 -16
eval.py CHANGED
@@ -3,6 +3,7 @@ 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
@@ -19,8 +20,12 @@ def log_results(result: Dataset, args: Dict[str, str]):
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}"
@@ -49,12 +54,12 @@ def log_results(result: Dataset, args: Dict[str, str]):
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
- text = re.sub('’ ',' ',text)
56
- text = re.sub(' β€˜',' ',text)
57
- text = re.sub('’|β€˜','\'',text)
58
 
59
  # In addition, we can normalize the target text, e.g. removing new lines characters etc...
60
  # note that order is important here!
@@ -68,7 +73,9 @@ def normalize_text(text: str) -> str:
68
 
69
  def main(args):
70
  # load dataset
71
- dataset = load_dataset(args.dataset, args.config, split=args.split, use_auth_token=True)
 
 
72
 
73
  # for testing: only process the first two examples as a test
74
  # dataset = dataset.select(range(10))
@@ -81,12 +88,18 @@ def main(args):
81
  dataset = dataset.cast_column("audio", Audio(sampling_rate=sampling_rate))
82
 
83
  # load eval pipeline
84
- asr = pipeline("automatic-speech-recognition", model=args.model_id, device=0)
 
 
 
 
85
 
86
  # map function to decode audio
87
  def map_to_pred(batch):
88
  prediction = asr(
89
- batch["audio"]["array"], chunk_length_s=args.chunk_length_s, stride_length_s=args.stride_length_s
 
 
90
  )
91
 
92
  batch["prediction"] = prediction["text"]
@@ -105,7 +118,10 @@ if __name__ == "__main__":
105
  parser = argparse.ArgumentParser()
106
 
107
  parser.add_argument(
108
- "--model_id", type=str, required=True, help="Model identifier. Should be loadable with πŸ€— Transformers"
 
 
 
109
  )
110
  parser.add_argument(
111
  "--dataset",
@@ -114,18 +130,37 @@ if __name__ == "__main__":
114
  help="Dataset name to evaluate the `model_id`. Should be loadable with πŸ€— Datasets",
115
  )
116
  parser.add_argument(
117
- "--config", type=str, required=True, help="Config of the dataset. *E.g.* `'en'` for Common Voice"
 
 
 
 
 
 
 
 
 
 
 
 
118
  )
119
- parser.add_argument("--split", type=str, required=True, help="Split of the dataset. *E.g.* `'test'`")
120
  parser.add_argument(
121
- "--chunk_length_s", type=float, default=None, help="Chunk length in seconds. Defaults to 5 seconds."
 
 
 
122
  )
123
  parser.add_argument(
124
- "--stride_length_s", type=float, default=None, help="Stride of the audio chunks. Defaults to 1 second."
 
 
125
  )
126
  parser.add_argument(
127
- "--log_outputs", action="store_true", help="If defined, write outputs to log file for analysis."
 
 
 
128
  )
129
  args = parser.parse_args()
130
 
131
- main(args)
 
3
  import re
4
  from typing import Dict
5
 
6
+ import torch
7
  from datasets import Audio, Dataset, load_dataset, load_metric
8
 
9
  from transformers import AutoFeatureExtractor, pipeline
 
20
  cer = load_metric("cer")
21
 
22
  # compute metrics
23
+ wer_result = wer.compute(
24
+ references=result["target"], predictions=result["prediction"]
25
+ )
26
+ cer_result = cer.compute(
27
+ references=result["target"], predictions=result["prediction"]
28
+ )
29
 
30
  # print & log results
31
  result_str = f"WER: {wer_result}\n" f"CER: {cer_result}"
 
54
  def normalize_text(text: str) -> str:
55
  """DO ADAPT FOR YOUR USE CASE. this function normalizes the target text."""
56
 
57
+ chars_to_ignore_regex = """[\ΰ₯€\!\"\,\-\.\?\:\|\β€œ\”]""" # noqa: W605 IMPORTANT: this should correspond to the chars that were ignored during training
58
 
59
  text = re.sub(chars_to_ignore_regex, "", text.lower())
60
+ text = re.sub("’ ", " ", text)
61
+ text = re.sub(" β€˜", " ", text)
62
+ text = re.sub("’|β€˜", "'", text)
63
 
64
  # In addition, we can normalize the target text, e.g. removing new lines characters etc...
65
  # note that order is important here!
 
73
 
74
  def main(args):
75
  # load dataset
76
+ dataset = load_dataset(
77
+ args.dataset, args.config, split=args.split, use_auth_token=True
78
+ )
79
 
80
  # for testing: only process the first two examples as a test
81
  # dataset = dataset.select(range(10))
 
88
  dataset = dataset.cast_column("audio", Audio(sampling_rate=sampling_rate))
89
 
90
  # load eval pipeline
91
+ if args.device is None:
92
+ args.device = 0 if torch.cuda.is_available() else -1
93
+ asr = pipeline(
94
+ "automatic-speech-recognition", model=args.model_id, device=args.device
95
+ )
96
 
97
  # map function to decode audio
98
  def map_to_pred(batch):
99
  prediction = asr(
100
+ batch["audio"]["array"],
101
+ chunk_length_s=args.chunk_length_s,
102
+ stride_length_s=args.stride_length_s,
103
  )
104
 
105
  batch["prediction"] = prediction["text"]
 
118
  parser = argparse.ArgumentParser()
119
 
120
  parser.add_argument(
121
+ "--model_id",
122
+ type=str,
123
+ required=True,
124
+ help="Model identifier. Should be loadable with πŸ€— Transformers",
125
  )
126
  parser.add_argument(
127
  "--dataset",
 
130
  help="Dataset name to evaluate the `model_id`. Should be loadable with πŸ€— Datasets",
131
  )
132
  parser.add_argument(
133
+ "--config",
134
+ type=str,
135
+ required=True,
136
+ help="Config of the dataset. *E.g.* `'en'` for Common Voice",
137
+ )
138
+ parser.add_argument(
139
+ "--split", type=str, required=True, help="Split of the dataset. *E.g.* `'test'`"
140
+ )
141
+ parser.add_argument(
142
+ "--chunk_length_s",
143
+ type=float,
144
+ default=None,
145
+ help="Chunk length in seconds. Defaults to 5 seconds.",
146
  )
 
147
  parser.add_argument(
148
+ "--stride_length_s",
149
+ type=float,
150
+ default=None,
151
+ help="Stride of the audio chunks. Defaults to 1 second.",
152
  )
153
  parser.add_argument(
154
+ "--log_outputs",
155
+ action="store_true",
156
+ help="If defined, write outputs to log file for analysis.",
157
  )
158
  parser.add_argument(
159
+ "--device",
160
+ type=int,
161
+ default=None,
162
+ help="The device to run the pipeline on. -1 for CPU (default), 0 for the first GPU and so on.",
163
  )
164
  args = parser.parse_args()
165
 
166
+ main(args)