Clean up previous raw files
Browse files- Zero-CPU/style_gen.py +0 -128
Zero-CPU/style_gen.py
DELETED
|
@@ -1,128 +0,0 @@
|
|
| 1 |
-
import argparse
|
| 2 |
-
from concurrent.futures import ThreadPoolExecutor
|
| 3 |
-
import warnings
|
| 4 |
-
|
| 5 |
-
import numpy as np
|
| 6 |
-
import torch
|
| 7 |
-
from tqdm import tqdm
|
| 8 |
-
|
| 9 |
-
import utils
|
| 10 |
-
from common.log import logger
|
| 11 |
-
from common.stdout_wrapper import SAFE_STDOUT
|
| 12 |
-
from config import config
|
| 13 |
-
|
| 14 |
-
warnings.filterwarnings("ignore", category=UserWarning)
|
| 15 |
-
from pyannote.audio import Inference, Model
|
| 16 |
-
|
| 17 |
-
model = Model.from_pretrained("pyannote/wespeaker-voxceleb-resnet34-LM")
|
| 18 |
-
inference = Inference(model, window="whole")
|
| 19 |
-
device = torch.device(config.style_gen_config.device)
|
| 20 |
-
inference.to(device)
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
class NaNValueError(ValueError):
|
| 24 |
-
"""カスタム例外クラス。NaN値が見つかった場合に使用されます。"""
|
| 25 |
-
|
| 26 |
-
pass
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
# 推論時にインポートするために短いが関数を書く
|
| 30 |
-
def get_style_vector(wav_path):
|
| 31 |
-
return inference(wav_path)
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
def save_style_vector(wav_path):
|
| 35 |
-
try:
|
| 36 |
-
style_vec = get_style_vector(wav_path)
|
| 37 |
-
except Exception as e:
|
| 38 |
-
print("\n")
|
| 39 |
-
logger.error(f"Error occurred with file: {wav_path}, Details:\n{e}\n")
|
| 40 |
-
raise
|
| 41 |
-
# 値にNaNが含まれていると悪影響なのでチェックする
|
| 42 |
-
if np.isnan(style_vec).any():
|
| 43 |
-
print("\n")
|
| 44 |
-
logger.warning(f"NaN value found in style vector: {wav_path}")
|
| 45 |
-
raise NaNValueError(f"NaN value found in style vector: {wav_path}")
|
| 46 |
-
np.save(f"{wav_path}.npy", style_vec) # `test.wav` -> `test.wav.npy`
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
def process_line(line):
|
| 50 |
-
wavname = line.split("|")[0]
|
| 51 |
-
try:
|
| 52 |
-
save_style_vector(wavname)
|
| 53 |
-
return line, None
|
| 54 |
-
except NaNValueError:
|
| 55 |
-
return line, "nan_error"
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
def save_average_style_vector(style_vectors, filename="style_vectors.npy"):
|
| 59 |
-
average_vector = np.mean(style_vectors, axis=0)
|
| 60 |
-
np.save(filename, average_vector)
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
if __name__ == "__main__":
|
| 64 |
-
parser = argparse.ArgumentParser()
|
| 65 |
-
parser.add_argument(
|
| 66 |
-
"-c", "--config", type=str, default=config.style_gen_config.config_path
|
| 67 |
-
)
|
| 68 |
-
parser.add_argument(
|
| 69 |
-
"--num_processes", type=int, default=config.style_gen_config.num_processes
|
| 70 |
-
)
|
| 71 |
-
args, _ = parser.parse_known_args()
|
| 72 |
-
config_path = args.config
|
| 73 |
-
num_processes = args.num_processes
|
| 74 |
-
|
| 75 |
-
hps = utils.get_hparams_from_file(config_path)
|
| 76 |
-
|
| 77 |
-
device = config.style_gen_config.device
|
| 78 |
-
|
| 79 |
-
training_lines = []
|
| 80 |
-
with open(hps.data.training_files, encoding="utf-8") as f:
|
| 81 |
-
training_lines.extend(f.readlines())
|
| 82 |
-
with ThreadPoolExecutor(max_workers=num_processes) as executor:
|
| 83 |
-
training_results = list(
|
| 84 |
-
tqdm(
|
| 85 |
-
executor.map(process_line, training_lines),
|
| 86 |
-
total=len(training_lines),
|
| 87 |
-
file=SAFE_STDOUT,
|
| 88 |
-
)
|
| 89 |
-
)
|
| 90 |
-
ok_training_lines = [line for line, error in training_results if error is None]
|
| 91 |
-
nan_training_lines = [
|
| 92 |
-
line for line, error in training_results if error == "nan_error"
|
| 93 |
-
]
|
| 94 |
-
if nan_training_lines:
|
| 95 |
-
nan_files = [line.split("|")[0] for line in nan_training_lines]
|
| 96 |
-
logger.warning(
|
| 97 |
-
f"Found NaN value in {len(nan_training_lines)} files: {nan_files}, so they will be deleted from training data."
|
| 98 |
-
)
|
| 99 |
-
|
| 100 |
-
val_lines = []
|
| 101 |
-
with open(hps.data.validation_files, encoding="utf-8") as f:
|
| 102 |
-
val_lines.extend(f.readlines())
|
| 103 |
-
|
| 104 |
-
with ThreadPoolExecutor(max_workers=num_processes) as executor:
|
| 105 |
-
val_results = list(
|
| 106 |
-
tqdm(
|
| 107 |
-
executor.map(process_line, val_lines),
|
| 108 |
-
total=len(val_lines),
|
| 109 |
-
file=SAFE_STDOUT,
|
| 110 |
-
)
|
| 111 |
-
)
|
| 112 |
-
ok_val_lines = [line for line, error in val_results if error is None]
|
| 113 |
-
nan_val_lines = [line for line, error in val_results if error == "nan_error"]
|
| 114 |
-
if nan_val_lines:
|
| 115 |
-
nan_files = [line.split("|")[0] for line in nan_val_lines]
|
| 116 |
-
logger.warning(
|
| 117 |
-
f"Found NaN value in {len(nan_val_lines)} files: {nan_files}, so they will be deleted from validation data."
|
| 118 |
-
)
|
| 119 |
-
|
| 120 |
-
with open(hps.data.training_files, "w", encoding="utf-8") as f:
|
| 121 |
-
f.writelines(ok_training_lines)
|
| 122 |
-
|
| 123 |
-
with open(hps.data.validation_files, "w", encoding="utf-8") as f:
|
| 124 |
-
f.writelines(ok_val_lines)
|
| 125 |
-
|
| 126 |
-
ok_num = len(ok_training_lines) + len(ok_val_lines)
|
| 127 |
-
|
| 128 |
-
logger.info(f"Finished generating style vectors! total: {ok_num} npy files.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|