ales commited on
Commit
e4915d1
1 Parent(s): cf499da

update eval code & src readme

Browse files
src/bash_runners/eval_cv11_test.sh CHANGED
@@ -7,4 +7,6 @@ python src/run_eval_whisper_streaming.py \
7
  --text_column="sentence" \
8
  --device="0" \
9
  --batch_size="32" \
10
- --streaming="True"
 
 
 
7
  --text_column="sentence" \
8
  --device="0" \
9
  --batch_size="32" \
10
+ --streaming="True" \
11
+ --push_to_hub="True" \
12
+ --save_predictions="True"
src/bash_runners/eval_fleurs_test.sh CHANGED
@@ -7,4 +7,6 @@ python src/run_eval_whisper_streaming.py \
7
  --text_column="transcription" \
8
  --device="0" \
9
  --batch_size="32" \
10
- --streaming="True"
 
 
 
7
  --text_column="transcription" \
8
  --device="0" \
9
  --batch_size="32" \
10
+ --streaming="True" \
11
+ --push_to_hub="True" \
12
+ --save_predictions="True"
src/readme.md CHANGED
@@ -23,14 +23,6 @@ The code in this repository is a modified version of code from
23
  --logging_steps="50"
24
  --eval_steps="1000"
25
  ```
26
- * on the next run:
27
- * download the whole dataset before the launch.
28
- this will probably save some time for data processing,
29
- and allow to load and prepare data in a parallel fashion
30
- * can also decrease eval batch size. currently it's probably causing GPU to wait for CPU to prepare a next batch
31
- * perform evaluation of fine-tuned model on CommonVoice test set
32
- * add [Whisper fine-tuning Event repo](https://github.com/huggingface/community-events/tree/main/whisper-fine-tuning-event)
33
- to remotes and merge updates from this original event repo
34
  * Learning rate:
35
  * max learning rate is not the same as LR passed as a parameter to training script. it is actually lower.
36
  * when resuming training, LR scheduling behaves incorrectly
@@ -69,6 +61,7 @@ When resuming training from existing checkpoint:
69
  ## Questions:
70
  * What checkpoint (best, I guess) is saved in the `output_dir`?
71
  How is it overwritten when resuming training from existing checkpoint?
 
72
  * does `ShuffleCallback` work with StreamingDataset? it reshuffles data `on_epoch_begin()`,
73
  but does StreamingDataset have any epochs?
74
  * does streaming mode support parallel data load and processing?<br>
@@ -98,6 +91,9 @@ When resuming training from existing checkpoint:
98
  * Log tracking in Jupyter (not working) and in bash (works as expected with `tee`)
99
  * Loggers in `run_speech.....py` do not control `transformers` and `datasets` loggers.
100
  can't redirect their outputs using handlers. it's better and easier to redirect output in a bash
 
 
 
101
  * Need to set `use_cache` to False since we're using gradient checkpointing, and the two are incompatible
102
  * Default Linear scheduler is used
103
  * Default Adam optimizer is used
 
23
  --logging_steps="50"
24
  --eval_steps="1000"
25
  ```
 
 
 
 
 
 
 
 
26
  * Learning rate:
27
  * max learning rate is not the same as LR passed as a parameter to training script. it is actually lower.
28
  * when resuming training, LR scheduling behaves incorrectly
 
61
  ## Questions:
62
  * What checkpoint (best, I guess) is saved in the `output_dir`?
63
  How is it overwritten when resuming training from existing checkpoint?
64
+ * why dataset loading crashes when using `num_proc > 0`?
65
  * does `ShuffleCallback` work with StreamingDataset? it reshuffles data `on_epoch_begin()`,
66
  but does StreamingDataset have any epochs?
67
  * does streaming mode support parallel data load and processing?<br>
 
91
  * Log tracking in Jupyter (not working) and in bash (works as expected with `tee`)
92
  * Loggers in `run_speech.....py` do not control `transformers` and `datasets` loggers.
93
  can't redirect their outputs using handlers. it's better and easier to redirect output in a bash
