online_asr_tutorial.py 9.14 KB
Newer Older
1
2
3
4
"""
Online ASR with Emformer RNN-T
==============================

5
**Author**: `Jeff Hwang <jeffhwang@meta.com>`__, `Moto Hira <moto@meta.com>`__
6
7
8
9
10
11
12
13

This tutorial shows how to use Emformer RNN-T and streaming API
to perform online speech recognition.

"""

######################################################################
#
14
# .. note::
15
#
moto's avatar
moto committed
16
#    This tutorial requires FFmpeg libraries (>=5, <6) and SentencePiece.
17
#
18
#    There are multiple ways to install FFmpeg libraries.
19
#    If you are using Anaconda Python distribution,
moto's avatar
moto committed
20
#    ``conda install -c conda-forge 'ffmpeg<6'`` will install
21
#    the required FFmpeg libraries.
22
#
23
#    You can install SentencePiece by running ``pip install sentencepiece``.
24

25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
######################################################################
# 1. Overview
# -----------
#
# Performing online speech recognition is composed of the following steps
#
# 1. Build the inference pipeline
#    Emformer RNN-T is composed of three components: feature extractor,
#    decoder and token processor.
# 2. Format the waveform into chunks of expected sizes.
# 3. Pass data through the pipeline.

######################################################################
# 2. Preparation
# --------------
#

42
import os
43
44
45
import torch
import torchaudio

46
47
48
49
50
51
print(torch.__version__)
print(torchaudio.__version__)

######################################################################
#
import IPython
52
import matplotlib.pyplot as plt
53

54
try:
55
    from torchaudio.io import StreamReader
56
57
58
59
60
61
except ModuleNotFoundError:
    try:
        import google.colab

        print(
            """
62
63
            To enable running this notebook in Google Colab, install the requisite
            third party libraries by running the following code block:
64
65
66
67
68
69
70
71
72

            !add-apt-repository -y ppa:savoury1/ffmpeg4
            !apt-get -qq install -y ffmpeg
            """
        )
    except ModuleNotFoundError:
        pass
    raise

73
74
75
76
77
78

######################################################################
# 3. Construct the pipeline
# -------------------------
#
# Pre-trained model weights and related pipeline components are
79
# bundled as :py:class:`torchaudio.pipelines.RNNTBundle`.
80
#
81
# We use :py:data:`torchaudio.pipelines.EMFORMER_RNNT_BASE_LIBRISPEECH`,
82
83
84
85
86
87
88
89
90
91
92
# which is a Emformer RNN-T model trained on LibriSpeech dataset.
#

bundle = torchaudio.pipelines.EMFORMER_RNNT_BASE_LIBRISPEECH

feature_extractor = bundle.get_streaming_feature_extractor()
decoder = bundle.get_decoder()
token_processor = bundle.get_token_processor()

######################################################################
# Streaming inference works on input data with overlap.
moto's avatar
moto committed
93
94
95
96
97
# Emformer RNN-T model treats the newest portion of the input data
# as the "right context" — a preview of future context.
# In each inference call, the model expects the main segment
# to start from this right context from the previous inference call.
# The following figure illustrates this.
98
99
100
101
102
103
104
105
#
# .. image:: https://download.pytorch.org/torchaudio/tutorial-assets/emformer_rnnt_context.png
#
# The size of main segment and right context, along with
# the expected sample rate can be retrieved from bundle.
#

sample_rate = bundle.sample_rate
106
107
segment_length = bundle.segment_length * bundle.hop_length
context_length = bundle.right_context_length * bundle.hop_length
108
109

print(f"Sample rate: {sample_rate}")
110
111
print(f"Main segment: {segment_length} frames ({segment_length / sample_rate} seconds)")
print(f"Right context: {context_length} frames ({context_length / sample_rate} seconds)")
112
113
114
115
116

######################################################################
# 4. Configure the audio stream
# -----------------------------
#
117
# Next, we configure the input audio stream using :py:class:`torchaudio.io.StreamReader`.
118
119
#
# For the detail of this API, please refer to the
moto's avatar
moto committed
120
# `StreamReader Basic Usage <./streamreader_basic_tutorial.html>`__.
121
122
123
124
125
126
127
128
129
130
131
132
#

######################################################################
# The following audio file was originally published by LibriVox project,
# and it is in the public domain.
#
# https://librivox.org/great-pirate-stories-by-joseph-lewis-french/
#
# It was re-uploaded for the sake of the tutorial.
#
src = "https://download.pytorch.org/torchaudio/tutorial-assets/greatpiratestories_00_various.mp3"

