AI Logic

Data Augmentation

SpecAugment

Updated:

SpecAugment diagram. The left shows the training flow (audio, log mel spectrogram, time warping, frequency masking, time masking, speech recognition model, text). The right shows examples of time warping, frequency masking, and time masking applied to the original features, and a table of the parameters of the paper's four policies LB, LD, SM, and SS

What is SpecAugment

SpecAugment is a data augmentation method that trains a speech recognition model by directly deforming its input log mel spectrogram (a 2D feature with time on the horizontal axis and frequency on the vertical axis) as if it were an image. It was proposed by Park et al. at Google Brain in 2019 and presented at Interspeech 2019.

It uses only three kinds of deformation.

  • Time warping: Locally stretches and compresses the spectrogram along the time axis.
  • Frequency masking: Masks several consecutive frequency channels (a horizontal band).
  • Time masking: Masks several consecutive time steps (a vertical band).

The idea of masking comes from Cutout, which masks part of an image. Because it acts on the features right before they enter the model rather than on the audio waveform, it is cheap to compute and needs no additional data. It can therefore be applied on the fly (online) during training. The target transcript is left unchanged.

As an analogy, it is like practicing to understand speech even when part of it is drowned out by noise, sounds at certain pitches are hard to hear, or the speaker talks a little fast.

The paper showed that with this simple method alone, an end-to-end speech recognition model (Listen, Attend and Spell) outperforms carefully engineered conventional HMM hybrid systems and achieves the best error rates on LibriSpeech and Switchboard.

The three deformations

