diff --git a/pyannote/audio/features/wrapper.py b/pyannote/audio/features/wrapper.py index 5f74c4cc..e8c63777 100644 --- a/pyannote/audio/features/wrapper.py +++ b/pyannote/audio/features/wrapper.py @@ -31,7 +31,7 @@ from pathlib import Path from typing import Text from typing import Union from typing import Dict - +from functools import partial from pyannote.database.protocol.protocol import ProtocolFile from pyannote.core import Segment from pyannote.core import SlidingWindowFeature @@ -41,6 +41,10 @@ Wrappable = Union[ "Precomputed", "Pretrained", "RawAudio", "FeatureExtraction", Dict, Text, Path ] +# this needs to go here to make Wrapper instances pickable +def _use_existing_key(key, file): + return file[key] + class Wrapper: """FeatureExtraction-compliant wrapper @@ -181,7 +185,9 @@ class Wrapper: # it means that one should read the "key" key of protocol files if wrappable.startswith("@"): key = wrappable[1:] - scorer = lambda current_file: current_file[key] + + scorer = partial(_use_existing_key, key) + # scorer = lambda current_file: current_file[key] # If `wrappable` is a `Text` containing the name of an existing # `torch.hub` model, wrap the corresponding `Pretrained`. @@ -275,4 +281,10 @@ class Wrapper: # used to "inherit" most scorer_ attributes def __getattr__(self, name): + + # prevents a "RecursionError: maximum recursion depth exceeded" when pickling Wrapper + # https://stackoverflow.com/questions/49380224/how-to-make-classes-with-getattr-pickable + if "scorer_" not in vars(self): + raise AttributeError + return getattr(self.scorer_, name)