| import torch |
| import argparse |
| from tqdm import tqdm |
| from pathlib import Path |
| import numpy as np |
| import os |
| import torch.nn.functional as F |
| import soundfile as sf |
| from torchaudio.transforms import Resample |
| from modelscope.pipelines import pipeline |
| from modelscope.utils.constant import Tasks |
| from funasr import AutoModel |
|
|
| def load_audio(manifest_path): |
| import numpy as np |
| generated_pathes, tgt_pathes, gt_texts= [], [], [] |
| with open(manifest_path, "r") as f: |
| for ind, line in enumerate(f): |
| if len(line.strip()) < 2: |
| continue |
| generated_path, tgt_path, gt_text = line.strip().split("\t")[:3] |
| generated_pathes.append(generated_path) |
| tgt_pathes.append(tgt_path) |
| gt_texts.append(gt_text) |
| return generated_pathes, tgt_pathes, gt_texts |
|
|
| def load_tsv(path): |
| with open(path, "r") as rf: |
| lines = rf.readlines() |
| return lines |
| |
| if __name__ == "__main__": |
| parser = argparse.ArgumentParser(description='Inference') |
| parser.add_argument('-t', '--tsv', type=str) |
| parser.add_argument('-o', '--out_home', type=str) |
| args = parser.parse_args() |
|
|
| if os.path.exists(os.path.join(args.out_home, "emo.log")): |
| with open(os.path.join(args.out_home, "emo.log"), "r") as rf: |
| if len(rf.readlines()) > 100: |
| exit() |
| |
| model = AutoModel(model="/workspace/echoloc/modelscope/iic/emotion2vec_plus_large/") |
|
|
| generated_pathes, tgt_pathes, gt_texts = load_audio(args.tsv) |
| simis = [] |
| with torch.no_grad(): |
| with open(os.path.join(args.out_home, "emo.log"), "w") as f: |
| for index, (est_path, tgt_path, gt_text) in enumerate(tqdm(zip(generated_pathes, tgt_pathes, gt_texts))): |
| try: |
| generated_emb = model.generate(est_path, granularity="utterance", extract_embedding=True, disable_pbar=True)[0]["feats"] |
| tgt_emb = model.generate(tgt_path, granularity="utterance", extract_embedding=True, disable_pbar=True)[0]["feats"] |
| simi = float(F.cosine_similarity(torch.FloatTensor([generated_emb]), torch.FloatTensor([tgt_emb])).item()) |
| except Exception as e: |
| simi = -1.0 |
| print(e) |
| simis.append(simi) |
| print("%s %s %f"%(est_path, tgt_path, simi), file=f) |
| print("------------------------------------------", file=f) |
| simis = np.array(simis) |
| print("with -1: emo2vec large:", np.mean(simis), file=f) |
| print("without -1: emo2vec large:", np.mean(simis[simis != -1]), " -1 num:", len(simis[simis==-1]), file=f) |
|
|