pere commited on
Commit
710d819
·
1 Parent(s): 0da847f
run_flax_speech_recognition_seq2seq_streaming.py CHANGED
@@ -716,35 +716,33 @@ def main():
716
  return {"wer": wer, "cer": cer}
717
 
718
  # TODO: Sanchit. This part is not essential and a bit verbose.
719
- # We have also considered adding it to Tensorboard logs instead, but it is not so easily browsable.
720
  # We do howver think it is extemely useful info for debugging and improving the model
721
 
722
  def write_predictions(step,eval_samples,eval_metrics, pred_ids, label_ids):
723
  predictions_folder_name = os.path.join(training_args.output_dir,"predictions")
724
  eval_table = f"| STEP| loss | wer |cer|\n| ---| --- | --- |--- |\n| **{step}**| {eval_metrics['loss']:.3f} | {eval_metrics['wer']:.3f} |{eval_metrics['cer']:.3f} |"
725
 
726
- # Put all predictions into a table and calculate wer
727
- inference_df = pd.DataFrame(columns=['wer','target','prediction'])
728
 
729
  idx = 0
730
  for pred,label in zip(pred_ids,label_ids):
731
  pred_text = tokenizer.decode(pred,skip_special_tokens=True)
732
  label_text = tokenizer.decode(label,skip_special_tokens=True)
733
- wer = 100 * metric_wer.compute(predictions=[pred_text], references=[label_text])
734
- wer_formatted = f'{wer:.2f}'
735
- audio_control = f'<audio controls><source src="{os.path.join(predictions_folder_name, "mp3")+"/pred_"+str(idx)+".mp3"}" type="audio/mpeg"></audio>'
736
- new_row = pd.DataFrame({'mp3': audio_control, 'wer': wer_formatted, 'target': label_text, 'prediction': pred_text}, index=[0])
737
  inference_df = pd.concat([inference_df, new_row], ignore_index=True)
738
  idx += 1
739
 
740
  # Create the prediction table of the first N rows
741
- inference_df = inference_df[['mp3', 'wer', 'target','prediction']]
742
  predict_table = inference_df[0:data_args.number_write_predictions].to_markdown(index=False)
743
 
744
  # Build the markdown page
745
  markdown_str = f"{eval_table}\n\n{predict_table}"
746
 
747
-
748
  # Save the stats file
749
  stats_file_name = f"{predictions_folder_name}/step_{step}.md"
750
  with open(stats_file_name, "w") as f:
@@ -1042,7 +1040,7 @@ def main():
1042
  state.params, batch.data, min_device_batch=training_args.per_device_eval_batch_size
1043
  )
1044
  eval_metrics.append(metrics)
1045
- if training_args.predict_with_generate and data_args.number_write_predictions:
1046
  eval_samples.extend(samples['input_features'])
1047
 
1048
  # generation
 
716
  return {"wer": wer, "cer": cer}
717
 
718
  # TODO: Sanchit. This part is not essential and a bit verbose.
 
719
  # We do howver think it is extemely useful info for debugging and improving the model
720
 
721
  def write_predictions(step,eval_samples,eval_metrics, pred_ids, label_ids):
722
  predictions_folder_name = os.path.join(training_args.output_dir,"predictions")
723
  eval_table = f"| STEP| loss | wer |cer|\n| ---| --- | --- |--- |\n| **{step}**| {eval_metrics['loss']:.3f} | {eval_metrics['wer']:.3f} |{eval_metrics['cer']:.3f} |"
724
 
725
+ # Put all predictions into a table
726
+ inference_df = pd.DataFrame(columns=['mp3','target','prediction'])
727
 
728
  idx = 0
729
  for pred,label in zip(pred_ids,label_ids):
730
  pred_text = tokenizer.decode(pred,skip_special_tokens=True)
731
  label_text = tokenizer.decode(label,skip_special_tokens=True)
732
+ #wer = 100 * metric_wer.compute(predictions=[pred_text], references=[label_text])
733
+ #wer_formatted = f'{wer:.2f}'
734
+ audio_control = f'[↓]({os.path.join(predictions_folder_name, "mp3")+"/pred_"+str(idx)+".mp3"})'
735
+ new_row = pd.DataFrame({'mp3': audio_control, 'target': label_text, 'prediction': pred_text}, index=[0])
736
  inference_df = pd.concat([inference_df, new_row], ignore_index=True)
737
  idx += 1
738
 
739
  # Create the prediction table of the first N rows
740
+ inference_df = inference_df[['mp3', 'target','prediction']]
741
  predict_table = inference_df[0:data_args.number_write_predictions].to_markdown(index=False)
742
 
743
  # Build the markdown page
744
  markdown_str = f"{eval_table}\n\n{predict_table}"
745
 
 
746
  # Save the stats file
747
  stats_file_name = f"{predictions_folder_name}/step_{step}.md"
748
  with open(stats_file_name, "w") as f:
 
1040
  state.params, batch.data, min_device_batch=training_args.per_device_eval_batch_size
1041
  )
1042
  eval_metrics.append(metrics)
1043
+ if training_args.predict_with_generate and data_args.number_write_predictions and len(eval_samples)<data_args.number_write_predictions+eval_batch_size:
1044
  eval_samples.extend(samples['input_features'])
1045
 
1046
  # generation