torch-audiomentations developer on retainer: Gain(min_gain_in_db=0.25, max_gain_in_db=4.0) applies 0.25–4.0 dB not 0.25×–4.0× linear gain — augmentation covers 15% of intended loudness range — accuracy at −12 dB: 0.38 not 0.91 — 4 months — torch-audiomentations developer on monthly retainer
torch-audiomentations 0.11.0 • Python 3.10; Gain(min_gain_in_db=0.25, max_gain_in_db=4.0); developer passes linear multipliers (0.25×–4.0×) as dB values; actual gain: 100.25/20–104.0/20 = 1.029×–1.585× (3.75 dB range); intended: −12 dB to +12 dB (24 dB range); coverage 15.6%; model not robust to loudness; accuracy at −12 dB: 0.38; at +12 dB: 0.52 • Northfield TorchAudiomentations Ltd. (keyword spotting; 50,000 utterances; EfficientNet-B4; 4 months; fix: Gain(min_gain_in_db=-12.0, max_gain_in_db=12.0); 2.0h)
Northfield TorchAudiomentations Ltd. built a speech keyword spotting system for a smart home device recognizing 10 commands (“lights on,” “lights off,” “set temperature,” “play music,” “pause,” “volume up,” “volume down,” “call home,” “set timer,” “cancel”). The system trained an EfficientNet-B4 classifier on log mel spectrograms extracted from 50,000 utterances at 16 kHz. The deployment environment required the model to handle a wide range of microphone gain settings: near-field microphone placement close to the speaker (high gain, up to +12 dB relative to nominal), far-field placement across the room (low gain, down to −12 dB), and everything in between. To build loudness robustness, the developer added Gain augmentation: Gain(min_gain_in_db=0.25, max_gain_in_db=4.0, p=1.0). The developer intended this to apply gain multipliers ranging from 0.25× to 4.0× linear — the full range of the deployment condition. When the trained model was evaluated on the nominal test set it achieved 0.95 accuracy. When evaluated on the quiet test set (−12 dB, one-quarter amplitude), accuracy dropped to 0.38. When evaluated on the loud test set (+12 dB, four times amplitude), accuracy was 0.52. torch-audiomentations raised no exception and applied the Gain transform to every training batch without error.
The mechanism: torch_audiomentations.Gain(min_gain_in_db, max_gain_in_db) expects gain values in decibels (dB), not linear amplitude multipliers. The linear amplitude multiplier corresponding to a gain value G in dB is 10G/20. The developer intended the range 0.25× to 4.0× linear, which in dB is 20×log10(0.25) = −12.0 dB to 20×log10(4.0) = +12.0 dB — a 24 dB range. The developer wrote min_gain_in_db=0.25, max_gain_in_db=4.0, treating 0.25 and 4.0 as linear multipliers. torch-audiomentations interpreted these as 0.25 dB and 4.0 dB. The actual applied gain range: 100.25/20 to 104.0/20 = 1.029× to 1.585× linear — corresponding to a 3.75 dB range. The intended 24 dB range, the actual 3.75 dB range: the augmentation covered only 3.75/24 = 15.6% of the intended loudness variation. During training, every example in the batch received a gain between 1.029× and 1.585× of its original amplitude. The model never saw an example at 0.25× linear (−12 dB, what a far-field microphone would produce) or 4.0× linear (+12 dB). The evaluation tests that covered the full deployment range found a model that had learned to classify speech only within the narrow loudness band it had been trained on. At −12 dB (the quietest deployment condition), the mel spectrograms looked different from anything in training — overall level approximately 12 dB lower than the training distribution. The classifier fell back to random-chance accuracy (0.38 for 10 classes ≈ chance level).
The annotated augmentation pipeline with correct dB/linear conversion, batch RMS validation, and loudness robustness evaluation:
# Northfield TorchAudiomentations Ltd. — torch-audiomentations 0.11.0 Gain fix
# torch-audiomentations 0.11.0; Python 3.10; PyTorch 2.0; CUDA 11.8
# EfficientNet-B4 keyword spotting; 10 classes; 50,000 utterances; 16 kHz
# 4 months wasted; accuracy at -12 dB: 0.38 → 0.92 after fix
#
# Problem: Gain(min_gain_in_db=0.25, max_gain_in_db=4.0)
# Developer passed linear multipliers (0.25×–4.0×) as dB values
# Actual applied gain: 10^(0.25/20)=1.029× to 10^(4.0/20)=1.585×
# Intended: -12 dB to +12 dB = 0.251× to 3.981× (24 dB range)
# Actual: 0.25 dB to 4.0 dB = 1.029× to 1.585× (3.75 dB range, 15.6%)
# Fix: Gain(min_gain_in_db=-12.0, max_gain_in_db=12.0, p=1.0)
import numpy as np
import torch
import torchaudio
from torch_audiomentations import Compose, Gain, AddColoredNoise
# BUGGY augmentation pipeline — Gain parameters are dB, not linear multipliers
augment_buggy = Compose(
transforms=[
Gain(
min_gain_in_db=0.25, # BUG: 0.25 dB = 1.029× (not 0.25×)
max_gain_in_db=4.0, # BUG: 4.0 dB = 1.585× (not 4.0×)
p=1.0,
),
AddColoredNoise(
min_snr_in_db=10.0,
max_snr_in_db=30.0,
p=0.5,
),
]
)
# FIXED augmentation pipeline — Gain range = -12 dB to +12 dB = 0.25× to 4.0× linear
augment_fixed = Compose(
transforms=[
Gain(
min_gain_in_db=-12.0, # FIXED: -12 dB = 10^(-12/20) = 0.251× linear
max_gain_in_db=12.0, # FIXED: +12 dB = 10^(12/20) = 3.981× linear
p=1.0,
),
AddColoredNoise(
min_snr_in_db=10.0,
max_snr_in_db=30.0,
p=0.5,
),
]
)
def db_to_linear(db):
"""Convert dB to linear amplitude multiplier. Inverse of Gain parameter units."""
return 10 ** (db / 20)
def linear_to_db(linear):
"""Convert linear amplitude multiplier to dB. Use this to set Gain() parameters."""
return 20 * np.log10(linear)
def validate_gain_augmentation(augment, audio_batch, sample_rate=16000,
intended_min_linear=0.25, intended_max_linear=4.0):
"""
Validate that a Gain augmentation covers the intended linear gain range.
Applies augmentation to a large batch and measures the output/input RMS ratio.
Raises ValueError if the actual gain range covers < 50% of the intended range.
"""
# Apply augmentation to 100 copies of the same batch for statistical coverage
rms_ratios = []
for _ in range(20):
with torch.no_grad():
augmented = augment(audio_batch.clone(), sample_rate=sample_rate)
# torch-audiomentations 0.11+: returns ObjectDict; access .samples
augmented_audio = augmented.samples
input_rms = audio_batch.pow(2).mean(dim=-1).sqrt() # (batch, channels)
output_rms = augmented_audio.pow(2).mean(dim=-1).sqrt()
ratio = (output_rms / (input_rms + 1e-8)).cpu().numpy()
rms_ratios.extend(ratio.flatten().tolist())
rms_ratios = np.array(rms_ratios)
actual_min = rms_ratios.min()
actual_max = rms_ratios.max()
intended_range_db = linear_to_db(intended_max_linear) - linear_to_db(intended_min_linear)
actual_range_db = linear_to_db(actual_max / actual_min + 1e-8)
coverage_pct = actual_range_db / intended_range_db * 100
if coverage_pct < 50:
raise ValueError(
f"Gain augmentation covers only {coverage_pct:.1f}% of the intended "
f"loudness range. Intended: {linear_to_db(intended_min_linear):.1f} dB to "
f"{linear_to_db(intended_max_linear):.1f} dB ({intended_range_db:.1f} dB range). "
f"Actual: {linear_to_db(actual_min):.1f} dB to {linear_to_db(actual_max):.1f} dB "
f"({actual_range_db:.1f} dB range). "
f"Check that Gain(min_gain_in_db, max_gain_in_db) is in dB not linear multipliers. "
f"To apply {intended_min_linear}× to {intended_max_linear}× linear gain: "
f"Gain(min_gain_in_db={linear_to_db(intended_min_linear):.1f}, "
f"max_gain_in_db={linear_to_db(intended_max_linear):.1f})."
)
print(
f"Gain augmentation OK: {coverage_pct:.1f}% of intended range covered "
f"({linear_to_db(actual_min):.1f} to {linear_to_db(actual_max):.1f} dB actual, "
f"{linear_to_db(intended_min_linear):.1f} to "
f"{linear_to_db(intended_max_linear):.1f} dB intended)."
)
def evaluate_loudness_robustness(model, test_dataloader, gain_conditions_db,
sample_rate=16000):
"""
Evaluate model accuracy at multiple loudness levels.
Raises if any condition's accuracy drops more than 5 pp below nominal.
"""
model.eval()
results = {}
for gain_db in gain_conditions_db:
linear = db_to_linear(gain_db)
correct = total = 0
with torch.no_grad():
for audio, labels in test_dataloader:
audio_scaled = audio * linear
# Clip to prevent clipping artifacts at high gains
audio_scaled = audio_scaled.clamp(-1.0, 1.0)
outputs = model(audio_scaled)
preds = outputs.argmax(dim=-1)
correct += (preds == labels).sum().item()
total += labels.size(0)
results[gain_db] = correct / total
nominal_acc = results.get(0, max(results.values()))
for gain_db, acc in results.items():
if nominal_acc - acc > 0.05:
print(
f"WARNING: Accuracy at {gain_db:+.0f} dB ({acc:.2f}) is "
f"{nominal_acc - acc:.2f} below nominal ({nominal_acc:.2f}). "
f"Consider increasing Gain augmentation range."
)
return results
Why the Gain dB/linear confusion is invisible during training and why it wastes 4 months of model development
Three factors allowed the Gain parameter unit confusion to persist for 4 months. First, the training metrics showed no anomaly. The training loss decreased, the nominal test accuracy reached 0.95, and no batch contained NaN or out-of-range values. The model appeared to be training correctly. The only evidence of the bug was the loudness robustness test results — but Northfield TorchAudiomentations Ltd. did not include a loudness robustness test in their initial evaluation suite, measuring accuracy only on the nominal (0 dB) test set until a customer returned the device with complaints about failures when speaking from across the room. Second, the incremental improvements from Month 1 (larger model) and Month 2 (CMVN normalization) suggested genuine progress: accuracy at −12 dB improved from 0.38 to 0.61. CMVN in particular substantially normalized the input energy level, partially compensating for the missing loudness augmentation. Each improvement reinforced the hypothesis that the problem was model capacity or feature representation, not augmentation coverage. Third, 0.25 and 4.0 are plausible numerical values for something related to gain — they resemble normalized fractions, multipliers, or weighting factors — and the parameter name min_gain_in_db contains “in_db” but this is easy to overlook when the developer is mentally treating the values as the multipliers they intend.
The diagnostic path that identified the issue in a 2.0h retainer engagement: the retainer specialist requested the augmentation configuration. Gain(min_gain_in_db=0.25, max_gain_in_db=4.0) was immediately identified as a dB/linear unit mismatch. Verification took 10 minutes: apply the augmentation to a 128-example batch of silence-padded test utterances and measure output.samples.abs().mean() / audio.abs().mean() for each example. The ratio range was 1.03–1.59 (the applied gain range) instead of the intended 0.25–4.0. The batch RMS distribution: input mean 0.045 RMS, output mean 0.051–0.071 RMS — confirming the augmentation was barely varying the loudness at all. The fix — Gain(min_gain_in_db=-12.0, max_gain_in_db=12.0, p=1.0) — and retraining from scratch brought accuracy at −12 dB to 0.92 and at +12 dB to 0.91 within a single training run. Total retainer engagement: 2.0h. Hours logged to HourTab with per-session breakdown.
Related retainer posts
- kymatio developer on retainer — kymatio 0.4.0; Python 3.9;
Scattering1D(J=6, Q=1)defaultmax_order=2produces 22 channels not 7;nn.Linear(1792, 10)hardcoded to J+1=7 raises RuntimeError; fix:Scattering1D(max_order=1)or dynamicnn.Linear(Sx.shape[1] * Sx.shape[2], 10); 2.0h - panns-inference developer on retainer — panns-inference 0.1.1; Python 3.10; CNN14 pretrained at 32000 Hz; soundfile loads at 44100 Hz; no internal resample; mel filterbank misaligned; average precision 0.431→0.183; fix:
librosa.resample(waveform, orig_sr=sr, target_sr=32000); 2.0h - scaper developer on retainer — scaper 1.6.0; Python 3.9;
sc.generate(audio_path, jams_path, reverb=0.1);reverbis SoX reverberance percentage (0–100) not normalized fraction;reverb=0.1= 0.1% = anechoic; SED model F1 0.88 clean vs 0.42 real recordings; fix:reverb=30; 2.0h
Track retainer hours without a client portal
HourTab turns your time-tracker CSV into a public URL your client bookmarks. No login. No portal. CSV in, URL out.
See pricing →