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

feat(utils): add "soft" option to Powerset.to_multilabel conversion #1516

Merged
merged 2 commits into from
Oct 22, 2023
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- feat(pipeline): add support for list of hooks with `Hooks`
- BREAKING(pipeline): remove `logging_hook` (use `ArtifactHook` instead)
- fix(pipeline): add missing "embedding" hook call in `SpeakerDiarization`
- feat(utils): add `"soft"` option to `Powerset.to_multilabel`

## Version 3.0.1 (2023-09-28)

Expand Down
22 changes: 14 additions & 8 deletions pyannote/audio/utils/powerset.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,26 +84,32 @@ def build_cardinality(self) -> torch.Tensor:
powerset_k += 1
return cardinality

def to_multilabel(self, powerset: torch.Tensor) -> torch.Tensor:
"""Convert predictions from (soft) powerset to (hard) multi-label
def to_multilabel(self, powerset: torch.Tensor, soft: bool = False) -> torch.Tensor:
"""Convert predictions from powerset to multi-label

Parameter
---------
powerset : (batch_size, num_frames, num_powerset_classes) torch.Tensor
Soft predictions in "powerset" space.
soft : bool, optional
Return soft multi-label predictions. Defaults to False (i.e. hard predictions)
Assumes that `powerset` are "logits" (not "probabilities").

Returns
-------
multi_label : (batch_size, num_frames, num_classes) torch.Tensor
Hard predictions in "multi-label" space.
Predictions in "multi-label" space.
"""

hard_powerset = torch.nn.functional.one_hot(
torch.argmax(powerset, dim=-1),
self.num_powerset_classes,
).float()
if soft:
powerset_probs = torch.exp(powerset)
else:
powerset_probs = torch.nn.functional.one_hot(
torch.argmax(powerset, dim=-1),
self.num_powerset_classes,
).float()

return torch.matmul(hard_powerset, self.mapping)
return torch.matmul(powerset_probs, self.mapping)

def forward(self, powerset: torch.Tensor) -> torch.Tensor:
"""Alias for `to_multilabel`"""
Expand Down
Loading