DeividasM commited on
Commit
eb2a9f7
1 Parent(s): de9d076

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +15 -15
README.md CHANGED
@@ -68,7 +68,7 @@ from datasets import load_dataset
68
 
69
  from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
70
 
71
- test_dataset = load_dataset("common_voice", "lt", split="test[:2%]").
72
 
73
  processor = Wav2Vec2Processor.from_pretrained("DeividasM/wav2vec2-large-xlsr-53-lithuanian")
74
 
@@ -82,11 +82,11 @@ resampler = torchaudio.transforms.Resample(48_000, 16_000)
82
 
83
  def speech_file_to_array_fn(batch):
84
 
85
- speech_array, sampling_rate = torchaudio.load(batch["path"])
86
 
87
- batch["speech"] = resampler(speech_array).squeeze().numpy()
88
 
89
- return batch
90
 
91
  test_dataset = test_dataset.map(speech_file_to_array_fn)
92
 
@@ -94,7 +94,7 @@ inputs = processor(test_dataset["speech"][:2], sampling_rate=16_000, return_tens
94
 
95
  with torch.no_grad():
96
 
97
- logits = model(inputs.input_values, attention_mask=inputs.attention_mask).logits
98
 
99
  predicted_ids = torch.argmax(logits, dim=-1)
100
 
@@ -130,7 +130,7 @@ model = Wav2Vec2ForCTC.from_pretrained("DeividasM/wav2vec2-large-xlsr-53-lithuan
130
 
131
  model.to("cuda")
132
 
133
- chars_to_ignore_regex = '[\,\?\.\!\-\;\:\"\“]'
134
 
135
  resampler = torchaudio.transforms.Resample(48_000, 16_000)
136
 
@@ -140,13 +140,13 @@ resampler = torchaudio.transforms.Resample(48_000, 16_000)
140
 
141
  def speech_file_to_array_fn(batch):
142
 
143
- batch["sentence"] = re.sub(chars_to_ignore_regex, '', batch["sentence"]).lower()
144
 
145
- speech_array, sampling_rate = torchaudio.load(batch["path"])
146
 
147
- batch["speech"] = resampler(speech_array).squeeze().numpy()
148
 
149
- return batch
150
 
151
  test_dataset = test_dataset.map(speech_file_to_array_fn)
152
 
@@ -156,17 +156,17 @@ test_dataset = test_dataset.map(speech_file_to_array_fn)
156
 
157
  def evaluate(batch):
158
 
159
- inputs = processor(batch["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
160
 
161
- with torch.no_grad():
162
 
163
- logits = model(inputs.input_values.to("cuda"), attention_mask=inputs.attention_mask.to("cuda")).logits
164
 
165
  pred_ids = torch.argmax(logits, dim=-1)
166
 
167
- batch["pred_strings"] = processor.batch_decode(pred_ids)
168
 
169
- return batch
170
 
171
  result = test_dataset.map(evaluate, batched=True, batch_size=8)
172
 
 
68
 
69
  from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
70
 
71
+ test_dataset = load_dataset("common_voice", "lt", split="test[:2%]")
72
 
73
  processor = Wav2Vec2Processor.from_pretrained("DeividasM/wav2vec2-large-xlsr-53-lithuanian")
74
 
 
82
 
83
  def speech_file_to_array_fn(batch):
84
 
85
+ \tspeech_array, sampling_rate = torchaudio.load(batch["path"])
86
 
87
+ \tbatch["speech"] = resampler(speech_array).squeeze().numpy()
88
 
89
+ \treturn batch
90
 
91
  test_dataset = test_dataset.map(speech_file_to_array_fn)
92
 
 
94
 
95
  with torch.no_grad():
96
 
97
+ \tlogits = model(inputs.input_values, attention_mask=inputs.attention_mask).logits
98
 
99
  predicted_ids = torch.argmax(logits, dim=-1)
100
 
 
130
 
131
  model.to("cuda")
132
 
133
+ chars_to_ignore_regex = '[\\,\\?\\.\\!\\-\\;\\:\\"\\“]'
134
 
135
  resampler = torchaudio.transforms.Resample(48_000, 16_000)
136
 
 
140
 
141
  def speech_file_to_array_fn(batch):
142
 
143
+ \tbatch["sentence"] = re.sub(chars_to_ignore_regex, '', batch["sentence"]).lower()
144
 
145
+ \tspeech_array, sampling_rate = torchaudio.load(batch["path"])
146
 
147
+ \tbatch["speech"] = resampler(speech_array).squeeze().numpy()
148
 
149
+ \treturn batch
150
 
151
  test_dataset = test_dataset.map(speech_file_to_array_fn)
152
 
 
156
 
157
  def evaluate(batch):
158
 
159
+ \tinputs = processor(batch["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
160
 
161
+ \twith torch.no_grad():
162
 
163
+ \t\tlogits = model(inputs.input_values.to("cuda"), attention_mask=inputs.attention_mask.to("cuda")).logits
164
 
165
  pred_ids = torch.argmax(logits, dim=-1)
166
 
167
+ \tbatch["pred_strings"] = processor.batch_decode(pred_ids)
168
 
169
+ \treturn batch
170
 
171
  result = test_dataset.map(evaluate, batched=True, batch_size=8)
172