Skip to main content

torch-log-wmse-logo

LICENSE GitHub Repo stars

This repository contains the torch implementation of an audio quality metric, logWMSE, originally proposed by Iver Jordal. In addition to the original metric, this implementation can also be used as a loss function for training audio separation and denoising models.

logWMSE is a custom metric and loss function for audio signals that calculates the logarithm (log) of a frequency-weighted (W) Mean Squared Error (MSE). It is designed to address several shortcomings of common audio metrics, most importantly the lack of support for digital silence targets.

Installation

PyPI - Python Version PyPI - Version Downloads

pip install torch-log-wmse

Usage Example

import torch
from torch_log_wmse import LogWMSE, LogWMSELoss

sample_rate = 44100
batch, channels, stems = 4, 2, 4   # e.g. 4 stems: vocals, drums, bass, other
samples = sample_rate              # 1 second

# LogWMSE is the metric (higher is better). LogWMSELoss is the same thing negated, for training.
# One instance handles any length and any number of stems.
metric = LogWMSE(sample_rate=sample_rate)

unprocessed = 2 * torch.rand(batch, channels, samples) - 1            # the mixture
processed = unprocessed.unsqueeze(2).expand(-1, -1, stems, -1) * 0.1  # an estimate: 20 dB of residual
target = torch.zeros(batch, channels, stems, samples)                # a digital-silence target

print(metric(unprocessed, processed, target))
# tensor(18.4207)

loss = LogWMSELoss(sample_rate=sample_rate)
print(loss(unprocessed, processed, target))
# tensor(-18.4207)

Upgrading from 0.x? The API changed in 1.0.0. LogWMSE is now the positive metric and LogWMSELoss is the loss (the old return_as_loss flag is gone), audio_length is no longer needed, and all arguments are keyword-only. Multi-stem scores are unchanged by default. The CHANGELOG has a full migration guide.

logWMSE accepts three torch tensors of the following shapes:

  • unprocessed_audio: [batch, audio_channels, samples]
  • processed_audio: [batch, audio_channels, audio_stems, samples]
  • target_audio: [batch, audio_channels, audio_stems, samples]

Each dimension being:

  • batch: Number of audio files in a batch (i.e. batch size).
  • audio_channels: Number of channels (i.e. 1 for mono and 2 for stereo).
  • audio_stems: Number of separate audio sources. For source separation, this could be multiple different instruments, vocals, etc. For denoising audio, this will be 1.
  • samples: Number of audio samples (e.g. 1 second of audio @ 44.1kHz is 44100 samples).

Motivation

The goal of this metric is to account for several factors not present in current audio evaluation metrics, such as dealing with digital silence. Mean Squared Error (MSE) is well-defined for digital silence targets, but has its own set of drawbacks. Attempting to mitigate these issues, the following are some attributes of logWMSE:

  • Supports digital silence targets not supported by other audio metrics. i.e. (SI-)SDR, SIR, SAR, ISR, VISQOL_audio, STOI, CDPAM, and VISQOL.
  • Overcomes the small value range issue of MSE (i.e. between 1e-8 and 1e-3), making number formatting and sight-reading easier. It is scaled similarly to SI-SDR for consistency with current benchmark metrics (i.e. 3 is poor, 30 is very good).
  • Scale-invariant, aligns with the frequency sensitivity of human hearing.
  • Logarithmic, reflecting the logarithmic sensitivity of human hearing.
  • Tailored specifically for audio signals.
Frequency Weighting

To measure the frequencies of a signal closer to that of human hearing, the following frequency weighting is applied. This helps the model effectively pay less attention to errors at frequencies that humans are not sensitive to (e.g. 50 Hz) and give more weight to those that we are acutely tuned to (e.g. 3kHz).

Frequency Weighting

This metric is built for high-fidelity audio (sample rates ≥ 44.1kHz), and the weighting above is designed at 44.1kHz. It still works at other rates — the weighting filter is resampled to match your audio — but scores at other rates are internally consistent rather than comparable to 44.1kHz. See how it works for what changes.

Inputs

Unlike many audio quality metrics, logWMSE accepts 3 audio inputs rather than 2:

  • Unprocessed audio (e.g. raw, noisy audio)
  • Processed audio (e.g. denoised or separated audio)
  • Target audio (e.g. ground truth, clean audio)

