mirror of
https://github.com/pyannote/pyannote-audio.git
synced 2026-11-20 13:07:51 +09:00
138 KiB
138 KiB
In [ ]:
!pip install -qq pyannote.audio==3.1.1
!pip install -qq ipython==7.34.0
!git clone https://github.com/pyannote/AMI-diarization-setup.git
%cd ./AMI-diarization-setup/pyannote/
!bash ./download_ami_mini.sh
%cd /contentIn [ ]:
from pyannote.database import registry, FileFinder
registry.load_database("AMI-diarization-setup/pyannote/database.yml")
preprocessors = {"audio": FileFinder()}
ami = registry.get_protocol('AMI.SpeakerDiarization.mini',
preprocessors=preprocessors)In [2]:
first_training_file = next(ami.train())
reference = first_training_file["annotation"]In [ ]:
from pyannote.audio.utils.preview import listen
from pyannote.core import Segment
one_minute = Segment(240, 300)
listen(first_training_file, one_minute)In [4]:
from pyannote.core import notebook
notebook.crop = one_minute
referenceOut [4]:
In [5]:
from pyannote.audio.tasks import VoiceActivityDetection
vad = VoiceActivityDetection(ami, duration=2., batch_size=128)Protocol AMI.SpeakerDiarization.mini does not precompute the output of torchaudio.info(): adding a 'torchaudio.info' preprocessor for you to speed up dataloaders. See pyannote.database documentation on how to do that yourself.
In [6]:
from pyannote.audio.models.segmentation import PyanNet
model = PyanNet(sincnet={'stride': 10}, task=vad)In [7]:
import pytorch_lightning as pl
trainer = pl.Trainer(devices=1, max_epochs=1)
trainer.fit(model)INFO:pytorch_lightning.utilities.rank_zero:GPU available: True (cuda), used: True INFO:pytorch_lightning.utilities.rank_zero:TPU available: False, using: 0 TPU cores INFO:pytorch_lightning.utilities.rank_zero:IPU available: False, using: 0 IPUs INFO:pytorch_lightning.utilities.rank_zero:HPU available: False, using: 0 HPUs WARNING:pytorch_lightning.loggers.tensorboard:Missing logger folder: /content/lightning_logs INFO:pytorch_lightning.accelerators.cuda:LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0] INFO:pytorch_lightning.callbacks.model_summary: | Name | Type | Params | In sizes | Out sizes --------------------------------------------------------------------------------------------------------------------- 0 | sincnet | SincNet | 42.6 K | [1, 1, 32000] | [1, 60, 115] 1 | lstm | LSTM | 589 K | [1, 115, 60] | [[1, 115, 256], [[4, 1, 128], [4, 1, 128]]] 2 | linear | ModuleList | 49.4 K | ? | ? 3 | classifier | Linear | 129 | [1, 115, 128] | [1, 115, 1] 4 | activation | Sigmoid | 0 | [1, 115, 1] | [1, 115, 1] 5 | validation_metric | MetricCollection | 0 | ? | ? --------------------------------------------------------------------------------------------------------------------- 681 K Trainable params 0 Non-trainable params 681 K Total params 2.728 Total estimated model params size (MB)
Sanity Checking: | | 0/? [00:00<?, ?it/s]
Training: | | 0/? [00:00<?, ?it/s]
Validation: | | 0/? [00:00<?, ?it/s]
INFO:pytorch_lightning.utilities.rank_zero:`Trainer.fit` stopped: `max_epochs=1` reached.
In [8]:
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 [9]:
from pyannote.audio import Inference
inference = Inference(model)
vad_probability = inference(test_file)In [10]:
vad_probabilityOut [10]:
In [11]:
expected_output = test_file["annotation"].get_timeline().support()
expected_outputOut [11]:
In [ ]:
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)In [13]:
pipeline(test_file).get_timeline()Out [13]:
In [14]:
expected_outputOut [14]:
In [15]:
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 = 12.4%
In [ ]:
pipeline.freeze({'min_duration_on': 0.0, 'min_duration_off': 0.0})In [17]:
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 [18]:
print(optimized_params){'onset': 0.824238792126195, 'offset': 0.5375254799320842, 'min_duration_on': 0.0, 'min_duration_off': 0.0}
In [19]:
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 = 12.0%