marma commited on
Commit
04c1fce
1 Parent(s): f014c49

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +13 -10
README.md CHANGED
@@ -84,34 +84,37 @@ processor = Wav2Vec2Processor.from_pretrained("KBLab/wav2vec2-large-xlsr-53-swed
84
  model = Wav2Vec2ForCTC.from_pretrained("KBLab/wav2vec2-large-xlsr-53-swedish")
85
  model.to("cuda")
86
 
87
- chars_to_ignore_regex = '[\\\\\\\\,\\\\\\\\?\\\\\\\\.\\\\\\\\!\\\\\\\\-\\\\\\\\;\\\\\\\\:\\\\\\\\"\\\\\\\\“]'
88
  resampler = torchaudio.transforms.Resample(48_000, 16_000)
89
 
90
  # Preprocessing the datasets.
91
  # We need to read the aduio files as arrays
92
  def speech_file_to_array_fn(batch):
93
- \\\\tbatch["sentence"] = re.sub(chars_to_ignore_regex, '', batch["sentence"]).lower()
94
- \\\\tspeech_array, sampling_rate = torchaudio.load(batch["path"])
95
- \\\\tbatch["speech"] = resampler(speech_array).squeeze().numpy()
96
- \\\\treturn batch
 
97
 
98
  test_dataset = test_dataset.map(speech_file_to_array_fn)
99
 
100
  # Preprocessing the datasets.
101
  # We need to read the aduio files as arrays
102
  def evaluate(batch):
103
- \\\\tinputs = processor(batch["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
104
 
105
- \\\\twith torch.no_grad():
106
- \\\\t\\\\tlogits = model(inputs.input_values.to("cuda"), attention_mask=inputs.attention_mask.to("cuda")).logits
107
 
108
  pred_ids = torch.argmax(logits, dim=-1)
109
- \\\\tbatch["pred_strings"] = processor.batch_decode(pred_ids)
110
- \\\\treturn batch
 
111
 
112
  result = test_dataset.map(evaluate, batched=True, batch_size=8)
113
 
114
  print("WER: {:2f}".format(100 * wer.compute(predictions=result["pred_strings"], references=result["sentence"])))
 
115
  ```
116
 
117
  **Test result**: 18.252392%
 
84
  model = Wav2Vec2ForCTC.from_pretrained("KBLab/wav2vec2-large-xlsr-53-swedish")
85
  model.to("cuda")
86
 
87
+ chars_to_ignore_regex = '[,?.!\-;:"]'
88
  resampler = torchaudio.transforms.Resample(48_000, 16_000)
89
 
90
  # Preprocessing the datasets.
91
  # We need to read the aduio files as arrays
92
  def speech_file_to_array_fn(batch):
93
+ batch["sentence"] = re.sub(chars_to_ignore_regex, '', batch["sentence"]).lower()
94
+ speech_array, sampling_rate = torchaudio.load(batch["path"])
95
+ batch["speech"] = resampler(speech_array).squeeze().numpy()
96
+
97
+ return batch
98
 
99
  test_dataset = test_dataset.map(speech_file_to_array_fn)
100
 
101
  # Preprocessing the datasets.
102
  # We need to read the aduio files as arrays
103
  def evaluate(batch):
104
+ inputs = processor(batch["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
105
 
106
+ with torch.no_grad():
107
+ logits = model(inputs.input_values.to("cuda"), attention_mask=inputs.attention_mask.to("cuda")).logits
108
 
109
  pred_ids = torch.argmax(logits, dim=-1)
110
+ batch["pred_strings"] = processor.batch_decode(pred_ids)
111
+
112
+ return batch
113
 
114
  result = test_dataset.map(evaluate, batched=True, batch_size=8)
115
 
116
  print("WER: {:2f}".format(100 * wer.compute(predictions=result["pred_strings"], references=result["sentence"])))
117
+ print("CER: {:2f}".format(100 * wer.compute(predictions=[" ".join(list(entry)) for entry in result["pred_strings"]], references=[" ".join(list(entry)) for entry in result["sentence"]])))
118
  ```
119
 
120
  **Test result**: 18.252392%