experiment-speaker-embedding / experiment_speaker_verification.py
asahi417's picture
init
db53fea
raw
history blame
1.99 kB
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 to_json_serializable(val):
# if type(val) is list:
# return val
# if "float" in str(type(val)):
# return float(val)
# if "int" in str(type(val)):
# return int(val)
# return str(val)
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}.{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]))
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")
get_embedding(MetaVoiceSE, "meta_voice_se", "ylacombe/expresso")
# get_embedding(PyannoteSE, "pyannote_se", "ylacombe/expresso")
get_embedding(W2VBertSE, "w2v_bert_se", "ylacombe/expresso")