Typically audio loss functions only use the processed audio and target audio to compare against one another. However, logWMSE requires the initial, unprocessed audio because it needs to be able to measure how well the processed audio was attenuated from the unprocessed version. This adds a factor that accounts for when the input contains silence (digital zero).

This also adds a factor of scale invariance: the processed audio needs to be scaled appropriately relative to both the unprocessed audio and the ground truth. Conceptually, if all 3 inputs are gained by the same arbitrary amount, the score stays the same.

Using it as a loss

LogWMSELoss is the negated metric, so lower is better and you can minimise it directly. Two things are worth knowing up front:

  • The score is bounded above at +73.6827 — a perfect estimate, or an all-silent triplet, lands there.
  • The gradient grows as the estimate improves (the same behaviour as SI-SDR, the opposite of plain MSE), so you should use gradient clipping.

A p argument controls how per-stem errors combine; its default reproduces the aggregation every earlier version used, so you can ignore it to start. Mixed precision works through torch.autocast. The training guide covers all of this in the detail that matters inside a real training loop.

Limitations
  • This is a perceptual objective, not a signal-fidelity one. The weighting deliberately discounts what the ear is less sensitive to, so training against logWMSE will generally cost you SDR relative to an unweighted loss. That is the trade the metric exists to make; if SDR is your target, use an SDR-matched loss.
  • The metric isn't invariant to scaling, polarity inversion, or offsets applied to the estimate alone (as distinct from the joint-gain invariance above).
  • Although it incorporates frequency filtering inspired by human auditory sensitivity, it doesn't fully model human auditory perception. For instance, it doesn't consider auditory masking.

More on these, plus sample-rate behaviour and how to compare scores across models, is in how it works and behaves.

Documentation

  • Using logWMSE as a loss — the gradient regime and why it grows, gradient clipping, mixed precision (including Apple Silicon / MPS), and the p stem-combining knob.
  • How it works and how it behaves — the frequency weighting, the three-input design, scale invariance, other sample rates, and comparing scores.

Contributing

Contributions are welcome! Please open an issue or submit a pull request if you have any improvements or new features to suggest.

License

This project is licensed under the Apache License 2.0. See LICENSE for details.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

torch_log_wmse-1.1.0.tar.gz (105.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

torch_log_wmse-1.1.0-py3-none-any.whl (42.4 kB view details)

Uploaded Python 3

File details

Details for the file torch_log_wmse-1.1.0.tar.gz.

File metadata

  • Download URL: torch_log_wmse-1.1.0.tar.gz
  • Upload date:
  • Size: 105.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for torch_log_wmse-1.1.0.tar.gz
Algorithm Hash digest
SHA256 5306fb0273e8ad0131d43c572a17892097641dba76a4c612543ea886477db3f4
MD5 1fb13584cf46ed059eb5880c8ac114dc
BLAKE2b-256 06a1f5faf8aec444d44848f0f423c2f220c0b4fa487268ac8da1d098820690a6

See more details on using hashes here.

Provenance

The following attestation bundles were made for torch_log_wmse-1.1.0.tar.gz:

Publisher: pypi.yml on crlandsc/torch-log-wmse

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file torch_log_wmse-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: torch_log_wmse-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 42.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for torch_log_wmse-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0bd82051125b35d427b4ee22850f2d61645adf221404729b435b0a18a5c53dad
MD5 f273e6a34cc78b97022f00d5a448bac2
BLAKE2b-256 f312452c92c32879ee7384776d29bd556c572110cd56cf7fe3c53ac20501dbc4

See more details on using hashes here.

Provenance

The following attestation bundles were made for torch_log_wmse-1.1.0-py3-none-any.whl:

Publisher: pypi.yml on crlandsc/torch-log-wmse

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 files

1.0.0

2 files

0.3.1

2 files

0.3.0

2 files

0.2.9

2 files

0.2.8

2 files

0.2.7

2 files

0.2.6

2 files

0.2.5

2 files

0.2.4

2 files

0.2.3

2 files

0.2.2

2 files

0.2.1

2 files

0.2.0

2 files

0.1.9

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page