94
+ * to evaluate on `google/fleurs` dataset had to downgrade `numba` from `0.56.4` to `0.56.3`, then install `librosa`
95
+ (strange, because `librosa` should have been installed when `pip install -r ~/whisper-finetuning-be/requirements.txt`
96
+ was run) and then upgrade back to `numba==0.56.4` because couldn't `import numba` when it was `0.56.3`
97
  * Need to set `use_cache` to False since we're using gradient checkpointing, and the two are incompatible
98
  * Default Linear scheduler is used
99
  * Default Adam optimizer is used
src/run_eval_whisper_streaming.py CHANGED
@@ -2,6 +2,9 @@ import argparse
2
  import logging
3
  import sys
4
  import datetime
 
 
 
5
 
6
  from transformers import pipeline
7
  from transformers.models.whisper.english_normalizer import BasicTextNormalizer
@@ -27,7 +30,7 @@ logger.setLevel(logging.INFO)
27
 
28
 
29
  wer_metric = evaluate.load("wer")
30
- whisper_norm = BelarusianTextNormalizer()
31
 
32
 
33
  def is_target_text_in_range(ref):
@@ -38,18 +41,22 @@ def is_target_text_in_range(ref):
38
 
39
 
40
  def normalise(sample, text_column: str):
41
- sample["norm_text"] = whisper_norm(sample[text_column])
42
  return sample
43
 
44
 
45
- def data(dataset):
46
  for i, item in enumerate(dataset):
47
- yield {**item["audio"], "reference": item["norm_text"]}
 
 
 
 
48
 
49
 
50
  def main(args):
51
  logger.info(f'running evaluation script with following parameters: {args}')
52
- logger.info(f'using following text normalier: {whisper_norm}')
53
 
54
  batch_size = args.batch_size
55
  whisper_asr = pipeline("automatic-speech-recognition", model=args.model_id, device=args.device)
@@ -72,40 +79,64 @@ def main(args):
72
  # Only uncomment for debugging
73
  dataset = dataset.take(args.max_eval_samples)
74
 
 
75
  dataset = dataset.cast_column("audio", Audio(sampling_rate=16000))
76
  dataset = dataset.map(normalise, fn_kwargs=dict(text_column=args.text_column))
77
- dataset = dataset.filter(is_target_text_in_range, input_columns=["norm_text"])
78
 
79
  predictions = []
 
80
  references = []
 
 
81
 
82
  logger.info('running inference')
83
- for out in whisper_asr(data(dataset), batch_size=batch_size):
84
- predictions.append(whisper_norm(out["text"]))
 
85
  references.append(out["reference"][0])
 
 
86
 
87
  logger.info('computing metrics')
88
- wer = wer_metric.compute(references=references, predictions=predictions)
89
  wer = wer * 100
90
 
91
  logger.info('metrics computed')
92
  logger.info(f'WER: {wer}')
93
 
94
- evaluate.push_to_hub(
95
- model_id=args.model_id,
96
-
97
- metric_value=wer,
98
- metric_type="wer",
99
- metric_name="WER",
100
-
101
- dataset_name=args.dataset,
102
- dataset_type=args.dataset,
103
- dataset_config=args.config,
104
- dataset_split=args.split,
105
-
106
- task_type="automatic-speech-recognition",
107
- task_name="Automatic Speech Recognition"
108
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
 
110
 
111
  if __name__ == "__main__":
@@ -171,6 +202,18 @@ if __name__ == "__main__":
171
  required=True,
172
  help="Two letter language code for the transcription language, e.g. use 'en' for English.",
173
  )
 
 
 
 
 
 
 
 
 
 
 
 
174
  args = parser.parse_args()
175
 
176
  main(args)
 
2
  import logging
3
  import sys
4
  import datetime
5
+ import os
6
+
7
+ import pandas as pd
8
 
9
  from transformers import pipeline
10
  from transformers.models.whisper.english_normalizer import BasicTextNormalizer
 
30
 
31
 
32
  wer_metric = evaluate.load("wer")
33
+ text_normalizer = BelarusianTextNormalizer()
34
 
35
 
36
  def is_target_text_in_range(ref):
 
41
 
42
 
43
  def normalise(sample, text_column: str):
