mirror of
https://github.com/pyannote/pyannote-audio.git
synced 2026-11-20 13:07:51 +09:00
feat: add progress_hook option to inference
This commit is contained in:
1 parent
a08cbe0d26
commit
2827a9c129
3 files changed
+88
-1
No files matched your search
@@ -113,6 +113,19 @@
|
||||
"scores = inference.crop(dev_file, Segment(10, 15))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# inference with progress bar\n",
|
||||
"inference = Inference(model, step=0.1, batch_size=32, progress_hook='Processing...')\n",
|
||||
"scores = inference(dev_file)\n",
|
||||
"inference = Inference(model, step=0.1, batch_size=1, progress_hook=True)\n",
|
||||
"scores = inference.crop(dev_file, Segment(10, 15))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
|
||||
@@ -25,7 +25,7 @@ import warnings
|
||||
from collections import Counter, deque
|
||||
from functools import cached_property
|
||||
from pathlib import Path
|
||||
from typing import Deque, Dict, List, Optional, Text, Tuple, Union
|
||||
from typing import Any, Callable, Deque, Dict, List, Optional, Text, Tuple, Union
|
||||
|
||||
import numpy as np
|
||||
import torch
|
||||
@@ -36,6 +36,7 @@ from pyannote.audio.core.io import AudioFile
|
||||
from pyannote.audio.core.model import Model, ModelIntrospection, load_from_checkpoint
|
||||
from pyannote.audio.core.task import Scale, TaskSpecification
|
||||
from pyannote.audio.utils.permutation import permutate
|
||||
from pyannote.audio.utils.progress import InferenceProgressHook
|
||||
from pyannote.core import Segment, SlidingWindow, SlidingWindowFeature
|
||||
|
||||
TaskName = Union[Text, None]
|
||||
@@ -64,6 +65,12 @@ class Inference:
|
||||
device : torch.device, optional
|
||||
Device used for inference. Defaults to `model.device`.
|
||||
In case `device` and `model.device` are different, model is sent to device.
|
||||
progress_hook : {callable, True, str}, optional
|
||||
When a callable is provided, it is called everytime a batch is processed
|
||||
with two integer arguments:
|
||||
- the number of chunks that have been processed so far
|
||||
- the total number of chunks
|
||||
Set to True (or a descriptive string) to display a tqdm progress bar.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
@@ -74,6 +81,7 @@ class Inference:
|
||||
duration: float = None,
|
||||
step: float = None,
|
||||
batch_size: int = 32,
|
||||
progress_hook: Union[bool, Text, Callable[[int, int], Any]] = False,
|
||||
):
|
||||
|
||||
self.model = (
|
||||
@@ -128,6 +136,16 @@ class Inference:
|
||||
|
||||
self.batch_size = batch_size
|
||||
|
||||
if callable(progress_hook):
|
||||
pass
|
||||
elif isinstance(progress_hook, Text):
|
||||
progress_hook = InferenceProgressHook(desc=progress_hook)
|
||||
elif progress_hook:
|
||||
progress_hook = InferenceProgressHook()
|
||||
else:
|
||||
progress_hook = None
|
||||
self.progress_hook = progress_hook
|
||||
|
||||
@cached_property
|
||||
def is_multi_task(self) -> bool:
|
||||
return self.model.is_multi_task
|
||||
@@ -263,14 +281,21 @@ class Inference:
|
||||
task_name: list() for task_name, _ in self.task_specifications
|
||||
}
|
||||
|
||||
if self.progress_hook is not None:
|
||||
self.progress_hook(0, num_chunks + has_last_chunk)
|
||||
|
||||
# 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]
|
||||
|
||||
output = self.infer(batch)
|
||||
for task_name, task_output in output.items():
|
||||
outputs[task_name].append(task_output)
|
||||
|
||||
if self.progress_hook is not None:
|
||||
self.progress_hook(c + 1, num_chunks + has_last_chunk)
|
||||
|
||||
outputs = {
|
||||
task_name: np.vstack(task_outputs)
|
||||
for task_name, task_outputs in outputs.items()
|
||||
@@ -292,6 +317,10 @@ class Inference:
|
||||
task_name: output[0]
|
||||
for task_name, output in self.infer(last_chunk[None]).items()
|
||||
}
|
||||
if self.progress_hook is not None:
|
||||
self.progress_hook(
|
||||
num_chunks + has_last_chunk, num_chunks + has_last_chunk
|
||||
)
|
||||
|
||||
# use model introspection to estimate the total number of frames
|
||||
_, model_introspection = self.model_introspection[t]
|
||||
@@ -448,6 +477,7 @@ class Inference:
|
||||
}
|
||||
if self.is_multi_task:
|
||||
return outputs
|
||||
|
||||
return outputs[None]
|
||||
|
||||
def crop(
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
# MIT License
|
||||
#
|
||||
# Copyright (c) 2021 CNRS
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in all
|
||||
# copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
|
||||
|
||||
from typing import Text
|
||||
|
||||
from tqdm import tqdm
|
||||
|
||||
|
||||
class InferenceProgressHook:
|
||||
"""Default inference progress bar"""
|
||||
|
||||
def __init__(self, desc: Text = None):
|
||||
self.desc = desc
|
||||
|
||||
def __call__(self, chunk_idx, num_chunks):
|
||||
|
||||
if chunk_idx == 0:
|
||||
self.pbar = tqdm(desc=self.desc, total=num_chunks, unit="chunks")
|
||||
self.chunk_idx = chunk_idx
|
||||
|
||||
self.pbar.update(chunk_idx - self.chunk_idx)
|
||||
self.chunk_idx = chunk_idx
|
||||
if self.chunk_idx == num_chunks:
|
||||
self.pbar.close()
|
||||
Reference in new issue
Block a user