mirror of
https://github.com/FunAudioLLM/CosyVoice.git
synced 2026-02-04 17:39:25 +08:00
135 lines
4.4 KiB
Python
Executable File
135 lines
4.4 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# Copyright (c) 2024 Alibaba Inc (authors: Xiang Lyu)
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
import argparse
|
|
import torch
|
|
import torchaudio
|
|
from tqdm import tqdm
|
|
import onnxruntime
|
|
import torchaudio.compliance.kaldi as kaldi
|
|
from queue import Queue, Empty
|
|
from threading import Thread
|
|
|
|
|
|
class ExtractEmbedding:
|
|
def __init__(self, model_path: str, queue: Queue, out_queue: Queue):
|
|
self.model_path = model_path
|
|
self.queue = queue
|
|
self.out_queue = out_queue
|
|
self.is_run = True
|
|
|
|
def run(self):
|
|
self.consumer_thread = Thread(target=self.consumer)
|
|
self.consumer_thread.start()
|
|
|
|
def stop(self):
|
|
self.is_run = False
|
|
self.consumer_thread.join()
|
|
|
|
def consumer(self):
|
|
option = onnxruntime.SessionOptions()
|
|
option.graph_optimization_level = (
|
|
onnxruntime.GraphOptimizationLevel.ORT_ENABLE_ALL
|
|
)
|
|
option.intra_op_num_threads = 1
|
|
providers = ["CPUExecutionProvider"]
|
|
ort_session = onnxruntime.InferenceSession(
|
|
self.model_path, sess_options=option, providers=providers
|
|
)
|
|
|
|
while self.is_run:
|
|
try:
|
|
utt, wav_file = self.queue.get(timeout=1)
|
|
|
|
audio, sample_rate = torchaudio.load(wav_file)
|
|
if sample_rate != 16000:
|
|
audio = torchaudio.transforms.Resample(
|
|
orig_freq=sample_rate, new_freq=16000
|
|
)(audio)
|
|
feat = kaldi.fbank(
|
|
audio, num_mel_bins=80, dither=0, sample_frequency=16000
|
|
)
|
|
feat = feat - feat.mean(dim=0, keepdim=True)
|
|
embedding = (
|
|
ort_session.run(
|
|
None,
|
|
{
|
|
ort_session.get_inputs()[0]
|
|
.name: feat.unsqueeze(dim=0)
|
|
.cpu()
|
|
.numpy()
|
|
},
|
|
)[0]
|
|
.flatten()
|
|
.tolist()
|
|
)
|
|
self.out_queue.put((utt, embedding))
|
|
except Empty:
|
|
self.is_run = False
|
|
break
|
|
|
|
|
|
def main(args):
|
|
utt2wav, utt2spk = {}, {}
|
|
with open("{}/wav.scp".format(args.dir)) as f:
|
|
for l in f:
|
|
l = l.replace("\n", "").split()
|
|
utt2wav[l[0]] = l[1]
|
|
with open("{}/utt2spk".format(args.dir)) as f:
|
|
for l in f:
|
|
l = l.replace("\n", "").split()
|
|
utt2spk[l[0]] = l[1]
|
|
|
|
input_queue = Queue()
|
|
output_queue = Queue()
|
|
consumers = [
|
|
ExtractEmbedding(args.onnx_path, input_queue, output_queue)
|
|
for _ in range(args.num_thread)
|
|
]
|
|
|
|
utt2embedding, spk2embedding = {}, {}
|
|
for utt in tqdm(utt2wav.keys(), desc="Load data"):
|
|
input_queue.put((utt, utt2wav[utt]))
|
|
|
|
for c in consumers:
|
|
c.run()
|
|
|
|
with tqdm(desc="Process data: ", total=len(utt2wav)) as pbar:
|
|
while any([c.is_run for c in consumers]):
|
|
try:
|
|
utt, embedding = output_queue.get(timeout=1)
|
|
utt2embedding[utt] = embedding
|
|
spk = utt2spk[utt]
|
|
if spk not in spk2embedding:
|
|
spk2embedding[spk] = []
|
|
spk2embedding[spk].append(embedding)
|
|
pbar.update(1)
|
|
except Empty:
|
|
continue
|
|
|
|
for k, v in spk2embedding.items():
|
|
spk2embedding[k] = torch.tensor(v).mean(dim=0).tolist()
|
|
|
|
torch.save(utt2embedding, "{}/utt2embedding.pt".format(args.dir))
|
|
torch.save(spk2embedding, "{}/spk2embedding.pt".format(args.dir))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("--dir", type=str)
|
|
parser.add_argument("--onnx_path", type=str)
|
|
parser.add_argument("--num_thread", type=int, default=8)
|
|
args = parser.parse_args()
|
|
main(args)
|