Online CMVN
Running (online) cepstral mean and variance normalization, a port of kaldi's
OnlineCmvn / OnlineCmvnState
(feat/online-feature.h, see
kaldi-native-fbank/csrc/online-cmvn.h).
Unlike the offline CMVN of CMVN (which needs the whole utterance), this class looks only at the past: each frame is normalized with statistics accumulated from the frames seen so far. This is what kaldi's online decoding and i-vector front-ends use — if you train on features normalized this way you must decode with the same kind of normalization (kaldi's "matched" online training).
Offline vs online CMVN
offline (CMVN) |
online (this page) |
|---|---|
stats over the whole |
stats over the previous |
utterance (sees the future) |
(streaming, past only) |
output is zero-mean per |
output only approximately centered, |
utterance |
especially at the start |
one shot |
one frame at a time |
reversable ( |
frozen via |
In the steady state (middle of a long utterance) the class accumulates stats
from the previous cmn_window frames (default 600, i.e. 6 seconds) and uses
them to normalize the current frame. At the beginning of an utterance,
when fewer than cmn_window frames of context exist, the stats are
augmented first with up to speaker_frames frames of speaker stats (from
previous utterances of the same speaker) and then with up to global_frames
frames of global stats (from training data) — so the very first frames are
normalized against a "typical" distribution instead of almost nothing.
Usage
import numpy as np
import kaldi_native_fbank as knf
dim = 40
global_stats = ... # [2, dim+1] float64, e.g. kaldi's global_cmvn.stats
opts = knf.OnlineCmvnOptions() # kaldi defaults
state = knf.OnlineCmvnState(global_cmvn_stats=global_stats)
cmvn = knf.OnlineCmvn(opts, state)
# push raw feature frames one by one (or in batches)
feats = np.random.randn(200, dim).astype(np.float32)
out = cmvn.accept_features(feats) # [200, dim] float32, normalized
# out[t] = feats[t] - running_mean(t) (mean normalization)
# hand the speaker adaptation over to the next utterance:
state = cmvn.get_state()
cmvn2 = knf.OnlineCmvn(opts, state)
out2 = cmvn2.accept_features(next_utterance)
Single frames use OnlineCmvn.accept_feature(), which returns the
normalized frame of shape [dim].
Freezing the normalization
Calling OnlineCmvn.freeze() fixes the normalization at the current
frame: later frames are normalized with the frozen stats and the running stats
stop updating. This is kaldi's mechanism for estimating fMLLR (or anything
else) on top of a fixed CMVN. The frozen state travels with
OnlineCmvnState, so the next utterance of the speaker is normalized
the same way:
cmvn.accept_features(feats)
cmvn.freeze()
steady = cmvn.accept_features(more_feats) # constant offset per dim
state = cmvn.get_state() # carries frozen_state
cmvn2 = knf.OnlineCmvn(opts, state) # keeps the frozen normalization
Options
OnlineCmvnOptions mirrors kaldi's OnlineCmvnOptions (defaults
match):
option |
default |
meaning |
|---|---|---|
|
600 |
frames of sliding context for the running stats |
|
600 |
frames of previous utterance(s) of this speaker to
back off to at utterance start ( |
|
200 |
frames of global stats to back off to further
( |
|
True |
subtract the running mean |
|
False |
also divide by the running standard deviation
(requires |
|
20 |
kept for kaldi configuration compatibility; this port updates stats incrementally, so results are not affected |
|
20 |
kept for kaldi configuration compatibility (see
|
|
"" |
colon-separated dimensions to skip normalization
of, e.g. |
Formulas
Stats have kaldi's 2 x (dim+1) layout (same as CMVN):
row0 = [sum_1..sum_D, count], row1 = [sumsq_1..sumsq_D, 0].
For each accepted frame \(x_t\) the running window stats are updated
(frame \(t\) added, frame \(t - c\) removed if it leaves the window,
where \(c\) is the option cmn_window):
The window stats are then smoothed with the speaker and global stats (this is what happens when \(\mathrm{count} < c\), e.g. at the start of an utterance):
where \(n\) is the window count, \(s_f\)/speaker_frames,
\(g_f\)/global_frames, \(n_s\)/n_g the counts of the speaker
and global stats (same for sumsq). Finally the frame is normalized with
the smoothed stats:
Note the frame being normalized is included in the stats that normalize it (like kaldi). Variance is floored at \(10^{-20}\) before the square root.
Correspondence with kaldi
kaldi ( |
kaldi-native-fbank |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
pass the state to the |
|
|
Parity with kaldi is tested frame-by-frame against
apply-cmvn-online in
tests/parity/parity_online_cmvn.py (several option sets including
speaker-state handover agree to about 1e-5).