mirror of
https://github.com/pyannote/pyannote-audio.git
synced 2026-12-04 17:16:03 +09:00
chore: switch to pyannote.core 2.0
This commit is contained in:
1 parent
378c774d55
commit
e43df4e2ce
13 files changed
+23
-504
No files matched your search
@@ -38,7 +38,7 @@ from pyannote.audio.util import mkdir_p
|
||||
from sortedcontainers import SortedDict
|
||||
import tensorboardX
|
||||
from functools import partial
|
||||
from pyannote.audio.util import get_class_by_name
|
||||
from pyannote.core.utils.import import get_class_by_name
|
||||
import warnings
|
||||
|
||||
|
||||
|
||||
@@ -81,7 +81,7 @@ from pyannote.database import FileFinder
|
||||
from pyannote.database import get_unique_identifier
|
||||
from pyannote.database import get_protocol
|
||||
|
||||
from pyannote.audio.util import get_class_by_name
|
||||
from pyannote.core.utils.import import get_class_by_name
|
||||
|
||||
from pyannote.audio.features import Precomputed
|
||||
from pyannote.audio.features.utils import get_audio_duration
|
||||
|
||||
@@ -87,7 +87,7 @@ Configuration file:
|
||||
|
||||
import os
|
||||
import yaml
|
||||
from pyannote.audio.util import get_class_by_name
|
||||
from pyannote.core.utils.import import get_class_by_name
|
||||
import numpy as np
|
||||
from tqdm import tqdm
|
||||
from pathlib import Path
|
||||
|
||||
@@ -195,10 +195,10 @@ from scipy.cluster.hierarchy import fcluster
|
||||
from scipy.cluster.hierarchy import linkage
|
||||
from scipy.optimize import minimize_scalar
|
||||
|
||||
from pyannote.audio.util import get_class_by_name
|
||||
from pyannote.core.utils.import import get_class_by_name
|
||||
|
||||
from pyannote.audio.embedding.utils import pdist
|
||||
from pyannote.audio.embedding.utils import cdist
|
||||
from pyannote.core.utils.distance import pdist
|
||||
from pyannote.core.utils.distance import cdist
|
||||
from pyannote.audio.features.precomputed import Precomputed
|
||||
|
||||
from pyannote.metrics.binary_classification import det_curve
|
||||
|
||||
@@ -177,7 +177,7 @@ from pyannote.database import get_unique_identifier
|
||||
from pyannote.audio.features import Precomputed
|
||||
from pyannote.metrics.detection import DetectionErrorRate
|
||||
from pyannote.audio.labeling.extraction import SequenceLabeling
|
||||
from pyannote.audio.util import get_class_by_name
|
||||
from pyannote.core.utils.import import get_class_by_name
|
||||
from functools import partial
|
||||
import multiprocessing as mp
|
||||
|
||||
|
||||
@@ -33,7 +33,8 @@ from pyannote.audio.generators.speaker import SpeechSegmentGenerator
|
||||
from pyannote.audio.generators.speaker import SessionWiseSpeechSegmentGenerator
|
||||
from pyannote.audio.generators.speaker import UnsupervisedSpeechSegmentGenerator
|
||||
|
||||
from pyannote.audio.embedding.utils import to_condensed, pdist
|
||||
from pyannote.core.utils.distance import to_condensed
|
||||
from pyannote.core.utils.distance import pdist
|
||||
from scipy.spatial.distance import squareform
|
||||
from pyannote.metrics.binary_classification import det_curve
|
||||
from collections import deque
|
||||
|
||||
@@ -32,7 +32,10 @@ import numpy as np
|
||||
import networkx as nx
|
||||
from sortedcontainers import SortedSet
|
||||
from sortedcollections import ValueSortedDict
|
||||
from pyannote.audio.embedding.utils import pdist, cdist, l2_normalize
|
||||
|
||||
from pyannote.core.utils.distance import pdist
|
||||
from pyannote.core.utils.distance import cdist
|
||||
from pyannote.core.utils.distance import l2_normalize
|
||||
from scipy.spatial.distance import squareform
|
||||
|
||||
|
||||
|
||||
@@ -1,299 +0,0 @@
|
||||
#!/usr/bin/env python
|
||||
# encoding: utf-8
|
||||
|
||||
# The MIT License (MIT)
|
||||
|
||||
# Copyright (c) 2016-2018 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.
|
||||
|
||||
# AUTHORS
|
||||
# Hervé BREDIN - http://herve.niderb.fr
|
||||
|
||||
import numpy as np
|
||||
import scipy.spatial.distance
|
||||
import scipy.cluster.hierarchy
|
||||
|
||||
|
||||
def l2_normalize(fX):
|
||||
|
||||
norm = np.sqrt(np.sum(fX ** 2, axis=1))
|
||||
norm[norm == 0] = 1.
|
||||
return (fX.T / norm).T
|
||||
|
||||
|
||||
def get_range(metric='euclidean'):
|
||||
return {
|
||||
'angular': (0, np.pi),
|
||||
'euclidean': (0, 2),
|
||||
'sqeuclidean': (0, 4),
|
||||
'cosine': (-1.0, 1.0)
|
||||
}.get(metric, None)
|
||||
|
||||
|
||||
def _pdist_func_1D(X, func):
|
||||
"""Helper function for pdist"""
|
||||
|
||||
X = X.squeeze()
|
||||
n_items, = X.shape
|
||||
|
||||
distances = []
|
||||
|
||||
for i in range(n_items - 1):
|
||||
distance = func(X[i], X[i+1:])
|
||||
distances.append(distance)
|
||||
|
||||
return np.hstack(distances)
|
||||
|
||||
|
||||
def pdist(fX, metric='euclidean', **kwargs):
|
||||
"""Same as scipy.spatial.distance with support for additional metrics
|
||||
|
||||
* 'angular': pairwise angular distance
|
||||
* 'equal': pairwise equality check (only for 1-dimensional fX)
|
||||
* 'minimum': pairwise minimum (only for 1-dimensional fX)
|
||||
* 'maximum': pairwise maximum (only for 1-dimensional fX)
|
||||
* 'average': pairwise average (only for 1-dimensional fX)
|
||||
"""
|
||||
|
||||
if metric == 'angular':
|
||||
cosine = scipy.spatial.distance.pdist(
|
||||
fX, metric='cosine', **kwargs)
|
||||
return np.arccos(np.clip(1.0 - cosine, -1.0, 1.0))
|
||||
|
||||
elif metric == 'equal':
|
||||
return _pdist_func_1D(fX, lambda x, X: x == X)
|
||||
|
||||
elif metric == 'minimum':
|
||||
return _pdist_func_1D(fX, np.minimum)
|
||||
|
||||
elif metric == 'maximum':
|
||||
return _pdist_func_1D(fX, np.maximum)
|
||||
|
||||
elif metric == 'average':
|
||||
return _pdist_func_1D(fX, lambda x, X: .5 * (x + X))
|
||||
|
||||
else:
|
||||
return scipy.spatial.distance.pdist(fX, metric=metric, **kwargs)
|
||||
|
||||
|
||||
def _cdist_func_1D(X_trn, X_tst, func):
|
||||
"""Helper function for cdist"""
|
||||
X_trn = X_trn.squeeze()
|
||||
X_tst = X_tst.squeeze()
|
||||
return np.vstack(func(x_trn, X_tst) for x_trn in iter(X_trn))
|
||||
|
||||
|
||||
def cdist(fX_trn, fX_tst, metric='euclidean', **kwargs):
|
||||
"""Same as scipy.spatial.distance.cdist with support for additional metrics
|
||||
|
||||
* 'angular': pairwise angular distance
|
||||
* 'equal': pairwise equality check (only for 1-dimensional fX)
|
||||
* 'minimum': pairwise minimum (only for 1-dimensional fX)
|
||||
* 'maximum': pairwise maximum (only for 1-dimensional fX)
|
||||
* 'average': pairwise average (only for 1-dimensional fX)
|
||||
"""
|
||||
|
||||
if metric == 'angular':
|
||||
cosine = scipy.spatial.distance.cdist(
|
||||
fX_trn, fX_tst, metric='cosine', **kwargs)
|
||||
return np.arccos(np.clip(1.0 - cosine, -1.0, 1.0))
|
||||
|
||||
elif metric == 'equal':
|
||||
return _cdist_func_1D(fX_trn, fX_tst,
|
||||
lambda x_trn, X_tst: x_trn == X_tst)
|
||||
|
||||
elif metric == 'minimum':
|
||||
return _cdist_func_1D(fX_trn, fX_tst, np.minimum)
|
||||
|
||||
elif metric == 'maximum':
|
||||
return _cdist_func_1D(fX_trn, fX_tst, np.maximum)
|
||||
|
||||
elif metric == 'average':
|
||||
return _cdist_func_1D(fX_trn, fX_tst,
|
||||
lambda x_trn, X_tst: .5 * (x_trn + X_tst))
|
||||
|
||||
else:
|
||||
return scipy.spatial.distance.cdist(
|
||||
fX_trn, fX_tst, metric=metric, **kwargs)
|
||||
|
||||
|
||||
def to_condensed(n, i, j):
|
||||
"""Compute index in condensed pdist matrix
|
||||
|
||||
V
|
||||
0 | . 0 1 2 3
|
||||
-> 1 | . . 4 5 6 <- ==> 0 1 2 3 4 5 6 7 8 9
|
||||
2 | . . . 7 8 ^
|
||||
3 | . . . . 9
|
||||
4 | . . . . .
|
||||
----------
|
||||
0 1 2 3 4
|
||||
|
||||
Parameters
|
||||
----------
|
||||
n : int
|
||||
Number of inputs in squared pdist matrix
|
||||
i, j : `int` or `numpy.ndarray`
|
||||
Indices in squared pdist matrix
|
||||
|
||||
Returns
|
||||
-------
|
||||
k : `int` or `numpy.ndarray`
|
||||
Index in condensed pdist matrix
|
||||
"""
|
||||
i, j = np.array(i), np.array(j)
|
||||
if np.any(i == j):
|
||||
raise ValueError('i and j should be different.')
|
||||
i, j = np.minimum(i, j), np.maximum(i, j)
|
||||
return np.int64(i * n - i * i / 2 - 3 * i / 2 + j - 1)
|
||||
|
||||
|
||||
def to_squared(n, k):
|
||||
"""Compute indices in squared matrix
|
||||
|
||||
Parameters
|
||||
----------
|
||||
n : int
|
||||
Number of inputs in squared pdist matrix
|
||||
k : `int` or `numpy.ndarray`
|
||||
Index in condensed pdist matrix
|
||||
|
||||
Returns
|
||||
-------
|
||||
i, j : `int` or `numpy.ndarray`
|
||||
Indices in squared pdist matrix
|
||||
|
||||
"""
|
||||
k = np.array(k)
|
||||
i = np.int64(n - np.sqrt(-8*k + 4*n**2 - 4*n + 1)/2 - 1/2)
|
||||
j = np.int64(i**2/2 - i*n + 3*i/2 + k + 1)
|
||||
return i, j
|
||||
|
||||
|
||||
# for n in range(2, 10):
|
||||
# for k in range(int(n*(n-1)/2)):
|
||||
# assert to_condensed(n, *to_squared(n, k)) == k
|
||||
|
||||
def linkage(X, method='single', metric='euclidean'):
|
||||
"""Same as scipy.cluster.hierarchy.linkage with more metrics and methods
|
||||
"""
|
||||
|
||||
if method == 'pool':
|
||||
return pool(X, metric=metric, pooling_func=None)
|
||||
|
||||
# corner case when using non-euclidean distances with methods
|
||||
# designed for the euclidean distance
|
||||
if metric != 'euclidean' and method in ['centroid', 'median', 'ward']:
|
||||
# Those 3 methods only work with 'euclidean' distance.
|
||||
# Therefore, one has to unit-normalized embeddings before
|
||||
# comparison in case they were optimized for 'cosine' (or 'angular')
|
||||
# distance.
|
||||
X = l2_normalize(X)
|
||||
metric = 'euclidean'
|
||||
|
||||
distance = pdist(X, metric=metric)
|
||||
return scipy.cluster.hierarchy.linkage(distance, method=method,
|
||||
metric=metric)
|
||||
|
||||
def pool(X, metric='euclidean', pooling_func=None):
|
||||
"""'pool' linkage"""
|
||||
|
||||
if pooling_func is None:
|
||||
def pooling_func(C_u, C_v, X_u, X_v):
|
||||
S_u = len(X_u)
|
||||
S_v = len(X_v)
|
||||
return (C_u * S_u + C_v * S_v) / (S_u + S_v)
|
||||
|
||||
# obtain number of original observations
|
||||
n, dimension = X.shape
|
||||
|
||||
# K[j] contains the index of the cluster to which
|
||||
# the jth observation is currently assigned
|
||||
K = np.arange(n)
|
||||
|
||||
# S[k] contains the current size of kth cluster
|
||||
S = np.zeros(2 * n - 1, dtype=np.int16)
|
||||
S[:n] = 1
|
||||
|
||||
# C[k] contains the centroid of kth cluster
|
||||
C = np.zeros((2 * n - 1, dimension))
|
||||
# at the beginning, each observation is assigned to its own cluster
|
||||
C[:n, :] = X
|
||||
|
||||
# clustering tree (aka dendrogram)
|
||||
# Z[i, 0] and Z[i, 1] are merged at ith iteration
|
||||
# Z[i, 2] is the distance between Z[i, 0] and Z[i, 1]
|
||||
# Z[i, 3] is the total number of original observation in the newly formed cluster
|
||||
Z = np.zeros((n - 1, 4))
|
||||
|
||||
# convert condensed pdist matrix for the `n` original observation to a
|
||||
# condensed pdist matrix for the `2n-1` clusters (including the `n`
|
||||
# original observations) that will exist at some point during the process.
|
||||
D = np.infty * np.ones((2 * n - 1) * (2 * n - 2) // 2)
|
||||
D[to_condensed(2 * n - 1, *to_squared(n, np.arange(n * (n - 1) // 2)))] = \
|
||||
pdist(X, metric=metric)
|
||||
|
||||
for i in range(n - 1):
|
||||
|
||||
# find two most similar clusters
|
||||
k = np.argmin(D)
|
||||
u, v = to_squared(2 * n - 1, k)
|
||||
|
||||
# keep track of ...
|
||||
# ... which clusters are merged at this iteration
|
||||
Z[i, 0] = v if S[v] > S[u] else v
|
||||
Z[i, 1] = u if Z[i, 0] == v else v
|
||||
|
||||
# ... their distance
|
||||
Z[i, 2] = D[k]
|
||||
|
||||
# ... the size of the newly formed cluster
|
||||
Z[i, 3] = S[u] + S[v]
|
||||
S[n + i] = S[u] + S[v]
|
||||
|
||||
# merged clusters are now empty...
|
||||
S[u] = 0
|
||||
S[v] = 0
|
||||
|
||||
# compute centroid of newly formed cluster
|
||||
C[n + i] = pooling_func(C[u], C[v], X[K == u], X[K == v])
|
||||
|
||||
# move observations of merged clusters into the newly formed cluster
|
||||
K[K == u] = n + i
|
||||
K[K == v] = n + i
|
||||
|
||||
# distance to merged clusters u and v can no longer be computed
|
||||
D[to_condensed(2 * n - 1, u, np.arange(u))] = np.infty
|
||||
D[to_condensed(2 * n - 1, u, np.arange(u + 1, n + i + 1))] = np.infty
|
||||
D[to_condensed(2 * n - 1, v, np.arange(v))] = np.infty
|
||||
D[to_condensed(2 * n - 1, v, np.arange(v + 1, n + i + 1))] = np.infty
|
||||
|
||||
# compute distance to newly formed cluster
|
||||
empty = S[:n + i] == 0
|
||||
k = to_condensed(2 * n - 1, n + i, np.arange(n + i)[~empty])
|
||||
D[k] = cdist(C[np.newaxis, n + i, :],
|
||||
C[:n + i, :][~empty, :],
|
||||
metric=metric)
|
||||
|
||||
# is this really needed?
|
||||
k = to_condensed(2 * n - 1, n + i, np.arange(n + i)[empty])
|
||||
D[k] = np.infty
|
||||
|
||||
return Z
|
||||
@@ -33,7 +33,7 @@ from tqdm import tqdm
|
||||
from pyannote.metrics.binary_classification import det_curve
|
||||
from pyannote.database import get_unique_identifier
|
||||
from pyannote.database import get_annotated
|
||||
from pyannote.audio.util import one_hot_encoding
|
||||
from pyannote.core.utils.numpy import one_hot_encoding
|
||||
from pyannote.audio.features import Precomputed
|
||||
from pyannote.core import Segment
|
||||
from pyannote.core import Timeline
|
||||
@@ -150,9 +150,10 @@ class LabelingTaskGenerator(object):
|
||||
|
||||
for current_file in getattr(protocol, subset)():
|
||||
|
||||
y, _ = one_hot_encoding(
|
||||
current_file, self.feature_extraction.sliding_window,
|
||||
labels=self.labels_, mode='center')
|
||||
y, _ = one_hot_encoding(current_file['annotation'],
|
||||
get_annotated(current_file),
|
||||
self.feature_extraction.sliding_window,
|
||||
labels=self.labels_, mode='center')
|
||||
|
||||
uri = get_unique_identifier(current_file)
|
||||
|
||||
|
||||
@@ -30,7 +30,8 @@ import numpy as np
|
||||
import sklearn.cluster
|
||||
from pathlib import Path
|
||||
from scipy.spatial.distance import squareform
|
||||
from pyannote.audio.embedding.utils import l2_normalize, pdist
|
||||
from pyannote.core.utils.distance import pdist
|
||||
from pyannote.core.utils.distance import l2_normalize
|
||||
|
||||
from pyannote.audio.features import Precomputed
|
||||
from pyannote.audio.signal import Binarize, Peak
|
||||
|
||||
@@ -30,9 +30,9 @@
|
||||
import numpy as np
|
||||
import scipy.signal
|
||||
from pyannote.core import Segment, Timeline
|
||||
from pyannote.core.util import pairwise
|
||||
from pyannote.core.utils.generators import pairwise
|
||||
from sklearn.mixture import GaussianMixture
|
||||
from .util import one_hot_decoding
|
||||
from pyannote.core.utils.numpy import one_hot_decoding
|
||||
|
||||
|
||||
class Peak(object):
|
||||
|
||||
@@ -26,17 +26,8 @@
|
||||
# AUTHORS
|
||||
# Hervé BREDIN - http://herve.niderb.fr
|
||||
|
||||
import sys
|
||||
import os
|
||||
import errno
|
||||
from importlib import import_module
|
||||
import numpy as np
|
||||
from pyannote.core import Segment
|
||||
from pyannote.core import SlidingWindow
|
||||
from pyannote.core import Annotation
|
||||
from pyannote.core import SlidingWindowFeature
|
||||
from pyannote.core.util import string_generator
|
||||
from pyannote.database import get_annotated
|
||||
|
||||
|
||||
def mkdir_p(path):
|
||||
@@ -61,182 +52,3 @@ def mkdir_p(path):
|
||||
pass
|
||||
else:
|
||||
raise exc
|
||||
|
||||
|
||||
def one_hot_encoding(current_file, window, labels=None, mode='center'):
|
||||
"""Convert annotation to one-hot-encoded numpy array
|
||||
|
||||
Parameters
|
||||
----------
|
||||
current_file : `dict`
|
||||
Typically one would use a dictionary generated by `pyannote.database`
|
||||
protocols. At the very least, it must contain a `annotation` key.
|
||||
window : `SlidingWindow`
|
||||
Use this `window`.
|
||||
labels : list, optional
|
||||
Predefined list of labels. Defaults to using labels in `current_file`.
|
||||
|
||||
Returns
|
||||
-------
|
||||
y : `pyannote.core.SlidingWindowFeature`
|
||||
(N, K) array where y[t, k] > 0 when labels[k] is active at timestep t.
|
||||
y[t, k] = NAN means we have no idea.
|
||||
labels : list
|
||||
List of labels.
|
||||
|
||||
See also
|
||||
--------
|
||||
See `one_hot_decoding` to convert `y` back to a `pyannote.core.Annotation`
|
||||
instance
|
||||
"""
|
||||
|
||||
if not isinstance(window, SlidingWindow):
|
||||
if hasattr(window, 'sliding_window'):
|
||||
window = window.sliding_window
|
||||
else:
|
||||
msg = (f"`window` must be an instance of `SlidingWindow` "
|
||||
f"or have an attribute called 'sliding_window'.")
|
||||
raise TypeError(msg)
|
||||
|
||||
annotated = get_annotated(current_file)
|
||||
support = annotated.extent()
|
||||
window = SlidingWindow(start=support.start,
|
||||
step=window.step,
|
||||
duration=window.duration)
|
||||
|
||||
n_samples = window.samples(support.duration, mode=mode)
|
||||
|
||||
# defaults to `labels` contained by `annotation`
|
||||
annotation = current_file['annotation']
|
||||
labels = annotation.labels() if labels is None else labels
|
||||
indices = {label: i for i, label in enumerate(labels)}
|
||||
|
||||
# one-hot encoding
|
||||
# NAN = unknown / +1 = active / 0 = inactive
|
||||
y = np.NAN * np.ones((n_samples, len(labels)), dtype=np.int64)
|
||||
for i, j in window.crop(annotated, mode=mode, return_ranges=True):
|
||||
i = max(0, i)
|
||||
j = min(n_samples, j)
|
||||
y[i:j, :] = 0
|
||||
|
||||
for label in annotation.labels():
|
||||
try:
|
||||
k = indices[label]
|
||||
except KeyError as e:
|
||||
msg = f'List of `labels` does not contain label "{label}".'
|
||||
print(indices.keys())
|
||||
raise ValueError(msg)
|
||||
|
||||
for i, j in window.crop(annotation.label_timeline(label),
|
||||
mode=mode, return_ranges=True):
|
||||
i = max(0, i)
|
||||
j = min(n_samples, j)
|
||||
y[i:j, k] += 1
|
||||
|
||||
y = np.minimum(y, 1)
|
||||
|
||||
return SlidingWindowFeature(y, window), labels
|
||||
|
||||
|
||||
def one_hot_decoding(y, window, labels=None):
|
||||
"""Convert one-hot-encoded numpy array to annotation
|
||||
|
||||
Parameters
|
||||
----------
|
||||
y : (N, K) or (N, ) numpy.ndarray
|
||||
When y has shape (N, K), y[t, k] > 0 means kth label is active at
|
||||
timestep t. When y has shape (N, ), y[t] = 0 means no label is active
|
||||
at timestep t, y[t] = k means (k-1)th label is active.
|
||||
window : `SlidingWindow`
|
||||
Use this `window`.
|
||||
labels : list, optional
|
||||
Predefined list of labels. Defaults to labels generated by
|
||||
`pyannote.core.utils.string_generator`.
|
||||
|
||||
Returns
|
||||
-------
|
||||
annotation : pyannote.core.Annotation
|
||||
|
||||
See also
|
||||
--------
|
||||
`one_hot_encoding`
|
||||
"""
|
||||
|
||||
if not isinstance(window, SlidingWindow):
|
||||
if hasattr(window, 'sliding_window'):
|
||||
window = window.sliding_window
|
||||
else:
|
||||
msg = (f"`window` must be an instance of `SlidingWindow` "
|
||||
f"or have an attribute called 'sliding_window'.")
|
||||
raise TypeError(msg)
|
||||
|
||||
if len(y.shape) < 2:
|
||||
N, = y.shape
|
||||
if labels is not None:
|
||||
K = len(labels)
|
||||
else:
|
||||
K = np.max(y)
|
||||
|
||||
y_ = np.zeros((N, K), dtype=np.int64)
|
||||
for t, k in enumerate(y):
|
||||
if k == 0:
|
||||
continue
|
||||
y_[t, k - 1] = 1
|
||||
y = y_
|
||||
|
||||
N, K = y.shape
|
||||
|
||||
if labels is None:
|
||||
labels = string_generator()
|
||||
labels = [next(labels) for _ in range(K)]
|
||||
|
||||
annotation = Annotation()
|
||||
|
||||
y_off = np.zeros((1, K), dtype=np.int64)
|
||||
y = np.vstack((y_off, y, y_off))
|
||||
diff = np.diff(y, axis=0)
|
||||
for k, label in enumerate(labels):
|
||||
for t in np.where(diff[:, k] != 0)[0]:
|
||||
if diff[t, k] > 0:
|
||||
onset_t = window[t].middle
|
||||
else:
|
||||
segment = Segment(onset_t, window[t].middle)
|
||||
annotation[segment, k] = label
|
||||
|
||||
return annotation
|
||||
|
||||
def get_class_by_name(class_name, default_module_name=None):
|
||||
"""Load class by its name
|
||||
|
||||
Parameters
|
||||
----------
|
||||
class_name : str
|
||||
default_module_name : str, optional
|
||||
When provided and `class_name` does not contain the absolute path
|
||||
|
||||
Returns
|
||||
-------
|
||||
Klass : `type`
|
||||
Class.
|
||||
|
||||
Example
|
||||
-------
|
||||
>>> ClopiNet = get_class_by_name(
|
||||
... 'pyannote.audio.embedding.models.ClopiNet')
|
||||
>>> ClopiNet = get_class_by_name(
|
||||
... 'ClopiNet', default_module_name='pyannote.audio.embedding.models')
|
||||
"""
|
||||
tokens = class_name.split('.')
|
||||
|
||||
if len(tokens) == 1:
|
||||
if default_module_name is None:
|
||||
msg = (
|
||||
f'Could not infer module name from class name "{class_name}".'
|
||||
f'Please provide default module name.')
|
||||
raise ValueError(msg)
|
||||
module_name = default_module_name
|
||||
else:
|
||||
module_name = '.'.join(tokens[:-1])
|
||||
class_name = tokens[-1]
|
||||
|
||||
return getattr(import_module(module_name), class_name)
|
||||
Reference in new issue
Block a user