fix: make Wrapper pickable

This commit is contained in:
Hervé Bredin committed 2020-05-15 11:41:41 +02:00
1 parent 57372ed4a5
commit 93328fe35d
1 file changed
+14 -2
+14 -2
View File
@@ -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)