File size: 1,991 Bytes
7974918
 
 
 
dcc9bd4
7974918
 
 
 
 
 
 
f1c7699
 
 
 
 
 
 
 
 
 
85fe221
 
f1c7699
a772b7b
7974918
 
 
 
85fe221
dcc9bd4
7974918
f1c7699
7974918
 
 
85fe221
 
f1c7699
 
 
7974918
 
 
 
 
 
85fe221
f1c7699
85fe221
f1c7699
db53fea
f1c7699
 
7974918
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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")