lj1995 commited on
Commit
6b0dc77
·
1 Parent(s): 8d8416e

Delete prepare_datasets

Browse files
prepare_datasets/1-get-text.py DELETED
@@ -1,146 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
-
3
- import os
4
-
5
- inp_text = os.environ.get("inp_text")
6
- inp_wav_dir = os.environ.get("inp_wav_dir")
7
- exp_name = os.environ.get("exp_name")
8
- i_part = os.environ.get("i_part")
9
- all_parts = os.environ.get("all_parts")
10
- os.environ["CUDA_VISIBLE_DEVICES"] = os.environ.get("_CUDA_VISIBLE_DEVICES")
11
- opt_dir = os.environ.get("opt_dir")
12
- bert_pretrained_dir = os.environ.get("bert_pretrained_dir")
13
- import torch
14
- is_half = eval(os.environ.get("is_half", "True")) and torch.cuda.is_available()
15
- version = os.environ.get('version', None)
16
- import sys, numpy as np, traceback, pdb
17
- import os.path
18
- from glob import glob
19
- from tqdm import tqdm
20
- from text.cleaner import clean_text
21
- from transformers import AutoModelForMaskedLM, AutoTokenizer
22
- import numpy as np
23
- from tools.my_utils import clean_path
24
-
25
- # inp_text=sys.argv[1]
26
- # inp_wav_dir=sys.argv[2]
27
- # exp_name=sys.argv[3]
28
- # i_part=sys.argv[4]
29
- # all_parts=sys.argv[5]
30
- # os.environ["CUDA_VISIBLE_DEVICES"]=sys.argv[6]#i_gpu
31
- # opt_dir="/data/docker/liujing04/gpt-vits/fine_tune_dataset/%s"%exp_name
32
- # bert_pretrained_dir="/data/docker/liujing04/bert-vits2/Bert-VITS2-master20231106/bert/chinese-roberta-wwm-ext-large"
33
-
34
- from time import time as ttime
35
- import shutil
36
-
37
-
38
- def my_save(fea,path):#####fix issue: torch.save doesn't support chinese path
39
- dir=os.path.dirname(path)
40
- name=os.path.basename(path)
41
- # tmp_path="%s/%s%s.pth"%(dir,ttime(),i_part)
42
- tmp_path="%s%s.pth"%(ttime(),i_part)
43
- torch.save(fea,tmp_path)
44
- shutil.move(tmp_path,"%s/%s"%(dir,name))
45
-
46
-
47
- txt_path = "%s/2-name2text-%s.txt" % (opt_dir, i_part)
48
- if os.path.exists(txt_path) == False:
49
- bert_dir = "%s/3-bert" % (opt_dir)
50
- os.makedirs(opt_dir, exist_ok=True)
51
- os.makedirs(bert_dir, exist_ok=True)
52
- if torch.cuda.is_available():
53
- device = "cuda:0"
54
- # elif torch.backends.mps.is_available():
55
- # device = "mps"
56
- else:
57
- device = "cpu"
58
- if os.path.exists(bert_pretrained_dir):...
59
- else:raise FileNotFoundError(bert_pretrained_dir)
60
- tokenizer = AutoTokenizer.from_pretrained(bert_pretrained_dir)
61
- bert_model = AutoModelForMaskedLM.from_pretrained(bert_pretrained_dir)
62
- if is_half == True:
63
- bert_model = bert_model.half().to(device)
64
- else:
65
- bert_model = bert_model.to(device)
66
-
67
- def get_bert_feature(text, word2ph):
68
- with torch.no_grad():
69
- inputs = tokenizer(text, return_tensors="pt")
70
- for i in inputs:
71
- inputs[i] = inputs[i].to(device)
72
- res = bert_model(**inputs, output_hidden_states=True)
73
- res = torch.cat(res["hidden_states"][-3:-2], -1)[0].cpu()[1:-1]
74
-
75
- assert len(word2ph) == len(text)
76
- phone_level_feature = []
77
- for i in range(len(word2ph)):
78
- repeat_feature = res[i].repeat(word2ph[i], 1)
79
- phone_level_feature.append(repeat_feature)
80
-
81
- phone_level_feature = torch.cat(phone_level_feature, dim=0)
82
-
83
- return phone_level_feature.T
84
-
85
- def process(data, res):
86
- for name, text, lan in data:
87
- try:
88
- name=clean_path(name)
89
- name = os.path.basename(name)
90
- print(name)
91
- phones, word2ph, norm_text = clean_text(
92
- text.replace("%", "-").replace("¥", ","), lan, version
93
- )
94
- path_bert = "%s/%s.pt" % (bert_dir, name)
95
- if os.path.exists(path_bert) == False and lan == "zh":
96
- bert_feature = get_bert_feature(norm_text, word2ph)
97
- assert bert_feature.shape[-1] == len(phones)
98
- # torch.save(bert_feature, path_bert)
99
- my_save(bert_feature, path_bert)
100
- phones = " ".join(phones)
101
- # res.append([name,phones])
102
- res.append([name, phones, word2ph, norm_text])
103
- except:
104
- print(name, text, traceback.format_exc())
105
-
106
- todo = []
107
- res = []
108
- with open(inp_text, "r", encoding="utf8") as f:
109
- lines = f.read().strip("\n").split("\n")
110
-
111
- language_v1_to_language_v2 = {
112
- "ZH": "zh",
113
- "zh": "zh",
114
- "JP": "ja",
115
- "jp": "ja",
116
- "JA": "ja",
117
- "ja": "ja",
118
- "EN": "en",
119
- "en": "en",
120
- "En": "en",
121
- "KO": "ko",
122
- "Ko": "ko",
123
- "ko": "ko",
124
- "yue": "yue",
125
- "YUE": "yue",
126
- "Yue": "yue",
127
- }
128
- for line in lines[int(i_part) :: int(all_parts)]:
129
- try:
130
- wav_name, spk_name, language, text = line.split("|")
131
- # todo.append([name,text,"zh"])
132
- if language in language_v1_to_language_v2.keys():
133
- todo.append(
134
- [wav_name, text, language_v1_to_language_v2.get(language, language)]
135
- )
136
- else:
137
- print(f"\033[33m[Waring] The {language = } of {wav_name} is not supported for training.\033[0m")
138
- except:
139
- print(line, traceback.format_exc())
140
-
141
- process(todo, res)
142
- opt = []
143
- for name, phones, word2ph, norm_text in res:
144
- opt.append("%s\t%s\t%s\t%s" % (name, phones, word2ph, norm_text))
145
- with open(txt_path, "w", encoding="utf8") as f:
146
- f.write("\n".join(opt) + "\n")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
prepare_datasets/2-get-hubert-wav32k.py DELETED
@@ -1,122 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
-
3
- import sys,os
4
- inp_text= os.environ.get("inp_text")
5
- inp_wav_dir= os.environ.get("inp_wav_dir")
6
- exp_name= os.environ.get("exp_name")
7
- i_part= os.environ.get("i_part")
8
- all_parts= os.environ.get("all_parts")
9
- os.environ["CUDA_VISIBLE_DEVICES"]= os.environ.get("_CUDA_VISIBLE_DEVICES")
10
- from feature_extractor import cnhubert
11
- opt_dir= os.environ.get("opt_dir")
12
- cnhubert.cnhubert_base_path= os.environ.get("cnhubert_base_dir")
13
- import torch
14
- is_half = eval(os.environ.get("is_half", "True")) and torch.cuda.is_available()
15
-
16
- import pdb,traceback,numpy as np,logging
17
- from scipy.io import wavfile
18
- import librosa
19
- now_dir = os.getcwd()
20
- sys.path.append(now_dir)
21
- from tools.my_utils import load_audio,clean_path
22
-
23
- # from config import cnhubert_base_path
24
- # cnhubert.cnhubert_base_path=cnhubert_base_path
25
- # inp_text=sys.argv[1]
26
- # inp_wav_dir=sys.argv[2]
27
- # exp_name=sys.argv[3]
28
- # i_part=sys.argv[4]
29
- # all_parts=sys.argv[5]
30
- # os.environ["CUDA_VISIBLE_DEVICES"]=sys.argv[6]
31
- # cnhubert.cnhubert_base_path=sys.argv[7]
32
- # opt_dir="/data/docker/liujing04/gpt-vits/fine_tune_dataset/%s"%exp_name
33
-
34
- from time import time as ttime
35
- import shutil
36
- def my_save(fea,path):#####fix issue: torch.save doesn't support chinese path
37
- dir=os.path.dirname(path)
38
- name=os.path.basename(path)
39
- # tmp_path="%s/%s%s.pth"%(dir,ttime(),i_part)
40
- tmp_path="%s%s.pth"%(ttime(),i_part)
41
- torch.save(fea,tmp_path)
42
- shutil.move(tmp_path,"%s/%s"%(dir,name))
43
-
44
- hubert_dir="%s/4-cnhubert"%(opt_dir)
45
- wav32dir="%s/5-wav32k"%(opt_dir)
46
- os.makedirs(opt_dir,exist_ok=True)
47
- os.makedirs(hubert_dir,exist_ok=True)
48
- os.makedirs(wav32dir,exist_ok=True)
49
-
50
- maxx=0.95
51
- alpha=0.5
52
- if torch.cuda.is_available():
53
- device = "cuda:0"
54
- # elif torch.backends.mps.is_available():
55
- # device = "mps"
56
- else:
57
- device = "cpu"
58
- model=cnhubert.get_model()
59
- # is_half=False
60
- if(is_half==True):
61
- model=model.half().to(device)
62
- else:
63
- model = model.to(device)
64
-
65
- nan_fails=[]
66
- def name2go(wav_name,wav_path):
67
- hubert_path="%s/%s.pt"%(hubert_dir,wav_name)
68
- if(os.path.exists(hubert_path)):return
69
- tmp_audio = load_audio(wav_path, 32000)
70
- tmp_max = np.abs(tmp_audio).max()
71
- if tmp_max > 2.2:
72
- print("%s-filtered,%s" % (wav_name, tmp_max))
73
- return
74
- tmp_audio32 = (tmp_audio / tmp_max * (maxx * alpha*32768)) + ((1 - alpha)*32768) * tmp_audio
75
- tmp_audio32b = (tmp_audio / tmp_max * (maxx * alpha*1145.14)) + ((1 - alpha)*1145.14) * tmp_audio
76
- tmp_audio = librosa.resample(
77
- tmp_audio32b, orig_sr=32000, target_sr=16000
78
- )#不是重采样问题
79
- tensor_wav16 = torch.from_numpy(tmp_audio)
80
- if (is_half == True):
81
- tensor_wav16=tensor_wav16.half().to(device)
82
- else:
83
- tensor_wav16 = tensor_wav16.to(device)
84
- ssl=model.model(tensor_wav16.unsqueeze(0))["last_hidden_state"].transpose(1,2).cpu()#torch.Size([1, 768, 215])
85
- if np.isnan(ssl.detach().numpy()).sum()!= 0:
86
- nan_fails.append((wav_name,wav_path))
87
- print("nan filtered:%s"%wav_name)
88
- return
89
- wavfile.write(
90
- "%s/%s"%(wav32dir,wav_name),
91
- 32000,
92
- tmp_audio32.astype("int16"),
93
- )
94
- my_save(ssl,hubert_path)
95
-
96
- with open(inp_text,"r",encoding="utf8")as f:
97
- lines=f.read().strip("\n").split("\n")
98
-
99
- for line in lines[int(i_part)::int(all_parts)]:
100
- try:
101
- # wav_name,text=line.split("\t")
102
- wav_name, spk_name, language, text = line.split("|")
103
- wav_name=clean_path(wav_name)
104
- if (inp_wav_dir != "" and inp_wav_dir != None):
105
- wav_name = os.path.basename(wav_name)
106
- wav_path = "%s/%s"%(inp_wav_dir, wav_name)
107
-
108
- else:
109
- wav_path=wav_name
110
- wav_name = os.path.basename(wav_name)
111
- name2go(wav_name,wav_path)
112
- except:
113
- print(line,traceback.format_exc())
114
-
115
- if(len(nan_fails)>0 and is_half==True):
116
- is_half=False
117
- model=model.float()
118
- for wav in nan_fails:
119
- try:
120
- name2go(wav[0],wav[1])
121
- except:
122
- print(wav_name,traceback.format_exc())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
prepare_datasets/3-get-semantic.py DELETED
@@ -1,101 +0,0 @@
1
- import os
2
-
3
- inp_text = os.environ.get("inp_text")
4
- exp_name = os.environ.get("exp_name")
5
- i_part = os.environ.get("i_part")
6
- all_parts = os.environ.get("all_parts")
7
- os.environ["CUDA_VISIBLE_DEVICES"] = os.environ.get("_CUDA_VISIBLE_DEVICES")
8
- opt_dir = os.environ.get("opt_dir")
9
- pretrained_s2G = os.environ.get("pretrained_s2G")
10
- s2config_path = os.environ.get("s2config_path")
11
- version=os.environ.get("version","v2")
12
- import torch
13
- is_half = eval(os.environ.get("is_half", "True")) and torch.cuda.is_available()
14
- import math, traceback
15
- import multiprocessing
16
- import sys, pdb
17
-
18
- now_dir = os.getcwd()
19
- sys.path.append(now_dir)
20
- from random import shuffle
21
- import torch.multiprocessing as mp
22
- from glob import glob
23
- from tqdm import tqdm
24
- import logging, librosa, utils
25
- from module.models import SynthesizerTrn
26
- from tools.my_utils import clean_path
27
- logging.getLogger("numba").setLevel(logging.WARNING)
28
- # from config import pretrained_s2G
29
-
30
- # inp_text=sys.argv[1]
31
- # exp_name=sys.argv[2]
32
- # i_part=sys.argv[3]
33
- # all_parts=sys.argv[4]
34
- # os.environ["CUDA_VISIBLE_DEVICES"]=sys.argv[5]
35
- # opt_dir="/data/docker/liujing04/gpt-vits/fine_tune_dataset/%s"%exp_name
36
-
37
- if os.path.exists(pretrained_s2G):...
38
- else:raise FileNotFoundError(pretrained_s2G)
39
-
40
- hubert_dir = "%s/4-cnhubert" % (opt_dir)
41
- semantic_path = "%s/6-name2semantic-%s.tsv" % (opt_dir, i_part)
42
- if os.path.exists(semantic_path) == False:
43
- os.makedirs(opt_dir, exist_ok=True)
44
-
45
- if torch.cuda.is_available():
46
- device = "cuda"
47
- # elif torch.backends.mps.is_available():
48
- # device = "mps"
49
- else:
50
- device = "cpu"
51
- hps = utils.get_hparams_from_file(s2config_path)
52
- vq_model = SynthesizerTrn(
53
- hps.data.filter_length // 2 + 1,
54
- hps.train.segment_size // hps.data.hop_length,
55
- n_speakers=hps.data.n_speakers,
56
- version=version,
57
- **hps.model
58
- )
59
- if is_half == True:
60
- vq_model = vq_model.half().to(device)
61
- else:
62
- vq_model = vq_model.to(device)
63
- vq_model.eval()
64
- # utils.load_checkpoint(utils.latest_checkpoint_path(hps.s2_ckpt_dir, "G_*.pth"), vq_model, None, True)
65
- # utils.load_checkpoint(pretrained_s2G, vq_model, None, True)
66
- print(
67
- vq_model.load_state_dict(
68
- torch.load(pretrained_s2G, map_location="cpu")["weight"], strict=False
69
- )
70
- )
71
-
72
- def name2go(wav_name, lines):
73
- hubert_path = "%s/%s.pt" % (hubert_dir, wav_name)
74
- if os.path.exists(hubert_path) == False:
75
- return
76
- ssl_content = torch.load(hubert_path, map_location="cpu")
77
- if is_half == True:
78
- ssl_content = ssl_content.half().to(device)
79
- else:
80
- ssl_content = ssl_content.to(device)
81
- codes = vq_model.extract_latent(ssl_content)
82
- semantic = " ".join([str(i) for i in codes[0, 0, :].tolist()])
83
- lines.append("%s\t%s" % (wav_name, semantic))
84
-
85
- with open(inp_text, "r", encoding="utf8") as f:
86
- lines = f.read().strip("\n").split("\n")
87
-
88
- lines1 = []
89
- for line in lines[int(i_part) :: int(all_parts)]:
90
- # print(line)
91
- try:
92
- # wav_name,text=line.split("\t")
93
- wav_name, spk_name, language, text = line.split("|")
94
- wav_name=clean_path(wav_name)
95
- wav_name = os.path.basename(wav_name)
96
- # name2go(name,lines1)
97
- name2go(wav_name, lines1)
98
- except:
99
- print(line, traceback.format_exc())
100
- with open(semantic_path, "w", encoding="utf8") as f:
101
- f.write("\n".join(lines1))