sdelangen commited on
Commit
928cd6b
1 Parent(s): 1576489

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +332 -0
README.md ADDED
@@ -0,0 +1,332 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ inference: false # need to implement streaming API in HF
3
+ language:
4
+ - en
5
+ thumbnail: null
6
+ tags:
7
+ - automatic-speech-recognition
8
+ - Transducer
9
+ - Conformer
10
+ - pytorch
11
+ - speechbrain
12
+ license: apache-2.0
13
+ datasets:
14
+ - speechcolab/gigaspeech
15
+ metrics:
16
+ - wer
17
+ model-index:
18
+ - name: Conformer-Transducer by SpeechBrain
19
+ results:
20
+ - task:
21
+ name: Automatic Speech Recognition
22
+ type: automatic-speech-recognition
23
+ dataset:
24
+ name: GigaSpeech
25
+ type: gigaspeech
26
+ split: test
27
+ args:
28
+ language: en
29
+ metrics:
30
+ - name: Test WER (non-streaming greedy)
31
+ type: wer
32
+ value: TBD
33
+ - name: Test WER (960ms chunk size, 8 left context chunks)
34
+ type: wer
35
+ value: TBD
36
+ ---
37
+
38
+ <iframe src="https://ghbtns.com/github-btn.html?user=speechbrain&repo=speechbrain&type=star&count=true&size=large&v=2" frameborder="0" scrolling="0" width="170" height="30" title="GitHub"></iframe>
39
+ <br/><br/>
40
+
41
+ # Conformer for Gigaspeech
42
+
43
+ This repository provides all the necessary tools to perform automatic speech
44
+ recognition from an end-to-end system pretrained on Gigaspeech (EN, XL split) within
45
+ SpeechBrain. For a better experience, we encourage you to learn more about
46
+ [SpeechBrain](https://speechbrain.github.io).
47
+ The performance of the model in full context mode (no streaming) is the following:
48
+
49
+ | Release | Test WER | GPUs |
50
+ |:-------------:|:--------------:|:--------:|
51
+ | 24-02-26 | TBD | 4xA100 40GB |
52
+
53
+ With streaming, the results with different chunk sizes on test-clean are the following:
54
+
55
+ | | full | cs=32 (1280ms) | 24 (960ms) | 16 (640ms) | 12 (480ms) | 8 (320ms) |
56
+ |:-----:|:----:|:-----:|:-----:|:-----:|:-----:|:-----:|
57
+ | full | TBD | - | - | - | - | - |
58
+ | lc=32 | - | TBD | TBD | TBD | TBD | TBD |
59
+ | 16 | - | TBD | TBD | TBD | TBD | TBD |
60
+ | 8 | - | TBD | TBD | TBD | TBD | TBD |
61
+ | 4 | - | TBD | TBD | TBD | TBD | TBD |
62
+ | 2 | - | TBD | TBD | TBD | TBD | TBD |
63
+
64
+ ## Pipeline description
65
+
66
+ This ASR system is a Conformer model trained with the RNN-T loss (with an auxiliary CTC loss to stabilize training). The model operates with a unigram tokenizer.
67
+ Architecture details are described in the [training hyperparameters file](https://github.com/speechbrain/speechbrain/blob/develop/recipes/GigaSpeech/ASR/transducer/hparams/conformer_transducer.yaml).
68
+
69
+ Streaming support makes use of Dynamic Chunk Training. Chunked attention is used for the multi-head attention module, and an implementation of [Dynamic Chunk Convolutions](https://www.amazon.science/publications/dynamic-chunk-convolution-for-unified-streaming-and-non-streaming-conformer-asr) was used.
70
+ The model was trained with support for different chunk sizes (and even full context), and so is suitable for various chunk sizes and offline transcription.
71
+
72
+ The system is trained with recordings sampled at 16kHz (single channel).
73
+ The code will automatically normalize your audio (i.e., resampling + mono channel selection) when calling `transcribe_file` if needed.
74
+
75
+ ## Install SpeechBrain
76
+
77
+ First of all, please install SpeechBrain with the following command:
78
+
79
+ ```
80
+ pip install speechbrain
81
+ ```
82
+
83
+ Please notice that we encourage you to read our tutorials and learn more about
84
+ [SpeechBrain](https://speechbrain.github.io).
85
+
86
+ ### Transcribing your own audio files (in English)
87
+
88
+ ```python
89
+ from speechbrain.inference.ASR import StreamingASR
90
+ from speechbrain.utils.dynamic_chunk_training import DynChunkTrainConfig
91
+ asr_model = StreamingASR.from_hparams("speechbrain/asr-streaming-conformer-gigaspeech")
92
+ asr_model.transcribe_file(
93
+ "speechbrain/asr-streaming-conformer-librispeech/test-en.wav",
94
+ # select a chunk size of ~960ms with 4 chunks of left context
95
+ DynChunkTrainConfig(24, 4),
96
+ # disable torchaudio streaming to allow fetching from HuggingFace
97
+ # set this to True for your own files or streams to allow for streaming file decoding
98
+ use_torchaudio_streaming=False,
99
+ )
100
+ ```
101
+
102
+ The `DynChunkTrainConfig` values can be adjusted for a tradeoff of latency, computational power and transcription accuracy. Refer to the streaming WER table to pick a value that is suitable for your usecase.
103
+
104
+ <details>
105
+ <summary>Commandline tool to transcribe a file or a live stream</summary>
106
+
107
+ **Decoding from a live stream using ffmpeg (BBC Radio 4):**
108
+
109
+ `python3 asr.py http://as-hls-ww-live.akamaized.net/pool_904/live/ww/bbc_radio_fourfm/bbc_radio_fourfm.isml/bbc_radio_fourfm-audio%3d96000.norewind.m3u8 --model-source=speechbrain/asr-streaming-conformer-librispeech --device=cpu -v`
110
+
111
+ **Decoding from a file:**
112
+
113
+ `python3 asr.py some-english-speech.wav --model-source=speechbrain/asr-streaming-conformer-librispeech --device=cpu -v`
114
+
115
+ ```python
116
+ from argparse import ArgumentParser
117
+ import logging
118
+
119
+ parser = ArgumentParser()
120
+ parser.add_argument("audio_path")
121
+ parser.add_argument("--model-source", required=True)
122
+ parser.add_argument("--device", default="cpu")
123
+ parser.add_argument("--ip", default="127.0.0.1")
124
+ parser.add_argument("--port", default=9431)
125
+ parser.add_argument("--chunk-size", default=24, type=int)
126
+ parser.add_argument("--left-context-chunks", default=4, type=int)
127
+ parser.add_argument("--num-threads", default=None, type=int)
128
+ parser.add_argument("--verbose", "-v", default=False, action="store_true")
129
+ args = parser.parse_args()
130
+
131
+ if args.verbose:
132
+ logging.getLogger().setLevel(logging.INFO)
133
+
134
+ logging.info("Loading libraries")
135
+
136
+ from speechbrain.inference.ASR import StreamingASR
137
+ from speechbrain.utils.dynamic_chunk_training import DynChunkTrainConfig
138
+ import torch
139
+
140
+ device = args.device
141
+
142
+ if args.num_threads is not None:
143
+ torch.set_num_threads(args.num_threads)
144
+
145
+ logging.info(f"Loading model from \"{args.model_source}\" onto device {device}")
146
+
147
+ asr = StreamingASR.from_hparams(args.model_source, run_opts={"device": device})
148
+ config = DynChunkTrainConfig(args.chunk_size, args.left_context_chunks)
149
+
150
+ logging.info(f"Starting stream from URI \"{args.audio_path}\"")
151
+
152
+ for text_chunk in asr.transcribe_file_streaming(args.audio_path, config):
153
+ print(text_chunk, flush=True, end="")
154
+ ```
155
+ </details>
156
+
157
+ <details>
158
+ <summary>Live ASR decoding from a browser using Gradio</summary>
159
+
160
+ We want to optimize some things around the model before we create a proper HuggingFace space demonstrating live streaming on CPU.
161
+
162
+ In the mean time, this is a simple hacky demo of live ASR in the browser using Gradio's live microphone streaming feature.
163
+
164
+ If you run this, please note:
165
+
166
+ - Modern browsers refuse to stream microphone input over an untrusted connection (plain HTTP), unless it is localhost. If you are running this on a remote server, you could use SSH port forwarding to expose the remote's port on your machine.
167
+ - Streaming using Gradio on Firefox seems to cause some issues. Chromium-based browsers seem to behave better.
168
+
169
+ Run using:
170
+
171
+ `python3 gradio-asr.py --model-source speechbrain/asr-streaming-conformer-gigaspeech --ip=localhost --device=cpu`
172
+
173
+ ```python
174
+ from argparse import ArgumentParser
175
+ from dataclasses import dataclass
176
+ import logging
177
+
178
+ parser = ArgumentParser()
179
+ parser.add_argument("--model-source", required=True)
180
+ parser.add_argument("--device", default="cpu")
181
+ parser.add_argument("--ip", default="127.0.0.1")
182
+ parser.add_argument("--port", default=9431)
183
+ parser.add_argument("--chunk-size", default=24, type=int)
184
+ parser.add_argument("--left-context-chunks", default=4, type=int)
185
+ parser.add_argument("--num-threads", default=None, type=int)
186
+ parser.add_argument("--verbose", "-v", default=False, action="store_true")
187
+ args = parser.parse_args()
188
+
189
+ if args.verbose:
190
+ logging.getLogger().setLevel(logging.INFO)
191
+
192
+ logging.info("Loading libraries")
193
+
194
+ from speechbrain.inference.ASR import StreamingASR, ASRStreamingContext
195
+ from speechbrain.utils.dynamic_chunk_training import DynChunkTrainConfig
196
+ import torch
197
+ import gradio as gr
198
+ import torchaudio
199
+ import numpy as np
200
+
201
+ device = args.device
202
+
203
+ if args.num_threads is not None:
204
+ torch.set_num_threads(args.num_threads)
205
+
206
+ logging.info(f"Loading model from \"{args.model_source}\" onto device {device}")
207
+
208
+ asr = StreamingASR.from_hparams(args.model_source, run_opts={"device": device})
209
+ config = DynChunkTrainConfig(args.chunk_size, args.left_context_chunks)
210
+
211
+ @dataclass
212
+ class GradioStreamingContext:
213
+ context: ASRStreamingContext
214
+ chunk_size: int
215
+ waveform_buffer: torch.Tensor
216
+ decoded_text: str
217
+
218
+ def transcribe(stream, new_chunk):
219
+ sr, y = new_chunk
220
+
221
+ y = y.astype(np.float32)
222
+ y = torch.tensor(y, dtype=torch.float32, device=device)
223
+ y /= max(1, torch.max(torch.abs(y)).item()) # norm by max abs() within chunk & avoid NaN
224
+ if len(y.shape) > 1:
225
+ y = torch.mean(y, dim=1) # downmix to mono
226
+
227
+ # HACK: we are making poor use of the resampler across chunk boundaries
228
+ # which may degrade accuracy.
229
+ # NOTE: we should also absolutely avoid recreating a resampler every time
230
+ resampler = torchaudio.transforms.Resample(orig_freq=sr, new_freq=asr.audio_normalizer.sample_rate).to(device)
231
+ y = resampler(y) # janky resample (probably to 16kHz)
232
+
233
+
234
+ if stream is None:
235
+ stream = GradioStreamingContext(
236
+ context=asr.make_streaming_context(config),
237
+ chunk_size=asr.get_chunk_size_frames(config),
238
+ waveform_buffer=y,
239
+ decoded_text="",
240
+ )
241
+ else:
242
+ stream.waveform_buffer = torch.concat((stream.waveform_buffer, y))
243
+
244
+ while stream.waveform_buffer.size(0) > stream.chunk_size:
245
+ chunk = stream.waveform_buffer[:stream.chunk_size]
246
+ stream.waveform_buffer = stream.waveform_buffer[stream.chunk_size:]
247
+
248
+ # fake batch dim
249
+ chunk = chunk.unsqueeze(0)
250
+
251
+ # list of transcribed strings, of size 1 because the batch size is 1
252
+ with torch.no_grad():
253
+ transcribed = asr.transcribe_chunk(stream.context, chunk)
254
+ stream.decoded_text += transcribed[0]
255
+
256
+ return stream, stream.decoded_text
257
+
258
+ # NOTE: latency seems relatively high, which may be due to this:
259
+ # https://github.com/gradio-app/gradio/issues/6526
260
+
261
+ demo = gr.Interface(
262
+ transcribe,
263
+ ["state", gr.Audio(sources=["microphone"], streaming=True)],
264
+ ["state", "text"],
265
+ live=True,
266
+ )
267
+
268
+ demo.launch(server_name=args.ip, server_port=args.port)
269
+ ```
270
+
271
+ </details>
272
+
273
+ ### Inference on GPU
274
+
275
+ To perform inference on the GPU, add `run_opts={"device":"cuda"}` when calling the `from_hparams` method.
276
+
277
+ ## Parallel Inference on a Batch
278
+
279
+ Currently, the high level transcription interfaces do not support batched inference, but the low-level interfaces (i.e. `encode_chunk`) do.
280
+ We hope to provide efficient functionality for this in the future.
281
+
282
+ ### Training
283
+
284
+ The model was trained with SpeechBrain `v1.0.2`.
285
+ To train it from scratch, follow these steps:
286
+ 1. Clone SpeechBrain:
287
+ ```bash
288
+ git clone https://github.com/speechbrain/speechbrain/
289
+ ```
290
+ 2. Install it:
291
+ ```bash
292
+ cd speechbrain
293
+ pip install -r requirements.txt
294
+ pip install -e .
295
+ ```
296
+
297
+ 3. Follow the steps listed in the [README](https://github.com/speechbrain/speechbrain/blob/develop/recipes/GigaSpeech/ASR/transducer/README.md) for this recipe.
298
+
299
+ ### Limitations
300
+ The SpeechBrain team does not provide any warranty on the performance achieved by this model when used on other datasets.
301
+
302
+ # **About SpeechBrain**
303
+ - Website: https://speechbrain.github.io/
304
+ - Code: https://github.com/speechbrain/speechbrain/
305
+ - HuggingFace: https://huggingface.co/speechbrain/
306
+
307
+
308
+ # **Citing SpeechBrain**
309
+ Please, cite SpeechBrain if you use it for your research or business.
310
+
311
+
312
+ ```bibtex
313
+ @misc{speechbrainV1,
314
+ title={Open-Source Conversational AI with SpeechBrain 1.0},
315
+ author={Mirco Ravanelli and Titouan Parcollet and Adel Moumen and Sylvain de Langen and Cem Subakan and Peter Plantinga and Yingzhi Wang and Pooneh Mousavi and Luca Della Libera and Artem Ploujnikov and Francesco Paissan and Davide Borra and Salah Zaiem and Zeyu Zhao and Shucong Zhang and Georgios Karakasidis and Sung-Lin Yeh and Pierre Champion and Aku Rouhe and Rudolf Braun and Florian Mai and Juan Zuluaga-Gomez and Seyed Mahed Mousavi and Andreas Nautsch and Xuechen Liu and Sangeet Sagar and Jarod Duret and Salima Mdhaffar and Gaelle Laperriere and Mickael Rouvier and Renato De Mori and Yannick Esteve},
316
+ year={2024},
317
+ eprint={2407.00463},
318
+ archivePrefix={arXiv},
319
+ primaryClass={cs.LG},
320
+ url={https://arxiv.org/abs/2407.00463},
321
+ }
322
+ @misc{speechbrain,
323
+ title={{SpeechBrain}: A General-Purpose Speech Toolkit},
324
+ author={Mirco Ravanelli and Titouan Parcollet and Peter Plantinga and Aku Rouhe and Samuele Cornell and Loren Lugosch and Cem Subakan and Nauman Dawalatabad and Abdelwahab Heba and Jianyuan Zhong and Ju-Chieh Chou and Sung-Lin Yeh and Szu-Wei Fu and Chien-Feng Liao and Elena Rastorgueva and François Grondin and William Aris and Hwidong Na and Yan Gao and Renato De Mori and Yoshua Bengio},
325
+ year={2021},
326
+ eprint={2106.04624},
327
+ archivePrefix={arXiv},
328
+ primaryClass={eess.AS},
329
+ note={arXiv:2106.04624}
330
+ }
331
+ ```
332
+