udpated readme
Browse files
README.md
CHANGED
@@ -37,11 +37,44 @@ The model can be used directly (without a language model) as follows:
|
|
37 |
```python
|
38 |
import torch
|
39 |
import torchaudio
|
40 |
-
from datasets import load_dataset
|
41 |
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
processor = Wav2Vec2Processor.from_pretrained("cahya/wav2vec2-large-xlsr-javanese")
|
47 |
model = Wav2Vec2ForCTC.from_pretrained("cahya/wav2vec2-large-xlsr-javanese")
|
@@ -51,15 +84,15 @@ resampler = torchaudio.transforms.Resample(48_000, 16_000)
|
|
51 |
# Preprocessing the datasets.
|
52 |
# We need to read the aduio files as arrays
|
53 |
def speech_file_to_array_fn(batch):
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
|
58 |
test_dataset = test_dataset.map(speech_file_to_array_fn)
|
59 |
inputs = processor(test_dataset["speech"][:2], sampling_rate=16_000, return_tensors="pt", padding=True)
|
60 |
|
61 |
with torch.no_grad():
|
62 |
-
|
63 |
|
64 |
predicted_ids = torch.argmax(logits, dim=-1)
|
65 |
|
@@ -75,11 +108,45 @@ The model can be evaluated as follows on the Indonesian test data of Common Voic
|
|
75 |
```python
|
76 |
import torch
|
77 |
import torchaudio
|
78 |
-
from datasets import load_dataset, load_metric
|
79 |
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
|
80 |
import re
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
|
82 |
-
## TODO: load from OpenSLR https://openslr.org/41/
|
83 |
wer = load_metric("wer")
|
84 |
|
85 |
processor = Wav2Vec2Processor.from_pretrained("cahya/wav2vec2-large-xlsr-javanese")
|
@@ -93,24 +160,24 @@ resampler = torchaudio.transforms.Resample(48_000, 16_000)
|
|
93 |
# Preprocessing the datasets.
|
94 |
# We need to read the aduio files as arrays
|
95 |
def speech_file_to_array_fn(batch):
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
|
101 |
test_dataset = test_dataset.map(speech_file_to_array_fn)
|
102 |
|
103 |
# Preprocessing the datasets.
|
104 |
# We need to read the aduio files as arrays
|
105 |
def evaluate(batch):
|
106 |
-
|
107 |
|
108 |
-
|
109 |
-
|
110 |
|
111 |
pred_ids = torch.argmax(logits, dim=-1)
|
112 |
-
|
113 |
-
|
114 |
|
115 |
result = test_dataset.map(evaluate, batched=True, batch_size=8)
|
116 |
|
@@ -124,4 +191,3 @@ print("WER: {:2f}".format(100 * wer.compute(predictions=result["pred_strings"],
|
|
124 |
The Common Voice `train`, `validation`, and ... datasets were used for training as well as ... and ... # TODO
|
125 |
|
126 |
The script used for training can be found [here](https://github.com/cahya-wirawan/indonesian-speech-recognition)
|
127 |
-
(will be available soon)
|
|
|
37 |
```python
|
38 |
import torch
|
39 |
import torchaudio
|
40 |
+
from datasets import load_dataset, load_metric, Dataset
|
41 |
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
|
42 |
+
from datasets.utils.download_manager import DownloadManager
|
43 |
+
from pathlib import Path
|
44 |
+
import pandas as pd
|
45 |
+
|
46 |
+
def load_dataset_javanese():
|
47 |
+
urls = [
|
48 |
+
"https://www.openslr.org/resources/41/jv_id_female.zip",
|
49 |
+
"https://www.openslr.org/resources/41/jv_id_male.zip"
|
50 |
+
]
|
51 |
+
dm = DownloadManager()
|
52 |
+
download_dirs = dm.download_and_extract(urls)
|
53 |
+
data_dirs = [
|
54 |
+
Path(download_dirs[0])/"jv_id_female/wavs",
|
55 |
+
Path(download_dirs[1])/"jv_id_male/wavs",
|
56 |
+
]
|
57 |
+
filenames = [
|
58 |
+
Path(download_dirs[0])/"jv_id_female/line_index.tsv",
|
59 |
+
Path(download_dirs[1])/"jv_id_male/line_index.tsv",
|
60 |
+
]
|
61 |
+
|
62 |
+
dfs = []
|
63 |
+
dfs.append(pd.read_csv(filenames[0], sep='\t', names=["path", "sentence"]))
|
64 |
+
dfs.append(pd.read_csv(filenames[1], sep='\t', names=["path", "client_id", "sentence"]))
|
65 |
+
dfs[1] = dfs[1].drop(["client_id"], axis=1)
|
66 |
+
|
67 |
+
for i, dir in enumerate(data_dirs):
|
68 |
+
dfs[i]["path"] = dfs[i].apply(lambda row: str(data_dirs[i]) + "/" + row + ".wav", axis=1)
|
69 |
+
df = pd.concat(dfs)
|
70 |
+
# df = df.sample(frac=1, random_state=1).reset_index(drop=True)
|
71 |
+
dataset = Dataset.from_pandas(df)
|
72 |
+
dataset = dataset.remove_columns('__index_level_0__')
|
73 |
+
|
74 |
+
return dataset.train_test_split(test_size=0.1, seed=1)
|
75 |
+
|
76 |
+
dataset = load_dataset_javanese()
|
77 |
+
test_dataset = dataset['test']
|
78 |
|
79 |
processor = Wav2Vec2Processor.from_pretrained("cahya/wav2vec2-large-xlsr-javanese")
|
80 |
model = Wav2Vec2ForCTC.from_pretrained("cahya/wav2vec2-large-xlsr-javanese")
|
|
|
84 |
# Preprocessing the datasets.
|
85 |
# We need to read the aduio files as arrays
|
86 |
def speech_file_to_array_fn(batch):
|
87 |
+
speech_array, sampling_rate = torchaudio.load(batch["path"])
|
88 |
+
batch["speech"] = resampler(speech_array).squeeze().numpy()
|
89 |
+
return batch
|
90 |
|
91 |
test_dataset = test_dataset.map(speech_file_to_array_fn)
|
92 |
inputs = processor(test_dataset["speech"][:2], sampling_rate=16_000, return_tensors="pt", padding=True)
|
93 |
|
94 |
with torch.no_grad():
|
95 |
+
logits = model(inputs.input_values, attention_mask=inputs.attention_mask).logits
|
96 |
|
97 |
predicted_ids = torch.argmax(logits, dim=-1)
|
98 |
|
|
|
108 |
```python
|
109 |
import torch
|
110 |
import torchaudio
|
111 |
+
from datasets import load_dataset, load_metric, Dataset
|
112 |
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
|
113 |
import re
|
114 |
+
from pathlib import Path
|
115 |
+
import pandas as pd
|
116 |
+
|
117 |
+
def load_dataset_javanese():
|
118 |
+
urls = [
|
119 |
+
"https://www.openslr.org/resources/41/jv_id_female.zip",
|
120 |
+
"https://www.openslr.org/resources/41/jv_id_male.zip"
|
121 |
+
]
|
122 |
+
dm = DownloadManager()
|
123 |
+
download_dirs = dm.download_and_extract(urls)
|
124 |
+
data_dirs = [
|
125 |
+
Path(download_dirs[0])/"jv_id_female/wavs",
|
126 |
+
Path(download_dirs[1])/"jv_id_male/wavs",
|
127 |
+
]
|
128 |
+
filenames = [
|
129 |
+
Path(download_dirs[0])/"jv_id_female/line_index.tsv",
|
130 |
+
Path(download_dirs[1])/"jv_id_male/line_index.tsv",
|
131 |
+
]
|
132 |
+
|
133 |
+
dfs = []
|
134 |
+
dfs.append(pd.read_csv(filenames[0], sep='\t', names=["path", "sentence"]))
|
135 |
+
dfs.append(pd.read_csv(filenames[1], sep='\t', names=["path", "client_id", "sentence"]))
|
136 |
+
dfs[1] = dfs[1].drop(["client_id"], axis=1)
|
137 |
+
|
138 |
+
for i, dir in enumerate(data_dirs):
|
139 |
+
dfs[i]["path"] = dfs[i].apply(lambda row: str(data_dirs[i]) + "/" + row + ".wav", axis=1)
|
140 |
+
df = pd.concat(dfs)
|
141 |
+
# df = df.sample(frac=1, random_state=1).reset_index(drop=True)
|
142 |
+
dataset = Dataset.from_pandas(df)
|
143 |
+
dataset = dataset.remove_columns('__index_level_0__')
|
144 |
+
|
145 |
+
return dataset.train_test_split(test_size=0.1, seed=1)
|
146 |
+
|
147 |
+
dataset = load_dataset_javanese()
|
148 |
+
test_dataset = dataset['test']
|
149 |
|
|
|
150 |
wer = load_metric("wer")
|
151 |
|
152 |
processor = Wav2Vec2Processor.from_pretrained("cahya/wav2vec2-large-xlsr-javanese")
|
|
|
160 |
# Preprocessing the datasets.
|
161 |
# We need to read the aduio files as arrays
|
162 |
def speech_file_to_array_fn(batch):
|
163 |
+
batch["sentence"] = re.sub(chars_to_ignore_regex, '', batch["sentence"]).lower()
|
164 |
+
speech_array, sampling_rate = torchaudio.load(batch["path"])
|
165 |
+
batch["speech"] = resampler(speech_array).squeeze().numpy()
|
166 |
+
return batch
|
167 |
|
168 |
test_dataset = test_dataset.map(speech_file_to_array_fn)
|
169 |
|
170 |
# Preprocessing the datasets.
|
171 |
# We need to read the aduio files as arrays
|
172 |
def evaluate(batch):
|
173 |
+
inputs = processor(batch["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
|
174 |
|
175 |
+
with torch.no_grad():
|
176 |
+
logits = model(inputs.input_values.to("cuda"), attention_mask=inputs.attention_mask.to("cuda")).logits
|
177 |
|
178 |
pred_ids = torch.argmax(logits, dim=-1)
|
179 |
+
batch["pred_strings"] = processor.batch_decode(pred_ids)
|
180 |
+
return batch
|
181 |
|
182 |
result = test_dataset.map(evaluate, batched=True, batch_size=8)
|
183 |
|
|
|
191 |
The Common Voice `train`, `validation`, and ... datasets were used for training as well as ... and ... # TODO
|
192 |
|
193 |
The script used for training can be found [here](https://github.com/cahya-wirawan/indonesian-speech-recognition)
|
|