-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
pipeline.py
504 lines (392 loc) · 15.9 KB
/
pipeline.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
#!/usr/bin/env python
# encoding: utf-8
# The MIT License (MIT)
# Copyright (c) 2018-2019 CNRS
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# AUTHORS
# Hervé BREDIN - http://herve.niderb.fr
from typing import Optional
from typing import TextIO
from pathlib import Path
from collections import OrderedDict
from .parameter import Parameter, Frozen
from .typing import PipelineInput
from .typing import PipelineOutput
from filelock import FileLock
import yaml
from pyannote.core import Timeline
from pyannote.core import Annotation
from optuna.trial import Trial
class Pipeline:
"""Base tunable pipeline"""
def __init__(self):
# un-instantiated parameters (= `Parameter` instances)
self._parameters = OrderedDict()
# instantiated parameters
self._instantiated = OrderedDict()
# sub-pipelines
self._pipelines = OrderedDict()
def __hash__(self):
# FIXME -- also keep track of (sub)pipeline attribtes
frozen = self.parameters(frozen=True)
return hash(tuple(sorted(self._flatten(frozen).items())))
def __getattr__(self, name):
"""(Advanced) attribute getter"""
# in case `name` corresponds to an instantiated parameter value, returns it
if '_instantiated' in self.__dict__:
_instantiated = self.__dict__['_instantiated']
if name in _instantiated:
return _instantiated[name]
# in case `name` corresponds to a parameter, returns it
if '_parameters' in self.__dict__:
_parameters = self.__dict__['_parameters']
if name in _parameters:
return _parameters[name]
# in case `name` corresponds to a sub-pipeline, returns it
if '_pipelines' in self.__dict__:
_pipelines = self.__dict__['_pipelines']
if name in _pipelines:
return _pipelines[name]
msg = "'{}' object has no attribute '{}'".format(
type(self).__name__, name)
raise AttributeError(msg)
def __setattr__(self, name, value):
"""(Advanced) attribute setter
If `value` is an instance of `Parameter`, store it in `_parameters`.
If `value` is an instance of `Pipeline`, store it in `_pipelines`.
If `name` is in `_parameters`, store `value` in `_instantiated`.
"""
def remove_from(*dicts):
for d in dicts:
if name in d:
del d[name]
_parameters = self.__dict__.get('_parameters')
_instantiated = self.__dict__.get('_instantiated')
_pipelines = self.__dict__.get('_pipelines')
# if `value` is an instance of `Parameter`, store it in `_parameters`
if isinstance(value, Parameter):
if _parameters is None:
msg = ("cannot assign hyper-parameters "
"before Pipeline.__init__() call")
raise AttributeError(msg)
remove_from(self.__dict__, _instantiated, _pipelines)
_parameters[name] = value
return
# add/update one sub-pipeline
if isinstance(value, Pipeline):
if _pipelines is None:
msg = ("cannot assign sub-pipelines "
"before Pipeline.__init__() call")
raise AttributeError(msg)
remove_from(self.__dict__, _parameters, _instantiated)
_pipelines[name] = value
return
# store instantiated parameter value
if _parameters is not None and name in _parameters:
_instantiated[name] = value
return
object.__setattr__(self, name, value)
def __delattr__(self, name):
if name in self._parameters:
del self._parameters[name]
elif name in self._instantiated:
del self._instantiated[name]
elif name in self._pipelines:
del self._pipelines[name]
else:
object.__delattr__(self, name)
def _flattened_parameters(self, frozen: Optional[bool] = False,
instantiated: Optional[bool] = False) -> dict:
"""Get flattened dictionary of parameters
Parameters
----------
frozen : `bool`, optional
Only return value of frozen parameters.
instantiated : `bool`, optional
Only return value of instantiated parameters.
Returns
-------
params : `dict`
Flattened dictionary of parameters.
"""
if frozen and instantiated:
msg = ("one must choose between `frozen` and `instantiated`.")
raise ValueError(msg)
# initialize dictionary with root parameters
if instantiated:
params = dict(self._instantiated)
elif frozen:
params = {n: p.value for n, p in self._parameters.items()
if isinstance(p, Frozen)}
else:
params = dict(self._parameters)
# recursively add sub-pipeline parameters
for pipeline_name, pipeline in self._pipelines.items():
pipeline_params = pipeline._flattened_parameters(
frozen=frozen, instantiated=instantiated)
for name, value in pipeline_params.items():
params[f'{pipeline_name}>{name}'] = value
return params
def _flatten(self, nested_params: dict) -> dict:
"""Convert nested dictionary to flattened dictionary
For instance, a nested dictionary like this one:
~~~~~~~~~~~~~~~~~~~~~
param: value1
pipeline:
param: value2
subpipeline:
param: value3
~~~~~~~~~~~~~~~~~~~~~
becomes the following flattened dictionary:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
param : value1
pipeline>param : value2
pipeline>subpipeline>param : value3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Parameter
---------
nested_params : `dict`
Returns
-------
flattened_params : `dict`
"""
flattened_params = dict()
for name, value in nested_params.items():
if isinstance(value, dict):
for subname, subvalue in self._flatten(value).items():
flattened_params[f'{name}>{subname}'] = subvalue
else:
flattened_params[name] = value
return flattened_params
def _unflatten(self, flattened_params: dict) -> dict:
"""Convert flattened dictionary to nested dictionary
For instance, a flattened dictionary like this one:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
param : value1
pipeline>param : value2
pipeline>subpipeline>param : value3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
becomes the following nested dictionary:
~~~~~~~~~~~~~~~~~~~~~
param: value1
pipeline:
param: value2
subpipeline:
param: value3
~~~~~~~~~~~~~~~~~~~~~
Parameter
---------
flattened_params : `dict`
Returns
-------
nested_params : `dict`
"""
nested_params = {}
pipeline_params = {name: {} for name in self._pipelines}
for name, value in flattened_params.items():
# if name contains has multipe ">"-separated tokens
# it means that it is a sub-pipeline parameter
tokens = name.split('>')
if len(tokens) > 1:
# read sub-pipeline name
pipeline_name = tokens[0]
# read parameter name
param_name = '>'.join(tokens[1:])
# update sub-pipeline flattened dictionary
pipeline_params[pipeline_name][param_name] = value
# otherwise, it is an actual parameter of this pipeline
else:
# store it as such
nested_params[name] = value
# recursively unflatten sub-pipeline flattened dictionary
for name, pipeline in self._pipelines.items():
nested_params[name] = pipeline._unflatten(pipeline_params[name])
return nested_params
def parameters(self, trial: Optional[Trial] = None,
frozen: Optional[bool] = False,
instantiated: Optional[bool] = False) -> dict:
"""Returns nested dictionary of (optionnaly instantiated) parameters.
For a pipeline with one `param`, one sub-pipeline with its own param
and its own sub-pipeline, it will returns something like:
~~~~~~~~~~~~~~~~~~~~~
param: value1
pipeline:
param: value2
subpipeline:
param: value3
~~~~~~~~~~~~~~~~~~~~~
Parameter
---------
trial : `Trial`, optional
When provided, use trial to suggest new parameter values
and return them.
frozen : `bool`, optional
Return frozen parameter value
instantiated : `bool`, optional
Return instantiated parameter values.
Returns
-------
params : `dict`
Nested dictionary of parameters. See above for the actual format.
"""
if (instantiated or frozen) and trial is not None:
msg = "One must choose between `trial`, `instantiated`, or `frozen`"
raise ValueError(msg)
# get flattened dictionary of uninstantiated parameters
params = self._flattened_parameters(frozen=frozen,
instantiated=instantiated)
if trial is not None:
# use provided `trial` to suggest values for parameters
params = {name: param(name, trial)
for name, param in params.items()}
# un-flatten flattend dictionary
return self._unflatten(params)
def initialize(self):
"""Instantiate root pipeline with current set of parameters"""
pass
def freeze(self, params: dict) -> 'Pipeline':
"""Recursively freeze pipeline parameters
Parameters
----------
params : `dict`
Nested dictionary of parameters.
Returns
-------
self : `Pipeline`
Pipeline.
"""
for name, value in params.items():
# recursively freeze sub-pipelines parameters
if name in self._pipelines:
if not isinstance(value, dict):
msg = (f"only parameters of '{name}' pipeline can "
f"be frozen (not the whole pipeline)")
raise ValueError(msg)
self._pipelines[name].freeze(value)
continue
# instantiate parameter value
if name in self._parameters:
setattr(self, name, Frozen(value))
continue
msg = f"parameter '{name}' does not exist"
raise ValueError(msg)
return self
def instantiate(self, params: dict) -> 'Pipeline':
"""Recursively instantiate all pipelines
Parameters
----------
params : `dict`
Nested dictionary of parameters.
Returns
-------
self : `Pipeline`
Instantiated pipeline.
"""
for name, value in params.items():
# recursively call `instantiate` with sub-pipelines
if name in self._pipelines:
if not isinstance(value, dict):
msg = (f"only parameters of '{name}' pipeline can "
f"be instantiated (not the whole pipeline)")
raise ValueError(msg)
self._pipelines[name].instantiate(value)
continue
# instantiate parameter value
if name in self._parameters:
setattr(self, name, value)
continue
msg = f"parameter '{name}' does not exist"
raise ValueError(msg)
self.initialize()
return self
def dump_params(self, params_yml: Path,
params: Optional[dict] = None) -> str:
"""Dump parameters to disk
Parameters
----------
params_yml : `Path`
Path to YAML file.
params : `dict`, optional
Nested Parameters. Defaults to pipeline current parameters.
Returns
-------
content : `str`
Content written in `param_yml`.
"""
# use instantiated parameters when `params` is not provided
if params is None:
params = self.parameters(instantiated=True)
# format as valid YAML
content = yaml.dump(params, default_flow_style=False)
# (safely) dump YAML content
with FileLock(params_yml.with_suffix('.lock')):
with open(params_yml, mode='w') as fp:
fp.write(content)
return content
def load_params(self, params_yml: Path) -> 'Pipeline':
"""Instantiate pipeline using parameters from disk
Parameters
----------
param_yml : `Path`
Path to YAML file.
Returns
-------
self : `Pipeline`
Instantiated pipeline
"""
with open(params_yml, mode='r') as fp:
params = yaml.load(fp)
return self.instantiate(params)
def __call__(self, input: PipelineInput) -> PipelineOutput:
"""Apply pipeline on input and return its output"""
raise NotImplementedError
def get_metric(self) -> 'pyannote.metrics.base.BaseMetric':
"""Return new metric (from pyannote.metrics)
When this method is implemented, the returned metric is used as a
replacement for the loss method below.
Returns
-------
metric : `pyannote.metrics.base.BaseMetric`
"""
raise NotImplementedError
def loss(self, input: PipelineInput,
output: PipelineOutput) -> float:
"""Compute loss for given input/output pair
Parameters
----------
input : object
Pipeline input.
output : object
Pipeline output
Returns
-------
loss : `float`
Loss value
"""
raise NotImplementedError
def write(self, file: TextIO,
output: PipelineOutput):
"""Write pipeline output to file"""
if isinstance(output, Timeline):
for s in output:
file.write(f'{output.uri} {s.start:.3f} {s.end:.3f}\n')
return
if isinstance(output, Annotation):
for s, t, l in output.itertracks(yield_label=True):
file.write(f'{output.uri} {output.modality} {s.start:.3f} {s.end:.3f} {t} {l}\n')
return
raise NotImplementedError