mirror of
https://github.com/pyannote/pyannote-audio.git
synced 2026-11-20 13:07:51 +09:00
2.5 MiB
2.5 MiB
In [2]:
from pyannote.database import get_protocol, FileFinder
preprocessors = {"audio": FileFinder()}
ami = get_protocol('AMI.SpeakerDiarization.only_words',
preprocessors=preprocessors)In [40]:
first_training_file = next(ami.train())
reference = first_training_file["annotation"]In [72]:
from pyannote.audio.utils.preview import listen
from pyannote.core import Segment
one_minute = Segment(240, 300)
listen(first_training_file, one_minute)Out [72]:
In [73]:
from pyannote.core import notebook
notebook.crop = one_minute
referenceOut [73]:
In [17]:
from pyannote.audio.tasks import VoiceActivityDetection
vad = VoiceActivityDetection(ami, duration=2., batch_size=128)In [20]:
from pyannote.audio.models.segmentation import PyanNet
model = PyanNet(sincnet={'stride': 10}, task=vad)In [21]:
import pytorch_lightning as pl
trainer = pl.Trainer(devices=1, accelerator="gpu", max_epochs=2)
trainer.fit(model)GPU available: True, used: True TPU available: False, using: 0 TPU cores IPU available: False, using: 0 IPUs LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0] | Name | Type | Params | In sizes | Out sizes ----------------------------------------------------------------------------------------------------------------------- 0 | sincnet | SincNet | 42.6 K | [128, 1, 32000] | [128, 60, 115] 1 | lstm | LSTM | 589 K | [128, 115, 60] | [[128, 115, 256], [[4, 128, 128], [4, 128, 128]]] 2 | linear | ModuleList | 49.4 K | ? | ? 3 | classifier | Linear | 129 | [128, 115, 128] | [128, 115, 1] 4 | activation | Sigmoid | 0 | [128, 115, 1] | [128, 115, 1] 5 | validation_metric | AUROC | 0 | ? | ? ----------------------------------------------------------------------------------------------------------------------- 681 K Trainable params 0 Non-trainable params 681 K Total params 2.728 Total estimated model params size (MB)
HBox(children=(HTML(value='Validation sanity check'), FloatProgress(value=1.0, bar_style='info', layout=Layout…
HBox(children=(HTML(value='Training'), FloatProgress(value=1.0, bar_style='info', layout=Layout(flex='2'), max…
HBox(children=(HTML(value='Validating'), FloatProgress(value=1.0, bar_style='info', layout=Layout(flex='2'), m…
HBox(children=(HTML(value='Validating'), FloatProgress(value=1.0, bar_style='info', layout=Layout(flex='2'), m…
In [23]:
test_file = next(ami.test())
# here we use a test file provided by the protocol, but it could be any audio file
# e.g. test_file = "/path/to/test.wav".In [29]:
from pyannote.audio import Inference
inference = Inference(model)
vad_probability = inference(test_file)In [30]:
vad_probabilityOut [30]:
In [34]:
expected_output = test_file["annotation"].get_timeline().support()
expected_outputOut [34]:
In [56]:
from pyannote.audio.pipelines import VoiceActivityDetection as VoiceActivityDetectionPipeline
pipeline = VoiceActivityDetectionPipeline(segmentation=model)
initial_params = {"onset": 0.6, "offset": 0.4,
"min_duration_on": 0.0, "min_duration_off": 0.0}
pipeline.instantiate(initial_params)Out [56]:
<pyannote.audio.pipelines.voice_activity_detection.VoiceActivityDetection at 0x1536d13f8c40>
In [76]:
pipeline(test_file).get_timeline()Out [76]:
In [77]:
expected_outputOut [77]:
In [54]:
from pyannote.metrics.detection import DetectionErrorRate
metric = DetectionErrorRate()
for file in ami.test():
# apply the voice activity detection pipeline
speech = pipeline(file)
# evaluate its output
_ = metric(
file['annotation'], # this is the reference annotation
speech, # this is the hypothesized annotation
uem=file['annotated']) # this is the part of the file that should be evaluated
# aggregate the performance over the whole test set
detection_error_rate = abs(metric)
print(f'Detection error rate = {detection_error_rate * 100:.1f}%')Detection error rate = 7.3%
In [60]:
pipeline.freeze({'min_duration_on': 0.0, 'min_duration_off': 0.0})Out [60]:
<pyannote.audio.pipelines.voice_activity_detection.VoiceActivityDetection at 0x1536d13f8c40>
In [63]:
from pyannote.pipeline import Optimizer
optimizer = Optimizer(pipeline)
optimizer.tune(list(ami.development()),
warm_start=initial_params,
n_iterations=20,
show_progress=False)
optimized_params = optimizer.best_paramsIn [78]:
print(optimized_params){'onset': 0.8020095763639215, 'offset': 0.5643079355945013, 'min_duration_on': 0.0, 'min_duration_off': 0.0}
In [79]:
optimized_pipeline = pipeline.instantiate(optimized_params)
metric = DetectionErrorRate()
for file in ami.test():
speech = optimized_pipeline(file)
_ = metric(file['annotation'], speech, uem=file['annotated'])
detection_error_rate = abs(metric)
print(f'Detection error rate = {detection_error_rate * 100:.1f}%')Detection error rate = 6.9%