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 def get_embedding(model_class, model_name: str, dataset_name: str, data_split: str): dataset = load_dataset(dataset_name, split=data_split) file_path = p_join("experiment_cache", "embeddings", f"{model_name}.{os.path.basename(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"]) tmp = { "model": model_name, "embedding": v.tolist(), "sampling_rate": i["audio"]["sampling_rate"], "process_time": time() - start, "dataset_name": os.path.basename(dataset_name) } tmp.update({k: v for k, v in i.items() if k != "audio"}) embeddings.append(tmp) with open(file_path, "w") as f: f.write("\n".join([json.dumps(i) for i in embeddings])) def anlyze_embedding(model_class, model_name: str, dataset_name: str): file_path = p_join("experiment_cache", "embeddings", f"{model_name}.{os.path.basename(dataset_name)}.json") assert os.path.exists(file_path) with open(file_path) as f: embeddings = [json.loads(i) for i in f.readlines()] 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"]) tmp = { "model": model_name, "embedding": v.tolist(), "sampling_rate": i["audio"]["sampling_rate"], "process_time": time() - start, "dataset_name": os.path.basename(dataset_name) } tmp.update({k: v for k, v in i.items() if k != "audio"}) embeddings.append(tmp) 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", "test") get_embedding(PyannoteSE, "pyannote_se", "asahi417/voxceleb1-test-split", "test") get_embedding(W2VBertSE, "w2v_bert_se", "asahi417/voxceleb1-test-split", "test") get_embedding(MetaVoiceSE, "meta_voice_se", "ylacombe/expresso", "train") get_embedding(PyannoteSE, "pyannote_se", "ylacombe/expresso", "train") get_embedding(W2VBertSE, "w2v_bert_se", "ylacombe/expresso", "train")