133
streamer = StreamReader(src)
134
streamer.add_basic_audio_stream(frames_per_chunk=segment_length, sample_rate=bundle.sample_rate)
135
136
137
138
139

print(streamer.get_src_stream_info(0))
print(streamer.get_out_stream_info(0))

######################################################################
moto's avatar
moto committed
140
141
142
143
144
145
146
147
148
# As previously explained, Emformer RNN-T model expects input data with
# overlaps; however, `Streamer` iterates the source media without overlap,
# so we make a helper structure that caches a part of input data from
# `Streamer` as right context and then appends it to the next input data from
# `Streamer`.
#
# The following figure illustrates this.
#
# .. image:: https://download.pytorch.org/torchaudio/tutorial-assets/emformer_rnnt_streamer_context.png
149
150
151
152
#


class ContextCacher:
moto's avatar
moto committed
153
    """Cache the end of input data and prepend the next input data with it.
154
155

    Args:
156
157
158
        segment_length (int): The size of main segment.
            If the incoming segment is shorter, then the segment is padded.
        context_length (int): The size of the context, cached and appended.
159
160
    """

161
162
163
164
    def __init__(self, segment_length: int, context_length: int):
        self.segment_length = segment_length
        self.context_length = context_length
        self.context = torch.zeros([context_length])
165
166

    def __call__(self, chunk: torch.Tensor):
167
168
169
170
        if chunk.size(0) < self.segment_length:
            chunk = torch.nn.functional.pad(chunk, (0, self.segment_length - chunk.size(0)))
        chunk_with_context = torch.cat((self.context, chunk))
        self.context = chunk[-self.context_length :]
171
172
173
174
175
176
177
178
179
180
181
182
183
184
        return chunk_with_context


######################################################################
# 5. Run stream inference
# -----------------------
#
# Finally, we run the recognition.
#
# First, we initialize the stream iterator, context cacher, and
# state and hypothesis that are used by decoder to carry over the
# decoding state between inference calls.
#

185
cacher = ContextCacher(segment_length, context_length)
186
187
188
189
190
191
192
193
194
195
196

state, hypothesis = None, None

######################################################################
# Next we, run the inference.
#
# For the sake of better display, we create a helper function which
# processes the source stream up to the given times and call it
# repeatedly.
#

moto's avatar
moto committed
197
198
stream_iterator = streamer.stream()

199

200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
def _plot(feats, num_iter, unit=25):
    unit_dur = segment_length / sample_rate * unit
    num_plots = num_iter // unit + (1 if num_iter % unit else 0)
    fig, axes = plt.subplots(num_plots, 1)
    t0 = 0
    for i, ax in enumerate(axes):
        feats_ = feats[i*unit:(i+1)*unit]
        t1 = t0 + segment_length / sample_rate * len(feats_)
        feats_ = torch.cat([f[2:-2] for f in feats_])  # remove boundary effect and overlap
        ax.imshow(feats_.T, extent=[t0, t1, 0, 1], aspect="auto", origin="lower")
        ax.tick_params(which='both', left=False, labelleft=False)
        ax.set_xlim(t0, t0 + unit_dur)
        t0 = t1
    fig.suptitle("MelSpectrogram Feature")
    plt.tight_layout()


217
@torch.inference_mode()
218
def run_inference(num_iter=100):
219
220
    global state, hypothesis
    chunks = []
221
    feats = []
moto's avatar
moto committed
222
    for i, (chunk,) in enumerate(stream_iterator, start=1):
223
        segment = cacher(chunk[:, 0])
224
225
        features, length = feature_extractor(segment)
        hypos, state = decoder.infer(features, length, 10, state=state, hypothesis=hypothesis)
226
227
228
        hypothesis = hypos
        transcript = token_processor(hypos[0][0], lstrip=False)
        print(transcript, end="\r", flush=True)
229
230

        chunks.append(chunk)
231
        feats.append(features)
232
233
234
        if i == num_iter:
            break

235
236
    # Plot the features
    _plot(feats, num_iter)
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
    return IPython.display.Audio(torch.cat(chunks).T.numpy(), rate=bundle.sample_rate)


######################################################################
#

run_inference()

######################################################################
#

run_inference()

######################################################################
#

run_inference()

######################################################################
#

run_inference()

######################################################################
#

run_inference()

######################################################################
#

run_inference()

######################################################################
#

run_inference()
274

275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
######################################################################
#

run_inference()

######################################################################
#

run_inference()

######################################################################
#

run_inference()

######################################################################
#

run_inference()

######################################################################
#

run_inference()

######################################################################
#

run_inference()

305
306
307
######################################################################
#
# Tag: :obj:`torchaudio.io`