44
+ sample["reference_norm"] = text_normalizer(sample[text_column])
45
  return sample
46
 
47
 
48
+ def data(dataset,text_column: str):
49
  for i, item in enumerate(dataset):
50
+ yield {**item["audio"], "reference_norm": item["reference_norm"], 'reference': item[text_column]}
51
+
52
+
53
+ def clean_filename(filename: str):
54
+ return filename.replace(os.path.sep, '_')
55
 
56
 
57
  def main(args):
58
  logger.info(f'running evaluation script with following parameters: {args}')
59
+ logger.info(f'using following text normalier: {text_normalizer}')
60
 
61
  batch_size = args.batch_size
62
  whisper_asr = pipeline("automatic-speech-recognition", model=args.model_id, device=args.device)
 
79
  # Only uncomment for debugging
80
  dataset = dataset.take(args.max_eval_samples)
81
 
82
+ # TODO: probably no need in cast, because pipelien migh handle resampling internally. need to check
83
  dataset = dataset.cast_column("audio", Audio(sampling_rate=16000))
84
  dataset = dataset.map(normalise, fn_kwargs=dict(text_column=args.text_column))
85
+ dataset = dataset.filter(is_target_text_in_range, input_columns=["reference_norm"])
86
 
87
  predictions = []
88
+ predictions_norm = []
89
  references = []
90
+ references_norm = []
91
+ audio_paths = []
92
 
93
  logger.info('running inference')
94
+ for out in whisper_asr(data(dataset, text_column=args.text_column), batch_size=batch_size):
95
+ predictions.append(out["text"])
96
+ predictions_norm.append(text_normalizer(out["text"]))
97
  references.append(out["reference"][0])
98
+ references_norm.append(out["reference_norm"][0])
99
+ audio_paths.append(out['path'][0])
100
 
101
  logger.info('computing metrics')
102
+ wer = wer_metric.compute(references=references_norm, predictions=predictions_norm)
103
  wer = wer * 100
104
 
105
  logger.info('metrics computed')
106
  logger.info(f'WER: {wer}')
107
 
108
+ if args.save_predictions is True:
109
+ preds_fp = f'preds_{args.dataset}_{args.config}_{args.split}_{now_str}.tsv'
110
+ preds_fp = clean_filename(preds_fp)
111
+ logger.info(f'saving predictions to: "{preds_fp}"')
112
+ preds_df = pd.DataFrame({
113
+ 'audio_path': audio_paths,
114
+ 'prediction_norm': predictions_norm, 'reference_norm': references_norm,
115
+ 'prediction': predictions, 'reference': references,
116
+ })
117
+ preds_df.to_csv(preds_fp, sep='\t', index=False)
118
+ else:
119
+ logger.info('save_predictions is False. will not save predictions to a file')
120
+
121
+ if args.push_to_hub is True:
122
+ logger.info(f'updating model card and pushing to HuggingFace Hub')
123
+ evaluate.push_to_hub(
124
+ model_id=args.model_id,
125
+
126
+ metric_value=wer,
127
+ metric_type="wer",
128
+ metric_name="WER",
129
+
130
+ dataset_name=args.dataset,
131
+ dataset_type=args.dataset,
132
+ dataset_config=args.config,
133
+ dataset_split=args.split,
134
+
135
+ task_type="automatic-speech-recognition",
136
+ task_name="Automatic Speech Recognition"
137
+ )
138
+ else:
139
+ logger.info('push_to_hub is False. will not update model card and push to HuggingFace Hub')
140
 
141
 
142
  if __name__ == "__main__":
 
202
  required=True,
203
  help="Two letter language code for the transcription language, e.g. use 'en' for English.",
204
  )
205
+ parser.add_argument(
206
+ '--push_to_hub',
207
+ type=bool,
208
+ default=True,
209
+ help="Whether to update model card and push changes to HuggingFace Hub"
210
+ )
211
+ parser.add_argument(
212
+ '--save_predictions',
213
+ type=bool,
214
+ default=True,
215
+ help="Whether to store predictions and target transcriptions to a file"
216
+ )
217
  args = parser.parse_args()
218
 
219
  main(args)