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

Fix balance #1787

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
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 pyannote/audio/core/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,7 @@ def setup(self, stage=None):
try:
with open(self.cache, "rb") as cache_file:
self.prepared_data = dict(np.load(cache_file, allow_pickle=True))
self.prepared_data["metadata-values"] = self.prepared_data["metadata-values"].item()
except FileNotFoundError:
print(
"Cached data for protocol not found. Ensure that prepare_data() was called",
Expand Down
19 changes: 15 additions & 4 deletions pyannote/audio/tasks/segmentation/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,20 @@ def train__iter__helper(self, rng: random.Random, **filters):
)
for key, value in filters.items():
training &= self.prepared_data["audio-metadata"][key] == self.prepared_data[
"metadata"
"metadata-values"
][key].index(value)
file_ids = np.where(training)[0]

# turn annotated duration into a probability distribution
annotated_duration = self.prepared_data["audio-annotated"][file_ids]
annotated_duration_total = np.sum(annotated_duration)
# Exit early (this will discard this worker/helper).
# This should happen only when the filter contains no data.
# (which happens when balance is used since we use all combinations of balance filters)
if annotated_duration_total == 0:
yield None
cum_prob_annotated_duration = np.cumsum(
annotated_duration / np.sum(annotated_duration)
annotated_duration / annotated_duration_total
)

duration = self.duration
Expand Down Expand Up @@ -184,13 +190,18 @@ def train__iter__(self):
# create a subchunk generator for each combination of "balance" keys
subchunks = dict()
for product in itertools.product(
*[self.prepared_data["metadata"][key] for key in balance]
*[self.prepared_data["metadata-values"][key] for key in balance]
):
# we iterate on the cartesian product of the values in metadata_unique_values
# eg: for balance=["database", "split"], with 2 databases and 2 splits:
# ("DIHARD", "A"), ("DIHARD", "B"), ("REPERE", "A"), ("REPERE", "B")
filters = {key: value for key, value in zip(balance, product)}
subchunks[product] = self.train__iter__helper(rng, **filters)
product_iterator = self.train__iter__helper(rng, **filters)

# This specific product may not exist. For example, if balance=['database']
# and there is a database that's not present in the training set.
if next(product_iterator) is not None:
subchunks[product] = product_iterator

while True:
# select one subchunk generator at random (with uniform probability)
Expand Down
Loading