mirror of
https://github.com/pyannote/pyannote-audio.git
synced 2026-11-20 13:07:51 +09:00
* 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>
113 lines
2.7 KiB
Python
113 lines
2.7 KiB
Python
import numpy as np
|
|
import torch
|
|
|
|
from pyannote.audio.utils.permutation import permutate
|
|
|
|
|
|
def test_permutate_torch():
|
|
|
|
num_frames, num_speakers = 10, 3
|
|
|
|
actual_permutations = [
|
|
(0, 1, 2),
|
|
(0, 2, 1),
|
|
(1, 0, 2),
|
|
(1, 2, 0),
|
|
(2, 0, 1),
|
|
(2, 1, 0),
|
|
]
|
|
batch_size = len(actual_permutations)
|
|
|
|
y2 = torch.randn((num_frames, num_speakers))
|
|
y1 = torch.zeros((batch_size, num_frames, num_speakers))
|
|
|
|
for p, permutation in enumerate(actual_permutations):
|
|
y1[p] = y2[:, permutation]
|
|
|
|
permutated_y2, permutations = permutate(y1, y2)
|
|
assert actual_permutations == permutations
|
|
|
|
for p, permutation in enumerate(actual_permutations):
|
|
np.testing.assert_allclose(permutated_y2[p], y2[:, permutation])
|
|
|
|
|
|
def test_permutate_numpy():
|
|
|
|
num_frames, num_speakers = 10, 3
|
|
|
|
actual_permutations = [
|
|
(0, 1, 2),
|
|
(0, 2, 1),
|
|
(1, 0, 2),
|
|
(1, 2, 0),
|
|
(2, 0, 1),
|
|
(2, 1, 0),
|
|
]
|
|
batch_size = len(actual_permutations)
|
|
|
|
y2 = np.random.randn(num_frames, num_speakers)
|
|
y1 = np.zeros((batch_size, num_frames, num_speakers))
|
|
|
|
for p, permutation in enumerate(actual_permutations):
|
|
y1[p] = y2[:, permutation]
|
|
|
|
permutated_y2, permutations = permutate(y1, y2)
|
|
assert actual_permutations == permutations
|
|
|
|
for p, permutation in enumerate(actual_permutations):
|
|
np.testing.assert_allclose(permutated_y2[p], y2[:, permutation])
|
|
|
|
|
|
def test_permutate_less_speakers():
|
|
|
|
num_frames = 10
|
|
|
|
actual_permutations = [
|
|
(0, 1, None),
|
|
(0, None, 1),
|
|
(1, 0, None),
|
|
(1, None, 0),
|
|
(None, 0, 1),
|
|
(None, 1, 0),
|
|
]
|
|
batch_size = len(actual_permutations)
|
|
|
|
y2 = np.random.randn(num_frames, 2)
|
|
y1 = np.zeros((batch_size, num_frames, 3))
|
|
|
|
for p, permutation in enumerate(actual_permutations):
|
|
for i, j in enumerate(permutation):
|
|
if j is not None:
|
|
y1[p, :, i] = y2[:, j]
|
|
|
|
_permutated_y2, permutations = permutate(y1, y2)
|
|
|
|
assert permutations == actual_permutations
|
|
|
|
|
|
def test_permutate_more_speakers():
|
|
|
|
num_frames = 10
|
|
|
|
actual_permutations = [
|
|
(0, 1),
|
|
(0, 2),
|
|
(1, 0),
|
|
(1, 2),
|
|
(2, 0),
|
|
(2, 1),
|
|
]
|
|
batch_size = len(actual_permutations)
|
|
|
|
y2 = np.random.randn(num_frames, 3)
|
|
y1 = np.zeros((batch_size, num_frames, 2))
|
|
|
|
for p, permutation in enumerate(actual_permutations):
|
|
for i, j in enumerate(permutation):
|
|
y1[p, :, i] = y2[:, j]
|
|
|
|
permutated_y2, permutations = permutate(y1, y2)
|
|
|
|
assert permutations == actual_permutations
|
|
np.testing.assert_allclose(permutated_y2, y1)
|