init ascend tts
This commit is contained in:
@@ -0,0 +1,143 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import os
|
||||
|
||||
inp_text = os.environ.get("inp_text")
|
||||
inp_wav_dir = os.environ.get("inp_wav_dir")
|
||||
exp_name = os.environ.get("exp_name")
|
||||
i_part = os.environ.get("i_part")
|
||||
all_parts = os.environ.get("all_parts")
|
||||
if "_CUDA_VISIBLE_DEVICES" in os.environ:
|
||||
os.environ["CUDA_VISIBLE_DEVICES"] = os.environ["_CUDA_VISIBLE_DEVICES"]
|
||||
opt_dir = os.environ.get("opt_dir")
|
||||
bert_pretrained_dir = os.environ.get("bert_pretrained_dir")
|
||||
import torch
|
||||
|
||||
is_half = eval(os.environ.get("is_half", "True")) and torch.cuda.is_available()
|
||||
version = os.environ.get("version", None)
|
||||
import traceback
|
||||
import os.path
|
||||
from text.cleaner import clean_text
|
||||
from transformers import AutoModelForMaskedLM, AutoTokenizer
|
||||
from tools.my_utils import clean_path
|
||||
|
||||
# inp_text=sys.argv[1]
|
||||
# inp_wav_dir=sys.argv[2]
|
||||
# exp_name=sys.argv[3]
|
||||
# i_part=sys.argv[4]
|
||||
# all_parts=sys.argv[5]
|
||||
# os.environ["CUDA_VISIBLE_DEVICES"]=sys.argv[6]#i_gpu
|
||||
# opt_dir="/data/docker/liujing04/gpt-vits/fine_tune_dataset/%s"%exp_name
|
||||
# bert_pretrained_dir="/data/docker/liujing04/bert-vits2/Bert-VITS2-master20231106/bert/chinese-roberta-wwm-ext-large"
|
||||
|
||||
from time import time as ttime
|
||||
import shutil
|
||||
|
||||
|
||||
def my_save(fea, path): #####fix issue: torch.save doesn't support chinese path
|
||||
dir = os.path.dirname(path)
|
||||
name = os.path.basename(path)
|
||||
# tmp_path="%s/%s%s.pth"%(dir,ttime(),i_part)
|
||||
tmp_path = "%s%s.pth" % (ttime(), i_part)
|
||||
torch.save(fea, tmp_path)
|
||||
shutil.move(tmp_path, "%s/%s" % (dir, name))
|
||||
|
||||
|
||||
txt_path = "%s/2-name2text-%s.txt" % (opt_dir, i_part)
|
||||
if os.path.exists(txt_path) == False:
|
||||
bert_dir = "%s/3-bert" % (opt_dir)
|
||||
os.makedirs(opt_dir, exist_ok=True)
|
||||
os.makedirs(bert_dir, exist_ok=True)
|
||||
if torch.cuda.is_available():
|
||||
device = "cuda:0"
|
||||
# elif torch.backends.mps.is_available():
|
||||
# device = "mps"
|
||||
else:
|
||||
device = "cpu"
|
||||
if os.path.exists(bert_pretrained_dir):
|
||||
...
|
||||
else:
|
||||
raise FileNotFoundError(bert_pretrained_dir)
|
||||
tokenizer = AutoTokenizer.from_pretrained(bert_pretrained_dir)
|
||||
bert_model = AutoModelForMaskedLM.from_pretrained(bert_pretrained_dir)
|
||||
if is_half == True:
|
||||
bert_model = bert_model.half().to(device)
|
||||
else:
|
||||
bert_model = bert_model.to(device)
|
||||
|
||||
def get_bert_feature(text, word2ph):
|
||||
with torch.no_grad():
|
||||
inputs = tokenizer(text, return_tensors="pt")
|
||||
for i in inputs:
|
||||
inputs[i] = inputs[i].to(device)
|
||||
res = bert_model(**inputs, output_hidden_states=True)
|
||||
res = torch.cat(res["hidden_states"][-3:-2], -1)[0].cpu()[1:-1]
|
||||
|
||||
assert len(word2ph) == len(text)
|
||||
phone_level_feature = []
|
||||
for i in range(len(word2ph)):
|
||||
repeat_feature = res[i].repeat(word2ph[i], 1)
|
||||
phone_level_feature.append(repeat_feature)
|
||||
|
||||
phone_level_feature = torch.cat(phone_level_feature, dim=0)
|
||||
|
||||
return phone_level_feature.T
|
||||
|
||||
def process(data, res):
|
||||
for name, text, lan in data:
|
||||
try:
|
||||
name = clean_path(name)
|
||||
name = os.path.basename(name)
|
||||
print(name)
|
||||
phones, word2ph, norm_text = clean_text(text.replace("%", "-").replace("¥", ","), lan, version)
|
||||
path_bert = "%s/%s.pt" % (bert_dir, name)
|
||||
if os.path.exists(path_bert) == False and lan == "zh":
|
||||
bert_feature = get_bert_feature(norm_text, word2ph)
|
||||
assert bert_feature.shape[-1] == len(phones)
|
||||
# torch.save(bert_feature, path_bert)
|
||||
my_save(bert_feature, path_bert)
|
||||
phones = " ".join(phones)
|
||||
# res.append([name,phones])
|
||||
res.append([name, phones, word2ph, norm_text])
|
||||
except:
|
||||
print(name, text, traceback.format_exc())
|
||||
|
||||
todo = []
|
||||
res = []
|
||||
with open(inp_text, "r", encoding="utf8") as f:
|
||||
lines = f.read().strip("\n").split("\n")
|
||||
|
||||
language_v1_to_language_v2 = {
|
||||
"ZH": "zh",
|
||||
"zh": "zh",
|
||||
"JP": "ja",
|
||||
"jp": "ja",
|
||||
"JA": "ja",
|
||||
"ja": "ja",
|
||||
"EN": "en",
|
||||
"en": "en",
|
||||
"En": "en",
|
||||
"KO": "ko",
|
||||
"Ko": "ko",
|
||||
"ko": "ko",
|
||||
"yue": "yue",
|
||||
"YUE": "yue",
|
||||
"Yue": "yue",
|
||||
}
|
||||
for line in lines[int(i_part) :: int(all_parts)]:
|
||||
try:
|
||||
wav_name, spk_name, language, text = line.split("|")
|
||||
# todo.append([name,text,"zh"])
|
||||
if language in language_v1_to_language_v2.keys():
|
||||
todo.append([wav_name, text, language_v1_to_language_v2.get(language, language)])
|
||||
else:
|
||||
print(f"\033[33m[Waring] The {language = } of {wav_name} is not supported for training.\033[0m")
|
||||
except:
|
||||
print(line, traceback.format_exc())
|
||||
|
||||
process(todo, res)
|
||||
opt = []
|
||||
for name, phones, word2ph, norm_text in res:
|
||||
opt.append("%s\t%s\t%s\t%s" % (name, phones, word2ph, norm_text))
|
||||
with open(txt_path, "w", encoding="utf8") as f:
|
||||
f.write("\n".join(opt) + "\n")
|
||||
@@ -0,0 +1,134 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import sys
|
||||
import os
|
||||
|
||||
inp_text = os.environ.get("inp_text")
|
||||
inp_wav_dir = os.environ.get("inp_wav_dir")
|
||||
exp_name = os.environ.get("exp_name")
|
||||
i_part = os.environ.get("i_part")
|
||||
all_parts = os.environ.get("all_parts")
|
||||
if "_CUDA_VISIBLE_DEVICES" in os.environ:
|
||||
os.environ["CUDA_VISIBLE_DEVICES"] = os.environ["_CUDA_VISIBLE_DEVICES"]
|
||||
from feature_extractor import cnhubert
|
||||
|
||||
opt_dir = os.environ.get("opt_dir")
|
||||
cnhubert.cnhubert_base_path = os.environ.get("cnhubert_base_dir")
|
||||
import torch
|
||||
|
||||
is_half = eval(os.environ.get("is_half", "True")) and torch.cuda.is_available()
|
||||
|
||||
import traceback
|
||||
import numpy as np
|
||||
from scipy.io import wavfile
|
||||
import librosa
|
||||
|
||||
now_dir = os.getcwd()
|
||||
sys.path.append(now_dir)
|
||||
from tools.my_utils import load_audio, clean_path
|
||||
|
||||
# from config import cnhubert_base_path
|
||||
# cnhubert.cnhubert_base_path=cnhubert_base_path
|
||||
# inp_text=sys.argv[1]
|
||||
# inp_wav_dir=sys.argv[2]
|
||||
# exp_name=sys.argv[3]
|
||||
# i_part=sys.argv[4]
|
||||
# all_parts=sys.argv[5]
|
||||
# os.environ["CUDA_VISIBLE_DEVICES"]=sys.argv[6]
|
||||
# cnhubert.cnhubert_base_path=sys.argv[7]
|
||||
# opt_dir="/data/docker/liujing04/gpt-vits/fine_tune_dataset/%s"%exp_name
|
||||
|
||||
from time import time as ttime
|
||||
import shutil
|
||||
|
||||
|
||||
def my_save(fea, path): #####fix issue: torch.save doesn't support chinese path
|
||||
dir = os.path.dirname(path)
|
||||
name = os.path.basename(path)
|
||||
# tmp_path="%s/%s%s.pth"%(dir,ttime(),i_part)
|
||||
tmp_path = "%s%s.pth" % (ttime(), i_part)
|
||||
torch.save(fea, tmp_path)
|
||||
shutil.move(tmp_path, "%s/%s" % (dir, name))
|
||||
|
||||
|
||||
hubert_dir = "%s/4-cnhubert" % (opt_dir)
|
||||
wav32dir = "%s/5-wav32k" % (opt_dir)
|
||||
os.makedirs(opt_dir, exist_ok=True)
|
||||
os.makedirs(hubert_dir, exist_ok=True)
|
||||
os.makedirs(wav32dir, exist_ok=True)
|
||||
|
||||
maxx = 0.95
|
||||
alpha = 0.5
|
||||
if torch.cuda.is_available():
|
||||
device = "cuda:0"
|
||||
# elif torch.backends.mps.is_available():
|
||||
# device = "mps"
|
||||
else:
|
||||
device = "cpu"
|
||||
model = cnhubert.get_model()
|
||||
# is_half=False
|
||||
if is_half == True:
|
||||
model = model.half().to(device)
|
||||
else:
|
||||
model = model.to(device)
|
||||
|
||||
nan_fails = []
|
||||
|
||||
|
||||
def name2go(wav_name, wav_path):
|
||||
hubert_path = "%s/%s.pt" % (hubert_dir, wav_name)
|
||||
if os.path.exists(hubert_path):
|
||||
return
|
||||
tmp_audio = load_audio(wav_path, 32000)
|
||||
tmp_max = np.abs(tmp_audio).max()
|
||||
if tmp_max > 2.2:
|
||||
print("%s-filtered,%s" % (wav_name, tmp_max))
|
||||
return
|
||||
tmp_audio32 = (tmp_audio / tmp_max * (maxx * alpha * 32768)) + ((1 - alpha) * 32768) * tmp_audio
|
||||
tmp_audio32b = (tmp_audio / tmp_max * (maxx * alpha * 1145.14)) + ((1 - alpha) * 1145.14) * tmp_audio
|
||||
tmp_audio = librosa.resample(tmp_audio32b, orig_sr=32000, target_sr=16000) # 不是重采样问题
|
||||
tensor_wav16 = torch.from_numpy(tmp_audio)
|
||||
if is_half == True:
|
||||
tensor_wav16 = tensor_wav16.half().to(device)
|
||||
else:
|
||||
tensor_wav16 = tensor_wav16.to(device)
|
||||
ssl = model.model(tensor_wav16.unsqueeze(0))["last_hidden_state"].transpose(1, 2).cpu() # torch.Size([1, 768, 215])
|
||||
if np.isnan(ssl.detach().numpy()).sum() != 0:
|
||||
nan_fails.append((wav_name, wav_path))
|
||||
print("nan filtered:%s" % wav_name)
|
||||
return
|
||||
wavfile.write(
|
||||
"%s/%s" % (wav32dir, wav_name),
|
||||
32000,
|
||||
tmp_audio32.astype("int16"),
|
||||
)
|
||||
my_save(ssl, hubert_path)
|
||||
|
||||
|
||||
with open(inp_text, "r", encoding="utf8") as f:
|
||||
lines = f.read().strip("\n").split("\n")
|
||||
|
||||
for line in lines[int(i_part) :: int(all_parts)]:
|
||||
try:
|
||||
# wav_name,text=line.split("\t")
|
||||
wav_name, spk_name, language, text = line.split("|")
|
||||
wav_name = clean_path(wav_name)
|
||||
if inp_wav_dir != "" and inp_wav_dir != None:
|
||||
wav_name = os.path.basename(wav_name)
|
||||
wav_path = "%s/%s" % (inp_wav_dir, wav_name)
|
||||
|
||||
else:
|
||||
wav_path = wav_name
|
||||
wav_name = os.path.basename(wav_name)
|
||||
name2go(wav_name, wav_path)
|
||||
except:
|
||||
print(line, traceback.format_exc())
|
||||
|
||||
if len(nan_fails) > 0 and is_half == True:
|
||||
is_half = False
|
||||
model = model.float()
|
||||
for wav in nan_fails:
|
||||
try:
|
||||
name2go(wav[0], wav[1])
|
||||
except:
|
||||
print(wav_name, traceback.format_exc())
|
||||
@@ -0,0 +1,115 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import sys
|
||||
import os
|
||||
|
||||
inp_text = os.environ.get("inp_text")
|
||||
inp_wav_dir = os.environ.get("inp_wav_dir")
|
||||
exp_name = os.environ.get("exp_name")
|
||||
i_part = os.environ.get("i_part")
|
||||
all_parts = os.environ.get("all_parts")
|
||||
if "_CUDA_VISIBLE_DEVICES" in os.environ:
|
||||
os.environ["CUDA_VISIBLE_DEVICES"] = os.environ["_CUDA_VISIBLE_DEVICES"]
|
||||
|
||||
opt_dir = os.environ.get("opt_dir")
|
||||
sv_path = os.environ.get("sv_path")
|
||||
import torch
|
||||
|
||||
is_half = eval(os.environ.get("is_half", "True")) and torch.cuda.is_available()
|
||||
|
||||
import traceback
|
||||
import torchaudio
|
||||
|
||||
now_dir = os.getcwd()
|
||||
sys.path.append(now_dir)
|
||||
sys.path.append(f"{now_dir}/GPT_SoVITS/eres2net")
|
||||
from tools.my_utils import clean_path
|
||||
from time import time as ttime
|
||||
import shutil
|
||||
from ERes2NetV2 import ERes2NetV2
|
||||
import kaldi as Kaldi
|
||||
|
||||
|
||||
def my_save(fea, path): #####fix issue: torch.save doesn't support chinese path
|
||||
dir = os.path.dirname(path)
|
||||
name = os.path.basename(path)
|
||||
# tmp_path="%s/%s%s.pth"%(dir,ttime(),i_part)
|
||||
tmp_path = "%s%s.pth" % (ttime(), i_part)
|
||||
torch.save(fea, tmp_path)
|
||||
shutil.move(tmp_path, "%s/%s" % (dir, name))
|
||||
|
||||
|
||||
sv_cn_dir = "%s/7-sv_cn" % (opt_dir)
|
||||
wav32dir = "%s/5-wav32k" % (opt_dir)
|
||||
os.makedirs(opt_dir, exist_ok=True)
|
||||
os.makedirs(sv_cn_dir, exist_ok=True)
|
||||
os.makedirs(wav32dir, exist_ok=True)
|
||||
|
||||
maxx = 0.95
|
||||
alpha = 0.5
|
||||
if torch.cuda.is_available():
|
||||
device = "cuda:0"
|
||||
# elif torch.backends.mps.is_available():
|
||||
# device = "mps"
|
||||
else:
|
||||
device = "cpu"
|
||||
|
||||
|
||||
class SV:
|
||||
def __init__(self, device, is_half):
|
||||
pretrained_state = torch.load(sv_path, map_location="cpu")
|
||||
embedding_model = ERes2NetV2(baseWidth=24, scale=4, expansion=4)
|
||||
embedding_model.load_state_dict(pretrained_state)
|
||||
embedding_model.eval()
|
||||
self.embedding_model = embedding_model
|
||||
self.res = torchaudio.transforms.Resample(32000, 16000).to(device)
|
||||
if is_half == False:
|
||||
self.embedding_model = self.embedding_model.to(device)
|
||||
else:
|
||||
self.embedding_model = self.embedding_model.half().to(device)
|
||||
self.is_half = is_half
|
||||
|
||||
def compute_embedding3(self, wav): # (1,x)#-1~1
|
||||
with torch.no_grad():
|
||||
wav = self.res(wav)
|
||||
if self.is_half == True:
|
||||
wav = wav.half()
|
||||
feat = torch.stack(
|
||||
[Kaldi.fbank(wav0.unsqueeze(0), num_mel_bins=80, sample_frequency=16000, dither=0) for wav0 in wav]
|
||||
)
|
||||
sv_emb = self.embedding_model.forward3(feat)
|
||||
return sv_emb
|
||||
|
||||
|
||||
sv = SV(device, is_half)
|
||||
|
||||
|
||||
def name2go(wav_name, wav_path):
|
||||
sv_cn_path = "%s/%s.pt" % (sv_cn_dir, wav_name)
|
||||
if os.path.exists(sv_cn_path):
|
||||
return
|
||||
wav_path = "%s/%s" % (wav32dir, wav_name)
|
||||
wav32k, sr0 = torchaudio.load(wav_path)
|
||||
assert sr0 == 32000
|
||||
wav32k = wav32k.to(device)
|
||||
emb = sv.compute_embedding3(wav32k).cpu() # torch.Size([1, 20480])
|
||||
my_save(emb, sv_cn_path)
|
||||
|
||||
|
||||
with open(inp_text, "r", encoding="utf8") as f:
|
||||
lines = f.read().strip("\n").split("\n")
|
||||
|
||||
for line in lines[int(i_part) :: int(all_parts)]:
|
||||
try:
|
||||
wav_name, spk_name, language, text = line.split("|")
|
||||
wav_name = clean_path(wav_name)
|
||||
if inp_wav_dir != "" and inp_wav_dir != None:
|
||||
wav_name = os.path.basename(wav_name)
|
||||
wav_path = "%s/%s" % (inp_wav_dir, wav_name)
|
||||
|
||||
else:
|
||||
wav_path = wav_name
|
||||
wav_name = os.path.basename(wav_name)
|
||||
name2go(wav_name, wav_path)
|
||||
except:
|
||||
print(line, traceback.format_exc())
|
||||
@@ -0,0 +1,118 @@
|
||||
import os
|
||||
|
||||
inp_text = os.environ.get("inp_text")
|
||||
exp_name = os.environ.get("exp_name")
|
||||
i_part = os.environ.get("i_part")
|
||||
all_parts = os.environ.get("all_parts")
|
||||
if "_CUDA_VISIBLE_DEVICES" in os.environ:
|
||||
os.environ["CUDA_VISIBLE_DEVICES"] = os.environ["_CUDA_VISIBLE_DEVICES"]
|
||||
opt_dir = os.environ.get("opt_dir")
|
||||
pretrained_s2G = os.environ.get("pretrained_s2G")
|
||||
s2config_path = os.environ.get("s2config_path")
|
||||
|
||||
if os.path.exists(pretrained_s2G):
|
||||
...
|
||||
else:
|
||||
raise FileNotFoundError(pretrained_s2G)
|
||||
# version=os.environ.get("version","v2")
|
||||
size = os.path.getsize(pretrained_s2G)
|
||||
if size < 82978 * 1024:
|
||||
version = "v1"
|
||||
elif size < 100 * 1024 * 1024:
|
||||
version = "v2"
|
||||
elif size < 103520 * 1024:
|
||||
version = "v1"
|
||||
elif size < 700 * 1024 * 1024:
|
||||
version = "v2"
|
||||
else:
|
||||
version = "v3"
|
||||
import torch
|
||||
|
||||
is_half = eval(os.environ.get("is_half", "True")) and torch.cuda.is_available()
|
||||
import traceback
|
||||
import sys
|
||||
|
||||
now_dir = os.getcwd()
|
||||
sys.path.append(now_dir)
|
||||
import logging
|
||||
import utils
|
||||
|
||||
if version != "v3":
|
||||
from module.models import SynthesizerTrn
|
||||
else:
|
||||
from module.models import SynthesizerTrnV3 as SynthesizerTrn
|
||||
from tools.my_utils import clean_path
|
||||
|
||||
logging.getLogger("numba").setLevel(logging.WARNING)
|
||||
# from config import pretrained_s2G
|
||||
|
||||
# inp_text=sys.argv[1]
|
||||
# exp_name=sys.argv[2]
|
||||
# i_part=sys.argv[3]
|
||||
# all_parts=sys.argv[4]
|
||||
# os.environ["CUDA_VISIBLE_DEVICES"]=sys.argv[5]
|
||||
# opt_dir="/data/docker/liujing04/gpt-vits/fine_tune_dataset/%s"%exp_name
|
||||
|
||||
|
||||
hubert_dir = "%s/4-cnhubert" % (opt_dir)
|
||||
semantic_path = "%s/6-name2semantic-%s.tsv" % (opt_dir, i_part)
|
||||
if os.path.exists(semantic_path) == False:
|
||||
os.makedirs(opt_dir, exist_ok=True)
|
||||
|
||||
if torch.cuda.is_available():
|
||||
device = "cuda"
|
||||
# elif torch.backends.mps.is_available():
|
||||
# device = "mps"
|
||||
else:
|
||||
device = "cpu"
|
||||
hps = utils.get_hparams_from_file(s2config_path)
|
||||
vq_model = SynthesizerTrn(
|
||||
hps.data.filter_length // 2 + 1,
|
||||
hps.train.segment_size // hps.data.hop_length,
|
||||
n_speakers=hps.data.n_speakers,
|
||||
version=version,
|
||||
**hps.model,
|
||||
)
|
||||
if is_half == True:
|
||||
vq_model = vq_model.half().to(device)
|
||||
else:
|
||||
vq_model = vq_model.to(device)
|
||||
vq_model.eval()
|
||||
# utils.load_checkpoint(utils.latest_checkpoint_path(hps.s2_ckpt_dir, "G_*.pth"), vq_model, None, True)
|
||||
# utils.load_checkpoint(pretrained_s2G, vq_model, None, True)
|
||||
print(
|
||||
vq_model.load_state_dict(
|
||||
torch.load(pretrained_s2G, map_location="cpu", weights_only=False)["weight"], strict=False
|
||||
)
|
||||
)
|
||||
|
||||
def name2go(wav_name, lines):
|
||||
hubert_path = "%s/%s.pt" % (hubert_dir, wav_name)
|
||||
if os.path.exists(hubert_path) == False:
|
||||
return
|
||||
ssl_content = torch.load(hubert_path, map_location="cpu")
|
||||
if is_half == True:
|
||||
ssl_content = ssl_content.half().to(device)
|
||||
else:
|
||||
ssl_content = ssl_content.to(device)
|
||||
codes = vq_model.extract_latent(ssl_content)
|
||||
semantic = " ".join([str(i) for i in codes[0, 0, :].tolist()])
|
||||
lines.append("%s\t%s" % (wav_name, semantic))
|
||||
|
||||
with open(inp_text, "r", encoding="utf8") as f:
|
||||
lines = f.read().strip("\n").split("\n")
|
||||
|
||||
lines1 = []
|
||||
for line in lines[int(i_part) :: int(all_parts)]:
|
||||
# print(line)
|
||||
try:
|
||||
# wav_name,text=line.split("\t")
|
||||
wav_name, spk_name, language, text = line.split("|")
|
||||
wav_name = clean_path(wav_name)
|
||||
wav_name = os.path.basename(wav_name)
|
||||
# name2go(name,lines1)
|
||||
name2go(wav_name, lines1)
|
||||
except:
|
||||
print(line, traceback.format_exc())
|
||||
with open(semantic_path, "w", encoding="utf8") as f:
|
||||
f.write("\n".join(lines1))
|
||||
Reference in New Issue
Block a user