diff --git a/CHANGELOG.md b/CHANGELOG.md index b40e7ecf..b63a18a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - feat(cli): add `--subfolder` option to `benchmark` command [@antoinelaurent](https://github.com/antoinelaurent) - fix(utils): fix `SpeakerDiarizationMixin.optimal_mapping` `reference` is a mapping [@Kayvan-Zahiri](https://github.com/Kayvan-Zahiri) +- fix(task): fix `balance` option crashing training with `KeyError: 'metadata'` [@MohammadHijjawi97](https://github.com/MohammadHijjawi97) ## Version 4.0.7 (2026-06-30) diff --git a/src/pyannote/audio/core/task.py b/src/pyannote/audio/core/task.py index 01264558..6f856d17 100644 --- a/src/pyannote/audio/core/task.py +++ b/src/pyannote/audio/core/task.py @@ -624,6 +624,10 @@ class Task(lightning.LightningDataModule): try: with open(self.cache, "rb") as cache_file: self.prepared_data = dict(np.load(cache_file, allow_pickle=True)) + # np.savez stores the `metadata-values` dict as a 0-d object array: unwrap it + 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", diff --git a/src/pyannote/audio/tasks/segmentation/mixins.py b/src/pyannote/audio/tasks/segmentation/mixins.py index 0079de70..d5813c50 100644 --- a/src/pyannote/audio/tasks/segmentation/mixins.py +++ b/src/pyannote/audio/tasks/segmentation/mixins.py @@ -88,7 +88,7 @@ class SegmentationTask(Task): ) 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] @@ -163,7 +163,7 @@ class SegmentationTask(Task): # 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: diff --git a/src/pyannote/audio/tasks/separation/PixIT.py b/src/pyannote/audio/tasks/separation/PixIT.py index 239f402b..1963a0d4 100644 --- a/src/pyannote/audio/tasks/separation/PixIT.py +++ b/src/pyannote/audio/tasks/separation/PixIT.py @@ -503,7 +503,7 @@ class PixIT(SegmentationTask): for key, value in filters.items(): split_files &= self.prepared_data["audio-metadata"][ key - ] == self.prepared_data["metadata"][key].index(value) + ] == self.prepared_data["metadata-values"][key].index(value) file_ids = np.where(split_files)[0] # turn annotated duration into a probability distribution diff --git a/tests/test_train.py b/tests/test_train.py index ccb4b678..8395d7bc 100644 --- a/tests/test_train.py +++ b/tests/test_train.py @@ -76,6 +76,13 @@ def test_train_segmentation(protocol): trainer.fit(model) +def test_train_segmentation_with_balance(protocol): + segmentation = SpeakerDiarization(protocol, balance=["database"]) + model = SimpleSegmentationModel(task=segmentation) + trainer = Trainer(fast_dev_run=True, accelerator="cpu") + trainer.fit(model) + + def test_train_segmentation_with_cached_data_mono_device(protocol, cache): first_task = SpeakerDiarization(protocol, cache=cache) first_model = SimpleSegmentationModel(task=first_task)