jonatasgrosman commited on
Commit
de62d73
1 Parent(s): b3d28eb

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +16 -16
README.md CHANGED
@@ -52,17 +52,17 @@ model = Wav2Vec2ForCTC.from_pretrained(MODEL_ID)
52
  resampler = torchaudio.transforms.Resample(48_000, 16_000)
53
 
54
  # Preprocessing the datasets.
55
- # We need to read the aduio files as arrays
56
  def speech_file_to_array_fn(batch):
57
- speech_array, sampling_rate = torchaudio.load(batch["path"])
58
- batch["speech"] = resampler(speech_array).squeeze().numpy()
59
- return batch
60
 
61
  test_dataset = test_dataset.map(speech_file_to_array_fn)
62
  inputs = processor(test_dataset["speech"][:2], sampling_rate=16_000, return_tensors="pt", padding=True)
63
 
64
  with torch.no_grad():
65
- logits = model(inputs.input_values, attention_mask=inputs.attention_mask).logits
66
 
67
  predicted_ids = torch.argmax(logits, dim=-1)
68
 
@@ -73,7 +73,7 @@ print("Reference:", test_dataset["sentence"][:2])
73
 
74
  ## Evaluation
75
 
76
- The model can be evaluated as follows on the finnish test data of Common Voice.
77
 
78
  ```python
79
  import torch
@@ -88,7 +88,7 @@ MODEL_ID = "jonatasgrosman/wav2vec2-large-xlsr-53-finnish"
88
  DEVICE = "cuda"
89
 
90
  CHARS_TO_IGNORE = [",", "?", ".", "!", "-", ";", ":", '""', "%", "'", '"', "�", "·", "჻", "¿", "¡", "~", "՞", "؟", "،", "।", "॥", "«", "»", "„", "“", "”", "「", "」", "‘", "’", "《", "》"]
91
- CURRENCY_SYMBOLS = ["$", "£", "€", "¥", "₩", "₹", "₽", "₱", "₦", "₼", "ლ", "₭", "₴", "₲", "₫", "₡", "₵", "₿", "฿", "¢"]
92
 
93
  test_dataset = load_dataset("common_voice", LANG_ID, split="test")
94
  wer = load_metric("wer")
@@ -98,7 +98,7 @@ if LANG_ID in hg.Languages.get_all():
98
  # creating regex to match language specific non valid characters
99
  alphabet = list(hg.Languages.get_alphabet([LANG_ID]))
100
  valid_chars = alphabet + CURRENCY_SYMBOLS
101
- unk_regex = "[^"+re.escape("".join(valid_chars))+"\s\d]"
102
 
103
  chars_to_ignore_regex = f'[{re.escape("".join(CHARS_TO_IGNORE))}]'
104
 
@@ -109,7 +109,7 @@ model.to(DEVICE)
109
  resampler = torchaudio.transforms.Resample(48_000, 16_000)
110
 
111
  # Preprocessing the datasets.
112
- # We need to read the aduio files as arrays
113
  def speech_file_to_array_fn(batch):
114
  batch["sentence"] = re.sub(chars_to_ignore_regex, '', batch["sentence"]).lower()
115
  if unk_regex is not None:
@@ -121,16 +121,16 @@ def speech_file_to_array_fn(batch):
121
  test_dataset = test_dataset.map(speech_file_to_array_fn)
122
 
123
  # Preprocessing the datasets.
124
- # We need to read the aduio files as arrays
125
  def evaluate(batch):
126
- inputs = processor(batch["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
127
 
128
- with torch.no_grad():
129
- logits = model(inputs.input_values.to(DEVICE), attention_mask=inputs.attention_mask.to(DEVICE)).logits
130
 
131
- pred_ids = torch.argmax(logits, dim=-1)
132
- batch["pred_strings"] = processor.batch_decode(pred_ids)
133
- return batch
134
 
135
  result = test_dataset.map(evaluate, batched=True, batch_size=8)
136
 
 
52
  resampler = torchaudio.transforms.Resample(48_000, 16_000)
53
 
54
  # Preprocessing the datasets.
55
+ # We need to read the audio files as arrays
56
  def speech_file_to_array_fn(batch):
57
+ \tspeech_array, sampling_rate = torchaudio.load(batch["path"])
58
+ \tbatch["speech"] = resampler(speech_array).squeeze().numpy()
59
+ \treturn batch
60
 
61
  test_dataset = test_dataset.map(speech_file_to_array_fn)
62
  inputs = processor(test_dataset["speech"][:2], sampling_rate=16_000, return_tensors="pt", padding=True)
63
 
64
  with torch.no_grad():
65
+ \tlogits = model(inputs.input_values, attention_mask=inputs.attention_mask).logits
66
 
67
  predicted_ids = torch.argmax(logits, dim=-1)
68
 
 
73
 
74
  ## Evaluation
75
 
76
+ The model can be evaluated as follows on the Finnish test data of Common Voice.
77
 
78
  ```python
79
  import torch
 
88
  DEVICE = "cuda"
89
 
90
  CHARS_TO_IGNORE = [",", "?", ".", "!", "-", ";", ":", '""', "%", "'", '"', "�", "·", "჻", "¿", "¡", "~", "՞", "؟", "،", "।", "॥", "«", "»", "„", "“", "”", "「", "」", "‘", "’", "《", "》"]
91
+ CURRENCY_SYMBOLS = ["{{%htmlContent%}}quot;, "£", "€", "¥", "₩", "₹", "₽", "₱", "₦", "₼", "ლ", "₭", "₴", "₲", "₫", "₡", "₵", "₿", "฿", "¢"]
92
 
93
  test_dataset = load_dataset("common_voice", LANG_ID, split="test")
94
  wer = load_metric("wer")
 
98
  # creating regex to match language specific non valid characters
99
  alphabet = list(hg.Languages.get_alphabet([LANG_ID]))
100
  valid_chars = alphabet + CURRENCY_SYMBOLS
101
+ unk_regex = "[^"+re.escape("".join(valid_chars))+"\\s\\d]"
102
 
103
  chars_to_ignore_regex = f'[{re.escape("".join(CHARS_TO_IGNORE))}]'
104
 
 
109
  resampler = torchaudio.transforms.Resample(48_000, 16_000)
110
 
111
  # Preprocessing the datasets.
112
+ # We need to read the audio files as arrays
113
  def speech_file_to_array_fn(batch):
114
  batch["sentence"] = re.sub(chars_to_ignore_regex, '', batch["sentence"]).lower()
115
  if unk_regex is not None:
 
121
  test_dataset = test_dataset.map(speech_file_to_array_fn)
122
 
123
  # Preprocessing the datasets.
124
+ # We need to read the audio files as arrays
125
  def evaluate(batch):
126
+ \tinputs = processor(batch["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
127
 
128
+ \twith torch.no_grad():
129
+ \t\tlogits = model(inputs.input_values.to(DEVICE), attention_mask=inputs.attention_mask.to(DEVICE)).logits
130
 
131
+ \tpred_ids = torch.argmax(logits, dim=-1)
132
+ \tbatch["pred_strings"] = processor.batch_decode(pred_ids)
133
+ \treturn batch
134
 
135
  result = test_dataset.map(evaluate, batched=True, batch_size=8)
136