mirror of
https://github.com/pyannote/pyannote-audio.git
synced 2026-11-20 13:07:51 +09:00
10 KiB
10 KiB
In [ ]:
from copy import deepcopy
from pathlib import Path
import subprocess
import sys
import matplotlib.pyplot as plt
import yaml
from huggingface_hub import snapshot_download
from pyannote.audio import Pipeline
from pyannote.audio.core.pipeline import expand_subfoldersIn [ ]:
model_directory = Path("community1").resolve()
snapshot_download(
"pyannote/speaker-diarization-community-1",
local_dir=model_directory,
)
pipeline_config = model_directory / "config.yaml"In [ ]:
database_registry = Path("/path/to/database.yml")
protocol_name = "MyDatabase.SpeakerDiarization.MyProtocol"
subset = "development"
metrics = ["DiarizationPurity", "DiarizationCoverage"]
max_iterations = 100In [ ]:
command = [
sys.executable, "-m", "pyannote.audio", "optimize",
str(pipeline_config), protocol_name,
"--registry", str(database_registry),
"--subset", subset,
"--average-case",
"--max-iterations", str(max_iterations),
]
for metric in metrics:
command.extend(["--metric", metric])
subprocess.run(command, check=True)In [ ]:
study_name = f"{protocol_name}.{subset}.{'+'.join(metrics)}"
manifest_path = pipeline_config.with_suffix(f".{study_name}.pareto.yaml")
manifest = yaml.safe_load(manifest_path.read_text())
pareto_front = sorted(
manifest["optimization"]["status"]["pareto_front"],
key=lambda point: point["values"]["DiarizationPurity"],
)
if not pareto_front:
raise ValueError("No Pareto trials are available. Run optimization first.")
pareto_frontIn [ ]:
purity = [point["values"]["DiarizationPurity"] for point in pareto_front]
coverage = [point["values"]["DiarizationCoverage"] for point in pareto_front]
fig, ax = plt.subplots()
ax.plot(purity, coverage, "o-")
ax.set(xlabel="Diarization purity", ylabel="Diarization coverage")
ax.grid(True)In [ ]:
def harmonic_mean(point):
purity = point["values"]["DiarizationPurity"]
coverage = point["values"]["DiarizationCoverage"]
return 2 * purity * coverage / (purity + coverage) if purity + coverage else 0.0
def rank(point, criterion):
purity = point["values"]["DiarizationPurity"]
coverage = point["values"]["DiarizationCoverage"]
score = {
"purity": purity,
"coverage": coverage,
"harmonic_mean": harmonic_mean(point),
}[criterion]
return score, purity, coverage, -point["trial"]
selected_points = {
criterion: max(pareto_front, key=lambda point: rank(point, criterion))
for criterion in ("purity", "coverage", "harmonic_mean")
}
for criterion, point in selected_points.items():
print(
f"Best {criterion}: trial {point['trial']}, "
f"purity={point['values']['DiarizationPurity']:.3f}, "
f"coverage={point['values']['DiarizationCoverage']:.3f}, "
f"harmonic mean={harmonic_mean(point):.3f}"
)In [ ]:
best_configs = {}
best_config_paths = {}
for criterion, point in selected_points.items():
config = deepcopy(manifest)
config["params"] = deepcopy(point["params"])
config["optimization"] = {
"protocol": protocol_name,
"subset": subset,
"metrics": metrics.copy(),
"selection": criterion,
"trial": point["trial"],
"values": deepcopy(point["values"]),
"harmonic_mean": harmonic_mean(point),
"source": str(manifest_path.resolve()),
}
expand_subfolders(config, str(manifest_path.parent.resolve()))
best_configs[criterion] = config
config_path = pipeline_config.with_suffix(f".best_{criterion}.yaml")
config_path.write_text(yaml.safe_dump(config, sort_keys=False))
best_config_paths[criterion] = config_path
best_configs