For a log mel spectrogram with τ\tau time steps and ν\nu frequency channels, the deformations are applied in the following order (figure at the top).

  1. Time warping: Treat the spectrogram as an image with time on the horizontal axis and frequency on the vertical axis. On the horizontal line through the center, choose a random point in the time range (W,τ−W)(W, \tau - W) and move it left or right by a distance ww chosen uniformly from 00 to WW. The four corners and the midpoints of the vertical edges (6 points) are kept fixed, and the whole image is deformed smoothly (the paper uses TensorFlow's sparse_image_warp).
  2. Frequency masking: Choose a width ff uniformly from 00 to FF and a start f0f_0 from [0,ν−f)[0, \nu - f), and mask channels [f0,f0+f)[f_0, f_0 + f). Repeat this mFm_F times.
  3. Time masking: Choose a width tt uniformly from 00 to TT and a start t0t_0 from [0,τ−t)[0, \tau - t), and mask time steps [t0,t0+t)[t_0, t_0 + t). The width is kept from exceeding p⋅τp \cdot \tau. Repeat this mTm_T times.

Masked values are set to 0. Since the spectrogram is normalized to zero mean beforehand, masking with 0 is the same as masking with the mean. Multiple masks may overlap.

The set of six values W,F,mF,T,p,mTW, F, m_F, T, p, m_T is called a "policy." The paper uses four hand-crafted policies (Table 1).

PolicyData usedWWFFmFm_FTTppmTm_T
LB (LibriSpeech basic)LibriSpeech802711001.01
LD (LibriSpeech double)LibriSpeech802721001.02
SM (Switchboard mild)Switchboard40152700.22
SS (Switchboard strong)Switchboard40272700.22

The input features are 80-dimensional filter banks with first- and second-order differences (delta and delta-delta) appended.

The upper limit on the frequency mask width, F=27F = 27, is roughly one third of the 80 channels. This trains the model to distinguish phonemes from the remaining bands even when part of the frequency range is missing.

Why it works

The paper chose the three deformations based on the kinds of robustness the model should have.

  • Time warping: Lets the model recognize the same words even when the speaking rate changes locally.
  • Frequency masking: Lets the model recognize speech even when part of the frequency information is missing.
  • Time masking: Lets the model recognize speech from the surrounding context even when short segments of audio drop out.

The most important point in the paper's discussion is that data augmentation turned an "overfitting problem" into an "underfitting problem." With SpecAugment, the model could no longer fully fit not only the deformed training data but also the undeformed training data (Figure 3). This is the opposite of the usual situation, in which the model fits the training data too closely (overfitting).

Underfitting can be handled with well-known remedies. The paper improved performance by applying a strong policy and repeatedly making the network larger and deeper and training longer. In the LibriSpeech comparison (Table 2) as well, the stronger the policy (LD), the clearer the benefit of a larger network and longer training.

Of the three, time warping had a small effect. The paper says that because it is the most computationally expensive and the least effective, it should be the first to drop when compute is limited.

Results reported in the paper

LibriSpeech 960 hours (test word error rate (WER), %, Table 3)

Methodtest-clean (no LM)test-other (no LM)test-clean (with LM)test-other (with LM)
Previous best (HMM, Yang et al. 2018)2.977.50−−
LAS (without SpecAugment)4.112.53.29.8
LAS + SpecAugment2.86.82.55.8

Even without a language model, 6.8% beat the previous best HMM system (7.5%). Combined with a language model via shallow fusion, test-other reached 5.8%, which the paper describes as a 22% relative improvement over the previous best. The model is LAS-6-1280 (6-layer bidirectional LSTM, width 1280), the policy is LD, and training took about 24 days.

Switchboard 300 hours (Hub5'00 WER, %, Table 5)

MethodSwitchboard (no LM)CallHome (no LM)Switchboard (with LM)CallHome (with LM)
Previous best (HMM, Zeyer et al. 2018)8.317.3−−
LAS (without SpecAugment)11.221.610.919.4
LAS + SpecAugment (SM)7.214.66.814.1
LAS + SpecAugment (SS)7.314.47.114.0

Policy and training length (LibriSpeech, LAS-4-1024, test-other WER without LM, Table 2)

Training lengthNo policyLBLD
B (basic)13.410.09.2
D (double)13.39.28.3

Without a policy, longer training barely changed the result, but with a policy, longer training brought a corresponding improvement.

Removing the three deformations one at a time (LB, LAS-4-1024, no LM, Table 6)

Conditiontest-othertest-clean
All used (LB)10.03.7
Without time warping10.13.8
Without frequency masking11.04.0
Without time masking10.94.1

The two masks had large effects, and time warping had a small one.

Caveats

If applying it strongly, make the model larger and train longer: SpecAugment puts the model in an underfitting state. The paper's results were obtained in combination with a large network (LAS-6-1280) and long training (about 24 days). Applying a strong policy to a small model with short training should not be expected to give the same improvement.

  • Match the policy to the data: The paper itself uses different policies for read speech in LibriSpeech (LB, LD) and telephone conversations in Switchboard (SM, SS). In particular, the ratio pp that caps the time mask width is set to 0.2 for Switchboard.
  • Time warping can be omitted: It is the least effective and most computationally expensive deformation (from the paper's discussion). torchaudio's SpecAugment does not include time warping either.
  • Be careful when combining it with label smoothing: In the paper, combining SpecAugment with Label Smoothing on LibriSpeech made training unstable more often, so label smoothing was applied only before the learning rate started to decay. On Switchboard, the two effects added up.
  • Apply it only during training: At evaluation and inference, feed the undeformed features as is.
  • Check what torchaudio's arguments mean: FrequencyMasking(freq_mask_param=F) and TimeMasking(time_mask_param=T, p=p) each apply one mask. To apply mFm_F or mTm_T masks, call them that many times.

Comparison with similar data augmentation methods and how to choose

The table below lines up data augmentation methods commonly used in speech recognition from the same angles.

MethodWhat it changesWhere it is appliedHow labels are handledMain settings to tune
SpecAugmentTime warping; masking frequency and time bandsLog mel spectrogramUnchangedW,F,mF,T,p,mTW, F, m_F, T, p, m_T
Speed perturbationSpeed of the audioWaveformUnchangedSpeed factors
MixSpeechAdds the features of two utterancesFeaturesDoes not mix them; mixes the two losses in the same ratioDistribution of the mixing ratio α\alpha
Cutout (images)Masks a rectangular regionImageUnchangedMask size

Rough guidelines for choosing:

  • Training end-to-end speech recognition: Start with SpecAugment. It is cheap to compute, can be applied during training, and is also used to train later models such as Conformer.
  • Little data, and less policy-tuning effort is wanted: MixSpeech is another candidate. The MixSpeech paper reports lower error rates than SpecAugment on TIMIT, WSJ, and HKUST.
  • Robustness to variation in speaking rate is wanted: Combine it with speed perturbation, which changes the speed of the waveform.

PyTorch example

Show code
python
import torch
import torchaudio

# The two SpecAugment masks (torchaudio). Time warping is not included, so it is omitted (it also has the smallest effect in the paper).
# Policy LD (LibriSpeech double): F = 27, mF = 2, T = 100, p = 1.0, mT = 2
F, m_F, T, p, m_T = 27, 2, 100, 1.0, 2

freq_mask = torchaudio.transforms.FrequencyMasking(freq_mask_param=F)   # width drawn uniformly from [0, F); one mask per call
time_mask = torchaudio.transforms.TimeMasking(time_mask_param=T, p=p)   # width does not exceed p × number of time steps


def spec_augment(log_mel: torch.Tensor) -> torch.Tensor:
    """log_mel: log mel spectrogram of shape (..., freq, time). If normalized to zero mean, masking with 0 = masking with the mean."""
    x = log_mel
    for _ in range(m_F):
        x = freq_mask(x)      # default mask_value is 0
    for _ in range(m_T):
        x = time_mask(x)
    return x


feats = torch.randn(8, 80, 1000)          # (batch, 80 channels, 1000 steps)
feats = (feats - feats.mean()) / feats.std()

# Apply only during training; do not deform at evaluation or inference
training = True
x = spec_augment(feats) if training else feats

# Note: torchaudio.transforms.SpecAugment also exists, but it applies p to the frequency mask width cap as well,
# and by default (zero_masking=False) masks with the input's mean. To follow the paper, use them separately as above.

FAQ

Q. How does it differ from data augmentation that changes the audio waveform?
A.

SpecAugment deforms not the waveform but the log mel spectrogram right before it enters the model, as if it were an image. Because no audio has to be regenerated, it is cheap to compute and can be applied on the fly during training. It can also be combined with waveform-level data augmentation such as Speed perturbation, which changes the speed.

Q. How should the policy (values such as F and T) be chosen?
A.

Start from the paper's policies. For read speech, LB (F=27F = 27, T=100T = 100, one mask each) or LD (two each) is a good guide; for conversational speech, SM or SS (T=70T = 70, p=0.2p = 0.2). The stronger the policy, the more the model underfits, so tune the model size and training length together on validation data.

Q. With SpecAugment, the error rate on the training data is harder to bring down. Is that a problem?
A.

The same thing happened in the paper, and it is not a problem. SpecAugment turns overfitting into underfitting, so the training error rate becomes harder to reduce. In that case, making the network larger or training longer improves results.

References

  1. Park, D. S., Chan, W., Zhang, Y., Chiu, C.-C., Zoph, B., Cubuk, E. D., & Le, Q. V. (2019). SpecAugment: A Simple Data Augmentation Method for Automatic Speech Recognition. Proc. Interspeech 2019, 2613–2617.
  2. torchaudio.transforms (FrequencyMasking, TimeMasking, SpecAugment)