Align audio CI and releases; require Python 3.11 (#2081)

* feat(cli): add --subfolder option to benchmark command (#2063)
* fix(utils): fix `SpeakerDiarizationMixin.optimal_mapping` when `reference` is a mapping
* fix(task): fix `balance` option crashing training with `KeyError: 'metadata'` (#2067)
* fix(io): fix `Audio.crop` raising when an in-memory waveform is cropped up to its very end (#2068)
* fix(model): make Model.default_activation return identity for unsupported tasks
* fix: compute sample duration from preloaded waveform
* fix(pipeline): omit unset runtime options from model references (#2080)

Co-authored-by: abeeranajam31 <128603901+abeeranajam31@users.noreply.github.com>
Co-authored-by: antoinelaurent <antoinelaurent@me.com>
Co-authored-by: Kayvan Zahiri <123409429+Kayvan-Zahiri@users.noreply.github.com>
Co-authored-by: Mohammad Hijjawi <51020382+MohammadHijjawi97@users.noreply.github.com>
This commit is contained in:
authored and GitHub committed 2026-10-22 15:07:01 +02:00
1 parent b749285c5c
commit ea18bc70d8
105 files changed
+3906 -4283

No files matched your search

+1 -1
View File
@@ -51,6 +51,6 @@ body:
label: Minimal reproduction example (MRE)
description: |
Having reproducible issues is a prerequisite for contributors to be able to solve them.
Include a link to minimal reproduction example using [this Google Colab notebook](https://colab.research.google.com/github/pyannote/pyannote-audio/blob/develop/tutorials/MRE_template.ipynb) as a starting point.
Include a link to minimal reproduction example using [this Google Colab notebook](https://colab.research.google.com/github/pyannote/pyannote-audio/blob/main/tutorials/MRE_template.ipynb) as a starting point.
validations:
required: true
Binary file not shown.

After

Width:  |  Height:  |  Size: 705 KiB

+35
View File
@@ -0,0 +1,35 @@
name: Build Wheels
on:
workflow_call:
inputs:
release-tag:
description: Release tag; empty on pull requests
required: false
type: string
default: ""
permissions:
contents: read
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
# hatch-vcs needs Git history and tags to derive the package version.
fetch-depth: 0
- uses: astral-sh/setup-uv@37802adc94f370d6bfd71619e3f0bf239e1f3b78 # v7.6.0
- uses: actions/setup-python@ece7cb06caefa5fff74198d8649806c4678c61a1 # v6.3.0
with:
python-version-file: .python-version
- name: Build wheel and source distribution
run: uv build
- uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: python-package-distributions
path: dist/
if-no-files-found: error
retention-days: ${{ inputs.release-tag != '' && 90 || 3 }}
+87
View File
@@ -0,0 +1,87 @@
name: CI
on:
pull_request:
release:
types: [published]
permissions:
contents: read
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
# lint (PR only)
# build ──┬──► test ──────────┬──► publish (release only)
# └──► version-guard ─┘
# ci-complete collects all required results.
jobs:
lint:
if: github.event_name == 'pull_request'
uses: ./.github/workflows/lint.yml
build:
uses: ./.github/workflows/build-wheels.yml
with:
release-tag: ${{ github.event.release.tag_name || '' }}
test:
needs: build
uses: ./.github/workflows/test.yml
version-guard:
name: versions
if: github.event_name == 'release'
needs: build
uses: ./.github/workflows/version-guard.yml
with:
release-tag: ${{ github.event.release.tag_name }}
# PyPI trusted publishing must run in a top-level workflow.
publish:
if: github.event_name == 'release'
needs: [build, test, version-guard]
runs-on: ubuntu-latest
environment:
name: pypi
url: https://pypi.org/project/pyannote-audio/
permissions:
contents: write
id-token: write
steps:
- uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
with:
name: python-package-distributions
path: dist/
- name: Upload distributions to GitHub Release
uses: softprops/action-gh-release@3d0d9888cb7fd7b750713d6e236d1fcb99157228 # v3.0.2
with:
tag_name: ${{ github.event.release.tag_name }}
files: |
dist/*.whl
dist/*.tar.gz
fail_on_unmatched_files: true
- uses: astral-sh/setup-uv@37802adc94f370d6bfd71619e3f0bf239e1f3b78 # v7.6.0
- name: Upload to PyPI
run: >-
uv publish --trusted-publishing always
--publish-url https://upload.pypi.org/legacy/
--check-url https://pypi.org/simple/
ci-complete:
if: always()
needs: [lint, build, test, version-guard, publish]
runs-on: ubuntu-latest
env:
RESULTS: ${{ join(needs.*.result, ' ') }}
steps:
- name: Check results
run: |
for result in $RESULTS; do
if [[ "$result" == failure || "$result" == cancelled ]]; then
echo "::error::Required job finished with result: $result"
exit 1
fi
done
+48 -25
View File
@@ -1,33 +1,56 @@
name: Documentation
on:
pull_request:
push:
branches:
- master
branches: [main]
permissions:
contents: read
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
jobs:
build-and-deploy:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
persist-credentials: false
fetch-depth: 0
- name: Install uv
uses: astral-sh/setup-uv@v5
with:
enable-cache: true
cache-dependency-glob: uv.lock
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
# hatch-vcs needs Git history and tags to derive the package version.
fetch-depth: 0
- uses: astral-sh/setup-uv@37802adc94f370d6bfd71619e3f0bf239e1f3b78 # v7.6.0
- uses: actions/setup-python@ece7cb06caefa5fff74198d8649806c4678c61a1 # v6.3.0
with:
python-version-file: .python-version
- name: Install documentation dependencies
run: |
uv venv
uv pip install --no-deps .
uv pip install sphinx sphinx-rtd-theme
- name: Build documentation
run: uv run --no-project --python .venv/bin/python sphinx-build -W --keep-going -b html doc/source doc/build/html
- uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: documentation
path: doc/build/html
include-hidden-files: true
retention-days: 3
- name: Install the project
run: uv sync --extra doc
- name: Build documentation
run: |
make --directory=doc html
touch ./doc/build/html/.nojekyll
- name: Deploy
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./doc/build/html
deploy:
if: github.event_name == 'push'
needs: build
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
with:
name: documentation
path: doc/build/html
- uses: peaceiris/actions-gh-pages@373f7f263a76c20808c831209c920827a82a2847 # v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: doc/build/html
+30
View File
@@ -0,0 +1,30 @@
name: Lint
on:
workflow_call:
permissions:
contents: read
jobs:
ruff:
name: python
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
# Report lint/format issues without blocking PRs.
# Remove continue-on-error once the codebase is clean.
- name: Run ruff check
uses: astral-sh/ruff-action@0ce1b0bf8b818ef400413f810f8a11cdbda0034b # v4.0.0
continue-on-error: true
with:
version: "0.17.1"
args: "check ."
- name: Run ruff format
uses: astral-sh/ruff-action@0ce1b0bf8b818ef400413f810f8a11cdbda0034b # v4.0.0
continue-on-error: true
with:
version: "0.17.1"
args: "format --check ."
-125
View File
@@ -1,125 +0,0 @@
name: Publish Python 🐍 distribution 📦 to PyPI and TestPyPI
on: push
jobs:
build:
name: Build distribution 📦
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
persist-credentials: false
fetch-depth: 0
- name: Install uv
uses: astral-sh/setup-uv@v5
with:
enable-cache: true
cache-dependency-glob: uv.lock
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version-file: ".python-version"
- name: Build
run: uv build
- name: Store the distribution packages
uses: actions/upload-artifact@v4
with:
name: python-package-distributions
path: dist/
publish-to-pypi:
name: >-
Publish Python 🐍 distribution 📦 to PyPI
if: startsWith(github.ref, 'refs/tags/') # only publish to PyPI on tag pushes
needs:
- build
runs-on: ubuntu-latest
environment:
name: pypi
permissions:
id-token: write
steps:
- name: Download all the dists
uses: actions/download-artifact@v4
with:
name: python-package-distributions
path: dist/
- name: Install uv
uses: astral-sh/setup-uv@v5
with:
enable-cache: true
cache-dependency-glob: uv.lock
- name: Publish distribution 📦 to PyPI
run: uv publish --trusted-publishing always --publish-url https://upload.pypi.org/legacy/
github-release:
name: >-
Sign the Python 🐍 distribution 📦 with Sigstore
and upload them to GitHub Release
needs:
- publish-to-pypi
runs-on: ubuntu-latest
permissions:
contents: write # IMPORTANT: mandatory for making GitHub Releases
id-token: write # IMPORTANT: mandatory for sigstore
steps:
- name: Download all the dists
uses: actions/download-artifact@v4
with:
name: python-package-distributions
path: dist/
- name: Sign the dists with Sigstore
uses: sigstore/gh-action-sigstore-python@v3.0.0
with:
inputs: >-
./dist/*.tar.gz
./dist/*.whl
- name: Create GitHub Release
env:
GITHUB_TOKEN: ${{ github.token }}
run: >-
gh release create
"$GITHUB_REF_NAME"
--repo "$GITHUB_REPOSITORY"
--notes ""
- name: Upload artifact signatures to GitHub Release
env:
GITHUB_TOKEN: ${{ github.token }}
# Upload to GitHub Release using the `gh` CLI.
# `dist/` contains the built packages, and the
# sigstore-produced signatures and certificates.
run: >-
gh release upload
"$GITHUB_REF_NAME" dist/**
--repo "$GITHUB_REPOSITORY"
# publish-to-testpypi:
# name: Publish Python 🐍 distribution 📦 to TestPyPI
# needs:
# - build
# runs-on: ubuntu-latest
#
# environment:
# name: testpypi
#
# permissions:
# id-token: write # IMPORTANT: mandatory for trusted publishing
#
# steps:
# - name: Download all the dists
# uses: actions/download-artifact@v4
# with:
# name: python-package-distributions
# path: dist/
# - name: Install uv
# uses: astral-sh/setup-uv@v5
# with:
# enable-cache: true
# cache-dependency-glob: uv.lock
# - name: Publish distribution 📦 to PyPI
# run: uv publish --trusted-publishing always --publish-url https://test.pypi.org/legacy/
+92 -23
View File
@@ -1,36 +1,105 @@
name: Test
on:
pull_request:
branches:
- develop
push:
branches:
- develop
- master
- release/*
workflow_call:
permissions:
contents: read
jobs:
test:
name: Test
name: Test Python ${{ matrix.python-version }}
runs-on: ubuntu-latest
strategy:
matrix:
python-version:
- "3.10"
- "3.11"
- "3.12"
env:
UV_PYTHON: ${{ matrix.python-version }}
PYANNOTE_METRICS_ENABLED: "0"
HF_HUB_DISABLE_TELEMETRY: "1"
HF_HUB_OFFLINE: "1"
OMP_NUM_THREADS: "2"
strategy:
fail-fast: false
matrix:
python-version: ["3.11", "3.12", "3.13", "3.14"]
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- uses: astral-sh/setup-uv@37802adc94f370d6bfd71619e3f0bf239e1f3b78 # v7.6.0
- id: python
uses: actions/setup-python@ece7cb06caefa5fff74198d8649806c4678c61a1 # v6.3.0
with:
python-version: ${{ matrix.python-version }}
- uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
with:
name: python-package-distributions
path: dist/
- name: Install FFmpeg
run: sudo apt-get update && sudo apt-get install -y ffmpeg
# TorchCodec needs FFmpeg's shared libraries, but not recommended extras.
run: |
sudo apt-get update
sudo apt-get install -y --no-install-recommends ffmpeg
- name: Install the built wheel with test dependencies
run: |
uv venv --python "${{ steps.python.outputs.python-path }}"
uv pip install --torch-backend cpu "$(echo dist/*.whl)[test,cli]"
- name: Run tests against the installed wheel
run: uv run --no-project --python .venv/bin/python python -m pytest tests -m "not network and not notebook"
- name: Install uv
uses: astral-sh/setup-uv@v5
integration:
name: Public model integration (Python 3.11)
runs-on: ubuntu-latest
env:
PYANNOTE_METRICS_ENABLED: "0"
HF_HUB_DISABLE_TELEMETRY: "1"
OMP_NUM_THREADS: "2"
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- uses: astral-sh/setup-uv@37802adc94f370d6bfd71619e3f0bf239e1f3b78 # v7.6.0
- id: python
uses: actions/setup-python@ece7cb06caefa5fff74198d8649806c4678c61a1 # v6.3.0
with:
python-version: "3.11"
- uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
with:
name: python-package-distributions
path: dist/
- name: Install FFmpeg
# TorchCodec needs FFmpeg's shared libraries, but not recommended extras.
run: |
sudo apt-get update
sudo apt-get install -y --no-install-recommends ffmpeg
- name: Install the built wheel with test dependencies
run: |
uv venv --python "${{ steps.python.outputs.python-path }}"
uv pip install --torch-backend cpu "$(echo dist/*.whl)[test,cli]"
- name: Run tests against the installed wheel
run: uv run --no-project --python .venv/bin/python python -m pytest tests -m network
- name: Run tests
run: uv run --extra test pytest
separation:
name: Optional separation (Python 3.11)
runs-on: ubuntu-latest
env:
PYANNOTE_METRICS_ENABLED: "0"
HF_HUB_DISABLE_TELEMETRY: "1"
HF_HUB_OFFLINE: "1"
OMP_NUM_THREADS: "2"
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- uses: astral-sh/setup-uv@37802adc94f370d6bfd71619e3f0bf239e1f3b78 # v7.6.0
- id: python
uses: actions/setup-python@ece7cb06caefa5fff74198d8649806c4678c61a1 # v6.3.0
with:
python-version: "3.11"
- uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
with:
name: python-package-distributions
path: dist/
- name: Install the built wheel with test dependencies
run: |
uv venv --python "${{ steps.python.outputs.python-path }}"
uv pip install --torch-backend cpu "$(echo dist/*.whl)[test,cli,separation]"
- name: Run tests against the installed wheel
run: uv run --no-project --python .venv/bin/python python -m pytest tests/test_separation.py
+70
View File
@@ -0,0 +1,70 @@
name: Version Guard
on:
workflow_call:
inputs:
release-tag:
description: Release tag expected to match the package version
required: true
type: string
permissions:
contents: read
jobs:
not-already-published:
name: pyannote-audio
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- uses: actions/setup-python@ece7cb06caefa5fff74198d8649806c4678c61a1 # v6.3.0
with:
python-version-file: .python-version
- uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
with:
name: python-package-distributions
path: dist/
- name: Compare distributions with PyPI
env:
RELEASE_TAG: ${{ inputs.release-tag }}
shell: python
run: |
import hashlib
import json
import os
from pathlib import Path
from urllib.error import HTTPError
from urllib.request import urlopen
from zipfile import ZipFile
from email.parser import BytesParser
directory = Path("dist")
tag = os.environ["RELEASE_TAG"]
wheels = list(directory.glob("*.whl"))
sources = list(directory.glob("*.tar.gz"))
if len(wheels) != 1 or len(sources) != 1:
raise ValueError("Expected exactly one wheel and one source distribution")
with ZipFile(wheels[0]) as wheel:
metadata_path, = [n for n in wheel.namelist() if n.endswith(".dist-info/METADATA")]
metadata = BytesParser().parsebytes(wheel.read(metadata_path))
version = metadata["Version"]
if metadata["Name"] != "pyannote-audio" or version != tag.removeprefix("v"):
raise ValueError(f"Release tag {tag!r} does not match pyannote-audio {version}")
try:
with urlopen(f"https://pypi.org/pypi/pyannote-audio/{version}/json", timeout=30) as response:
published = json.load(response)["urls"]
except HTTPError as error:
if error.code != 404:
raise
published = []
hashes = {item["filename"]: item["digests"]["sha256"] for item in published}
for artifact in wheels + sources:
if artifact.name not in hashes:
print(f"{artifact.name}: not published yet")
elif hashlib.sha256(artifact.read_bytes()).hexdigest() == hashes[artifact.name]:
print(f"{artifact.name}: already published with identical contents")
else:
raise ValueError(f"{artifact.name} already exists with different contents. Use a new version.")
+4
View File
@@ -89,3 +89,7 @@ pyannote/audio/version.py
# vim
.vim
.venv/
.ruff_cache/
.pytest_cache/
+6 -39
View File
@@ -1,40 +1,7 @@
exclude: '^docs/conf.py'
repos:
# # Clean Notebooks
# - repo: https://github.com/kynan/nbstripout
# rev: master
# hooks:
# - id: nbstripout
# Format Code
- repo: https://github.com/ambv/black
rev: 22.3.0
hooks:
- id: black
# Sort imports
- repo: https://github.com/PyCQA/isort
rev: 5.12.0
hooks:
- id: isort
args: ["--profile", "black"]
# Formatting, Whitespace, etc
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.2.3
hooks:
- id: trailing-whitespace
- id: check-added-large-files
args: ['--maxkb=1000']
- id: check-ast
- id: check-json
- id: check-merge-conflict
- id: check-xml
- id: check-yaml
- id: debug-statements
- id: end-of-file-fixer
- id: requirements-txt-fixer
- id: mixed-line-ending
args: ['--fix=no']
- id: flake8
args: ['--ignore=E203,E501,F811,E712,W503']
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.17.1
hooks:
- id: ruff-check
args: [--fix]
- id: ruff-format
+1 -1
View File
@@ -1 +1 @@
3.10
3.11
+21 -3
View File
@@ -1,5 +1,18 @@
# CHANGELOG
## next
- BREAKING: require Python 3.11 or newer; Python 3.10 is no longer supported
- fix(model): restore `fbank_only` mode in WeSpeaker embedding models
- fix(task): avoid training crashes with `spawn`/`forkserver` workers, including
Python 3.14's default on Linux, by warning and using `num_workers=0`
- fix(pipeline): omit unset `token` and `cache_dir` when expanding `$model/` references so loaders can inherit runtime options
- fix(io): fix `Audio.crop` raising when an in-memory waveform is cropped up to its very end [@MohammadHijjawi97](https://github.com/MohammadHijjawi97)
- feat(cli): add `--subfolder` option to `benchmark` command [@antoinelaurent](https://github.com/antoinelaurent)
- fix(utils): fix `SpeakerDiarizationMixin.optimal_mapping` `reference` is a mapping [@Kayvan-Zahiri](https://github.com/Kayvan-Zahiri)
- fix(task): fix `balance` option crashing training with `KeyError: 'metadata'` [@MohammadHijjawi97](https://github.com/MohammadHijjawi97)
## Version 4.0.7 (2026-06-30)
- fix(pipeline): add missing support for `subfolder` in `Pipeline.from_pretrained`
@@ -56,10 +69,12 @@ This is a feature which is [backported from our latest commercial model](https:/
```python
from pyannote.audio import Pipeline
pipeline = Pipeline.from_pretrained(
"pyannote/speaker-diarization-community-1", token="huggingface-access-token")
"pyannote/speaker-diarization-community-1", token="huggingface-access-token"
)
output = pipeline("/path/to/conversation.wav")
print(output.speaker_diarization) # regular speaker diarization
print(output.speaker_diarization) # regular speaker diarization
print(output.exclusive_speaker_diarization) # exclusive speaker diarization
```
@@ -97,7 +112,10 @@ Pipelines can now be stored alongside their internal models in the same reposito
```python
# load pipeline from disk (works without internet connection)
from pyannote.audio import Pipeline
pipeline = Pipeline.from_pretrained('/path/to/directory/pyannote-speaker-diarization-community-1')
pipeline = Pipeline.from_pretrained(
"/path/to/directory/pyannote-speaker-diarization-community-1"
)
# run the pipeline locally on your computer
diarization = pipeline("audio.wav")
-6
View File
@@ -1,6 +0,0 @@
recursive-include src/pyannote *.py
recursive-include src/pyannote *.yaml
recursive-include src/pyannote *.wav
recursive-include src/pyannote *.rttm
global-exclude *.pyc
global-exclude __pycache__
+21 -12
View File
@@ -1,6 +1,4 @@
<p align="center">
<a href="https://pyannote.ai/" target="blank"><img src="https://avatars.githubusercontent.com/u/162698670" width="64" /></a>
</p>
![pyannote.audio social preview](https://raw.githubusercontent.com/pyannote/pyannote-audio/main/.github/social-preview.png)
<div align="center">
<h1><code>pyannote</code> speaker diarization toolkit</h1>
@@ -35,8 +33,8 @@ from pyannote.audio.pipelines.utils.hook import ProgressHook
# Community-1 open-source speaker diarization pipeline
pipeline = Pipeline.from_pretrained(
"pyannote/speaker-diarization-community-1",
token="HUGGINGFACE_ACCESS_TOKEN")
"pyannote/speaker-diarization-community-1", token="HUGGINGFACE_ACCESS_TOKEN"
)
# send pipeline to GPU (when available)
pipeline.to(torch.device("cuda"))
@@ -52,7 +50,6 @@ for turn, speaker in output.speaker_diarization:
# start=1.8s stop=3.9s speaker_1
# start=4.2s stop=5.7s speaker_0
# ...
```
## `precision-2` premium speaker diarization
@@ -65,7 +62,8 @@ from pyannote.audio import Pipeline
# Precision-2 premium speaker diarization service
pipeline = Pipeline.from_pretrained(
"pyannote/speaker-diarization-precision-2", token="PYANNOTEAI_API_KEY")
"pyannote/speaker-diarization-precision-2", token="PYANNOTEAI_API_KEY"
)
output = pipeline("audio.wav") # runs on pyannoteAI servers
@@ -73,7 +71,7 @@ output = pipeline("audio.wav") # runs on pyannoteAI servers
for turn, speaker in output.speaker_diarization:
print(f"start={turn.start:.1f}s stop={turn.end:.1f}s {speaker}")
# start=0.2s stop=1.6s SPEAKER_00
# start=1.8s stop=4.0s SPEAKER_01
# start=1.8s stop=4.0s SPEAKER_01
# start=4.2s stop=5.6s SPEAKER_00
# ...
```
@@ -182,7 +180,7 @@ set_telemetry_metrics(False, save_choice_as_default=True)
## Documentation
- [Changelog](CHANGELOG.md)
- [Changelog](https://github.com/pyannote/pyannote-audio/blob/main/CHANGELOG.md)
- Videos
- [Speaker diarization, a ~~love~~ loss story](https://www.youtube.com/watch?v=CtjDotATEI0&list=PLSeS0sl8xpTwz7h5iJSniiF89iUdZXNJ2&index=13) / JSALT 2025 plenary talk / 60 min
- [Introduction to speaker diarization](https://umotion.univ-lemans.fr/video/9513-speech-segmentation-and-speaker-diarization/) / JSALT 2023 summer school / 90 min
@@ -233,12 +231,23 @@ If you use `pyannote.audio` please use the following citations:
The commands below will setup pre-commit hooks and packages needed for developing the `pyannote.audio` library.
```bash
pip install -e .[dev,testing]
pre-commit install
uv sync --extra dev --extra test --extra cli
uvx pre-commit install
```
## Test
```bash
pytest
uv run --extra test --extra cli pytest -m "not network and not notebook"
```
Tests marked `network` download a public model from Hugging Face:
```bash
uv run --extra test --extra cli pytest -m network
```
Historical notebooks are opt-in (`pytest -m notebook`) and may require additional
models, datasets, credentials, and updates for the current API.
Run lint and formatting checks with `uvx ruff check .` and `uvx ruff format --check .`.
+6 -13
View File
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
#
# pyannote.audio documentation build configuration file, created by
# sphinx-quickstart on Tue Jan 24 15:45:55 2017.
@@ -18,11 +17,7 @@
#
# allow pyannote.audio import
import os
import sys
sys.path.insert(0, os.path.abspath("../.."))
from importlib.metadata import version as package_version
# -- General configuration ------------------------------------------------
@@ -73,19 +68,18 @@ master_doc = "index"
# General information about the project.
project = "pyannote.audio"
copyright = "2017, CNRS"
copyright = "2025–, pyannoteAI"
author = "Hervé Bredin"
# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
import pyannote.audio # noqa
# The short X.Y version.
version = pyannote.audio.__version__.split("+")[0]
version = package_version("pyannote-audio").split("+")[0]
# The full version, including alpha/beta/rc tags.
release = pyannote.audio.__version__
release = package_version("pyannote-audio")
# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
@@ -122,7 +116,7 @@ html_theme = "sphinx_rtd_theme"
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ["_static"]
html_static_path = []
# -- Options for HTMLHelp output ------------------------------------------
@@ -188,9 +182,8 @@ texinfo_documents = [
# Example configuration for intersphinx: refer to the Python standard library.
intersphinx_mapping = {
"python": ("https://docs.python.org/", None),
"python": ("https://docs.python.org/3/", None),
"pyannote.core": ("https://pyannote.github.io/pyannote-core", None),
"pyannote.database": ("https://pyannote.github.io/pyannote-database", None),
"pyannote.metrics": ("https://pyannote.github.io/pyannote-metrics", None),
"pyannote.pipeline": ("https://pyannote.github.io/pyannote-pipeline", None),
}
+5
View File
@@ -4,3 +4,8 @@ pyannote.audio
##############
`pyannote.audio` is an open-source toolkit for state-of-the-art speaker diarization.
.. toctree::
:maxdepth: 1
Changelog <https://github.com/pyannote/pyannote-audio/blob/main/CHANGELOG.md>
+28 -3
View File
@@ -2,8 +2,9 @@
name = "pyannote-audio"
description = "State-of-the-art speaker diarization toolkit"
readme = "README.md"
license = "MIT"
authors = [{ name = "Hervé BREDIN", email = "herve@pyannote.ai" }]
requires-python = ">=3.10"
requires-python = ">=3.11"
dynamic = ["version"]
@@ -27,7 +28,7 @@ dependencies = [
"torchaudio>=2.8.0",
"torchcodec>=0.7.0",
"torchmetrics>=1.6.1",
"matplotlib>=3.10.0",
"matplotlib>=3.11.0",
"pyannoteai-sdk>=0.3.0",
]
@@ -42,7 +43,7 @@ separation = ["asteroid>=0.5.2", "transformers>=4.48.3"]
dev = ["ipython>=8.32.0"]
[build-system]
requires = ["hatchling", "hatch-vcs"]
requires = ["hatchling==1.32.0", "hatch-vcs"]
build-backend = "hatchling.build"
[tool.hatch.version]
@@ -51,5 +52,29 @@ source = "vcs"
[tool.hatch.build.targets.wheel]
packages = ["src/pyannote"]
[tool.hatch.build.targets.sdist]
include = [
"/src", "/tests", "/doc", "/README.md", "/CHANGELOG.md", "/LICENSE",
"/pyproject.toml",
]
[dependency-groups]
dev = ["ipykernel>=6.29.5"]
[tool.pytest.ini_options]
markers = [
"network: downloads public models from Hugging Face",
"notebook: executes historical tutorial notebooks (opt in explicitly)",
]
addopts = "-m 'not notebook'"
[tool.ruff]
target-version = "py311"
# Historical notebooks target older APIs and are maintained separately.
extend-exclude = ["*.ipynb", "tutorials/AMI-diarization-setup"]
[tool.ruff.lint]
select = ["E4", "E7", "E9", "F", "I", "UP"]
# Imports intentionally follow version initialization or notebook setup.
# Preserve existing enum string behavior and mathematical variable names.
ignore = ["E402", "E741", "UP042"]
-64
View File
@@ -1,64 +0,0 @@
import os
import sys
from pathlib import Path
from pkg_resources import VersionConflict, require
from setuptools import find_packages, setup
with open("README.md", mode="r", encoding="utf-8") as f:
long_description = f.read()
with open("requirements.txt", mode="r", encoding="utf-8") as f:
requirements = f.read().splitlines()
try:
require("setuptools>=38.3")
except VersionConflict:
print("Error: version of setuptools is too old (<38.3)!")
sys.exit(1)
ROOT_DIR = Path(__file__).parent.resolve()
# Creating the version file
with open("version.txt", mode="r", encoding="utf-8") as f:
version = f.read()
version = version.strip()
sha = "Unknown"
if os.getenv("BUILD_VERSION"):
version = os.getenv("BUILD_VERSION")
elif sha != "Unknown":
version += "+" + sha[:7]
print("-- Building version " + version)
version_path = ROOT_DIR / "pyannote" / "audio" / "version.py"
with open(version_path, mode="w", encoding="utf-8") as f:
f.write("__version__ = '{}'\n".format(version))
if __name__ == "__main__":
setup(
name="pyannote.audio",
namespace_packages=["pyannote"],
version=version,
packages=find_packages(),
install_requires=requirements,
description="Neural building blocks for speaker diarization",
long_description=long_description,
long_description_content_type="text/markdown",
author="Hervé Bredin",
author_email="herve.bredin@irit.fr",
url="https://github.com/pyannote/pyannote-audio",
classifiers=[
"Development Status :: 4 - Beta",
"Intended Audience :: Science/Research",
"License :: OSI Approved :: MIT License",
"Natural Language :: English",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Topic :: Scientific/Engineering",
],
)
+1 -1
View File
@@ -30,4 +30,4 @@ from .core.io import Audio
from .core.model import Model
from .core.pipeline import Pipeline
__all__ = ["Audio", "Model", "Inference", "Pipeline"]
__all__ = ["Audio", "Inference", "Model", "Pipeline"]
+35 -33
View File
@@ -1,6 +1,3 @@
#!/usr/bin/env python
# encoding: utf-8
# MIT License
#
# Copyright (c) 2024-2025 CNRS
@@ -33,21 +30,21 @@ from datetime import datetime
from enum import Enum
from functools import partial
from pathlib import Path
from typing import Optional
from typing import TYPE_CHECKING, Annotated
import numpy as np
import pyannote.database
import torch
import typer
import yaml
from pyannote.audio import Audio, Model, Pipeline
from pyannote.core import Annotation
from pyannote.metrics.base import BaseMetric
from pyannote.metrics.diarization import DiarizationErrorRate, JaccardErrorRate
from pyannote.pipeline.optimizer import Optimizer
from rich.progress import track
from scipy.optimize import minimize_scalar
from typing_extensions import Annotated
from pyannote.audio import Audio, Model, Pipeline
class Subset(str, Enum):
@@ -140,7 +137,7 @@ def optimize(
Device, typer.Option(help="Accelerator to use (CPU, CUDA, MPS)")
] = Device.AUTO,
registry: Annotated[
Optional[Path],
Path | None,
typer.Option(
help="Loaded registry",
exists=True,
@@ -150,7 +147,7 @@ def optimize(
),
] = None,
max_iterations: Annotated[
Optional[int],
int | None,
typer.Option(help="Number of iterations to run. Defaults to run indefinitely."),
] = None,
num_speakers: Annotated[
@@ -174,7 +171,7 @@ def optimize(
# load pipeline configuration file in memory. this will
# be dumped later to disk with optimized parameters
with open(pipeline, "r") as fp:
with open(pipeline) as fp:
original_config = yaml.load(fp, Loader=yaml.SafeLoader)
# load pipeline
@@ -241,7 +238,6 @@ def optimize(
global_best_loss: float = optimizer.best_loss
local_best_loss: float = global_best_loss
#
try:
warm_start = optimized_pipeline.default_parameters()
except NotImplementedError:
@@ -291,17 +287,17 @@ def download(
),
],
revision: Annotated[
Optional[str],
str | None,
typer.Option(
help="Pretrained pipeline revision.",
),
] = None,
token: Annotated[
Optional[str],
str | None,
typer.Argument(help="Huggingface token."),
] = None,
cache: Annotated[
Optional[Path],
Path | None,
typer.Option(
help="Path to the folder where files downloaded from Huggingface are stored.",
exists=True,
@@ -344,7 +340,7 @@ def apply(
),
],
into: Annotated[
Optional[Path],
Path | None,
typer.Option(
help="Path to file or directory where results are saved.",
exists=False,
@@ -355,17 +351,17 @@ def apply(
),
] = None,
revision: Annotated[
Optional[str],
str | None,
typer.Option(
help="Pretrained pipeline revision.",
),
] = None,
token: Annotated[
Optional[str],
str | None,
typer.Argument(help="Huggingface token."),
] = None,
cache: Annotated[
Optional[Path],
Path | None,
typer.Option(
help="Path to the folder where files downloaded from Huggingface are stored.",
exists=True,
@@ -427,6 +423,10 @@ def apply(
json.dump(serialized, j, indent=2)
if TYPE_CHECKING:
from pandas import DataFrame
class MinDurationOffOptimizer:
"""Utility to optimize `min_duration_off`
@@ -489,7 +489,7 @@ class MinDurationOffOptimizer:
# assumes metric should be minimized
# TODO: use -inf when/if metric should be maximized
self._best_metric = float("inf")
self._reports: dict[float, "DataFrame"] = dict()
self._reports: dict[float, DataFrame] = {}
# force test with no collar
no_collar_metric = self._compute_metric(files, metric, 0.0)
@@ -541,17 +541,23 @@ def benchmark(
),
] = Subset.test,
revision: Annotated[
Optional[str],
str | None,
typer.Option(
help="Pretrained pipeline revision.",
),
] = None,
subfolder: Annotated[
str | None,
typer.Option(
help="Folder inside the hf.co model repo (or local directory) to load the pipeline from.",
),
] = None,
token: Annotated[
Optional[str],
str | None,
typer.Argument(help="Huggingface token."),
] = None,
cache: Annotated[
Optional[Path],
Path | None,
typer.Option(
help="Path to the folder where files downloaded from Huggingface are stored.",
exists=True,
@@ -565,7 +571,7 @@ def benchmark(
Device, typer.Option(help="Accelerator to use (CPU, CUDA, MPS)")
] = Device.AUTO,
registry: Annotated[
Optional[Path],
Path | None,
typer.Option(
help="Loaded registry",
exists=True,
@@ -606,6 +612,7 @@ def benchmark(
pretrained_pipeline = Pipeline.from_pretrained(
pipeline,
revision=revision,
subfolder=subfolder,
token=token,
cache_dir=cache,
)
@@ -651,7 +658,7 @@ def benchmark(
benchmark_name += ".OracleNumSpeakers"
# used to store raw predictions in JSON format
serialized_predictions: dict[str, dict] = dict()
serialized_predictions: dict[str, dict] = {}
if not skip_metric:
# initialize diarization error rate metric
@@ -660,7 +667,7 @@ def benchmark(
# speaker count confusion matrix
# speaker_count[i][j] is the number of files with i speakers in the
# manual annotation and j speakers in the prediction
speaker_count: dict[int, dict[int, int]] = dict()
speaker_count: dict[int, dict[int, int]] = {}
if per_file:
benchmark_dir = into / benchmark_name
@@ -717,9 +724,7 @@ def benchmark(
# increment speaker count confusion matrix
pred_num_speakers: int = len(speaker_diarization.labels())
true_num_speakers: int = len(file["annotation"].labels())
speaker_count.setdefault(true_num_speakers, dict()).setdefault(
pred_num_speakers, 0
)
speaker_count.setdefault(true_num_speakers, {}).setdefault(pred_num_speakers, 0)
speaker_count[true_num_speakers][pred_num_speakers] += 1
# keep track of prediction for later "min_duration_off" optimization
@@ -734,7 +739,7 @@ def benchmark(
json.dump(serialized_predictions, f, indent=2)
# log processing time and capacity
processing = dict()
processing = {}
total_processing_time = tac - tic
total_playing_time = sum(Audio().get_duration(file) for file in files)
processing["seconds_per_hour"] = total_processing_time / (total_playing_time / 3600)
@@ -779,8 +784,7 @@ def benchmark(
# and save it to disk as a CSV file
max_true_speakers = max(speaker_count.keys())
max_pred_speakers = max(
max(speaker_count[true_speakers].keys())
for true_speakers in speaker_count.keys()
max(speaker_count[true_speakers].keys()) for true_speakers in speaker_count
)
speaker_count_matrix = np.zeros(
(max_true_speakers + 1, max_pred_speakers + 1), dtype=int
@@ -821,9 +825,7 @@ def benchmark(
with open(into / f"{benchmark_name}.OptimizedMinDurationOff.txt", "w") as txt:
txt.write(
best_report.to_string(
sparsify=False, float_format=lambda f: "{0:.2f}".format(f)
)
best_report.to_string(sparsify=False, float_format=lambda f: f"{f:.2f}")
)
# keep track of the best `min_duration_off` value for later reference
+14 -14
View File
@@ -21,7 +21,7 @@
# SOFTWARE.
from typing import Optional
from typing import ClassVar
import torch
from torch import Tensor
@@ -46,7 +46,7 @@ class MixSpeakerDiarization(Mix):
of speakers in each batch.
"""
supported_modes = {"per_example", "per_channel"}
supported_modes: ClassVar[set] = {"per_example", "per_channel"}
supports_multichannel = True
requires_sample_rate = False
@@ -60,10 +60,10 @@ class MixSpeakerDiarization(Mix):
max_snr_in_db: float = 5.0,
mode: str = "per_example",
p: float = 0.5,
p_mode: Optional[str] = None,
sample_rate: Optional[int] = None,
target_rate: Optional[int] = None,
max_num_speakers: Optional[int] = None,
p_mode: str | None = None,
sample_rate: int | None = None,
target_rate: int | None = None,
max_num_speakers: int | None = None,
output_type: str = "tensor",
):
super().__init__(
@@ -80,12 +80,12 @@ class MixSpeakerDiarization(Mix):
def randomize_parameters(
self,
samples: Optional[Tensor] = None,
sample_rate: Optional[int] = None,
targets: Optional[Tensor] = None,
target_rate: Optional[int] = None,
samples: Tensor | None = None,
sample_rate: int | None = None,
targets: Tensor | None = None,
target_rate: int | None = None,
):
batch_size, num_channels, num_samples = samples.shape
batch_size, _num_channels, _num_samples = samples.shape
snr_distribution = torch.distributions.Uniform(
low=torch.tensor(
self.min_snr_in_db,
@@ -136,6 +136,6 @@ class MixSpeakerDiarization(Mix):
device=samples.device,
)
]
self.transform_parameters["sample_idx"][
samples_with_n_speakers
] = selected_candidates
self.transform_parameters["sample_idx"][samples_with_n_speakers] = (
selected_candidates
)
+2 -2
View File
@@ -23,7 +23,7 @@
from functools import singledispatch
import torch
import torch.nn as nn
from torch import nn
from torch_audiomentations.core.transforms_interface import BaseWaveformTransform
from pyannote.audio.core.model import Model
@@ -98,7 +98,7 @@ def register_augmentation(
if not hasattr(module, "__augmentation"):
module.__augmentation = nn.ModuleDict()
module.__augmentation_handle = dict()
module.__augmentation_handle = {}
# unregister any augmentation that might already exist
if when in module.__augmentation:
+3 -2
View File
@@ -29,10 +29,11 @@ from typing import Optional
import numpy as np
import safetensors.numpy
import scipy.interpolate
from pyannote.audio.utils.hf_hub import AssetFileName, download_from_hf_hub
from sklearn.isotonic import IsotonicRegression
from sklearn.utils.validation import NotFittedError, check_is_fitted
from pyannote.audio.utils.hf_hub import AssetFileName, download_from_hf_hub
class Calibration(IsotonicRegression):
"""Logit/distance calibration"""
@@ -46,7 +47,7 @@ class Calibration(IsotonicRegression):
nan_value: float = 2.0,
) -> np.ndarray:
"""Apply calibration handling NaN values and any shape gracefully
Parameters
----------
values : np.ndarray
+7 -7
View File
@@ -20,7 +20,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from typing import List, Mapping, Optional, Text, Union
from collections.abc import Mapping
from lightning import Callback, Trainer
from lightning.pytorch.utilities.model_summary import ModelSummary
@@ -66,13 +66,13 @@ class GraduallyUnfreeze(Callback):
def __init__(
self,
schedule: Union[Mapping[Text, int], List[Union[List[Text], Text]]] = None,
epochs_per_stage: Optional[int] = None,
schedule: Mapping[str, int] | list[list[str] | str] | None = None,
epochs_per_stage: int | None = None,
):
super().__init__()
if (
(schedule is None) or (isinstance(schedule, List))
(schedule is None) or (isinstance(schedule, list))
) and epochs_per_stage is None:
epochs_per_stage = 1
@@ -92,10 +92,10 @@ class GraduallyUnfreeze(Callback):
if schedule is None:
schedule = backbone_layers
if isinstance(schedule, List):
_schedule = dict()
if isinstance(schedule, list):
_schedule = {}
for depth, layers in enumerate(schedule):
layers = layers if isinstance(layers, List) else [layers]
layers = layers if isinstance(layers, list) else [layers]
for layer in layers:
_schedule[layer] = (depth + 1) * self.epochs_per_stage
schedule = _schedule
+37 -43
View File
@@ -22,21 +22,22 @@
# SOFTWARE.
import warnings
from typing import Callable, List, Optional, Text, Tuple, Union
from collections.abc import Callable
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from einops import rearrange
from lightning.pytorch.utilities.memory import is_oom_error
from pyannote.core import Segment, SlidingWindow, SlidingWindowFeature
from torch import nn
from pyannote.audio.core.io import AudioFile
from pyannote.audio.core.model import Model, Specifications
from pyannote.audio.core.task import Resolution
from pyannote.audio.utils.multi_task import map_with_specifications
from pyannote.audio.utils.powerset import Powerset
from pyannote.audio.utils.reproducibility import fix_reproducibility
from pyannote.core import Segment, SlidingWindow, SlidingWindowFeature
class BaseInference:
@@ -78,13 +79,13 @@ class Inference(BaseInference):
def __init__(
self,
model: Model,
window: Text = "sliding",
duration: Optional[float] = None,
step: Optional[float] = None,
pre_aggregation_hook: Callable[[np.ndarray], np.ndarray] = None,
window: str = "sliding",
duration: float | None = None,
step: float | None = None,
pre_aggregation_hook: Callable[[np.ndarray], np.ndarray] | None = None,
skip_aggregation: bool = False,
skip_conversion: bool = False,
device: Optional[torch.device] = None,
device: torch.device | None = None,
batch_size: int = 32,
):
# ~~~~ model ~~~~~
@@ -127,7 +128,7 @@ class Inference(BaseInference):
self.skip_conversion = skip_conversion
conversion = list()
conversion = []
for s in specifications:
if s.powerset and not skip_conversion:
c = Powerset(len(s.classes), s.powerset_max_classes)
@@ -179,7 +180,7 @@ class Inference(BaseInference):
self.device = device
return self
def infer(self, chunks: torch.Tensor) -> Union[np.ndarray, Tuple[np.ndarray]]:
def infer(self, chunks: torch.Tensor) -> np.ndarray | tuple[np.ndarray]:
"""Forward pass
Takes care of sending chunks to right device and outputs back to CPU
@@ -205,7 +206,7 @@ class Inference(BaseInference):
f"Try with a smaller value until memory error disappears."
)
else:
raise exception
raise
def __convert(output: torch.Tensor, conversion: nn.Module, **kwargs):
return conversion(output).cpu().numpy()
@@ -218,8 +219,8 @@ class Inference(BaseInference):
self,
waveform: torch.Tensor,
sample_rate: int,
hook: Optional[Callable],
) -> Union[SlidingWindowFeature, Tuple[SlidingWindowFeature]]:
hook: Callable | None,
) -> SlidingWindowFeature | tuple[SlidingWindowFeature]:
"""Slide model on a waveform
Parameters
@@ -229,7 +230,7 @@ class Inference(BaseInference):
sample_rate : int
Sample rate.
hook: Optional[Callable]
When a callable is provided, it is called everytime a batch is
When a callable is provided, it is called every time a batch is
processed with two keyword arguments:
- `completed`: the number of chunks that have been processed so far
- `total`: the total number of chunks
@@ -246,13 +247,13 @@ class Inference(BaseInference):
_, num_samples = waveform.shape
def __frames(
receptive_field, specifications: Optional[Specifications] = None
receptive_field, specifications: Specifications | None = None
) -> SlidingWindow:
if specifications.resolution == Resolution.CHUNK:
return SlidingWindow(start=0.0, duration=self.duration, step=self.step)
return receptive_field
frames: Union[SlidingWindow, Tuple[SlidingWindow]] = map_with_specifications(
frames: SlidingWindow | tuple[SlidingWindow] = map_with_specifications(
self.model.specifications, __frames, self.model.receptive_field
)
@@ -278,10 +279,10 @@ class Inference(BaseInference):
last_chunk = F.pad(last_chunk, (0, last_pad))
def __empty_list(**kwargs):
return list()
return []
outputs: list[np.ndarray] | tuple[list[np.ndarray]] = (
map_with_specifications(self.model.specifications, __empty_list)
outputs: list[np.ndarray] | tuple[list[np.ndarray]] = map_with_specifications(
self.model.specifications, __empty_list
)
if hook is not None:
@@ -289,13 +290,12 @@ class Inference(BaseInference):
def __append_batch(output, batch_output, **kwargs) -> None:
output.append(batch_output)
return
# slide over audio chunks in batch
for c in np.arange(0, num_chunks, self.batch_size):
batch: torch.Tensor = chunks[c : c + self.batch_size]
batch_outputs: Union[np.ndarray, Tuple[np.ndarray]] = self.infer(batch)
batch_outputs: np.ndarray | tuple[np.ndarray] = self.infer(batch)
_ = map_with_specifications(
self.model.specifications, __append_batch, outputs, batch_outputs
@@ -318,17 +318,17 @@ class Inference(BaseInference):
total=num_chunks + has_last_chunk,
)
def __vstack(output: List[np.ndarray], **kwargs) -> np.ndarray:
def __vstack(output: list[np.ndarray], **kwargs) -> np.ndarray:
return np.vstack(output)
outputs: Union[np.ndarray, Tuple[np.ndarray]] = map_with_specifications(
outputs: np.ndarray | tuple[np.ndarray] = map_with_specifications(
self.model.specifications, __vstack, outputs
)
def __aggregate(
outputs: np.ndarray,
frames: SlidingWindow,
specifications: Optional[Specifications] = None,
specifications: Specifications | None = None,
) -> SlidingWindowFeature:
# skip aggregation when requested,
# or when model outputs just one vector per chunk
@@ -373,11 +373,8 @@ class Inference(BaseInference):
)
def __call__(
self, file: AudioFile, hook: Optional[Callable] = None
) -> Union[
Tuple[Union[SlidingWindowFeature, np.ndarray]],
Union[SlidingWindowFeature, np.ndarray],
]:
self, file: AudioFile, hook: Callable | None = None
) -> tuple[SlidingWindowFeature | np.ndarray] | SlidingWindowFeature | np.ndarray:
"""Run inference on a whole file
Parameters
@@ -385,7 +382,7 @@ class Inference(BaseInference):
file : AudioFile
Audio file.
hook : callable, optional
When a callable is provided, it is called everytime a batch is processed
When a callable is provided, it is called every time a batch is processed
with two keyword arguments:
- `completed`: the number of chunks that have been processed so far
- `total`: the total number of chunks
@@ -405,7 +402,7 @@ class Inference(BaseInference):
if self.window == "sliding":
return self.slide(waveform, sample_rate, hook=hook)
outputs: Union[np.ndarray, Tuple[np.ndarray]] = self.infer(waveform[None])
outputs: np.ndarray | tuple[np.ndarray] = self.infer(waveform[None])
def __first_sample(outputs: np.ndarray, **kwargs) -> np.ndarray:
return outputs[0]
@@ -417,12 +414,9 @@ class Inference(BaseInference):
def crop(
self,
file: AudioFile,
chunk: Union[Segment, List[Segment]],
hook: Optional[Callable] = None,
) -> Union[
Tuple[Union[SlidingWindowFeature, np.ndarray]],
Union[SlidingWindowFeature, np.ndarray],
]:
chunk: Segment | list[Segment],
hook: Callable | None = None,
) -> tuple[SlidingWindowFeature | np.ndarray] | SlidingWindowFeature | np.ndarray:
"""Run inference on a chunk or a list of chunks
Parameters
@@ -436,7 +430,7 @@ class Inference(BaseInference):
to "whole", this is equivalent to concatenating each chunk into one
(artificial) chunk before processing it.
hook : callable, optional
When a callable is provided, it is called everytime a batch is processed
When a callable is provided, it is called every time a batch is processed
with two keyword arguments:
- `completed`: the number of chunks that have been processed so far
- `total`: the total number of chunks
@@ -466,8 +460,8 @@ class Inference(BaseInference):
chunk = Segment(start=start, end=end)
waveform, sample_rate = self.model.audio.crop(file, chunk)
outputs: SlidingWindowFeature | tuple[SlidingWindowFeature] = (
self.slide(waveform, sample_rate, hook=hook)
outputs: SlidingWindowFeature | tuple[SlidingWindowFeature] = self.slide(
waveform, sample_rate, hook=hook
)
def __shift(output: SlidingWindowFeature, **kwargs) -> SlidingWindowFeature:
@@ -486,7 +480,7 @@ class Inference(BaseInference):
[self.model.audio.crop(file, c)[0] for c in chunk], dim=1
)
outputs: Union[np.ndarray, Tuple[np.ndarray]] = self.infer(waveform[None])
outputs: np.ndarray | tuple[np.ndarray] = self.infer(waveform[None])
def __first_sample(outputs: np.ndarray, **kwargs) -> np.ndarray:
return outputs[0]
@@ -499,7 +493,7 @@ class Inference(BaseInference):
def aggregate(
scores: SlidingWindowFeature,
frames: SlidingWindow,
warm_up: Tuple[float, float] = (0.0, 0.0),
warm_up: tuple[float, float] = (0.0, 0.0),
epsilon: float = 1e-12,
hamming: bool = False,
missing: float = np.nan,
@@ -622,7 +616,7 @@ class Inference(BaseInference):
@staticmethod
def trim(
scores: SlidingWindowFeature,
warm_up: Tuple[float, float] = (0.1, 0.1),
warm_up: tuple[float, float] = (0.1, 0.1),
) -> SlidingWindowFeature:
"""Trim left and right warm-up regions
+8 -10
View File
@@ -30,9 +30,9 @@ pyannote.audio relies on torchcodec for reading and torchaudio for resampling.
import random
import warnings
from collections.abc import Mapping
from io import IOBase
from pathlib import Path
from typing import Mapping, Optional, Tuple
import torch.nn.functional as F
import torchaudio
@@ -40,9 +40,9 @@ from pyannote.core import Segment
from torch import Tensor
try:
import torchcodec
from torchcodec import AudioSamples
from torchcodec.decoders import AudioDecoder, AudioStreamMetadata
TORCHCODEC_AVAILABLE = True
except Exception as e:
warnings.warn(
@@ -215,14 +215,14 @@ class Audio:
return file
def __init__(self, sample_rate: int = None, mono=None):
def __init__(self, sample_rate: int | None = None, mono=None):
super().__init__()
self.sample_rate = sample_rate
self.mono = mono
def downmix_and_resample(
self, waveform: Tensor, sample_rate: int, channel: int | None = None
) -> Tuple[Tensor, int]:
) -> tuple[Tensor, int]:
"""Downmix and resample
Parameters
@@ -289,9 +289,7 @@ class Audio:
return metadata.duration_seconds_from_header
def get_num_samples(
self, duration: float, sample_rate: Optional[int] = None
) -> int:
def get_num_samples(self, duration: float, sample_rate: int | None = None) -> int:
"""Deterministic number of samples from duration and sample rate"""
sample_rate = sample_rate or self.sample_rate
@@ -303,7 +301,7 @@ class Audio:
return round(duration * sample_rate)
def __call__(self, file: AudioFile) -> Tuple[Tensor, int]:
def __call__(self, file: AudioFile) -> tuple[Tensor, int]:
"""Obtain waveform
Parameters
@@ -355,7 +353,7 @@ class Audio:
file: AudioFile,
segment: Segment,
mode="raise",
) -> Tuple[Tensor, int]:
) -> tuple[Tensor, int]:
"""Fast version of self(file).crop(segment, **kwargs)
Parameters
@@ -400,7 +398,7 @@ class Audio:
end_sample: int = self.get_num_samples(segment.end, sample_rate)
pad_end: int = max(end_sample, num_samples) - num_samples
if end_sample >= num_samples:
if end_sample > num_samples:
if mode == "raise":
raise ValueError(
f"requested chunk with end time (t={segment.end:.3f}s) greater than "
+19 -19
View File
@@ -30,14 +30,17 @@ from dataclasses import dataclass
from functools import cached_property
from importlib import import_module
from pathlib import Path
from typing import Any, Optional
from typing import Any
import lightning
import torch
import torch.nn as nn
import torch.optim
from lightning.fabric.utilities.cloud_io import _load as pl_load
from lightning.pytorch.utilities.model_summary.model_summary import ModelSummary
from pyannote.core import SlidingWindow
from torch import nn
from torch.utils.data import DataLoader
from pyannote.audio import __version__
from pyannote.audio.core.io import Audio
from pyannote.audio.core.task import (
@@ -50,8 +53,6 @@ from pyannote.audio.telemetry import track_model_init
from pyannote.audio.utils.dependencies import check_dependencies
from pyannote.audio.utils.hf_hub import AssetFileName, download_from_hf_hub
from pyannote.audio.utils.multi_task import map_with_specifications
from pyannote.core import SlidingWindow
from torch.utils.data import DataLoader
# NOTE: needed to backward compatibility to load models trained before pyannote.audio 3.x
@@ -135,11 +136,11 @@ class Model(lightning.LightningModule):
"Only regular specifications or tuple of specifications are supported."
)
durations = set(s.duration for s in specifications)
durations = {s.duration for s in specifications}
if len(durations) > 1:
raise ValueError("All tasks must share the same (maximum) duration.")
min_durations = set(s.min_duration for s in specifications)
min_durations = {s.min_duration for s in specifications}
if len(min_durations) > 1:
raise ValueError("All tasks must share the same minimum duration.")
@@ -197,7 +198,7 @@ class Model(lightning.LightningModule):
self.task.setup(stage)
# list of layers before adding task-dependent layers
before = set((name, id(module)) for name, module in self.named_modules())
before = {(name, id(module)) for name, module in self.named_modules()}
# add task-dependent layers (e.g. final classification layer)
# and re-use original weights when compatible
@@ -206,7 +207,7 @@ class Model(lightning.LightningModule):
self.build()
try:
missing_keys, unexpected_keys = self.load_state_dict(
_missing_keys, _unexpected_keys = self.load_state_dict(
original_state_dict, strict=False
)
@@ -219,7 +220,7 @@ class Model(lightning.LightningModule):
)
warnings.warn(msg)
else:
raise e
raise
# move layers that were added by build() to same device as the rest of the model
for name, module in self.named_modules():
@@ -236,10 +237,10 @@ class Model(lightning.LightningModule):
self.task.setup_validation_metric()
# list of layers after adding task-dependent layers
after = set((name, id(module)) for name, module in self.named_modules())
after = {(name, id(module)) for name, module in self.named_modules()}
# list of task-dependent layers
self.task_dependent = list(name for name, _ in after - before)
self.task_dependent = [name for name, _ in after - before]
def on_save_checkpoint(self, checkpoint):
# put everything pyannote.audio-specific under pyannote.audio
@@ -294,8 +295,7 @@ class Model(lightning.LightningModule):
return nn.Sigmoid()
else:
msg = "TODO: implement default activation for other types of problems"
raise NotImplementedError(msg)
return nn.Identity()
return map_with_specifications(self.specifications, __default_activation)
@@ -326,15 +326,15 @@ class Model(lightning.LightningModule):
"""Helper function for freeze_up_to and unfreeze_up_to"""
tokens = module_name.split(".")
updated_modules = list()
updated_modules = []
for name, module in ModelSummary(self, max_depth=-1).named_modules:
name_tokens = name.split(".")
matching_tokens = list(
matching_tokens = [
token
for token, other_token in zip(name_tokens, tokens)
if token == other_token
)
]
# if module is A.a.1 & name is A.a, we do not want to freeze the whole A.a module
# because it might contain other modules like A.a.2 and A.a.3
@@ -414,7 +414,7 @@ class Model(lightning.LightningModule):
) -> list[str]:
"""Helper function for freeze_by_name and unfreeze_by_name"""
updated_modules = list()
updated_modules = []
# Force modules to be a list
if isinstance(modules, str):
@@ -505,7 +505,7 @@ class Model(lightning.LightningModule):
token: str | bool | None = None,
cache_dir: Path | str | None = None,
**kwargs,
) -> Optional["Model"]:
) -> Model | None:
"""Load pretrained model
Parameters
@@ -646,7 +646,7 @@ class Model(lightning.LightningModule):
)
return model
raise e
raise
# save model origin (HF, local, etc) as attribute for telemetry purposes
model._otel_origin = otel_origin
+28 -23
View File
@@ -26,14 +26,18 @@ from __future__ import annotations
import os
import warnings
from collections import OrderedDict
from collections.abc import Iterator
from collections.abc import Callable, Iterator, Mapping
from functools import partial
from pathlib import Path
from typing import Any, Callable, Dict, List, Mapping, Optional
from typing import Any
import torch
import torch.nn as nn
import yaml
from pyannote.core.utils.helper import get_class_by_name
from pyannote.database import FileFinder, ProtocolFile
from pyannote.pipeline import Pipeline as _Pipeline
from torch import nn
from pyannote.audio import Audio
from pyannote.audio.core.inference import BaseInference
from pyannote.audio.core.io import AudioFile
@@ -42,9 +46,6 @@ from pyannote.audio.telemetry import track_pipeline_apply, track_pipeline_init
from pyannote.audio.utils.dependencies import check_dependencies
from pyannote.audio.utils.hf_hub import AssetFileName, download_from_hf_hub
from pyannote.audio.utils.reproducibility import fix_reproducibility
from pyannote.core.utils.helper import get_class_by_name
from pyannote.database import FileFinder, ProtocolFile
from pyannote.pipeline import Pipeline as _Pipeline
def expand_subfolders(
@@ -62,6 +63,9 @@ def expand_subfolders(
"subfolder": {subfolder},
"token": token}
Token and cache directory options are included only when not None, so
loaders can inherit these options from the calling pipeline.
Parameters
----------
config : dict
@@ -99,9 +103,11 @@ def expand_subfolders(
"checkpoint": model_id,
"revision": revision,
"subfolder": subfolder,
"token": token,
"cache_dir": cache_dir,
}
if token is not None:
config[key]["token"] = token
if cache_dir is not None:
config[key]["cache_dir"] = cache_dir
else:
expand_subfolders(
value,
@@ -133,9 +139,11 @@ def expand_subfolders(
"checkpoint": model_id,
"revision": revision,
"subfolder": subfolder,
"token": token,
"cache_dir": cache_dir,
}
if token is not None:
config[idx]["token"] = token
if cache_dir is not None:
config[idx]["cache_dir"] = cache_dir
else:
expand_subfolders(
@@ -158,7 +166,7 @@ class Pipeline(_Pipeline):
subfolder: str | None = None,
token: str | bool | None = None,
cache_dir: Path | str | None = None,
) -> Optional["Pipeline"]:
) -> Pipeline | None:
"""Load pretrained pipeline
Parameters
@@ -244,7 +252,7 @@ class Pipeline(_Pipeline):
)
if not isinstance(checkpoint, dict):
with open(config_yml, "r") as fp:
with open(config_yml) as fp:
config = yaml.load(fp, Loader=yaml.SafeLoader)
# expand $model/{subfolder}-like entries in config
@@ -264,7 +272,7 @@ class Pipeline(_Pipeline):
del config["version"]
# check that dependencies are available (in their required version)
dependencies: dict[str, str] = config.get("dependencies", dict())
dependencies: dict[str, str] = config.get("dependencies", {})
check_dependencies(dependencies, "Pipeline")
# initialize pipeline
@@ -335,8 +343,8 @@ class Pipeline(_Pipeline):
def __init__(self):
super().__init__()
self._models: Dict[str, Model] = OrderedDict()
self._inferences: Dict[str, BaseInference] = OrderedDict()
self._models: dict[str, Model] = OrderedDict()
self._inferences: dict[str, BaseInference] = OrderedDict()
def __getattr__(self, name):
"""(Advanced) attribute getter
@@ -419,7 +427,7 @@ class Pipeline(_Pipeline):
def default_parameters(self):
raise NotImplementedError()
def classes(self) -> List | Iterator:
def classes(self) -> list | Iterator:
"""Classes returned by the pipeline
Returns
@@ -469,10 +477,7 @@ class Pipeline(_Pipeline):
# pre-load the audio in memory if requested
if preload:
# raise error if `waveform`` is already in memory (or will be via a preprocessor)
if (
"waveform" in getattr(self, "preprocessors", dict())
or "waveform" in file
):
if "waveform" in getattr(self, "preprocessors", {}) or "waveform" in file:
raise ValueError(
"Cannot preload audio: `waveform` key is already available or will be via a preprocessor."
)
@@ -596,14 +601,14 @@ class Pipeline(_Pipeline):
f"`device` must be an instance of `torch.device`, got `{type(device).__name__}`"
)
for _, pipeline in self._pipelines.items():
for pipeline in self._pipelines.values():
if hasattr(pipeline, "to"):
_ = pipeline.to(device)
for _, model in self._models.items():
for model in self._models.values():
_ = model.to(device)
for _, inference in self._inferences.items():
for inference in self._inferences.values():
_ = inference.to(device)
self.device = device
+1
View File
@@ -26,6 +26,7 @@ from pathlib import Path
from typing import Optional
import numpy as np
from pyannote.audio.utils.hf_hub import download_from_hf_hub
from pyannote.audio.utils.vbx import vbx_setup
+63 -52
View File
@@ -30,20 +30,19 @@ import multiprocessing
import sys
import warnings
from collections import defaultdict
from collections.abc import Sequence
from dataclasses import dataclass
from enum import Enum
from functools import cached_property, partial
from numbers import Number
from pathlib import Path
from tempfile import mkstemp
from typing import Dict, List, Literal, Optional, Sequence, Text, Tuple, Union
from typing import Literal
import lightning
import numpy as np
import scipy.special
import torch
from pyannote.audio.utils.loss import binary_cross_entropy, nll_loss
from pyannote.audio.utils.protocol import check_protocol
from pyannote.database import Protocol
from pyannote.database.protocol.protocol import Scope, Subset
from torch.utils.data import DataLoader, Dataset, IterableDataset
@@ -51,6 +50,9 @@ from torch_audiomentations import Identity
from torch_audiomentations.core.transforms_interface import BaseWaveformTransform
from torchmetrics import Metric, MetricCollection
from pyannote.audio.utils.loss import binary_cross_entropy, nll_loss
from pyannote.audio.utils.protocol import check_protocol
Subsets = list(Subset.__args__)
Scopes = list(Scope.__args__)
@@ -86,7 +88,7 @@ class Specifications:
duration: float
# (for variable-duration tasks only) minimum chunk duration in seconds
min_duration: Optional[float] = None
min_duration: float | None = None
# use that many seconds on the left- and rightmost parts of each chunk
# to warm up the model. This is mostly useful for segmentation tasks.
@@ -94,14 +96,14 @@ class Specifications:
# the remaining central part of each chunk is used for computing the
# loss during training, and for aggregating scores during inference.
# Defaults to 0. (i.e. no warm-up).
warm_up: Optional[Tuple[float, float]] = (0.0, 0.0)
warm_up: tuple[float, float] | None = (0.0, 0.0)
# (for classification tasks only) list of classes
classes: Optional[List[Text]] = None
classes: list[str] | None = None
# (for powerset only) max number of simultaneous classes
# (n choose k with k <= powerset_max_classes)
powerset_max_classes: Optional[int] = None
powerset_max_classes: int | None = None
# whether classes are permutation-invariant (e.g. diarization)
permutation_invariant: bool = False
@@ -126,7 +128,7 @@ class Specifications:
return int(
sum(
scipy.special.binom(len(self.classes), i)
for i in range(0, self.powerset_max_classes + 1)
for i in range(self.powerset_max_classes + 1)
)
)
@@ -228,6 +230,8 @@ class Task(lightning.LightningDataModule):
num_workers : int, optional
Number of workers used for generating training samples.
Defaults to multiprocessing.cpu_count() // 2.
Falls back to zero with a warning on macOS or when the multiprocessing
start method is spawn/forkserver, because protocols cannot be pickled.
pin_memory : bool, optional
If True, data loaders will copy tensors into CUDA pinned
memory before returning them. See pytorch documentation
@@ -249,15 +253,15 @@ class Task(lightning.LightningDataModule):
def __init__(
self,
protocol: Protocol,
cache: Optional[Union[str, None]] = None,
cache: str | None = None,
duration: float = 2.0,
min_duration: Optional[float] = None,
warm_up: Union[float, Tuple[float, float]] = 0.0,
min_duration: float | None = None,
warm_up: float | tuple[float, float] = 0.0,
batch_size: int = 32,
num_workers: Optional[int] = None,
num_workers: int | None = None,
pin_memory: bool = False,
augmentation: Optional[BaseWaveformTransform] = None,
metric: Union[Metric, Sequence[Metric], Dict[str, Metric]] = None,
augmentation: BaseWaveformTransform | None = None,
metric: Metric | Sequence[Metric] | dict[str, Metric] = None,
):
super().__init__()
@@ -288,18 +292,28 @@ class Task(lightning.LightningDataModule):
if num_workers is None:
num_workers = multiprocessing.cpu_count() // 2
if (
num_workers > 0
and sys.platform == "darwin"
and sys.version_info[0] >= 3
and sys.version_info[1] >= 8
):
if num_workers > 0 and sys.platform == "darwin":
warnings.warn(
"num_workers > 0 is not supported with macOS and Python 3.8+: "
"setting num_workers = 0."
)
num_workers = 0
if num_workers > 0:
# Protocols and their preprocessors cannot currently be pickled for
# spawn/forkserver workers (the latter is the Python 3.14 default).
# Inspect the default without fixing the global multiprocessing context.
start_method = multiprocessing.get_start_method(allow_none=True)
if start_method is None:
start_method = multiprocessing.get_all_start_methods()[0]
if start_method != "fork":
warnings.warn(
f"num_workers > 0 is not supported with {start_method!r} "
"multiprocessing because database protocols cannot be pickled: "
"setting num_workers = 0."
)
num_workers = 0
self.num_workers = num_workers
self.pin_memory = pin_memory
self.augmentation = augmentation or Identity(output_type="dict")
@@ -347,19 +361,17 @@ class Task(lightning.LightningDataModule):
metadata_unique_values["subset"] = Subsets
metadata_unique_values["scope"] = Scopes
audios = list() # list of path to audio files
metadata = list() # list of metadata
audios = [] # list of path to audio files
metadata = [] # list of metadata
annotated_duration = list() # total duration of annotated regions (per file)
annotated_regions = list() # annotated regions
audio_regions_ids = list() # start/end indices of annotated regions (per file)
annotated_duration = [] # total duration of annotated regions (per file)
annotated_regions = [] # annotated regions
audio_regions_ids = [] # start/end indices of annotated regions (per file)
annotations = list() # actual annotations
audio_segments_ids = (
list()
) # start/end indices of annotated segments (per file)
annotations = [] # actual annotations
audio_segments_ids = [] # start/end indices of annotated segments (per file)
unique_labels = list()
unique_labels = []
database_unique_labels = {}
if self.has_validation:
@@ -376,7 +388,7 @@ class Task(lightning.LightningDataModule):
for file_id, (subset, file) in enumerate(files_iter):
# gather metadata and update metadata_unique_values so that each metadatum
# (e.g. source database or label) is represented by an integer.
metadatum = dict()
metadatum = {}
# keep track of source database and subset (train, development, or test)
if file["database"] not in metadata_unique_values["database"]:
@@ -390,18 +402,16 @@ class Task(lightning.LightningDataModule):
# keep track of label scope (file, database, or global)
metadatum["scope"] = Scopes.index(file["scope"])
remaining_metadata_keys = set(file) - set(
[
"uri",
"database",
"subset",
"audio",
"scope",
"classes",
"annotation",
"annotated",
]
)
remaining_metadata_keys = set(file) - {
"uri",
"database",
"subset",
"audio",
"scope",
"classes",
"annotation",
"annotated",
}
# keep track of any other (integer or string) metadata provided by the protocol
# (e.g. a "domain" key for domain-adversarial training)
@@ -427,7 +437,7 @@ class Task(lightning.LightningDataModule):
metadata.append(metadatum)
# reset list of file-scoped labels
file_unique_labels = list()
file_unique_labels = []
# path to audio file
audios.append(str(file["audio"]))
@@ -596,7 +606,7 @@ class Task(lightning.LightningDataModule):
with open(self.cache, "wb") as cache_file:
np.savez_compressed(cache_file, **prepared_data)
def post_prepare_data(self, prepared_data: Dict):
def post_prepare_data(self, prepared_data: dict):
"""Method for completing `prepared_data` with task-specific data.
For instance, for a classification task, this could be a list of
possible classes.
@@ -611,7 +621,6 @@ class Task(lightning.LightningDataModule):
This method does not return anything. Thus, user have to directly modify
`prepared_data`, for updates to be taken into account
"""
pass
def setup(self, stage=None):
"""Setup data cached by prepare_data into the task on each device"""
@@ -624,6 +633,10 @@ class Task(lightning.LightningDataModule):
try:
with open(self.cache, "rb") as cache_file:
self.prepared_data = dict(np.load(cache_file, allow_pickle=True))
# np.savez stores the `metadata-values` dict as a 0-d object array: unwrap it
self.prepared_data["metadata-values"] = self.prepared_data[
"metadata-values"
].item()
except FileNotFoundError:
print(
"Cached data for protocol not found. Ensure that prepare_data() was called",
@@ -647,7 +660,7 @@ class Task(lightning.LightningDataModule):
self.model.automatic_optimization = automatic_optimisation
@property
def specifications(self) -> Union[Specifications, Tuple[Specifications]]:
def specifications(self) -> Specifications | tuple[Specifications]:
# setup metadata on-demand the first time specifications are requested and missing
if not hasattr(self, "_specifications"):
raise UnknownSpecificationsError(
@@ -658,9 +671,7 @@ class Task(lightning.LightningDataModule):
return self._specifications
@specifications.setter
def specifications(
self, specifications: Union[Specifications, Tuple[Specifications]]
):
def specifications(self, specifications: Specifications | tuple[Specifications]):
self._specifications = specifications
def setup_loss_func(self):
@@ -813,7 +824,7 @@ class Task(lightning.LightningDataModule):
msg = f"Missing '{self.__class__.__name__}.val__len__' method."
raise NotImplementedError(msg)
def val_dataloader(self) -> Optional[DataLoader]:
def val_dataloader(self) -> DataLoader | None:
if self.has_validation:
return DataLoader(
ValDataset(self),
@@ -831,7 +842,7 @@ class Task(lightning.LightningDataModule):
def validation_step(self, batch, batch_idx: int):
return self.common_step(batch, batch_idx, "val")
def default_metric(self) -> Union[Metric, Sequence[Metric], Dict[str, Metric]]:
def default_metric(self) -> Metric | Sequence[Metric] | dict[str, Metric]:
"""Default validation metric"""
msg = f"Missing '{self.__class__.__name__}.default_metric' method."
raise NotImplementedError(msg)
+2 -3
View File
@@ -20,11 +20,10 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from typing import Optional
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch import nn
def _pool(sequences: torch.Tensor, weights: torch.Tensor) -> torch.Tensor:
@@ -74,7 +73,7 @@ class StatsPool(nn.Module):
"""
def forward(
self, sequences: torch.Tensor, weights: Optional[torch.Tensor] = None
self, sequences: torch.Tensor, weights: torch.Tensor | None = None
) -> torch.Tensor:
"""Forward pass
+1 -1
View File
@@ -26,9 +26,9 @@
from functools import lru_cache
import torch
import torch.nn as nn
import torch.nn.functional as F
from asteroid_filterbanks import Encoder, ParamSincFB
from torch import nn
from pyannote.audio.utils.receptive_field import (
multi_conv_num_frames,
@@ -30,10 +30,10 @@ from .wespeaker import (
from .xvector import XVectorMFCC, XVectorSincNet
__all__ = [
"XVectorSincNet",
"XVectorMFCC",
"WeSpeakerResNet34",
"WeSpeakerResNet152",
"WeSpeakerResNet221",
"WeSpeakerResNet293",
"XVectorMFCC",
"XVectorSincNet",
]
+3 -4
View File
@@ -22,11 +22,10 @@
from functools import lru_cache
from typing import Optional
import torch
import torch.nn as nn
from einops import rearrange, reduce
from torch import nn
from torchaudio.transforms import MFCC
from pyannote.audio.core.model import Model
@@ -38,7 +37,7 @@ class SimpleEmbeddingModel(Model):
self,
sample_rate: int = 16000,
num_channels: int = 1,
task: Optional[Task] = None,
task: Task | None = None,
):
super().__init__(sample_rate=sample_rate, num_channels=num_channels, task=task)
@@ -146,6 +145,6 @@ class SimpleEmbeddingModel(Model):
"""
mfcc = self.mfcc(waveforms)
output, hidden = self.lstm(rearrange(mfcc, "b c f t -> b t (c f)"))
output, _hidden = self.lstm(rearrange(mfcc, "b c f t -> b t (c f)"))
# mean temporal pooling
return reduce(output, "b t f -> b f", "mean")
@@ -22,11 +22,10 @@
from functools import lru_cache, partial
from typing import Optional
import torch
import torch.nn.functional as F
import torchaudio.compliance.kaldi as kaldi
from torchaudio.compliance import kaldi
from pyannote.audio.core.model import Model
from pyannote.audio.core.task import Task
@@ -66,8 +65,8 @@ class BaseWeSpeakerResNet(Model):
dither: float = 0.0,
window_type: str = "hamming",
use_energy: bool = False,
fbank_centering_span: Optional[float] = None,
task: Optional[Task] = None,
fbank_centering_span: float | None = None,
task: Task | None = None,
):
super().__init__(sample_rate=sample_rate, num_channels=num_channels, task=task)
@@ -262,7 +261,7 @@ class BaseWeSpeakerResNet(Model):
)
def forward(
self, waveforms: torch.Tensor, weights: Optional[torch.Tensor] = None
self, waveforms: torch.Tensor, weights: torch.Tensor | None = None
) -> torch.Tensor:
"""Extract speaker embeddings
@@ -321,27 +320,6 @@ class BaseWeSpeakerResNet(Model):
"""
return self.resnet.forward_embedding(frames, weights=weights)[1]
def forward(
self, waveforms: torch.Tensor, weights: Optional[torch.Tensor] = None
) -> torch.Tensor:
"""Extract speaker embeddings
Parameters
----------
waveforms : torch.Tensor
Batch of waveforms with shape (batch, channel, sample)
weights : (batch, frames) or (batch, speakers, frames) torch.Tensor, optional
Batch of weights passed to statistics pooling layer.
Returns
-------
embeddings : (batch, dimension) or (batch, speakers, dimension) torch.Tensor
Batch of embeddings.
"""
fbank = self.compute_fbank(waveforms)
return self.resnet(fbank, weights=weights)[1]
class WeSpeakerResNet34(BaseWeSpeakerResNet):
def __init__(
@@ -354,7 +332,7 @@ class WeSpeakerResNet34(BaseWeSpeakerResNet):
dither: float = 0.0,
window_type: str = "hamming",
use_energy: bool = False,
task: Optional[Task] = None,
task: Task | None = None,
):
super().__init__(
sample_rate=sample_rate,
@@ -383,7 +361,7 @@ class WeSpeakerResNet152(BaseWeSpeakerResNet):
dither: float = 0.0,
window_type: str = "hamming",
use_energy: bool = False,
task: Optional[Task] = None,
task: Task | None = None,
):
super().__init__(
sample_rate=sample_rate,
@@ -412,7 +390,7 @@ class WeSpeakerResNet221(BaseWeSpeakerResNet):
dither: float = 0.0,
window_type: str = "hamming",
use_energy: bool = False,
task: Optional[Task] = None,
task: Task | None = None,
):
super().__init__(
sample_rate=sample_rate,
@@ -441,7 +419,7 @@ class WeSpeakerResNet293(BaseWeSpeakerResNet):
dither: float = 0.0,
window_type: str = "hamming",
use_energy: bool = False,
task: Optional[Task] = None,
task: Task | None = None,
):
super().__init__(
sample_rate=sample_rate,
@@ -28,9 +28,9 @@ from pathlib import Path
import lightning
import torch
import pyannote.audio.models.embedding.wespeaker as wespeaker
from pyannote.audio import Model
from pyannote.audio.core.task import Problem, Resolution, Specifications
from pyannote.audio.models.embedding import wespeaker
wespeaker_checkpoint_dir = sys.argv[1] # /path/to/wespeaker_cnceleb-resnet34-LM
@@ -16,12 +16,11 @@
# limitations under the License.
from functools import lru_cache
from typing import Optional
import torch
import torch.nn as nn
import torch.nn.functional as F
from einops import rearrange
from torch import nn
from pyannote.audio.models.blocks.pooling import StatsPool
from pyannote.audio.utils.receptive_field import (
@@ -42,11 +41,11 @@ class TSTP(nn.Module):
"""
def __init__(self, in_dim=0, **kwargs):
super(TSTP, self).__init__()
super().__init__()
self.in_dim = in_dim
self.stats_pool = StatsPool()
def forward(self, features, weights: Optional[torch.Tensor] = None):
def forward(self, features, weights: torch.Tensor | None = None):
"""
Parameters
@@ -85,7 +84,7 @@ class BasicBlock(nn.Module):
expansion = 1
def __init__(self, in_planes, planes, stride=1):
super(BasicBlock, self).__init__()
super().__init__()
self.stride = stride
self.conv1 = nn.Conv2d(
in_planes, planes, kernel_size=3, stride=stride, padding=1, bias=False
@@ -149,7 +148,7 @@ class Bottleneck(nn.Module):
expansion = 4
def __init__(self, in_planes, planes, stride=1):
super(Bottleneck, self).__init__()
super().__init__()
self.stride = stride
self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=1, bias=False)
self.bn1 = nn.BatchNorm2d(planes)
@@ -223,7 +222,7 @@ class ResNet(nn.Module):
pooling_func="TSTP",
two_emb_layer=True,
):
super(ResNet, self).__init__()
super().__init__()
self.in_planes = m_channels
self.feat_dim = feat_dim
self.embed_dim = embed_dim
@@ -396,7 +395,7 @@ class ResNet(nn.Module):
else:
return torch.tensor(0.0), embed_a
def forward(self, fbank: torch.Tensor, weights: Optional[torch.Tensor] = None):
def forward(self, fbank: torch.Tensor, weights: torch.Tensor | None = None):
"""Extract speaker embeddings
Parameters
+15 -10
View File
@@ -21,10 +21,10 @@
# SOFTWARE.
from functools import lru_cache
from typing import Optional
from typing import ClassVar
import torch
import torch.nn as nn
from torch import nn
from torchaudio.transforms import MFCC
from pyannote.audio.core.model import Model
@@ -40,15 +40,20 @@ from pyannote.audio.utils.receptive_field import (
class XVectorMFCC(Model):
MFCC_DEFAULTS = {"n_mfcc": 40, "dct_type": 2, "norm": "ortho", "log_mels": False}
MFCC_DEFAULTS: ClassVar[dict] = {
"n_mfcc": 40,
"dct_type": 2,
"norm": "ortho",
"log_mels": False,
}
def __init__(
self,
sample_rate: int = 16000,
num_channels: int = 1,
mfcc: Optional[dict] = None,
mfcc: dict | None = None,
dimension: int = 512,
task: Optional[Task] = None,
task: Task | None = None,
):
super().__init__(sample_rate=sample_rate, num_channels=num_channels, task=task)
@@ -183,7 +188,7 @@ class XVectorMFCC(Model):
return receptive_field_center * hop_length + n_fft // 2
def forward(
self, waveforms: torch.Tensor, weights: Optional[torch.Tensor] = None
self, waveforms: torch.Tensor, weights: torch.Tensor | None = None
) -> torch.Tensor:
"""
@@ -203,15 +208,15 @@ class XVectorMFCC(Model):
class XVectorSincNet(Model):
SINCNET_DEFAULTS = {"stride": 10}
SINCNET_DEFAULTS: ClassVar[dict] = {"stride": 10}
def __init__(
self,
sample_rate: int = 16000,
num_channels: int = 1,
sincnet: Optional[dict] = None,
sincnet: dict | None = None,
dimension: int = 512,
task: Optional[Task] = None,
task: Task | None = None,
):
super().__init__(sample_rate=sample_rate, num_channels=num_channels, task=task)
@@ -330,7 +335,7 @@ class XVectorSincNet(Model):
return self.sincnet.receptive_field_center(frame=receptive_field_center)
def forward(
self, waveforms: torch.Tensor, weights: Optional[torch.Tensor] = None
self, waveforms: torch.Tensor, weights: torch.Tensor | None = None
) -> torch.Tensor:
"""
@@ -21,13 +21,13 @@
# SOFTWARE.
from functools import lru_cache
from typing import Optional
from typing import ClassVar
import torch
import torch.nn as nn
import torch.nn.functional as F
from einops import rearrange
from pyannote.core.utils.generators import pairwise
from torch import nn
from pyannote.audio.core.model import Model
from pyannote.audio.core.task import Task
@@ -61,24 +61,24 @@ class PyanNet(Model):
i.e. two linear layers with 128 units each.
"""
SINCNET_DEFAULTS = {"stride": 10}
LSTM_DEFAULTS = {
SINCNET_DEFAULTS: ClassVar[dict] = {"stride": 10}
LSTM_DEFAULTS: ClassVar[dict] = {
"hidden_size": 128,
"num_layers": 2,
"bidirectional": True,
"monolithic": True,
"dropout": 0.0,
}
LINEAR_DEFAULTS = {"hidden_size": 128, "num_layers": 2}
LINEAR_DEFAULTS: ClassVar[dict] = {"hidden_size": 128, "num_layers": 2}
def __init__(
self,
sincnet: Optional[dict] = None,
lstm: Optional[dict] = None,
linear: Optional[dict] = None,
sincnet: dict | None = None,
lstm: dict | None = None,
linear: dict | None = None,
sample_rate: int = 16000,
num_channels: int = 1,
task: Optional[Task] = None,
task: Task | None = None,
):
super().__init__(sample_rate=sample_rate, num_channels=num_channels, task=task)
@@ -113,7 +113,7 @@ class PyanNet(Model):
60
if i == 0
else lstm["hidden_size"] * (2 if lstm["bidirectional"] else 1),
**one_layer_lstm
**one_layer_lstm,
)
for i in range(num_layers)
]
@@ -21,13 +21,13 @@
# SOFTWARE.
from functools import lru_cache
from typing import Optional, Union
from typing import ClassVar
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchaudio
from pyannote.core.utils.generators import pairwise
from torch import nn
from pyannote.audio.core.model import Model
from pyannote.audio.core.task import Task
@@ -71,25 +71,25 @@ class SSeRiouSS(Model):
WAV2VEC_DEFAULTS = "WAVLM_BASE"
LSTM_DEFAULTS = {
LSTM_DEFAULTS: ClassVar[dict] = {
"hidden_size": 128,
"num_layers": 4,
"bidirectional": True,
"monolithic": True,
"dropout": 0.0,
}
LINEAR_DEFAULTS = {"hidden_size": 128, "num_layers": 2}
LINEAR_DEFAULTS: ClassVar[dict] = {"hidden_size": 128, "num_layers": 2}
def __init__(
self,
wav2vec: Union[dict, str] = None,
wav2vec: dict | str | None = None,
wav2vec_frozen: bool = False,
wav2vec_layer: int = -1,
lstm: Optional[dict] = None,
linear: Optional[dict] = None,
lstm: dict | None = None,
linear: dict | None = None,
sample_rate: int = 16000,
num_channels: int = 1,
task: Optional[Task] = None,
task: Task | None = None,
):
super().__init__(sample_rate=sample_rate, num_channels=num_channels, task=task)
@@ -22,11 +22,10 @@
from functools import lru_cache
from typing import Optional
import torch
import torch.nn as nn
from einops import rearrange
from torch import nn
from torchaudio.transforms import MFCC
from pyannote.audio.core.model import Model
@@ -38,7 +37,7 @@ class SimpleSegmentationModel(Model):
self,
sample_rate: int = 16000,
num_channels: int = 1,
task: Optional[Task] = None,
task: Task | None = None,
):
super().__init__(sample_rate=sample_rate, num_channels=num_channels, task=task)
@@ -159,6 +158,6 @@ class SimpleSegmentationModel(Model):
# extract MFCC
mfcc = self.mfcc(waveforms)
# pass MFCC sequence into the recurrent layer
output, hidden = self.lstm(rearrange(mfcc, "b c f t -> b t (c f)"))
output, _hidden = self.lstm(rearrange(mfcc, "b c f t -> b t (c f)"))
# apply the final classifier to get logits
return self.activation(self.classifier(output))
@@ -23,13 +23,13 @@
# AUTHOR: Joonas Kalda (github.com/joonaskalda)
from functools import lru_cache
from typing import Optional
from typing import ClassVar
import torch
import torch.nn as nn
import torch.nn.functional as F
from asteroid_filterbanks import make_enc_dec
from pyannote.core.utils.generators import pairwise
from torch import nn
from pyannote.audio.core.model import Model
from pyannote.audio.core.task import Task
@@ -110,14 +110,14 @@ class ToTaToNet(Model):
Odyssey 2024. https://arxiv.org/abs/2403.02288
"""
ENCODER_DECODER_DEFAULTS = {
ENCODER_DECODER_DEFAULTS: ClassVar[dict] = {
"fb_name": "free",
"kernel_size": 32,
"n_filters": 64,
"stride": 16,
}
LINEAR_DEFAULTS = {"hidden_size": 64, "num_layers": 2}
DPRNN_DEFAULTS = {
LINEAR_DEFAULTS: ClassVar[dict] = {"hidden_size": 64, "num_layers": 2}
DPRNN_DEFAULTS: ClassVar[dict] = {
"n_repeats": 6,
"bn_chan": 128,
"hid_size": 128,
@@ -126,17 +126,17 @@ class ToTaToNet(Model):
"mask_act": "relu",
"rnn_type": "LSTM",
}
DIAR_DEFAULTS = {"frames_per_second": 125}
DIAR_DEFAULTS: ClassVar[dict] = {"frames_per_second": 125}
def __init__(
self,
encoder_decoder: dict = None,
linear: Optional[dict] = None,
diar: Optional[dict] = None,
dprnn: dict = None,
encoder_decoder: dict | None = None,
linear: dict | None = None,
diar: dict | None = None,
dprnn: dict | None = None,
sample_rate: int = 16000,
num_channels: int = 1,
task: Optional[Task] = None,
task: Task | None = None,
n_sources: int = 3,
use_wavlm: bool = True,
wavlm_frozen: bool = False,
+1 -1
View File
@@ -28,7 +28,7 @@ from .voice_activity_detection import VoiceActivityDetection
__all__ = [
"MultiLabelSegmentation",
"VoiceActivityDetection",
"SpeakerDiarization",
"SpeechSeparation",
"VoiceActivityDetection",
]
+21 -19
View File
@@ -27,11 +27,6 @@ from enum import Enum
import numpy as np
from einops import rearrange
from pyannote.audio.core.io import AudioFile
from pyannote.audio.core.plda import PLDA
from pyannote.audio.pipelines.utils import oracle_segmentation
from pyannote.audio.utils.permutation import permutate
from pyannote.audio.utils.vbx import cluster_vbx
from pyannote.core import SlidingWindow, SlidingWindowFeature
from pyannote.pipeline import Pipeline
from pyannote.pipeline.parameter import Categorical, Integer, Uniform
@@ -40,6 +35,12 @@ from scipy.optimize import linear_sum_assignment
from scipy.spatial.distance import cdist
from sklearn.cluster import KMeans
from pyannote.audio.core.io import AudioFile
from pyannote.audio.core.plda import PLDA
from pyannote.audio.pipelines.utils import oracle_segmentation
from pyannote.audio.utils.permutation import permutate
from pyannote.audio.utils.vbx import cluster_vbx
class BaseClustering(Pipeline):
def __init__(
@@ -106,12 +107,12 @@ class BaseClustering(Pipeline):
_, num_frames, _ = segmentations.data.shape
# frames where only one speaker is active
single_active_mask = (np.sum(segmentations.data, axis=2, keepdims=True) == 1)
single_active_mask = np.sum(segmentations.data, axis=2, keepdims=True) == 1
# count number of clean frames per chunk and speaker
num_clean_frames = np.sum(segmentations.data * single_active_mask, axis=1)
# num_chunks, num_speakers
# whether speaker is active enough on their own
active = num_clean_frames >= min_active_ratio * num_frames
# num_chunks, num_speakers
@@ -125,9 +126,9 @@ class BaseClustering(Pipeline):
return embeddings[chunk_idx, speaker_idx], chunk_idx, speaker_idx
def constrained_argmax(self, soft_clusters: np.ndarray) -> np.ndarray:
soft_clusters = np.nan_to_num(soft_clusters, nan=np.nanmin(soft_clusters))
num_chunks, num_speakers, num_clusters = soft_clusters.shape
num_chunks, num_speakers, _num_clusters = soft_clusters.shape
# num_chunks, num_speakers, num_clusters
hard_clusters = -2 * np.ones((num_chunks, num_speakers), dtype=np.int8)
@@ -175,7 +176,7 @@ class BaseClustering(Pipeline):
# TODO: option to add a new (dummy) cluster in case num_clusters < max(frame_speaker_count)
num_clusters = np.max(train_clusters) + 1
num_chunks, num_speakers, dimension = embeddings.shape
num_chunks, num_speakers, _dimension = embeddings.shape
train_embeddings = embeddings[train_chunk_idx, train_speaker_idx]
@@ -548,10 +549,8 @@ class KMeansClustering(BaseClustering):
class VBxClustering(BaseClustering):
expects_num_clusters: bool = False
def __init__(
self,
plda: PLDA,
@@ -578,7 +577,7 @@ class VBxClustering(BaseClustering):
max_clusters: int | None = None,
**kwargs,
) -> np.ndarray:
constrained_assignment = self.constrained_assignment
train_embeddings, _, _ = self.filter_embeddings(
@@ -616,8 +615,10 @@ class VBxClustering(BaseClustering):
)
num_chunks, num_speakers, dimension = embeddings.shape
W = q[:, sp > 1e-7] # responsibilities of speakers that VBx kept
centroids = W.T @ train_embeddings.reshape(-1, dimension) / W.sum(0, keepdims=True).T
W = q[:, sp > 1e-7] # responsibilities of speakers that VBx kept
centroids = (
W.T @ train_embeddings.reshape(-1, dimension) / W.sum(0, keepdims=True).T
)
# (optional) K-Means
# re-cluster with Kmeans only in case the automatically determined
@@ -639,7 +640,8 @@ class VBxClustering(BaseClustering):
[
np.mean(train_embeddings[kmeans_clusters == k], axis=0)
for k in range(num_clusters)
])
]
)
# calculate distance
e2k_distance = rearrange(
@@ -656,7 +658,7 @@ class VBxClustering(BaseClustering):
# assign each embedding to the cluster with the most similar centroid
if constrained_assignment:
const = soft_clusters.min() - 1. # const < any_valid_score
const = soft_clusters.min() - 1.0 # const < any_valid_score
soft_clusters[segmentations.data.sum(1) == 0] = const
hard_clusters = self.constrained_argmax(
soft_clusters,
@@ -665,7 +667,7 @@ class VBxClustering(BaseClustering):
hard_clusters = np.argmax(soft_clusters, axis=2)
hard_clusters = hard_clusters.reshape(num_chunks, num_speakers)
return hard_clusters, soft_clusters, centroids
+12 -11
View File
@@ -25,17 +25,18 @@
# Hadrien TITEUX - https://github.com/hadware
# Hervé BREDIN - http://herve.niderb.fr
from collections.abc import Callable
from functools import partial
from pathlib import Path
from typing import Callable, Optional, Text, Union
from pyannote.core import Annotation, SlidingWindowFeature
from pyannote.metrics.identification import IdentificationErrorRate
from pyannote.pipeline.parameter import ParamDict, Uniform
from pyannote.audio import Inference
from pyannote.audio.core.io import AudioFile
from pyannote.audio.core.pipeline import Pipeline
from pyannote.audio.utils.metric import MacroAverageFMeasure
from pyannote.core import Annotation, SlidingWindowFeature
from pyannote.metrics.identification import IdentificationErrorRate
from pyannote.pipeline.parameter import ParamDict, Uniform
from ..utils.signal import Binarize
from .utils import PipelineModel, get_model
@@ -76,11 +77,11 @@ class MultiLabelSegmentation(Pipeline):
def __init__(
self,
segmentation: Optional[PipelineModel] = None,
segmentation: PipelineModel | None = None,
fscore: bool = False,
share_min_duration: bool = False,
token: Union[Text, None] = None,
cache_dir: Union[Path, Text, None] = None,
token: str | None = None,
cache_dir: Path | str | None = None,
**inference_kwargs,
):
super().__init__()
@@ -141,19 +142,19 @@ class MultiLabelSegmentation(Pipeline):
self.thresholds[label]["min_duration_on"]
if not self.share_min_duration
else self.min_duration_on
), # noqa
),
min_duration_off=(
self.thresholds[label]["min_duration_off"]
if not self.share_min_duration
else self.min_duration_off
), # noqa
),
)
for label in self._classes
}
CACHED_SEGMENTATION = "cache/segmentation"
def apply(self, file: AudioFile, hook: Optional[Callable] = None) -> Annotation:
def apply(self, file: AudioFile, hook: Callable | None = None) -> Annotation:
"""Apply multi-label detection
Parameters
@@ -215,7 +216,7 @@ class MultiLabelSegmentation(Pipeline):
return detection
def get_metric(self) -> Union[MacroAverageFMeasure, IdentificationErrorRate]:
def get_metric(self) -> MacroAverageFMeasure | IdentificationErrorRate:
"""Return new instance of identification metric"""
if self.fscore:
@@ -22,9 +22,10 @@
import os
from pyannote.core import Annotation, Segment
from pyannote.audio import Pipeline
from pyannote.audio.core.io import AudioFile
from pyannote.core import Annotation, Segment
from ..speaker_diarization import DiarizeOutput
@@ -22,14 +22,14 @@
import os
from pyannote.audio import Pipeline
from pyannote.audio.core.io import AudioFile
from pyannote.core import Annotation, Segment
from pyannoteai.sdk import Client
from pyannote.audio import Pipeline
from pyannote.audio.core.io import AudioFile
from pyannote.audio.pipelines.speaker_diarization import DiarizeOutput
class SDK(Pipeline):
"""Wrapper around official pyannoteAI API client
@@ -65,7 +65,6 @@ class SDK(Pipeline):
annotation[segment, t] = speaker
return annotation.rename_tracks("string")
def apply(
self,
file: AudioFile,
@@ -123,11 +122,14 @@ class SDK(Pipeline):
# retrieve job output (once completed)
job_output = self._client.retrieve(job_id)
speaker_diarization: Annotation = self._deserialize(job_output["output"]["diarization"])
exclusive_speaker_diarization: Annotation = self._deserialize(job_output["output"]["exclusiveDiarization"])
speaker_diarization: Annotation = self._deserialize(
job_output["output"]["diarization"]
)
exclusive_speaker_diarization: Annotation = self._deserialize(
job_output["output"]["exclusiveDiarization"]
)
return DiarizeOutput(
speaker_diarization=speaker_diarization,
exclusive_speaker_diarization=exclusive_speaker_diarization,
)
@@ -28,13 +28,18 @@ import itertools
import math
import textwrap
import warnings
from pathlib import Path
from typing import Callable, Mapping, Optional, Text, Union, Any
from collections.abc import Callable, Mapping
from dataclasses import dataclass
from pathlib import Path
from typing import Any
import numpy as np
import torch
from einops import rearrange
from pyannote.core import Annotation, SlidingWindowFeature
from pyannote.metrics.diarization import GreedyDiarizationErrorRate
from pyannote.pipeline.parameter import ParamDict, Uniform
from pyannote.audio import Audio, Inference, Model, Pipeline
from pyannote.audio.core.io import AudioFile
from pyannote.audio.pipelines.clustering import Clustering
@@ -48,9 +53,6 @@ from pyannote.audio.pipelines.utils import (
)
from pyannote.audio.pipelines.utils.diarization import set_num_speakers
from pyannote.audio.utils.signal import binarize
from pyannote.core import Annotation, SlidingWindowFeature
from pyannote.metrics.diarization import GreedyDiarizationErrorRate
from pyannote.pipeline.parameter import ParamDict, Uniform
def batchify(iterable, batch_size: int = 32, fillvalue=None):
@@ -74,7 +76,6 @@ class DiarizeOutput:
# sorted in speaker_diarization.labels() order
speaker_embeddings: np.ndarray | None = None
def serialize(self) -> dict[str, Any]:
"""Serialize diarization output
@@ -133,14 +134,14 @@ class SpeakerDiarization(SpeakerDiarizationMixin, Pipeline):
Return only the diarization output. Defaults to return the full output
with diarization, exclusive diarization, and speaker embeddings.
segmentation : Model, str, or dict, optional
Pretrained segmentation model.
Pretrained segmentation model.
See pyannote.audio.pipelines.utils.get_model for supported format.
segmentation_step: float, optional
The segmentation model is applied on a window sliding over the whole audio file.
`segmentation_step` controls the step of this window, provided as a ratio of its
duration. Defaults to 0.1 (i.e. 90% overlap between two consecuive windows).
embedding : Model, str, or dict, optional
Pretrained embedding model.
Pretrained embedding model.
See pyannote.audio.pipelines.utils.get_model for supported format.
embedding_exclude_overlap : bool, optional
Exclude overlapping speech regions when extracting embeddings.
@@ -150,7 +151,7 @@ class SpeakerDiarization(SpeakerDiarizationMixin, Pipeline):
See pyannote.audio.pipelines.utils.get_plda for supported format.
clustering : str, optional
Clustering algorithm. See pyannote.audio.pipelines.clustering.Clustering
for available options.
for available options.
segmentation_batch_size : int, optional
Batch size used for speaker segmentation. Defaults to 1.
embedding_batch_size : int, optional
@@ -163,7 +164,7 @@ class SpeakerDiarization(SpeakerDiarizationMixin, Pipeline):
Huggingface token to be used for downloading from Huggingface hub.
cache_dir: Path or str, optional
Path to the folder where files downloaded from Huggingface hub are stored.
Usage
-----
# process audio file
@@ -173,13 +174,13 @@ class SpeakerDiarization(SpeakerDiarizationMixin, Pipeline):
>>> assert isinstance(output.speaker_diarization, pyannote.core.Annotation)
>>> for turn, speaker in output.speaker_diarization:
... print(f"start={turn.start:.1f}s stop={turn.end:.1f}s speaker_{speaker}")
# get one speaker embedding per speaker
>>> assert isinstance(output.speaker_embeddings, np.ndarray)
>>> for s, speaker in enumerate(output.speaker_diarization.labels()):
... # output.speaker_embeddings[s] is the embedding of speaker `speaker`
# exclusive diarization is the same as diarization except
# exclusive diarization is the same as diarization except
# that it does not contain overlapping speech segments
>>> assert isinstance(output.exclusive_speaker_diarization, pyannote.core.Annotation)
@@ -193,27 +194,33 @@ class SpeakerDiarization(SpeakerDiarizationMixin, Pipeline):
def __init__(
self,
legacy: bool = False,
segmentation: PipelineModel = {
"checkpoint": "pyannote/speaker-diarization-community-1",
"subfolder": "segmentation",
},
segmentation: PipelineModel = None,
segmentation_step: float = 0.1,
embedding: PipelineModel = {
"checkpoint": "pyannote/speaker-diarization-community-1",
"subfolder": "embedding",
},
embedding: PipelineModel = None,
embedding_exclude_overlap: bool = False,
plda: PipelinePLDA = {
"checkpoint": "pyannote/speaker-diarization-community-1",
"subfolder": "plda",
},
plda: PipelinePLDA = None,
clustering: str = "VBxClustering",
embedding_batch_size: int = 1,
segmentation_batch_size: int = 1,
der_variant: Optional[dict] = None,
token: Union[Text, None] = None,
cache_dir: Union[Path, Text, None] = None,
der_variant: dict | None = None,
token: str | None = None,
cache_dir: Path | str | None = None,
):
if plda is None:
plda = {
"checkpoint": "pyannote/speaker-diarization-community-1",
"subfolder": "plda",
}
if embedding is None:
embedding = {
"checkpoint": "pyannote/speaker-diarization-community-1",
"subfolder": "embedding",
}
if segmentation is None:
segmentation = {
"checkpoint": "pyannote/speaker-diarization-community-1",
"subfolder": "segmentation",
}
super().__init__()
self.legacy = legacy
@@ -334,7 +341,7 @@ class SpeakerDiarization(SpeakerDiarizationMixin, Pipeline):
file,
binary_segmentations: SlidingWindowFeature,
exclude_overlap: bool = False,
hook: Optional[Callable] = None,
hook: Callable | None = None,
):
"""Extract embeddings for each (chunk, speaker) pair
@@ -362,7 +369,7 @@ class SpeakerDiarization(SpeakerDiarizationMixin, Pipeline):
# we only re-use embeddings if they were extracted based on the same value of the
# "segmentation.threshold" hyperparameter or if the segmentation model relies on
# `powerset` mode
cache = file.get("training_cache/embeddings", dict())
cache = file.get("training_cache/embeddings", {})
if ("embeddings" in cache) and (
self._segmentation.model.specifications.powerset
or (cache["segmentation.threshold"] == self.segmentation.threshold)
@@ -500,7 +507,7 @@ class SpeakerDiarization(SpeakerDiarizationMixin, Pipeline):
Discrete (0s and 1s) diarization.
"""
num_chunks, num_frames, local_num_speakers = segmentations.data.shape
num_chunks, num_frames, _local_num_speakers = segmentations.data.shape
num_clusters = np.max(hard_clusters) + 1
clustered_segmentations = np.nan * np.zeros(
@@ -530,10 +537,10 @@ class SpeakerDiarization(SpeakerDiarizationMixin, Pipeline):
def apply(
self,
file: AudioFile,
num_speakers: Optional[int] = None,
min_speakers: Optional[int] = None,
max_speakers: Optional[int] = None,
hook: Optional[Callable] = None,
num_speakers: int | None = None,
min_speakers: int | None = None,
max_speakers: int | None = None,
hook: Callable | None = None,
**kwargs,
) -> DiarizeOutput | Annotation:
"""Apply speaker diarization
@@ -593,7 +600,7 @@ class SpeakerDiarization(SpeakerDiarizationMixin, Pipeline):
segmentations = self.get_segmentations(file, hook=hook)
hook("segmentation", segmentations)
# shape: (num_chunks, num_frames, local_num_speakers)
num_chunks, num_frames, local_num_speakers = segmentations.data.shape
_num_chunks, _num_frames, _local_num_speakers = segmentations.data.shape
# binarize segmentation
if self._segmentation.model.specifications.powerset:
@@ -715,7 +722,7 @@ class SpeakerDiarization(SpeakerDiarizationMixin, Pipeline):
# at this point, `diarization` speaker labels are integers
# from 0 to `num_speakers - 1`, aligned with `centroids` rows.
if "annotation" in file and file["annotation"]:
if file.get("annotation"):
# when reference is available, use it to map hypothesized speakers
# to reference speakers (this makes later error analysis easier
# but does not modify the actual output of the diarization pipeline)
@@ -23,15 +23,14 @@
import warnings
from functools import cached_property
from pathlib import Path
from typing import Optional, Text, Union
import numpy as np
import torch
import torch.nn.functional as F
import torchaudio.compliance.kaldi as kaldi
from huggingface_hub import hf_hub_download
from huggingface_hub.utils import RepositoryNotFoundError
from torch.nn.utils.rnn import pad_sequence
from torchaudio.compliance import kaldi
from pyannote.audio import Inference, Model, Pipeline
from pyannote.audio.core.inference import BaseInference
@@ -65,8 +64,8 @@ except ImportError:
class NeMoPretrainedSpeakerEmbedding(BaseInference):
def __init__(
self,
embedding: Text = "nvidia/speakerverification_en_titanet_large",
device: Optional[torch.device] = None,
embedding: str = "nvidia/speakerverification_en_titanet_large",
device: torch.device | None = None,
):
if not NEMO_IS_AVAILABLE:
raise ImportError(
@@ -132,7 +131,7 @@ class NeMoPretrainedSpeakerEmbedding(BaseInference):
return upper
def __call__(
self, waveforms: torch.Tensor, masks: Optional[torch.Tensor] = None
self, waveforms: torch.Tensor, masks: torch.Tensor | None = None
) -> np.ndarray:
"""
@@ -230,10 +229,10 @@ class SpeechBrainPretrainedSpeakerEmbedding(BaseInference):
def __init__(
self,
embedding: Text = "speechbrain/spkrec-ecapa-voxceleb",
device: Optional[torch.device] = None,
token: Union[Text, None] = None,
cache_dir: Union[Path, Text, None] = None,
embedding: str = "speechbrain/spkrec-ecapa-voxceleb",
device: torch.device | None = None,
token: str | None = None,
cache_dir: Path | str | None = None,
):
if not SPEECHBRAIN_IS_AVAILABLE:
raise ImportError(
@@ -311,7 +310,7 @@ class SpeechBrainPretrainedSpeakerEmbedding(BaseInference):
return upper
def __call__(
self, waveforms: torch.Tensor, masks: Optional[torch.Tensor] = None
self, waveforms: torch.Tensor, masks: torch.Tensor | None = None
) -> np.ndarray:
"""
@@ -414,10 +413,10 @@ class ONNXWeSpeakerPretrainedSpeakerEmbedding(BaseInference):
def __init__(
self,
embedding: Text = "hbredin/wespeaker-voxceleb-resnet34-LM",
device: Optional[torch.device] = None,
token: Union[Text, None] = None,
cache_dir: Union[Path, Text, None] = None,
embedding: str = "hbredin/wespeaker-voxceleb-resnet34-LM",
device: torch.device | None = None,
token: str | None = None,
cache_dir: Path | str | None = None,
):
if not ONNX_IS_AVAILABLE:
raise ImportError(
@@ -427,7 +426,6 @@ class ONNXWeSpeakerPretrainedSpeakerEmbedding(BaseInference):
super().__init__()
if not Path(embedding).exists():
try:
embedding = hf_hub_download(
repo_id=embedding,
@@ -566,7 +564,7 @@ class ONNXWeSpeakerPretrainedSpeakerEmbedding(BaseInference):
return features - torch.mean(features, dim=1, keepdim=True)
def __call__(
self, waveforms: torch.Tensor, masks: Optional[torch.Tensor] = None
self, waveforms: torch.Tensor, masks: torch.Tensor | None = None
) -> np.ndarray:
"""
@@ -582,7 +580,7 @@ class ONNXWeSpeakerPretrainedSpeakerEmbedding(BaseInference):
"""
batch_size, num_channels, num_samples = waveforms.shape
batch_size, num_channels, _num_samples = waveforms.shape
assert num_channels == 1
features = self.compute_fbank(waveforms.to(self.device))
@@ -651,9 +649,9 @@ class PyannoteAudioPretrainedSpeakerEmbedding(BaseInference):
def __init__(
self,
embedding: PipelineModel = "pyannote/embedding",
device: Optional[torch.device] = None,
token: Union[Text, None] = None,
cache_dir: Union[Path, Text, None] = None,
device: torch.device | None = None,
token: str | None = None,
cache_dir: Path | str | None = None,
):
super().__init__()
self.embedding = embedding
@@ -702,7 +700,7 @@ class PyannoteAudioPretrainedSpeakerEmbedding(BaseInference):
return upper
def __call__(
self, waveforms: torch.Tensor, masks: Optional[torch.Tensor] = None
self, waveforms: torch.Tensor, masks: torch.Tensor | None = None
) -> np.ndarray:
with torch.inference_mode():
if masks is None:
@@ -718,9 +716,9 @@ class PyannoteAudioPretrainedSpeakerEmbedding(BaseInference):
def PretrainedSpeakerEmbedding(
embedding: PipelineModel,
device: Optional[torch.device] = None,
token: Union[Text, None] = None,
cache_dir: Union[Path, Text, None] = None,
device: torch.device | None = None,
token: str | None = None,
cache_dir: Path | str | None = None,
):
"""Pretrained speaker embedding
@@ -811,9 +809,9 @@ class SpeakerEmbedding(Pipeline):
def __init__(
self,
embedding: PipelineModel = "pyannote/embedding",
segmentation: Optional[PipelineModel] = None,
token: Union[Text, None] = None,
cache_dir: Union[Path, Text, None] = None,
segmentation: PipelineModel | None = None,
token: str | None = None,
cache_dir: Path | str | None = None,
):
super().__init__()
@@ -859,7 +857,7 @@ def main(
protocol: str = "VoxCeleb.SpeakerVerification.VoxCeleb1",
subset: str = "test",
embedding: str = "pyannote/embedding",
segmentation: Optional[str] = None,
segmentation: str | None = None,
):
import typer
from pyannote.database import FileFinder, get_protocol
@@ -873,7 +871,7 @@ def main(
y_true, y_pred = [], []
emb = dict()
emb = {}
trials = getattr(protocol, f"{subset}_trial")()
@@ -28,12 +28,17 @@ import itertools
import math
import textwrap
import warnings
from collections.abc import Callable
from pathlib import Path
from typing import Callable, Optional, Text, Tuple, Union
import numpy as np
import torch
from einops import rearrange
from pyannote.core import Annotation, SlidingWindow, SlidingWindowFeature
from pyannote.metrics.diarization import GreedyDiarizationErrorRate
from pyannote.pipeline.parameter import Categorical, ParamDict, Uniform
from scipy.ndimage import binary_dilation
from pyannote.audio import Audio, Inference, Model, Pipeline
from pyannote.audio.core.io import AudioFile
from pyannote.audio.pipelines.clustering import Clustering
@@ -45,10 +50,6 @@ from pyannote.audio.pipelines.utils import (
)
from pyannote.audio.pipelines.utils.diarization import set_num_speakers
from pyannote.audio.utils.signal import binarize
from pyannote.core import Annotation, SlidingWindow, SlidingWindowFeature
from pyannote.metrics.diarization import GreedyDiarizationErrorRate
from pyannote.pipeline.parameter import Categorical, ParamDict, Uniform
from scipy.ndimage import binary_dilation
def batchify(iterable, batch_size: int = 32, fillvalue=None):
@@ -133,9 +134,9 @@ class SpeechSeparation(SpeakerDiarizationMixin, Pipeline):
clustering: str = "AgglomerativeClustering",
embedding_batch_size: int = 1,
segmentation_batch_size: int = 1,
der_variant: Optional[dict] = None,
token: Union[Text, None] = None,
cache_dir: Union[Path, Text, None] = None,
der_variant: dict | None = None,
token: str | None = None,
cache_dir: Path | str | None = None,
):
super().__init__()
@@ -218,7 +219,7 @@ class SpeechSeparation(SpeakerDiarizationMixin, Pipeline):
def get_segmentations(
self, file, hook=None
) -> Tuple[SlidingWindowFeature, SlidingWindowFeature]:
) -> tuple[SlidingWindowFeature, SlidingWindowFeature]:
"""Apply segmentation model
Parameter
@@ -251,7 +252,7 @@ class SpeechSeparation(SpeakerDiarizationMixin, Pipeline):
file,
binary_segmentations: SlidingWindowFeature,
exclude_overlap: bool = False,
hook: Optional[Callable] = None,
hook: Callable | None = None,
):
"""Extract embeddings for each (chunk, speaker) pair
@@ -279,7 +280,7 @@ class SpeechSeparation(SpeakerDiarizationMixin, Pipeline):
# we only re-use embeddings if they were extracted based on the same value of the
# "segmentation.threshold" hyperparameter or if the segmentation model relies on
# `powerset` mode
cache = file.get("training_cache/embeddings", dict())
cache = file.get("training_cache/embeddings", {})
if ("embeddings" in cache) and (
self._segmentation.model.specifications[0].powerset
or (cache["segmentation.threshold"] == self.segmentation.threshold)
@@ -419,7 +420,7 @@ class SpeechSeparation(SpeakerDiarizationMixin, Pipeline):
Discrete (0s and 1s) diarization.
"""
num_chunks, num_frames, local_num_speakers = segmentations.data.shape
num_chunks, num_frames, _local_num_speakers = segmentations.data.shape
num_clusters = np.max(hard_clusters) + 1
clustered_segmentations = np.nan * np.zeros(
@@ -448,11 +449,11 @@ class SpeechSeparation(SpeakerDiarizationMixin, Pipeline):
def apply(
self,
file: AudioFile,
num_speakers: Optional[int] = None,
min_speakers: Optional[int] = None,
max_speakers: Optional[int] = None,
num_speakers: int | None = None,
min_speakers: int | None = None,
max_speakers: int | None = None,
return_embeddings: bool = False,
hook: Optional[Callable] = None,
hook: Callable | None = None,
) -> Annotation:
"""Apply speaker diarization
@@ -600,7 +601,7 @@ class SpeechSeparation(SpeakerDiarizationMixin, Pipeline):
active_speakers = np.sum(discrete_diarization, axis=0) > 0
# shape: (num_speakers, )
discrete_diarization.data = discrete_diarization.data[:, active_speakers]
num_frames, num_speakers = discrete_diarization.data.shape
_num_frames, num_speakers = discrete_diarization.data.shape
hook("discrete_diarization", discrete_diarization)
clustered_separations = self.reconstruct(separations, hard_clusters, count)
@@ -670,7 +671,7 @@ class SpeechSeparation(SpeakerDiarizationMixin, Pipeline):
# at this point, `diarization` speaker labels are integers
# from 0 to `num_speakers - 1`, aligned with `centroids` rows.
if "annotation" in file and file["annotation"]:
if file.get("annotation"):
# when reference is available, use it to map hypothesized speakers
# to reference speakers (this makes later error analysis easier
# but does not modify the actual output of the diarization pipeline)
@@ -35,15 +35,15 @@ from .getter import (
from .oracle import oracle_segmentation
__all__ = [
"SpeakerDiarizationMixin",
"oracle_segmentation",
"get_augmentation",
"PipelineAugmentation",
"PipelineCalibration",
"PipelineModel",
"PipelinePLDA",
"SpeakerDiarizationMixin",
"get_augmentation",
"get_calibration",
"get_devices",
"get_model",
"PipelineModel",
"get_plda",
"PipelinePLDA",
"get_calibration",
"PipelineCalibration",
"oracle_segmentation",
]
@@ -20,7 +20,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from typing import Dict, Mapping, Optional, Tuple, Union
from collections.abc import Mapping
import numpy as np
from pyannote.core import Annotation, SlidingWindow, SlidingWindowFeature
@@ -32,9 +32,9 @@ from pyannote.audio.utils.signal import Binarize
def set_num_speakers(
num_speakers: Optional[int] = None,
min_speakers: Optional[int] = None,
max_speakers: Optional[int] = None,
num_speakers: int | None = None,
min_speakers: int | None = None,
max_speakers: int | None = None,
):
"""Validate number of speakers
@@ -74,9 +74,9 @@ class SpeakerDiarizationMixin:
@staticmethod
def set_num_speakers(
num_speakers: Optional[int] = None,
min_speakers: Optional[int] = None,
max_speakers: Optional[int] = None,
num_speakers: int | None = None,
min_speakers: int | None = None,
max_speakers: int | None = None,
):
"""Validate number of speakers
@@ -103,10 +103,10 @@ class SpeakerDiarizationMixin:
@staticmethod
def optimal_mapping(
reference: Union[Mapping, Annotation],
reference: Mapping | Annotation,
hypothesis: Annotation,
return_mapping: bool = False,
) -> Union[Annotation, Tuple[Annotation, Dict[Label, Label]]]:
) -> Annotation | tuple[Annotation, dict[Label, Label]]:
"""Find the optimal bijective mapping between reference and hypothesis labels
Parameters
@@ -129,8 +129,8 @@ class SpeakerDiarizationMixin:
"""
if isinstance(reference, Mapping):
annotated = reference.get("annotated", None)
reference = reference["annotation"]
annotated = reference["annotated"] if "annotated" in reference else None
else:
annotated = None
@@ -150,7 +150,7 @@ class SpeakerDiarizationMixin:
def speaker_count(
binarized_segmentations: SlidingWindowFeature,
frames: SlidingWindow,
warm_up: Tuple[float, float] = (0.1, 0.1),
warm_up: tuple[float, float] = (0.1, 0.1),
) -> SlidingWindowFeature:
"""Estimate frame-level number of instantaneous speakers
+8 -7
View File
@@ -22,16 +22,17 @@
# SOFTWARE.
import itertools
from collections.abc import Mapping
from pathlib import Path
from typing import Dict, Mapping
import torch
from torch_audiomentations.core.transforms_interface import BaseWaveformTransform
from torch_audiomentations.utils.config import from_dict as augmentation_from_dict
from pyannote.audio.core.calibration import Calibration
from pyannote.audio.core.model import Model
from pyannote.audio.core.pipeline import Pipeline
from pyannote.audio.core.plda import PLDA
from torch_audiomentations.core.transforms_interface import BaseWaveformTransform
from torch_audiomentations.utils.config import from_dict as augmentation_from_dict
def get_pipeline(
@@ -139,7 +140,7 @@ def get_model(
PipelineAugmentation = BaseWaveformTransform | Mapping
PipelineCalibration = Calibration | str | Dict
PipelineCalibration = Calibration | str | dict
def get_calibration(
@@ -181,7 +182,7 @@ def get_calibration(
cache_dir=cache_dir,
)
elif isinstance(calibration, Dict):
elif isinstance(calibration, dict):
calibration.setdefault("token", token)
calibration.setdefault("cache_dir", cache_dir)
loaded_calibration = Calibration.from_pretrained(**calibration)
@@ -195,7 +196,7 @@ def get_calibration(
return loaded_calibration
PipelinePLDA = PLDA | str | Dict
PipelinePLDA = PLDA | str | dict
def get_plda(
@@ -233,7 +234,7 @@ def get_plda(
elif isinstance(plda, str):
loaded_plda = PLDA.from_pretrained(plda, token=token, cache_dir=cache_dir)
elif isinstance(plda, Dict):
elif isinstance(plda, dict):
plda.setdefault("token", token)
plda.setdefault("cache_dir", cache_dir)
loaded_plda = PLDA.from_pretrained(**plda)
+22 -21
View File
@@ -21,8 +21,9 @@
# SOFTWARE.
import time
from collections.abc import Mapping
from copy import deepcopy
from typing import Any, Mapping, Optional, Text
from typing import Any
import torch
from rich.progress import (
@@ -65,11 +66,11 @@ class ArtifactHook:
def __call__(
self,
step_name: Text,
step_name: str,
step_artifact: Any,
file: Optional[Mapping] = None,
total: Optional[int] = None,
completed: Optional[int] = None,
file: Mapping | None = None,
total: int | None = None,
completed: int | None = None,
):
if (step_artifact is None) or (
self.artifacts and step_name not in self.artifacts
@@ -79,7 +80,7 @@ class ArtifactHook:
if isinstance(step_artifact, torch.Tensor):
step_artifact = step_artifact.numpy(force=True)
file.setdefault(self.file_key, dict())[step_name] = deepcopy(step_artifact)
file.setdefault(self.file_key, {})[step_name] = deepcopy(step_artifact)
class ProgressHook:
@@ -124,11 +125,11 @@ class ProgressHook:
def __call__(
self,
step_name: Text,
step_name: str,
step_artifact: Any,
file: Optional[Mapping] = None,
total: Optional[int] = None,
completed: Optional[int] = None,
file: Mapping | None = None,
total: int | None = None,
completed: int | None = None,
):
if self.hidden:
return
@@ -168,13 +169,13 @@ class TimingHook:
def __enter__(self):
self._pipeline_start_time = time.time()
self._start_time = dict()
self._end_time = dict()
self._start_time = {}
self._end_time = {}
return self
def __exit__(self, *args):
_pipeline_end_time = time.time()
processing_time = dict()
processing_time = {}
processing_time["total"] = _pipeline_end_time - self._pipeline_start_time
for step_name, _start_time in self._start_time.items():
_end_time = self._end_time[step_name]
@@ -184,11 +185,11 @@ class TimingHook:
def __call__(
self,
step_name: Text,
step_name: str,
step_artifact: Any,
file: Optional[Mapping] = None,
total: Optional[int] = None,
completed: Optional[int] = None,
file: Mapping | None = None,
total: int | None = None,
completed: int | None = None,
):
if not hasattr(self, "_file"):
self._file = file
@@ -229,11 +230,11 @@ class Hooks:
def __call__(
self,
step_name: Text,
step_name: str,
step_artifact: Any,
file: Optional[Mapping] = None,
total: Optional[int] = None,
completed: Optional[int] = None,
file: Mapping | None = None,
total: int | None = None,
completed: int | None = None,
):
for hook in self.hooks:
hook(step_name, step_artifact, file=file, total=total, completed=completed)
+2 -3
View File
@@ -20,7 +20,6 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from typing import Optional, Union
import numpy as np
from pyannote.core import Annotation, Segment, SlidingWindow, SlidingWindowFeature
@@ -31,8 +30,8 @@ from pyannote.audio.core.io import Audio, AudioFile
def oracle_segmentation(
file: AudioFile,
window: SlidingWindow,
frames: Union[SlidingWindow, float],
num_speakers: Optional[int] = None,
frames: SlidingWindow | float,
num_speakers: int | None = None,
) -> SlidingWindowFeature:
"""Oracle speaker segmentation
@@ -22,9 +22,9 @@
"""Voice activity detection pipelines"""
from collections.abc import Callable
from functools import partial
from pathlib import Path
from typing import Callable, Optional, Text, Union
import numpy as np
from pyannote.core import Annotation, SlidingWindowFeature
@@ -96,8 +96,8 @@ class VoiceActivityDetection(Pipeline):
self,
segmentation: PipelineModel = "pyannote/segmentation",
fscore: bool = False,
token: Union[Text, None] = None,
cache_dir: Union[Path, Text, None] = None,
token: str | None = None,
cache_dir: Path | str | None = None,
**inference_kwargs,
):
super().__init__()
@@ -158,7 +158,7 @@ class VoiceActivityDetection(Pipeline):
CACHED_SEGMENTATION = "cache/segmentation/inference"
def apply(self, file: AudioFile, hook: Optional[Callable] = None) -> Annotation:
def apply(self, file: AudioFile, hook: Callable | None = None) -> Annotation:
"""Apply voice activity detection
Parameters
@@ -204,7 +204,7 @@ class VoiceActivityDetection(Pipeline):
speech.uri = file["uri"]
return speech.rename_labels({label: "SPEECH" for label in speech.labels()})
def get_metric(self) -> Union[DetectionErrorRate, DetectionPrecisionRecallFMeasure]:
def get_metric(self) -> DetectionErrorRate | DetectionPrecisionRecallFMeasure:
"""Return new instance of detection metric"""
if self.fscore:
+4 -3
View File
@@ -24,15 +24,16 @@
from pathlib import Path
from pyannote.audio.core.io import Audio, AudioFile
from pyannote.core import Annotation, Segment, Timeline
from pyannote.database.util import load_rttm
from pyannote.audio.core.io import Audio, AudioFile
def load_stm(file_stm: str | Path) -> dict[str, list[dict]]:
session_ids = {}
with open(file_stm, "r") as stm:
with open(file_stm) as stm:
for line in stm:
infos = line.strip().split()
session_id, _, spk, start, end, *words = infos
@@ -57,8 +58,8 @@ def _sample(uri: str = "sample") -> AudioFile:
sample_rttm = Path(__file__).parent / f"{uri}.rttm"
diarization: Annotation = load_rttm(sample_rttm)[uri]
duration = audio.get_duration(sample_wav)
duration = waveform.shape[1] / sample_rate
annotated: Timeline = Timeline([Segment(0.0, duration)], uri=uri)
sample_stm = Path(__file__).parent / f"{uri}.stm"
+3 -3
View File
@@ -31,9 +31,9 @@ from .embedding.arcface import SupervisedRepresentationLearningWithArcFace # is
SpeakerEmbedding = SupervisedRepresentationLearningWithArcFace
__all__ = [
"SpeakerDiarization",
"VoiceActivityDetection",
"MultiLabelSegmentation",
"SpeakerEmbedding",
"PixIT",
"SpeakerDiarization",
"SpeakerEmbedding",
"VoiceActivityDetection",
]
@@ -23,7 +23,7 @@
from __future__ import annotations
from typing import Dict, Optional, Sequence, Union
from collections.abc import Sequence
import pytorch_metric_learning.losses
from pyannote.database import Protocol
@@ -82,16 +82,16 @@ class SupervisedRepresentationLearningWithArcFace(
def __init__(
self,
protocol: Protocol,
min_duration: Optional[float] = None,
min_duration: float | None = None,
duration: float = 2.0,
num_classes_per_batch: int = 32,
num_chunks_per_class: int = 1,
margin: float = 28.6,
scale: float = 64.0,
num_workers: Optional[int] = None,
num_workers: int | None = None,
pin_memory: bool = False,
augmentation: Optional[BaseWaveformTransform] = None,
metric: Union[Metric, Sequence[Metric], Dict[str, Metric]] = None,
augmentation: BaseWaveformTransform | None = None,
metric: Metric | Sequence[Metric] | dict[str, Metric] = None,
):
self.num_chunks_per_class = num_chunks_per_class
+10 -9
View File
@@ -22,13 +22,10 @@
# SOFTWARE.
import math
from typing import Dict, Sequence, Union
from collections.abc import Sequence
import torch
import torch.nn.functional as F
from pyannote.audio.core.task import Problem, Resolution, Specifications
from pyannote.audio.torchmetrics.classification import EqualErrorRate
from pyannote.audio.utils.random import create_rng_for_worker
from pyannote.core import Segment
from pyannote.database.protocol import (
SpeakerDiarizationProtocol,
@@ -39,6 +36,10 @@ from torchmetrics import Metric
from torchmetrics.classification import BinaryAUROC
from tqdm import tqdm
from pyannote.audio.core.task import Problem, Resolution, Specifications
from pyannote.audio.torchmetrics.classification import EqualErrorRate
from pyannote.audio.utils.random import create_rng_for_worker
class SupervisedRepresentationLearningTaskMixin:
"""Methods common to most supervised representation tasks"""
@@ -79,7 +80,7 @@ class SupervisedRepresentationLearningTaskMixin:
# loop over the training set, remove annotated regions shorter than
# chunk duration, and keep track of the reference annotations, per class.
self._train = dict()
self._train = {}
desc = f"Loading {self.protocol.name} training labels"
for f in tqdm(iterable=self.protocol.train(), desc=desc, unit="file"):
@@ -100,7 +101,7 @@ class SupervisedRepresentationLearningTaskMixin:
# add class to the list of classes
if klass not in self._train:
self._train[klass] = list()
self._train[klass] = []
self._train[klass].append(
{
@@ -121,7 +122,7 @@ class SupervisedRepresentationLearningTaskMixin:
def default_metric(
self,
) -> Union[Metric, Sequence[Metric], Dict[str, Metric]]:
) -> Metric | Sequence[Metric] | dict[str, Metric]:
return [
EqualErrorRate(compute_on_cpu=True, distances=False),
BinaryAUROC(compute_on_cpu=True),
@@ -243,7 +244,7 @@ class SupervisedRepresentationLearningTaskMixin:
return {"loss": loss}
def prepare_validation(self, prepared_dict: Dict):
def prepare_validation(self, prepared_dict: dict):
if isinstance(self.protocol, SpeakerVerificationProtocol):
prepared_dict["validation"] = list(self.protocol.development_trial())
@@ -251,7 +252,7 @@ class SupervisedRepresentationLearningTaskMixin:
if isinstance(self.protocol, SpeakerVerificationProtocol):
trial = self.prepared_data["validation"][idx]
data = dict()
data = {}
for idx in [1, 2]:
file = trial[f"file{idx:d}"]
duration = self.model.audio.get_duration(file)
+12 -11
View File
@@ -24,20 +24,21 @@
import itertools
import math
import random
from typing import Dict, Sequence, Union
from collections.abc import Sequence
import matplotlib.pyplot as plt
import numpy as np
import torch
from lightning.pytorch.loggers import MLFlowLogger, TensorBoardLogger
from pyannote.audio.core.task import Problem, Task, get_dtype
from pyannote.audio.utils.random import create_rng_for_worker
from pyannote.database.protocol.protocol import Scope, Subset
from torch.nn import functional as F
from torch.utils.data._utils.collate import default_collate
from torchmetrics import Metric
from torchmetrics.classification import BinaryAUROC, MulticlassAUROC, MultilabelAUROC
from pyannote.audio.core.task import Problem, Task, get_dtype
from pyannote.audio.utils.random import create_rng_for_worker
Subsets = list(Subset.__args__)
Scopes = list(Scope.__args__)
@@ -50,7 +51,7 @@ class SegmentationTask(Task):
def default_metric(
self,
) -> Union[Metric, Sequence[Metric], Dict[str, Metric]]:
) -> Metric | Sequence[Metric] | dict[str, Metric]:
"""Returns macro-average of the area under the ROC curve"""
num_classes = len(self.specifications.classes)
@@ -88,7 +89,7 @@ class SegmentationTask(Task):
)
for key, value in filters.items():
training &= self.prepared_data["audio-metadata"][key] == self.prepared_data[
"metadata"
"metadata-values"
][key].index(value)
file_ids = np.where(training)[0]
@@ -147,7 +148,7 @@ class SegmentationTask(Task):
Audio chunks.
y: (frame, )
Frame-level targets. Note that frame < time.
`frame` is infered automagically from the
`frame` is inferred automagically from the
example model output.
...
"""
@@ -161,9 +162,9 @@ class SegmentationTask(Task):
else:
# create a subchunk generator for each combination of "balance" keys
subchunks = dict()
subchunks = {}
for product in itertools.product(
*[self.prepared_data["metadata"][key] for key in balance]
*[self.prepared_data["metadata-values"][key] for key in balance]
):
# we iterate on the cartesian product of the values in metadata_unique_values
# eg: for balance=["database", "split"], with 2 databases and 2 splits:
@@ -181,7 +182,7 @@ class SegmentationTask(Task):
yield next(chunks)
def collate_X(self, batch) -> torch.Tensor:
lengths = set(b["X"].shape[-1] for b in batch)
lengths = {b["X"].shape[-1] for b in batch}
# just stack waveforms as they are if they all have the same length
if len(lengths) == 1:
@@ -251,8 +252,8 @@ class SegmentationTask(Task):
duration = np.sum(self.prepared_data["audio-annotated"][train_file_ids])
return max(self.batch_size, math.ceil(duration / self.duration))
def prepare_validation(self, prepared_data: Dict):
validation_chunks = list()
def prepare_validation(self, prepared_data: dict):
validation_chunks = []
# obtain indexes of files in the validation subset
validation_file_ids = np.where(
@@ -23,19 +23,20 @@
import itertools
import textwrap
from typing import Dict, List, Optional, Sequence, Text, Tuple, Union
from collections.abc import Sequence
import numpy as np
import torch
import torch.nn.functional as F
from pyannote.audio.core.task import Problem, Resolution, Specifications
from pyannote.audio.tasks.segmentation.mixins import SegmentationTask
from pyannote.core import Segment, SlidingWindowFeature
from pyannote.database import Protocol
from pyannote.database.protocol import SegmentationProtocol
from torch_audiomentations.core.transforms_interface import BaseWaveformTransform
from torchmetrics import Metric
from pyannote.audio.core.task import Problem, Resolution, Specifications
from pyannote.audio.tasks.segmentation.mixins import SegmentationTask
class MultiLabelSegmentation(SegmentationTask):
"""Generic multi-label segmentation
@@ -92,17 +93,17 @@ class MultiLabelSegmentation(SegmentationTask):
def __init__(
self,
protocol: Protocol,
cache: Optional[Union[str, None]] = None,
classes: Optional[List[str]] = None,
cache: str | None = None,
classes: list[str] | None = None,
duration: float = 2.0,
warm_up: Union[float, Tuple[float, float]] = 0.0,
balance: Optional[Sequence[Text]] = None,
weight: Optional[Text] = None,
warm_up: float | tuple[float, float] = 0.0,
balance: Sequence[str] | None = None,
weight: str | None = None,
batch_size: int = 32,
num_workers: Optional[int] = None,
num_workers: int | None = None,
pin_memory: bool = False,
augmentation: Optional[BaseWaveformTransform] = None,
metric: Union[Metric, Sequence[Metric], Dict[str, Metric]] = None,
augmentation: BaseWaveformTransform | None = None,
metric: Metric | Sequence[Metric] | dict[str, Metric] = None,
):
if not isinstance(protocol, SegmentationProtocol):
raise ValueError(
@@ -129,7 +130,7 @@ class MultiLabelSegmentation(SegmentationTask):
# classes should be detected. therefore, we postpone the definition of
# specifications to setup()
def post_prepare_data(self, prepared_data: Dict):
def post_prepare_data(self, prepared_data: dict):
# as different files may be annotated using a different set of classes
# (e.g. one database for speech/music/noise, and another one for
# male/female/child), we keep track of this information. this is used
@@ -154,8 +155,8 @@ class MultiLabelSegmentation(SegmentationTask):
files_iter = self.protocol.train()
if self.classes is None:
classes = list() # overall list of classes
annotated_classes = list() # list of annotated classes (per file)
classes = [] # overall list of classes
annotated_classes = [] # list of annotated classes (per file)
for file in files_iter:
file_classes = file.get("classes", None)
@@ -163,7 +164,7 @@ class MultiLabelSegmentation(SegmentationTask):
if not file_classes:
msg = textwrap.dedent(
f"""
File "{file['uri']}" (from {file['database']} database) does not
File "{file["uri"]}" (from {file["database"]} database) does not
provide a 'classes' entry. Please make sure the corresponding
training protocol provides a 'classes' entry for all files. See
https://github.com/pyannote/pyannote-database#segmentation for more
@@ -183,14 +184,14 @@ class MultiLabelSegmentation(SegmentationTask):
self.classes = classes
else:
annotated_classes = list() # list of annotated classes (per file)
annotated_classes = [] # list of annotated classes (per file)
for file in files_iter:
file_classes = file.get("classes", None)
if not file_classes:
msg = textwrap.dedent(
f"""
File "{file['uri']}" (from {file['database']} database) does not
File "{file["uri"]}" (from {file["database"]} database) does not
provide a 'classes' entry. Please make sure the corresponding
training protocol provides a 'classes' entry for all files. See
https://github.com/pyannote/pyannote-database#segmentation for more
@@ -203,7 +204,7 @@ class MultiLabelSegmentation(SegmentationTask):
if extra_classes:
msg = textwrap.dedent(
f"""
File "{file['uri']}" (from {file['database']} database) provides
File "{file["uri"]}" (from {file["database"]} database) provides
extra classes ({", ".join(extra_classes)}) that are ignored.
"""
)
@@ -279,7 +280,7 @@ class MultiLabelSegmentation(SegmentationTask):
chunk = Segment(start_time, start_time + duration)
sample = dict()
sample = {}
sample["X"], _ = self.model.audio.crop(file, chunk)
# gather all annotations of current file
annotations = self.prepared_data["annotations-segments"][
@@ -24,12 +24,20 @@
import math
import warnings
from collections import Counter
from typing import Dict, Literal, Optional, Sequence, Text, Union
from collections.abc import Sequence
from typing import Literal
import numpy as np
import torch
from lightning.pytorch.loggers import MLFlowLogger, TensorBoardLogger
from matplotlib import pyplot as plt
from pyannote.core import Segment, SlidingWindowFeature
from pyannote.database.protocol import SpeakerDiarizationProtocol
from pyannote.database.protocol.protocol import Scope, Subset
from rich.progress import track
from torch_audiomentations.core.transforms_interface import BaseWaveformTransform
from torchmetrics import Metric
from pyannote.audio.core.task import Problem, Resolution, Specifications
from pyannote.audio.tasks.segmentation.mixins import SegmentationTask
from pyannote.audio.torchmetrics import (
@@ -44,12 +52,6 @@ from pyannote.audio.torchmetrics import (
from pyannote.audio.utils.loss import nll_loss
from pyannote.audio.utils.permutation import permutate
from pyannote.audio.utils.powerset import Powerset
from pyannote.core import Segment, SlidingWindowFeature
from pyannote.database.protocol import SpeakerDiarizationProtocol
from pyannote.database.protocol.protocol import Scope, Subset
from rich.progress import track
from torch_audiomentations.core.transforms_interface import BaseWaveformTransform
from torchmetrics import Metric
Subsets = list(Subset.__args__)
Scopes = list(Scope.__args__)
@@ -112,21 +114,20 @@ class SpeakerDiarization(SegmentationTask):
def __init__(
self,
protocol: SpeakerDiarizationProtocol,
cache: Optional[Union[str, None]] = None,
cache: str | None = None,
duration: float = 10.0,
max_speakers_per_chunk: Optional[int] = None,
max_speakers_per_chunk: int | None = None,
max_speakers_per_frame: int = 2,
balance: Optional[Sequence[Text]] = None,
weight: Optional[Text] = None,
balance: Sequence[str] | None = None,
weight: str | None = None,
batch_size: int = 32,
num_workers: Optional[int] = None,
num_workers: int | None = None,
pin_memory: bool = False,
augmentation: Optional[BaseWaveformTransform] = None,
metric: Union[Metric, Sequence[Metric], Dict[str, Metric]] = None,
max_num_speakers: Optional[
int
] = None, # deprecated in favor of `max_speakers_per_chunk``
loss: Literal["bce", "mse"] = None, # deprecated
augmentation: BaseWaveformTransform | None = None,
metric: Metric | Sequence[Metric] | dict[str, Metric] = None,
max_num_speakers: int
| None = None, # deprecated in favor of `max_speakers_per_chunk``
loss: Literal["bce", "mse"] | None = None, # deprecated
):
super().__init__(
protocol,
@@ -298,10 +299,9 @@ class SpeakerDiarization(SegmentationTask):
label_scope = Scopes[self.prepared_data["audio-metadata"][file_id]["scope"]]
label_scope_key = f"{label_scope}_label_idx"
#
chunk = Segment(start_time, start_time + duration)
sample = dict()
sample = {}
sample["X"], _ = self.model.audio.crop(file, chunk)
# gather all annotations of current file
@@ -486,7 +486,7 @@ class SpeakerDiarization(SegmentationTask):
def default_metric(
self,
) -> Union[Metric, Sequence[Metric], Dict[str, Metric]]:
) -> Metric | Sequence[Metric] | dict[str, Metric]:
"""Returns diarization error rate and its components"""
return {
@@ -639,12 +639,13 @@ class SpeakerDiarization(SegmentationTask):
def evaluate(protocol: str, subset: str = "test", model: str = "pyannote/segmentation"):
"""Evaluate a segmentation model"""
from pyannote.database import FileFinder, get_protocol
from rich.progress import Progress
from pyannote.audio import Inference
from pyannote.audio.pipelines.utils import get_devices
from pyannote.audio.utils.metric import DiscreteDiarizationErrorRate
from pyannote.audio.utils.signal import binarize
from pyannote.database import FileFinder, get_protocol
from rich.progress import Progress
(device,) = get_devices(needs=1)
metric = DiscreteDiarizationErrorRate()
@@ -655,7 +656,7 @@ def evaluate(protocol: str, subset: str = "test", model: str = "pyannote/segment
main_task = progress.add_task(protocol.name, total=len(files))
file_task = progress.add_task("Processing", total=1.0)
def progress_hook(completed: Optional[int] = None, total: Optional[int] = None):
def progress_hook(completed: int | None = None, total: int | None = None):
progress.update(file_task, completed=completed / total)
inference = Inference(model, device=device)
@@ -21,16 +21,17 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from typing import Dict, Optional, Sequence, Text, Tuple, Union
from collections.abc import Sequence
import numpy as np
from pyannote.audio.core.task import Problem, Resolution, Specifications
from pyannote.audio.tasks.segmentation.mixins import SegmentationTask
from pyannote.core import Segment, SlidingWindowFeature
from pyannote.database import Protocol
from torch_audiomentations.core.transforms_interface import BaseWaveformTransform
from torchmetrics import Metric
from pyannote.audio.core.task import Problem, Resolution, Specifications
from pyannote.audio.tasks.segmentation.mixins import SegmentationTask
class VoiceActivityDetection(SegmentationTask):
"""Voice activity detection
@@ -86,16 +87,16 @@ class VoiceActivityDetection(SegmentationTask):
def __init__(
self,
protocol: Protocol,
cache: Optional[Union[str, None]] = None,
cache: str | None = None,
duration: float = 2.0,
warm_up: Union[float, Tuple[float, float]] = 0.0,
balance: Optional[Sequence[Text]] = None,
weight: Optional[Text] = None,
warm_up: float | tuple[float, float] = 0.0,
balance: Sequence[str] | None = None,
weight: str | None = None,
batch_size: int = 32,
num_workers: Optional[int] = None,
num_workers: int | None = None,
pin_memory: bool = False,
augmentation: Optional[BaseWaveformTransform] = None,
metric: Union[Metric, Sequence[Metric], Dict[str, Metric]] = None,
augmentation: BaseWaveformTransform | None = None,
metric: Metric | Sequence[Metric] | dict[str, Metric] = None,
):
super().__init__(
protocol,
@@ -150,7 +151,7 @@ class VoiceActivityDetection(SegmentationTask):
chunk = Segment(start_time, start_time + duration)
sample = dict()
sample = {}
sample["X"], _ = self.model.audio.crop(file, chunk)
# gather all annotations of current file
+29 -29
View File
@@ -28,13 +28,22 @@ import math
import random
import warnings
from collections import Counter
from collections.abc import Sequence
from functools import partial
from typing import Dict, Literal, Optional, Sequence, Text, Union
from typing import Literal
import numpy as np
import torch
from lightning.pytorch.loggers import MLFlowLogger, TensorBoardLogger
from matplotlib import pyplot as plt
from pyannote.core import Segment, SlidingWindowFeature
from pyannote.database.protocol import SpeakerDiarizationProtocol
from pyannote.database.protocol.protocol import Scope, Subset
from rich.progress import track
from torch.utils.data import DataLoader, IterableDataset
from torch_audiomentations.core.transforms_interface import BaseWaveformTransform
from torchmetrics import Metric
from pyannote.audio.core.task import Problem, Resolution, Specifications
from pyannote.audio.tasks.segmentation.mixins import SegmentationTask, Task
from pyannote.audio.torchmetrics import (
@@ -47,13 +56,6 @@ from pyannote.audio.torchmetrics import (
from pyannote.audio.utils.loss import binary_cross_entropy
from pyannote.audio.utils.permutation import permutate
from pyannote.audio.utils.random import create_rng_for_worker
from pyannote.core import Segment, SlidingWindowFeature
from pyannote.database.protocol import SpeakerDiarizationProtocol
from pyannote.database.protocol.protocol import Scope, Subset
from rich.progress import track
from torch.utils.data import DataLoader, IterableDataset
from torch_audiomentations.core.transforms_interface import BaseWaveformTransform
from torchmetrics import Metric
try:
from asteroid.losses import MixITLossWrapper, multisrc_neg_sisdr
@@ -154,22 +156,21 @@ class PixIT(SegmentationTask):
def __init__(
self,
protocol: SpeakerDiarizationProtocol,
cache: Optional[Union[str, None]] = None,
cache: str | None = None,
duration: float = 5.0,
max_speakers_per_chunk: Optional[int] = None,
max_speakers_per_frame: Optional[int] = None,
max_speakers_per_chunk: int | None = None,
max_speakers_per_frame: int | None = None,
weigh_by_cardinality: bool = False,
balance: Optional[Sequence[Text]] = None,
weight: Optional[Text] = None,
balance: Sequence[str] | None = None,
weight: str | None = None,
batch_size: int = 32,
num_workers: Optional[int] = None,
num_workers: int | None = None,
pin_memory: bool = False,
augmentation: Optional[BaseWaveformTransform] = None,
metric: Union[Metric, Sequence[Metric], Dict[str, Metric]] = None,
max_num_speakers: Optional[
int
] = None, # deprecated in favor of `max_speakers_per_chunk``
loss: Literal["bce", "mse"] = None, # deprecated
augmentation: BaseWaveformTransform | None = None,
metric: Metric | Sequence[Metric] | dict[str, Metric] = None,
max_num_speakers: int
| None = None, # deprecated in favor of `max_speakers_per_chunk``
loss: Literal["bce", "mse"] | None = None, # deprecated
separation_loss_weight: float = 0.5,
):
if not ASTEROID_IS_AVAILABLE:
@@ -363,10 +364,9 @@ class PixIT(SegmentationTask):
label_scope = Scopes[self.prepared_data["audio-metadata"][file_id]["scope"]]
label_scope_key = f"{label_scope}_label_idx"
#
chunk = Segment(start_time, start_time + duration)
sample = dict()
sample = {}
sample["X"], _ = self.model.audio.crop(file, chunk)
# gather all annotations of current file
@@ -460,7 +460,7 @@ class PixIT(SegmentationTask):
else:
# create a subchunk generator for each combination of "balance" keys
subchunks = dict()
subchunks = {}
for product in itertools.product(
[self.metadata_unique_values[key] for key in balance]
):
@@ -503,7 +503,7 @@ class PixIT(SegmentationTask):
for key, value in filters.items():
split_files &= self.prepared_data["audio-metadata"][
key
] == self.prepared_data["metadata"][key].index(value)
] == self.prepared_data["metadata-values"][key].index(value)
file_ids = np.where(split_files)[0]
# turn annotated duration into a probability distribution
@@ -791,7 +791,7 @@ class PixIT(SegmentationTask):
self,
permutated_prediction: torch.Tensor,
target: torch.Tensor,
weight: Optional[torch.Tensor] = None,
weight: torch.Tensor | None = None,
) -> torch.Tensor:
"""Permutation-invariant segmentation loss
@@ -968,9 +968,9 @@ class PixIT(SegmentationTask):
(
seg_loss,
separation_loss,
diarization,
permutated_diarization,
target,
_diarization,
_permutated_diarization,
_target,
) = self.common_step(batch)
self.model.log(
@@ -1029,7 +1029,7 @@ class PixIT(SegmentationTask):
def default_metric(
self,
) -> Union[Metric, Sequence[Metric], Dict[str, Metric]]:
) -> Metric | Sequence[Metric] | dict[str, Metric]:
"""Returns diarization error rate and its components"""
return {
+3 -3
View File
@@ -2,14 +2,14 @@ from .metrics import (
set_opentelemetry_log_level,
set_telemetry_metrics,
track_model_init,
track_pipeline_init,
track_pipeline_apply,
track_pipeline_init,
)
__all__ = [
"set_telemetry_metrics",
"set_opentelemetry_log_level",
"set_telemetry_metrics",
"track_model_init",
"track_pipeline_init",
"track_pipeline_apply",
"track_pipeline_init",
]
+18 -7
View File
@@ -19,7 +19,7 @@ from __future__ import annotations
import logging
import os
from pathlib import Path
from typing import Any
from typing import TYPE_CHECKING, Any
from uuid import uuid4
import numpy as np
@@ -27,8 +27,13 @@ import yaml
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from pyannote.audio import __version__
if TYPE_CHECKING:
from pyannote.audio import Model, Pipeline
from pyannote.audio.core.io import AudioFile
CONFIG_FILE = Path(__file__).parent / "config.yaml"
# Load initial configuration
@@ -43,11 +48,13 @@ DEFAULT_LOG_LEVEL = CONFIG["telemetry_log_level"]
# Initialize metrics with basic setup
exporter = OTLPSpanExporter(endpoint=OTLP_ENDPOINT, headers=OTLP_HEADERS, timeout=5)
provider = TracerProvider(shutdown_on_exit=False)
provider.add_span_processor(BatchSpanProcessor(exporter, schedule_delay_millis=500, export_timeout_millis=5000))
provider.add_span_processor(
BatchSpanProcessor(exporter, schedule_delay_millis=500, export_timeout_millis=5000)
)
tracer = provider.get_tracer(__name__)
def track_model_init(model: "Model") -> None:
def track_model_init(model: Model) -> None:
"""
Increment the model initialization counter.
@@ -67,7 +74,7 @@ def track_model_init(model: "Model") -> None:
)
def track_pipeline_init(pipeline: "Pipeline") -> None:
def track_pipeline_init(pipeline: Pipeline) -> None:
"""
Track pipeline initialization
@@ -89,8 +96,8 @@ def track_pipeline_init(pipeline: "Pipeline") -> None:
def track_pipeline_apply(
pipeline: "Pipeline",
file: "AudioFile",
pipeline: Pipeline,
file: AudioFile,
num_speakers: int | None = None,
min_speakers: int | None = None,
max_speakers: int | None = None,
@@ -111,9 +118,11 @@ def track_pipeline_apply(
if is_metrics_enabled():
from pyannote.audio.core.io import Audio
duration: float = Audio().get_duration(file)
from pyannote.audio.pipelines.utils.diarization import set_num_speakers
num_speakers, min_speakers, max_speakers = set_num_speakers(
num_speakers=num_speakers,
min_speakers=min_speakers,
@@ -130,7 +139,9 @@ def track_pipeline_apply(
"duration": duration,
"num_speakers": num_speakers,
"min_speakers": min_speakers,
"max_speakers": max_speakers if np.isfinite(max_speakers) else "inf",
"max_speakers": max_speakers
if np.isfinite(max_speakers)
else "inf",
},
)
+1 -1
View File
@@ -44,9 +44,9 @@ __all__ = [
"FalseAlarmRate",
"MissedDetectionRate",
"OptimalDiarizationErrorRate",
"OptimalDiarizationErrorRateThreshold",
"OptimalFalseAlarmRate",
"OptimalMissedDetectionRate",
"OptimalSpeakerConfusionRate",
"OptimalDiarizationErrorRateThreshold",
"SpeakerConfusionRate",
]
@@ -40,15 +40,15 @@ from .diarization_error_rate import (
__all__ = [
"DetectionErrorRate",
"DiarizationErrorRate",
"SegmentationErrorRate",
"DiarizationPrecision",
"DiarizationRecall",
"FalseAlarmRate",
"MissedDetectionRate",
"OptimalDiarizationErrorRate",
"OptimalDiarizationErrorRateThreshold",
"OptimalFalseAlarmRate",
"OptimalMissedDetectionRate",
"OptimalSpeakerConfusionRate",
"OptimalDiarizationErrorRateThreshold",
"SegmentationErrorRate",
"SpeakerConfusionRate",
]
@@ -20,7 +20,6 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from typing import Optional
import torch
from einops import rearrange
@@ -116,7 +115,7 @@ class SegmentationErrorRate(DiarizationErrorRate):
"""
def __init__(
self, window_size: int, step_size: Optional[int] = None, threshold: float = 0.5
self, window_size: int, step_size: int | None = None, threshold: float = 0.5
):
super().__init__(threshold=threshold)
self.window_size = window_size
@@ -290,7 +289,7 @@ class OptimalDiarizationErrorRate(Metric):
higher_is_better = False
is_differentiable = False
def __init__(self, threshold: Optional[torch.Tensor] = None):
def __init__(self, threshold: torch.Tensor | None = None):
super().__init__()
threshold = threshold or torch.linspace(0.0, 1.0, 51)
@@ -21,8 +21,6 @@
# SOFTWARE.
from typing import Optional
import torch
from pyannote.metrics.binary_classification import det_curve
from torchmetrics import Metric
@@ -30,9 +28,8 @@ from torchmetrics.utilities.data import dim_zero_cat
class EqualErrorRate(Metric):
is_differentiable: Optional[bool] = False
higher_is_better: Optional[bool] = False
is_differentiable: bool | None = False
higher_is_better: bool | None = False
full_state_update: bool = True
def __init__(self, distances: bool = True, compute_on_cpu: bool = True, **kwargs):
@@ -22,7 +22,6 @@
from numbers import Number
from typing import Optional, Tuple, Union
import torch
import torch.nn.functional as F
@@ -33,9 +32,9 @@ from pyannote.audio.utils.permutation import permutate
def _der_update(
preds: torch.Tensor,
target: torch.Tensor,
threshold: Union[torch.Tensor, float] = 0.5,
threshold: torch.Tensor | float = 0.5,
reduce: str = "batch",
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
"""Compute components of diarization error rate
Parameters
@@ -190,7 +189,7 @@ def _der_compute(
def diarization_error_rate(
preds: torch.Tensor,
target: torch.Tensor,
threshold: Union[torch.Tensor, float] = 0.5,
threshold: torch.Tensor | float = 0.5,
reduce: str = "batch",
return_components: bool = False,
) -> torch.Tensor:
@@ -235,7 +234,7 @@ def diarization_error_rate(
def optimal_diarization_error_rate(
preds: torch.Tensor,
target: torch.Tensor,
threshold: Optional[torch.Tensor] = None,
threshold: torch.Tensor | None = None,
) -> torch.Tensor:
"""Compute optimal diarization error rate
+1 -4
View File
@@ -67,10 +67,7 @@ def check_dependencies(dependencies: dict[str, str], what: str) -> None:
If a required dependency has an incompatible version.
"""
skip_dependency_check: bool = os.getenv(
"PYANNOTE_SKIP_DEPENDENCY_CHECK", False
) in [
True,
skip_dependency_check: bool = os.getenv("PYANNOTE_SKIP_DEPENDENCY_CHECK", "0") in [
"1",
"true",
"True",
+3 -1
View File
@@ -89,7 +89,9 @@ def download_from_hf_hub(
token=token,
)
except HfHubHTTPError:
asset_file_name: str = asset_file.name if isinstance(asset_file, AssetFileName) else asset_file
asset_file_name: str = (
asset_file.name if isinstance(asset_file, AssetFileName) else asset_file
)
print(
f"""
Could not download {asset_file_name} from {model_id}.
+5 -7
View File
@@ -23,13 +23,11 @@
"""Frame-weighted versions of common loss functions"""
from typing import Optional
import torch
import torch.nn.functional as F
def interpolate(target: torch.Tensor, weight: Optional[torch.Tensor] = None):
def interpolate(target: torch.Tensor, weight: torch.Tensor | None = None):
"""Interpolate weight to match target frame resolution
Parameters
@@ -59,7 +57,7 @@ def interpolate(target: torch.Tensor, weight: Optional[torch.Tensor] = None):
def binary_cross_entropy(
prediction: torch.Tensor,
target: torch.Tensor,
weight: Optional[torch.Tensor] = None,
weight: torch.Tensor | None = None,
) -> torch.Tensor:
"""Frame-weighted binary cross entropy
@@ -97,7 +95,7 @@ def binary_cross_entropy(
def mse_loss(
prediction: torch.Tensor,
target: torch.Tensor,
weight: Optional[torch.Tensor] = None,
weight: torch.Tensor | None = None,
) -> torch.Tensor:
"""Frame-weighted mean-squared error loss
@@ -137,8 +135,8 @@ def mse_loss(
def nll_loss(
prediction: torch.Tensor,
target: torch.Tensor,
class_weight: Optional[torch.Tensor] = None,
weight: Optional[torch.Tensor] = None,
class_weight: torch.Tensor | None = None,
weight: torch.Tensor | None = None,
) -> torch.Tensor:
"""Frame-weighted negative log-likelihood loss
+9 -13
View File
@@ -21,7 +21,6 @@
# SOFTWARE.
from functools import singledispatchmethod
from typing import Dict, List, Optional
import numpy as np
from pyannote.core import (
@@ -108,13 +107,13 @@ class DiscreteDiarizationErrorRate(BaseMetric):
self,
reference,
hypothesis,
uem: Optional[Timeline] = None,
uem: Timeline | None = None,
):
return self.compute_components_helper(hypothesis, reference, uem=uem)
@singledispatchmethod
def compute_components_helper(
self, hypothesis, reference, uem: Optional[Timeline] = None
self, hypothesis, reference, uem: Timeline | None = None
):
klass = hypothesis.__class__.__name__
raise NotImplementedError(
@@ -126,7 +125,7 @@ class DiscreteDiarizationErrorRate(BaseMetric):
self,
hypothesis: np.ndarray,
reference: np.ndarray,
uem: Optional[Timeline] = None,
uem: Timeline | None = None,
):
if reference.ndim != 2:
@@ -167,7 +166,7 @@ class DiscreteDiarizationErrorRate(BaseMetric):
self,
hypothesis: SlidingWindowFeature,
reference: Annotation,
uem: Optional[Timeline] = None,
uem: Timeline | None = None,
):
ndim = hypothesis.data.ndim
@@ -194,7 +193,6 @@ class DiscreteDiarizationErrorRate(BaseMetric):
# if (num_frames, num_speakers)-shaped, compute just one DER for the whole file
if ndim == 2:
if uem is None:
return self.compute_components_helper(hypothesis.data, reference.data)
@@ -212,10 +210,8 @@ class DiscreteDiarizationErrorRate(BaseMetric):
# if (num_chunks, num_frames, num_speakers)-shared, compute one DER per chunk and aggregate
elif ndim == 3:
components = self.init_components()
for window, hypothesis_window in hypothesis:
# Skip any window not fully covered by a segment of the uem
if uem is not None and not uem.covers(Timeline([window])):
continue
@@ -259,7 +255,7 @@ class SlidingDiarizationErrorRate(BaseMetric):
self,
reference,
hypothesis,
uem: Optional[Timeline] = None,
uem: Timeline | None = None,
):
if uem is None:
@@ -313,7 +309,7 @@ class MacroAverageFMeasure(BaseMetric):
def __init__(
self,
classes: List[str], # noqa
classes: list[str],
collar: float = 0.0,
beta: float = 1.0,
**kwargs,
@@ -326,7 +322,7 @@ class MacroAverageFMeasure(BaseMetric):
self.collar = collar
self.beta = beta
self._sub_metrics: Dict[str, DetectionPrecisionRecallFMeasure] = {
self._sub_metrics: dict[str, DetectionPrecisionRecallFMeasure] = {
label: DetectionPrecisionRecallFMeasure(collar=collar, beta=beta, **kwargs)
for label in self.classes
}
@@ -352,7 +348,7 @@ class MacroAverageFMeasure(BaseMetric):
)
return details
def compute_metric(self, detail: Dict[str, float]):
def compute_metric(self, detail: dict[str, float]):
return np.mean(list(detail.values()))
def report(self, display=False):
@@ -367,7 +363,7 @@ class MacroAverageFMeasure(BaseMetric):
index=True,
sparsify=False,
justify="right",
float_format=lambda f: "{0:.2f}".format(f),
float_format=lambda f: f"{f:.2f}",
)
)
+4 -3
View File
@@ -21,16 +21,17 @@
# SOFTWARE.
from typing import Any, Callable, Tuple, Union
from collections.abc import Callable
from typing import Any
from pyannote.audio.core.model import Specifications
def map_with_specifications(
specifications: Union[Specifications, Tuple[Specifications]],
specifications: Specifications | tuple[Specifications],
func: Callable,
*iterables,
) -> Union[Any, Tuple[Any]]:
) -> Any | tuple[Any]:
"""Compute the function using arguments from each of the iterables
Returns a tuple if provided `specifications` is a tuple,
+1 -3
View File
@@ -1,10 +1,8 @@
# TODO - make it depth-recursive
# TODO - switch to Omegaconf maybe?
from typing import Optional
def merge_dict(defaults: dict, custom: Optional[dict] = None):
def merge_dict(defaults: dict, custom: dict | None = None):
params = dict(defaults)
if custom is not None:
params.update(custom)
+11 -7
View File
@@ -23,8 +23,9 @@
import math
from collections.abc import Callable
from functools import partial, singledispatch
from typing import Callable, List, Literal, Tuple
from typing import Literal
import networkx as nx
import numpy as np
@@ -35,7 +36,12 @@ from scipy.optimize import linear_sum_assignment
@singledispatch
def permutate(y1, y2, cost_func: Callable | Literal["mse", "mae"] = "mse", return_cost: bool = False):
def permutate(
y1,
y2,
cost_func: Callable | Literal["mse", "mae"] = "mse",
return_cost: bool = False,
):
"""Find cost-minimizing permutation
Parameters
@@ -46,7 +52,7 @@ def permutate(y1, y2, cost_func: Callable | Literal["mse", "mae"] = "mse", retur
(num_samples, num_classes_2) or (batch_size, num_samples, num_classes_2)
cost_func : callable or {"mse", "mae"}, optional
Can be either "mse" (mean squared error) or "mae" (mean absolute error) or a callable.
When callable, takes two (num_samples, num_classes) sequences
When callable, takes two (num_samples, num_classes) sequences
and returns (num_classes, ) pairwise cost.
Defaults to computing mean squared error ("mse").
return_cost : bool, optional
@@ -102,7 +108,7 @@ def permutate_torch(
y2: torch.Tensor,
cost_func: Callable | Literal["mse", "mae"] = "mse",
return_cost: bool = False,
) -> Tuple[torch.Tensor, List[Tuple[int]]]:
) -> tuple[torch.Tensor, list[tuple[int]]]:
batch_size, num_samples, num_classes_1 = y1.shape
@@ -179,7 +185,7 @@ def permutate_numpy(
y2: np.ndarray,
cost_func: Callable | Literal["mse", "mae"] = "mse",
return_cost: bool = False,
) -> Tuple[np.ndarray, List[Tuple[int]]]:
) -> tuple[np.ndarray, list[tuple[int]]]:
output = permutate(
torch.from_numpy(y1),
@@ -233,7 +239,6 @@ def build_permutation_graph(
for C, (chunk, segmentation) in enumerate(segmentations):
for c in range(max(0, C - lookahead[0]), min(num_chunks, C + lookahead[1] + 1)):
if c == C:
continue
@@ -257,7 +262,6 @@ def build_permutation_graph(
)
for this, that in enumerate(permutation):
this_is_active = np.any(this_segmentations[:, this] > onset)
that_is_active = np.any(that_segmentations[:, that] > onset)
+9 -11
View File
@@ -27,11 +27,10 @@
from functools import cached_property
from itertools import combinations, permutations
from typing import Dict, Tuple
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch import nn
class Powerset(nn.Module):
@@ -53,7 +52,6 @@ class Powerset(nn.Module):
self.register_buffer("mapping", self.build_mapping(), persistent=False)
self.register_buffer("cardinality", self.build_cardinality(), persistent=False)
@cached_property
def powerset_classes(self) -> list[set[int]]:
"""List of powerset classes
@@ -67,7 +65,7 @@ class Powerset(nn.Module):
List of powerset classes, each represented as a set of regular class indices.
"""
powerset_classes = []
for set_size in range(0, self.max_set_size + 1):
for set_size in range(self.max_set_size + 1):
for current_set in combinations(range(self.num_classes), set_size):
powerset_classes.append(set(current_set))
return powerset_classes
@@ -101,7 +99,7 @@ class Powerset(nn.Module):
"""
mapping = torch.zeros(self.num_powerset_classes, self.num_classes)
powerset_k = 0
for set_size in range(0, self.max_set_size + 1):
for set_size in range(self.max_set_size + 1):
for current_set in combinations(range(self.num_classes), set_size):
mapping[powerset_k, current_set] = 1
powerset_k += 1
@@ -168,8 +166,8 @@ class Powerset(nn.Module):
)
def _permutation_powerset(
self, multilabel_permutation: Tuple[int, ...]
) -> Tuple[int, ...]:
self, multilabel_permutation: tuple[int, ...]
) -> tuple[int, ...]:
"""Helper function for `permutation_mapping` property
Takes a (num_classes,)-shaped permutation in multilabel space and returns
@@ -213,7 +211,7 @@ class Powerset(nn.Module):
return tuple(powerset_permutation.tolist())
@cached_property
def permutation_mapping(self) -> Dict[Tuple[int, ...], Tuple[int, ...]]:
def permutation_mapping(self) -> dict[tuple[int, ...], tuple[int, ...]]:
"""Mapping between multilabel and powerset permutations
Example
@@ -234,8 +232,8 @@ class Powerset(nn.Module):
for multilabel_permutation in permutations(
range(self.num_classes), self.num_classes
):
permutation_mapping[
tuple(multilabel_permutation)
] = self._permutation_powerset(multilabel_permutation)
permutation_mapping[tuple(multilabel_permutation)] = (
self._permutation_powerset(multilabel_permutation)
)
return permutation_mapping
+9 -12
View File
@@ -1,6 +1,3 @@
#!/usr/bin/env python
# encoding: utf-8
# The MIT License (MIT)
# Copyright (c) 2022-2025 CNRS
@@ -28,12 +25,12 @@
# Hervé BREDIN - http://herve.niderb.fr
from functools import reduce
from typing import Dict, List, Optional, Set
from pyannote.audio.core.io import Audio, get_audio_metadata
from pyannote.core import Annotation, Segment
from pyannote.database import ProtocolFile
from pyannote.audio.core.io import Audio, get_audio_metadata
class LowerTemporalResolution:
"""Artificially degrade temporal resolution of reference annotation
@@ -84,16 +81,16 @@ class DeriveMetaLabels:
def __init__(
self,
classes: List[str],
unions: Optional[Dict[str, List[str]]] = None,
intersections: Optional[Dict[str, List[str]]] = None,
classes: list[str],
unions: dict[str, list[str]] | None = None,
intersections: dict[str, list[str]] | None = None,
):
self.classes: Set[str] = set(classes)
self.unions = unions if unions is not None else dict()
self.intersections = intersections if intersections is not None else dict()
self.classes: set[str] = set(classes)
self.unions = unions if unions is not None else {}
self.intersections = intersections if intersections is not None else {}
@property
def all_classes(self) -> List[str]:
def all_classes(self) -> list[str]:
"""A list of all the classes (base, union-based and intersection-based) that can be found
in output annotations from this preprocessor"""
return sorted(
+5 -7
View File
@@ -21,8 +21,6 @@
# SOFTWARE.
from typing import Union
try:
from IPython.display import Audio as IPythonAudio
from IPython.display import Video as IPythonVideo
@@ -47,7 +45,7 @@ except ImportError:
MOVIEPY_INSTALLED = False
from typing import Mapping, Optional
from collections.abc import Mapping
import torch
from pyannote.core import (
@@ -64,7 +62,7 @@ from pyannote.audio.core.model import Model
from pyannote.audio.utils.signal import Binarize
def listen(audio_file: AudioFile, segment: Optional[Segment] = None) -> None:
def listen(audio_file: AudioFile, segment: Segment | None = None) -> None:
"""listen to audio
Allows playing of audio files. It will play the whole thing unless
@@ -91,7 +89,7 @@ def listen(audio_file: AudioFile, segment: Optional[Segment] = None) -> None:
def preview(
audio_file: AudioFile,
segment: Optional[Segment] = None,
segment: Segment | None = None,
zoom: float = 10.0,
video_fps: int = 5,
video_ext: str = "webm",
@@ -262,7 +260,7 @@ def BROKEN_preview_training_samples(
video_fps: int = 5,
video_ext: str = "webm",
display: bool = True,
) -> Union[IPythonVideo, str]:
) -> IPythonVideo | str:
"""Preview training samples of a given model
Parameters
@@ -291,7 +289,7 @@ def BROKEN_preview_training_samples(
"""
batch = next(iter(model.train_dataloader()))
batch_size, num_channels, num_samples = batch["X"].shape
batch_size, _num_channels, num_samples = batch["X"].shape
batch_size, num_frames, num_speakers = batch["y"].shape
sample_rate = model.audio.sample_rate
+5 -5
View File
@@ -21,13 +21,13 @@
# SOFTWARE.
from collections.abc import Callable
from functools import partial
from typing import Callable, Dict, Set, Text
import torch.nn as nn
from torch import nn
def probe(trunk: nn.Module, branches: Dict[Text, Text]) -> Callable:
def probe(trunk: nn.Module, branches: dict[str, str]) -> Callable:
"""Add probing branches to a trunk module
Parameters
@@ -99,7 +99,7 @@ def probe(trunk: nn.Module, branches: Dict[Text, Text]) -> Callable:
trunk.__probe_handles = []
def __probe_init(module, input):
trunk.__probe = dict()
trunk.__probe = {}
handle = trunk.register_forward_pre_hook(__probe_init)
trunk.__probe_handles.append(handle)
@@ -110,7 +110,7 @@ def probe(trunk: nn.Module, branches: Dict[Text, Text]) -> Callable:
if not isinstance(branches, dict):
branches = {b: b for b in branches}
sehcnarb: Dict[Text, Set] = dict()
sehcnarb: dict[str, set] = {}
for branch_name, layer_name in branches.items():
if layer_name not in sehcnarb:
sehcnarb[layer_name] = set()
+6 -9
View File
@@ -21,13 +21,13 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from typing import Optional
from pyannote.audio.core.io import Audio
from pyannote.core import Annotation
from pyannote.database import FileFinder, Protocol, get_annotated
from pyannote.database.protocol import SpeakerVerificationProtocol
from pyannote.audio.core.io import Audio
get_duration = Audio(mono="downmix").get_duration
@@ -136,9 +136,9 @@ class FilterByNumberOfSpeakers:
def __init__(
self,
num_speakers: Optional[int] = None,
min_speakers: Optional[int] = None,
max_speakers: Optional[int] = None,
num_speakers: int | None = None,
min_speakers: int | None = None,
max_speakers: int | None = None,
):
from pyannote.audio.pipelines.utils.diarization import set_num_speakers
@@ -161,7 +161,4 @@ class FilterByNumberOfSpeakers:
if self.min_speakers is not None and self.min_speakers > num_speakers:
return False
if self.max_speakers is not None and self.max_speakers < num_speakers:
return False
return True
return not (self.max_speakers is not None and self.max_speakers < num_speakers)
+12 -14
View File
@@ -20,8 +20,6 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from typing import List
def conv1d_num_frames(
num_samples, kernel_size=5, stride=1, padding=0, dilation=1
@@ -55,10 +53,10 @@ def conv1d_num_frames(
def multi_conv_num_frames(
num_samples: int,
kernel_size: List[int] = None,
stride: List[int] = None,
padding: List[int] = None,
dilation: List[int] = None,
kernel_size: list[int] | None = None,
stride: list[int] | None = None,
padding: list[int] | None = None,
dilation: list[int] | None = None,
) -> int:
num_frames = num_samples
for k, s, p, d in zip(kernel_size, stride, padding, dilation):
@@ -99,10 +97,10 @@ def conv1d_receptive_field_size(
def multi_conv_receptive_field_size(
num_frames: int,
kernel_size: List[int] = None,
stride: List[int] = None,
padding: List[int] = None,
dilation: List[int] = None,
kernel_size: list[int] | None = None,
stride: list[int] | None = None,
padding: list[int] | None = None,
dilation: list[int] | None = None,
) -> int:
receptive_field_size = num_frames
@@ -147,10 +145,10 @@ def conv1d_receptive_field_center(
def multi_conv_receptive_field_center(
frame: int,
kernel_size: List[int] = None,
stride: List[int] = None,
padding: List[int] = None,
dilation: List[int] = None,
kernel_size: list[int] | None = None,
stride: list[int] | None = None,
padding: list[int] | None = None,
dilation: list[int] | None = None,
) -> int:
receptive_field_center = frame
for k, s, p, d in reversed(list(zip(kernel_size, stride, padding, dilation))):
+2 -4
View File
@@ -27,12 +27,10 @@ import warnings
import torch
class ReproducibilityError(Exception):
...
class ReproducibilityError(Exception): ...
class ReproducibilityWarning(UserWarning):
...
class ReproducibilityWarning(UserWarning): ...
def raise_reproducibility(device: torch.device):
+8 -12
View File
@@ -1,5 +1,3 @@
#!/usr/bin/env python
# encoding: utf-8
#
# The MIT License (MIT)
#
@@ -32,7 +30,6 @@
from functools import singledispatch
from itertools import zip_longest
from typing import Optional, Union
import einops
import numpy as np
@@ -45,8 +42,8 @@ from pyannote.core.utils.generators import pairwise, string_generator
def binarize(
scores,
onset: float = 0.5,
offset: Optional[float] = None,
initial_state: Optional[Union[bool, np.ndarray]] = None,
offset: float | None = None,
initial_state: bool | np.ndarray | None = None,
):
"""(Batch) hysteresis thresholding
@@ -79,8 +76,8 @@ def binarize(
def binarize_ndarray(
scores: np.ndarray,
onset: float = 0.5,
offset: Optional[float] = None,
initial_state: Optional[Union[bool, np.ndarray]] = None,
offset: float | None = None,
initial_state: bool | np.ndarray | None = None,
):
"""(Batch) hysteresis thresholding
@@ -145,8 +142,8 @@ def binarize_ndarray(
def binarize_swf(
scores: SlidingWindowFeature,
onset: float = 0.5,
offset: Optional[float] = None,
initial_state: Optional[bool] = None,
offset: float | None = None,
initial_state: bool | None = None,
):
"""(Batch) hysteresis thresholding
@@ -233,7 +230,7 @@ class Binarize:
def __init__(
self,
onset: float = 0.5,
offset: Optional[float] = None,
offset: float | None = None,
min_duration_on: float = 0.0,
min_duration_off: float = 0.0,
pad_onset: float = 0.0,
@@ -265,7 +262,7 @@ class Binarize:
Binarized scores.
"""
num_frames, num_classes = scores.data.shape
num_frames, _num_classes = scores.data.shape
frames = scores.sliding_window
timestamps = [frames[i].middle for i in range(num_frames)]
@@ -282,7 +279,6 @@ class Binarize:
is_active = k_scores[0] > self.onset
for t, y in zip(timestamps[1:], k_scores[1:]):
# currently active
if is_active:
# switching from active to inactive
+9 -8
View File
@@ -173,8 +173,7 @@ def l2_norm(vec_or_matrix):
)
else:
raise ValueError(
"Wrong number of dimensions, 1 or 2 is supported, not %i."
% len(vec_or_matrix.shape)
f"Wrong number of dimensions, 1 or 2 is supported, not {len(vec_or_matrix.shape)}."
)
@@ -208,11 +207,13 @@ def vbx_setup(transform_npz, plda_npz):
plda_tr = wccn.T[::-1]
# tf for preprocessing x-vectors (centering, whitening, LDA)
xvec_tf = lambda x: np.sqrt(lda.shape[1]) * l2_norm(
lda.T.dot(np.sqrt(lda.shape[0]) * l2_norm(x - mean1).T).T - mean2
)
def xvec_tf(x):
return np.sqrt(lda.shape[1]) * l2_norm(
lda.T.dot(np.sqrt(lda.shape[0]) * l2_norm(x - mean1).T).T - mean2
)
# tf to map x-vectors to the PLDA latent space (center, apply transform, optional truncate to 'lda_dim')
plda_tf = lambda x0, lda_dim=lda.shape[1]: (x0 - plda_mu).dot(plda_tr.T)[
:, :lda_dim
]
def plda_tf(x0, lda_dim=lda.shape[1]):
return (x0 - plda_mu).dot(plda_tr.T)[:, :lda_dim]
return xvec_tf, plda_tf, plda_psi
+9 -6
View File
@@ -1,6 +1,6 @@
from lightning import Trainer
import numpy as np
import pytest
from lightning import Trainer
from pyannote.core import SlidingWindowFeature
from pyannote.database import FileFinder, registry
@@ -12,12 +12,14 @@ from pyannote.audio.tasks import VoiceActivityDetection
HF_SAMPLE_MODEL_ID = "pyannote/ci-segmentation"
@pytest.mark.network
def test_hf_download_inference():
model = Model.from_pretrained(HF_SAMPLE_MODEL_ID)
inference = Inference(model, device="cpu")
assert isinstance(inference, Inference)
@pytest.mark.network
def test_hf_download_model():
model = Model.from_pretrained(HF_SAMPLE_MODEL_ID)
assert isinstance(model, Model)
@@ -49,7 +51,7 @@ def dev_file():
def test_duration_warning(trained):
protocol, model = trained
_protocol, model = trained
with pytest.warns(UserWarning):
duration = model.specifications.duration
new_duration = duration + 1
@@ -57,20 +59,20 @@ def test_duration_warning(trained):
def test_step_check_warning(trained):
protocol, model = trained
_protocol, model = trained
with pytest.raises(ValueError):
duration = model.specifications.duration
Inference(model, step=duration + 1, batch_size=128)
def test_invalid_window_fails(trained):
protocol, model = trained
_protocol, model = trained
with pytest.raises(ValueError):
Inference(model, window="unknown")
def test_invalid_resolution_fails(trained):
protocol, model = trained
_protocol, model = trained
with pytest.warns(UserWarning):
model.specifications.resolution = Resolution.FRAME
Inference(model, window="whole", batch_size=128)
@@ -85,12 +87,13 @@ def test_whole_window_slide(trained):
def test_on_file_path(trained):
protocol, model = trained
_protocol, model = trained
inference = Inference(model, batch_size=128)
output = inference("tests/data/dev00.wav")
assert isinstance(output, SlidingWindowFeature)
@pytest.mark.network
def test_skip_aggregation(pretrained_model, dev_file):
inference = Inference(pretrained_model, skip_aggregation=True)
scores = inference(dev_file)
+11 -3
View File
@@ -1,8 +1,7 @@
import torch
from torchcodec.decoders import AudioDecoder, AudioStreamMetadata
from pyannote.core import Segment
from torch import Tensor
from torchcodec.decoders import AudioDecoder, AudioStreamMetadata
from pyannote.audio.core.io import Audio
@@ -21,7 +20,7 @@ def test_audio_resample():
def test_basic_load_with_defaults():
test_file = "tests/data/dev00.wav"
loader = Audio(mono="downmix")
wav, sr = loader(test_file)
wav, _sr = loader(test_file)
assert isinstance(wav, Tensor)
@@ -62,6 +61,15 @@ def test_can_crop_waveform():
assert sr == 16000
def test_can_crop_waveform_up_to_its_end():
"Cropping a raw waveform up to its very last sample does not raise"
waveform = torch.rand(1, 16000 * 2)
loader = Audio(mono="downmix")
segment = Segment(1.5, 2.0)
wav, _sr = loader.crop({"waveform": waveform, "sample_rate": 16000}, segment)
assert torch.equal(wav, waveform[:, 24000:])
# File Like Object Tests
def test_can_load_from_file_like():
"Load entire wav of file like"
+4 -4
View File
@@ -33,12 +33,12 @@ def test_seeding_ensures_data_loaders():
"Setting a global seed for the dataloaders ensures that we get data back in the same order"
seed_everything(1)
protocol, vad = setup_tasks(VoiceActivityDetection)
_protocol, vad = setup_tasks(VoiceActivityDetection)
dl = create_dl(SimpleSegmentationModel, vad)
last5a = get_next5(dl)
seed_everything(1)
protocol, vad = setup_tasks(VoiceActivityDetection)
__protocol, vad = setup_tasks(VoiceActivityDetection)
dl = create_dl(SimpleSegmentationModel, vad)
last5b = get_next5(dl)
@@ -49,12 +49,12 @@ def test_seeding_ensures_data_loaders():
def test_different_seeds():
"Changing the global seed will change the order of the data that loads"
protocol, vad = setup_tasks(VoiceActivityDetection)
_protocol, vad = setup_tasks(VoiceActivityDetection)
seed_everything(4)
dl = create_dl(SimpleSegmentationModel, vad)
last5a = get_next5(dl)
protocol, vad = setup_tasks(VoiceActivityDetection)
__protocol, vad = setup_tasks(VoiceActivityDetection)
seed_everything(5)
dl = create_dl(SimpleSegmentationModel, vad)
last5b = get_next5(dl)
+2 -2
View File
@@ -1,8 +1,8 @@
import pytest
from pyannote.database import FileFinder, registry
from pyannote.audio.core.model import Model
from pyannote.audio.core.task import UnknownSpecificationsError
from pyannote.audio.models.segmentation.debug import SimpleSegmentationModel
from pyannote.audio.tasks import SpeakerDiarization
@@ -21,7 +21,7 @@ def test_unknown_specifications_error_raised_on_non_setup_task(protocol):
def test_unknown_specifications_error_raised_on_non_setup_model_task(protocol):
task = SpeakerDiarization(protocol=protocol)
model = Model.from_pretrained("pyannote/ci-segmentation")
model = SimpleSegmentationModel()
model.task = task
with pytest.raises(UnknownSpecificationsError):
_ = model.specifications
+39
View File
@@ -0,0 +1,39 @@
# Copyright (c) 2025- pyannoteAI
import multiprocessing
import sys
import warnings
import pytest
from pyannote.database import FileFinder, registry
from pyannote.audio.tasks import VoiceActivityDetection
@pytest.mark.parametrize("start_method", ["fork", "spawn", "forkserver", None])
@pytest.mark.parametrize("requested_workers", [0, 2, None])
def test_worker_start_method(monkeypatch, start_method, requested_workers):
monkeypatch.setattr(sys, "platform", "linux")
monkeypatch.setattr(
multiprocessing, "get_start_method", lambda allow_none: start_method
)
monkeypatch.setattr(
multiprocessing,
"get_all_start_methods",
lambda: ["forkserver", "fork", "spawn"],
)
monkeypatch.setattr(multiprocessing, "cpu_count", lambda: 4)
protocol = registry.get_protocol(
"Debug.SpeakerDiarization.Debug", preprocessors={"audio": FileFinder()}
)
fallback = requested_workers != 0 and start_method != "fork"
with warnings.catch_warnings(record=True) as caught:
task = VoiceActivityDetection(protocol, num_workers=requested_workers)
if fallback:
assert task.num_workers == 0
assert any("cannot be pickled" in str(w.message) for w in caught)
else:
assert task.num_workers == (
2 if requested_workers is None else requested_workers
)
assert not any("cannot be pickled" in str(w.message) for w in caught)
+18
View File
@@ -0,0 +1,18 @@
# Copyright (c) 2025- pyannoteAI
import subprocess
import sys
import pytest
def test_cli_help():
pytest.importorskip("typer")
result = subprocess.run(
[sys.executable, "-m", "pyannote.audio", "--help"],
check=True,
capture_output=True,
text=True,
)
assert "Usage:" in result.stdout
assert "benchmark" in result.stdout
+8 -5
View File
@@ -122,11 +122,14 @@ def test_batch_der(target, prediction):
def test_batch_der_with_components(target, prediction):
der, (
false_alarm,
missed_detection,
speaker_confusion,
speech_total,
(
der,
(
false_alarm,
missed_detection,
speaker_confusion,
speech_total,
),
) = diarization_error_rate(
prediction, target, reduce="batch", return_components=True
)
+42 -10
View File
@@ -22,12 +22,12 @@
"""Tests for subfolder support in Pipeline.from_pretrained"""
import pytest
import yaml
from pathlib import Path
from pyannote.audio.core.pipeline import Pipeline, expand_subfolders
import pytest
import yaml
from pyannote.audio.core.pipeline import Pipeline, expand_subfolders
# ---------------------------------------------------------------------------
# Helpers
@@ -99,9 +99,21 @@ class TestExpandSubfolders:
"revision": None,
"subfolder": "embeddings",
"token": "tok",
"cache_dir": None,
}
@pytest.mark.parametrize("as_list", [False, True])
@pytest.mark.parametrize("token", [None, False, "tok"])
@pytest.mark.parametrize("cache_dir", [None, Path("cache")])
def test_optional_runtime_options(self, as_list, token, cache_dir):
config = ["$model/weights"] if as_list else {"model": "$model/weights"}
expand_subfolders(config, model_id="org/repo", token=token, cache_dir=cache_dir)
model = config[0] if as_list else config["model"]
for key, value in {"token": token, "cache_dir": cache_dir}.items():
if value is None:
assert key not in model
else:
assert model[key] == value
def test_dict_model_reference_with_parent_subfolder(self):
"""Child $model refs are resolved under parent_subfolder."""
config = {"segmentation": "$model/seg"}
@@ -195,7 +207,7 @@ class TestFromPretrainedLocalSubfolder:
def test_subfolder_does_not_exist_raises(self, tmp_path):
"""Requesting a non-existent subfolder should raise (file not found)."""
write_config(tmp_path, self._minimal_config())
with pytest.raises(Exception):
with pytest.raises(FileNotFoundError):
Pipeline.from_pretrained(str(tmp_path), subfolder="missing")
def test_model_refs_resolved_under_subfolder(self, tmp_path):
@@ -259,12 +271,22 @@ class TestNestedSubfolderPipelines:
def _write_three_level_tree(self, root: Path) -> None:
write_config(
root,
{"pipeline": {"name": _DUMMY_PARENT_CLS_DOTPATH, "params": {"sub": "$model/child"}}},
{
"pipeline": {
"name": _DUMMY_PARENT_CLS_DOTPATH,
"params": {"sub": "$model/child"},
}
},
subfolder="v1",
)
write_config(
root,
{"pipeline": {"name": _DUMMY_PARENT_CLS_DOTPATH, "params": {"sub": "$model/grandchild"}}},
{
"pipeline": {
"name": _DUMMY_PARENT_CLS_DOTPATH,
"params": {"sub": "$model/grandchild"},
}
},
subfolder="v1/child",
)
write_config(
@@ -307,9 +329,16 @@ class TestNestedSubfolderPipelines:
"""Without subfolder=, the root config.yaml is used (one-level tree)."""
write_config(
tmp_path,
{"pipeline": {"name": _DUMMY_PARENT_CLS_DOTPATH, "params": {"sub": "$model/child"}}},
{
"pipeline": {
"name": _DUMMY_PARENT_CLS_DOTPATH,
"params": {"sub": "$model/child"},
}
},
)
write_config(
tmp_path, {"pipeline": {"name": _DUMMY_CLS_DOTPATH}}, subfolder="child"
)
write_config(tmp_path, {"pipeline": {"name": _DUMMY_CLS_DOTPATH}}, subfolder="child")
parent = Pipeline.from_pretrained(str(tmp_path))
assert isinstance(parent, _DummyParentPipeline)
assert isinstance(parent.sub, _DummyPipeline)
@@ -365,7 +394,10 @@ class TestFromPretrainedNonSubfolderCases:
write_config(tmp_path, child_config, subfolder="seg")
config = {
"pipeline": {"name": _DUMMY_PARENT_CLS_DOTPATH, "params": {"sub": "$model/seg"}},
"pipeline": {
"name": _DUMMY_PARENT_CLS_DOTPATH,
"params": {"sub": "$model/seg"},
},
}
pipeline = Pipeline.from_pretrained(config)
assert isinstance(pipeline, _DummyParentPipeline)
+10 -12
View File
@@ -1,16 +1,14 @@
from glob import glob
"""Opt-in checks for historical notebooks: pytest -m notebook."""
from pathlib import Path
import papermill as pm
import pytest
def test_can_run_notebooks():
# Search for all notebooks in directory
notebooks = glob("**/notebook/**/*.ipynb")
for nb in notebooks:
try:
pm.execute_notebook(
nb, "/dev/null", progress_bar=False, kernel_name="python"
)
except Exception as e:
# Which notebook caused the error
raise Exception(nb, e)
@pytest.mark.notebook
@pytest.mark.parametrize("notebook", sorted(Path("notebook").glob("*.ipynb")))
def test_can_run_notebook(notebook, tmp_path):
pm.execute_notebook(
notebook, tmp_path / notebook.name, progress_bar=False, kernel_name="python"
)
+21
View File
@@ -0,0 +1,21 @@
# Copyright (c) 2025- pyannoteAI
import pytest
import torch
def test_separation_masker():
pytest.importorskip("asteroid")
pytest.importorskip("transformers")
from pyannote.audio.models.separation.ToTaToNet import ToTaToNet
model = ToTaToNet(
use_wavlm=False,
n_sources=2,
dprnn={"n_repeats": 1, "bn_chan": 8, "hid_size": 8, "chunk_size": 10},
).eval()
with torch.inference_mode():
encoded = model.encoder(torch.randn(1, 1, 1600))
masks = model.masker(encoded)
assert masks.shape == (1, 2, *encoded.shape[1:])
assert torch.isfinite(masks).all()
+9 -1
View File
@@ -26,6 +26,8 @@ from tempfile import mkstemp
import pytest
from lightning import Trainer
from pyannote.database import FileFinder, registry
from pyannote.audio.models.embedding.debug import SimpleEmbeddingModel
from pyannote.audio.models.segmentation.debug import SimpleSegmentationModel
from pyannote.audio.tasks import (
@@ -34,7 +36,6 @@ from pyannote.audio.tasks import (
SupervisedRepresentationLearningWithArcFace,
VoiceActivityDetection,
)
from pyannote.database import FileFinder, registry
@pytest.fixture()
@@ -76,6 +77,13 @@ def test_train_segmentation(protocol):
trainer.fit(model)
def test_train_segmentation_with_balance(protocol):
segmentation = SpeakerDiarization(protocol, balance=["database"])
model = SimpleSegmentationModel(task=segmentation)
trainer = Trainer(fast_dev_run=True, accelerator="cpu")
trainer.fit(model)
def test_train_segmentation_with_cached_data_mono_device(protocol, cache):
first_task = SpeakerDiarization(protocol, cache=cache)
first_model = SimpleSegmentationModel(task=first_task)
Loaded 100 of 105 files, more files were not shown because too many files have changed in this diff. Show more