feat: add "skip_aggregation" option to Inference (#612)

This commit is contained in:
Hervé BREDIN authored and GitHub committed 2021-02-11 13:03:52 +01:00
1 parent 44ce416310
commit b17a6db36f
2 files changed
+39 -9

No files matched your search

+9 -3
View File
@@ -53,6 +53,8 @@ class Inference:
window : {"sliding", "whole"}, optional
Use a "sliding" window and aggregate the corresponding outputs (default)
or just one (potentially long) window covering the "whole" file or chunk.
skip_aggregation : bool, optional
Do not aggregate outputs when using "sliding" window. Defaults to False.
duration : float, optional
Chunk duration, in seconds. Defaults to duration used for training the model.
Has no effect when `window` is "whole".
@@ -76,10 +78,13 @@ class Inference:
token that can be obtained by running `huggingface-cli login`
"""
# TODO: add option to automatically find maximum batch size
def __init__(
self,
model: Union[Model, Text, Path],
window: Text = "sliding",
skip_aggregation: bool = False,
device: torch.device = None,
duration: float = None,
step: float = None,
@@ -110,6 +115,7 @@ class Inference:
)
self.window = window
self.skip_aggregation = skip_aggregation
if device is None:
device = self.model.device
@@ -303,9 +309,9 @@ class Inference:
}
for task_name, specifications in self.model.specifications.items():
# if model outputs just one vector per chunk, return the outputs as they are
# (i.e. do not aggregate them)
if specifications.resolution == Resolution.CHUNK:
# skip aggregation when requested
# or when model outputs just one vector per chunk
if self.skip_aggregation or specifications.resolution == Resolution.CHUNK:
frames = SlidingWindow(
start=0.0, duration=self.duration, step=self.step
)
+30 -6
View File
@@ -2,7 +2,7 @@ import numpy as np
import pytest
import pytorch_lightning as pl
from pyannote.audio import Inference
from pyannote.audio import Inference, Model
from pyannote.audio.core.task import Resolution
from pyannote.audio.models.segmentation.debug import (
MultiTaskSegmentationModel,
@@ -15,8 +15,17 @@ from pyannote.audio.tasks.segmentation.multi_task_segmentation import (
from pyannote.core import SlidingWindowFeature
from pyannote.database import FileFinder, get_protocol
# TODO: upload a very light model just for unit tests
# HF_SAMPLE_MODEL_ID = "julien-c/voice-activity-detection"
HF_SAMPLE_MODEL_ID = "pyannote/TestModelForContinuousIntegration"
def test_hf_download_inference():
inference = Inference(HF_SAMPLE_MODEL_ID, device="cpu")
assert isinstance(inference, Inference)
def test_hf_download_model():
model = Model.from_pretrained(HF_SAMPLE_MODEL_ID)
assert isinstance(model, Model)
@pytest.fixture()
@@ -31,6 +40,19 @@ def trained():
return protocol, model
@pytest.fixture()
def pretrained_model():
return Model.from_pretrained(HF_SAMPLE_MODEL_ID)
@pytest.fixture()
def dev_file():
protocol = get_protocol(
"Debug.SpeakerDiarization.Debug", preprocessors={"audio": FileFinder()}
)
return next(protocol.development())
def test_duration_warning(trained):
protocol, model = trained
with pytest.warns(UserWarning):
@@ -101,6 +123,8 @@ def test_multi_seg_infer():
assert isinstance(scores[attr], SlidingWindowFeature)
# def test_hf_download():
# inference = Inference(HF_SAMPLE_MODEL_ID, device="cpu")
# assert isinstance(inference, Inference)
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