import json import os from os.path import join as p_join from tqdm import tqdm from time import time from datasets import load_dataset from model_meta_voice import MetaVoiceSE from model_pyannote_embedding import PyannoteSE from model_w2v_bert import W2VBertSE cache_dir = p_join("experiment_cache", "voxceleb1") def get_embedding(model_class, model_name: str, dataset_name: str): dataset = load_dataset(dataset_name, split="test") file_path = p_join("experiment_cache", "embeddings", f"{model_name}.{dataset_name}.json") os.makedirs(os.path.dirname(file_path), exist_ok=True) if os.path.exists(file_path): return model = model_class() embeddings = [] for i in tqdm(dataset, total=len(dataset)): start = time() v = model.get_speaker_embedding(i["audio"]["array"], i["audio"]["sampling_rate"]) embeddings.append({ "model": model_name, "embedding": v.tolist(), "sampling_rate": i["audio"]["sampling_rate"], "id": i["id"], "speaker_id": i["speaker_id"], "process_time": time() - start, "dataset_name": os.path.basename(dataset_name) }) with open(file_path, "w") as f: f.write("\n".join([json.dumps(i) for i in embeddings])) if __name__ == '__main__': # cache embedding get_embedding(MetaVoiceSE, "meta_voice_se", "asahi417/voxceleb1-test-split") get_embedding(PyannoteSE, "pyannote_se", "asahi417/voxceleb1-test-split") get_embedding(W2VBertSE, "w2v_bert_se", "asahi417/voxceleb1-test-split")