Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize Binarize() performance when onset == offset #1721

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyannote/audio/pipelines/utils/diarization.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def to_annotation(
min_duration_off=min_duration_off,
)

return binarize(discrete_diarization).rename_tracks(generator="string")
return binarize(discrete_diarization)

@staticmethod
def to_diarization(
Expand Down
60 changes: 47 additions & 13 deletions pyannote/audio/utils/signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,19 +269,35 @@ def __call__(self, scores: SlidingWindowFeature) -> Annotation:
frames = scores.sliding_window
timestamps = [frames[i].middle for i in range(num_frames)]

# annotation meant to store 'active' regions
if self.onset == self.offset:
active = self._opt_binarize(scores, timestamps)
else:
active = self._binarize(scores, timestamps)

# because of padding, some active regions might be overlapping: merge them.
# also: fill same speaker gaps shorter than min_duration_off
if self.pad_offset > 0.0 or self.pad_onset > 0.0 or self.min_duration_off > 0.0:
active = active.support(collar=self.min_duration_off)

# remove tracks shorter than min_duration_on
if self.min_duration_on > 0:
for segment, track in list(active.itertracks()):
if segment.duration < self.min_duration_on:
del active[segment, track]

return active

def _binarize(self, scores, timestamps):
active = Annotation()

for k, k_scores in enumerate(scores.data.T):

label = k if scores.labels is None else scores.labels[k]

# initial state
start = timestamps[0]
is_active = k_scores[0] > self.onset

for t, y in zip(timestamps[1:], k_scores[1:]):

# currently active
if is_active:
# switching from active to inactive
Expand All @@ -303,19 +319,37 @@ def __call__(self, scores: SlidingWindowFeature) -> Annotation:
region = Segment(start - self.pad_onset, t + self.pad_offset)
active[region, k] = label

# because of padding, some active regions might be overlapping: merge them.
# also: fill same speaker gaps shorter than min_duration_off
if self.pad_offset > 0.0 or self.pad_onset > 0.0 or self.min_duration_off > 0.0:
active = active.support(collar=self.min_duration_off)
return active

# remove tracks shorter than min_duration_on
if self.min_duration_on > 0:
for segment, track in list(active.itertracks()):
if segment.duration < self.min_duration_on:
del active[segment, track]
def _opt_binarize(self, scores, timestamps):
active = Annotation()

return active
for k, k_scores in enumerate(scores.data.T):
label = k if scores.labels is None else scores.labels[k]

# Detect transitions
is_active = k_scores > self.onset
transitions = np.diff(is_active.astype(int))
starts = np.where(transitions == 1)[0] + 1
ends = np.where(transitions == -1)[0] + 1

# If the first frame is active, add it as a start
if is_active[0]:
starts = np.insert(starts, 0, 0)

# If the last frame is active, add it as an end
if is_active[-1]:
ends = np.append(ends, len(is_active) - 1)

# Create segments
for start, end in zip(starts, ends):
region = Segment(
timestamps[start] - self.pad_onset,
timestamps[end] + self.pad_offset,
)
active[region, k] = label

return active

class Peak:
"""Peak detection
Expand Down