Files
pyannote-audio/tests/inference_test.py
T
ea18bc70d8 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>
2026-10-22 15:07:01 +02:00

101 lines
3.0 KiB
Python

import numpy as np
import pytest
from lightning import Trainer
from pyannote.core import SlidingWindowFeature
from pyannote.database import FileFinder, registry
from pyannote.audio import Inference, Model
from pyannote.audio.core.task import Resolution
from pyannote.audio.models.segmentation.debug import SimpleSegmentationModel
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)
@pytest.fixture()
def trained():
protocol = registry.get_protocol(
"Debug.SpeakerDiarization.Debug", preprocessors={"audio": FileFinder()}
)
vad = VoiceActivityDetection(protocol, duration=2.0, batch_size=16, num_workers=4)
model = SimpleSegmentationModel(task=vad)
trainer = Trainer(fast_dev_run=True, accelerator="cpu")
trainer.fit(model)
return protocol, model
@pytest.fixture()
def pretrained_model():
return Model.from_pretrained(HF_SAMPLE_MODEL_ID)
@pytest.fixture()
def dev_file():
protocol = registry.get_protocol(
"Debug.SpeakerDiarization.Debug", preprocessors={"audio": FileFinder()}
)
return next(protocol.development())
def test_duration_warning(trained):
_protocol, model = trained
with pytest.warns(UserWarning):
duration = model.specifications.duration
new_duration = duration + 1
Inference(model, duration=new_duration, step=0.1, batch_size=128)
def test_step_check_warning(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
with pytest.raises(ValueError):
Inference(model, window="unknown")
def test_invalid_resolution_fails(trained):
_protocol, model = trained
with pytest.warns(UserWarning):
model.specifications.resolution = Resolution.FRAME
Inference(model, window="whole", batch_size=128)
def test_whole_window_slide(trained):
protocol, model = trained
inference = Inference(model, window="whole", batch_size=128)
dev_file = next(protocol.development())
output = inference(dev_file)
assert isinstance(output, np.ndarray)
def test_on_file_path(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)
assert len(scores.data.shape) == 3