Upload 122 files
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- .gitattributes +1 -0
- app.py +182 -166
- melo/__init__.py +0 -0
- melo/__pycache__/__init__.cpython-312.pyc +0 -0
- melo/__pycache__/api.cpython-312.pyc +0 -0
- melo/__pycache__/attentions.cpython-312.pyc +0 -0
- melo/__pycache__/commons.cpython-312.pyc +0 -0
- melo/__pycache__/download_utils.cpython-312.pyc +0 -0
- melo/__pycache__/mel_processing.cpython-312.pyc +0 -0
- melo/__pycache__/models.cpython-312.pyc +0 -0
- melo/__pycache__/modules.cpython-312.pyc +0 -0
- melo/__pycache__/split_utils.cpython-312.pyc +0 -0
- melo/__pycache__/transforms.cpython-312.pyc +0 -0
- melo/__pycache__/utils.cpython-312.pyc +0 -0
- melo/api.py +135 -0
- melo/app.py +61 -0
- melo/attentions.py +459 -0
- melo/commons.py +160 -0
- melo/configs/config.json +94 -0
- melo/data/example/metadata.list +20 -0
- melo/data_utils.py +413 -0
- melo/download_utils.py +67 -0
- melo/infer.py +25 -0
- melo/init_downloads.py +14 -0
- melo/losses.py +58 -0
- melo/main.py +36 -0
- melo/mel_processing.py +174 -0
- melo/models.py +1030 -0
- melo/modules.py +598 -0
- melo/monotonic_align/__init__.py +16 -0
- melo/monotonic_align/__pycache__/__init__.cpython-312.pyc +0 -0
- melo/monotonic_align/__pycache__/core.cpython-312.pyc +0 -0
- melo/monotonic_align/core.py +46 -0
- melo/preprocess_text.py +135 -0
- melo/split_utils.py +174 -0
- melo/text/__init__.py +35 -0
- melo/text/__pycache__/__init__.cpython-312.pyc +0 -0
- melo/text/__pycache__/chinese.cpython-312.pyc +0 -0
- melo/text/__pycache__/chinese_bert.cpython-312.pyc +0 -0
- melo/text/__pycache__/chinese_mix.cpython-312.pyc +0 -0
- melo/text/__pycache__/cleaner.cpython-312.pyc +0 -0
- melo/text/__pycache__/english.cpython-312.pyc +0 -0
- melo/text/__pycache__/english_bert.cpython-312.pyc +0 -0
- melo/text/__pycache__/french.cpython-312.pyc +0 -0
- melo/text/__pycache__/french_bert.cpython-312.pyc +0 -0
- melo/text/__pycache__/japanese.cpython-312.pyc +0 -0
- melo/text/__pycache__/japanese_bert.cpython-312.pyc +0 -0
- melo/text/__pycache__/ko_dictionary.cpython-312.pyc +0 -0
- melo/text/__pycache__/korean.cpython-312.pyc +0 -0
- melo/text/__pycache__/spanish.cpython-312.pyc +0 -0
.gitattributes
CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
melo/text/fr_phonemizer/example_ipa.txt filter=lfs diff=lfs merge=lfs -text
|
app.py
CHANGED
@@ -1,166 +1,182 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import os
|
3 |
-
import warnings
|
4 |
-
import asyncio
|
5 |
-
from
|
6 |
-
from llama_index.
|
7 |
-
from llama_index.
|
8 |
-
from
|
9 |
-
import
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
import warnings
|
4 |
+
import asyncio
|
5 |
+
from melo.api import TTS
|
6 |
+
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, Document, Settings
|
7 |
+
from llama_index.llms.cerebras import Cerebras
|
8 |
+
from llama_index.embeddings.mixedbreadai import MixedbreadAIEmbedding, EncodingFormat
|
9 |
+
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
10 |
+
from groq import Groq
|
11 |
+
import io
|
12 |
+
import nltk
|
13 |
+
from dotenv import load_dotenv # Import dotenv to load .env variables
|
14 |
+
|
15 |
+
# Load environment variables from .env file
|
16 |
+
load_dotenv()
|
17 |
+
|
18 |
+
nltk.download('averaged_perceptron_tagger_eng')
|
19 |
+
|
20 |
+
# Suppress warnings
|
21 |
+
warnings.filterwarnings("ignore", message=".*clean_up_tokenization_spaces.*")
|
22 |
+
|
23 |
+
# Global variables
|
24 |
+
index = None
|
25 |
+
query_engine = None
|
26 |
+
|
27 |
+
# Inisialisasi MeloTTS untuk TTS
|
28 |
+
device = 'cpu' # Atur menjadi 'cuda' jika GPU tersedia
|
29 |
+
language = 'EN' # Bahasa default
|
30 |
+
model = TTS(language=language, device=device)
|
31 |
+
|
32 |
+
# Load Cerebras API key from environment
|
33 |
+
api_key = os.getenv("CEREBRAS_API_KEY")
|
34 |
+
if not api_key:
|
35 |
+
raise ValueError("CEREBRAS_API_KEY is not set in environment variables.")
|
36 |
+
else:
|
37 |
+
print("Cerebras API key loaded successfully.")
|
38 |
+
|
39 |
+
# Initialize Cerebras LLM and embedding model
|
40 |
+
os.environ["CEREBRAS_API_KEY"] = api_key
|
41 |
+
llm = Cerebras(model="llama-3.3-70b", api_key=os.environ["CEREBRAS_API_KEY"]) # Change model to Llama3.1-70b from Cerebras
|
42 |
+
Settings.llm = llm # Ensure Cerebras is the LLM being used
|
43 |
+
|
44 |
+
# Initialize Mixedbread Embedding model
|
45 |
+
mixedbread_api_key = os.getenv("MXBAI_API_KEY")
|
46 |
+
embed_model = MixedbreadAIEmbedding(api_key=mixedbread_api_key, model_name="mixedbread-ai/mxbai-embed-large-v1")
|
47 |
+
|
48 |
+
# Initialize Groq client for Whisper Large V3
|
49 |
+
groq_api_key = os.getenv("GROQ_API_KEY")
|
50 |
+
if not groq_api_key:
|
51 |
+
raise ValueError("GROQ_API_KEY is not set in environment variables.")
|
52 |
+
else:
|
53 |
+
print("Groq API key loaded successfully.")
|
54 |
+
client = Groq(api_key=groq_api_key) # Groq client initialization
|
55 |
+
|
56 |
+
# Function for audio transcription and translation (Whisper Large V3 from Groq)
|
57 |
+
def transcribe_or_translate_audio(audio_file, translate=False):
|
58 |
+
"""
|
59 |
+
Transcribes or translates audio using Whisper Large V3 via Groq API.
|
60 |
+
"""
|
61 |
+
try:
|
62 |
+
with open(audio_file, "rb") as file:
|
63 |
+
if translate:
|
64 |
+
result = client.audio.translations.create(
|
65 |
+
file=(audio_file, file.read()),
|
66 |
+
model="whisper-large-v3", # Use Groq Whisper Large V3
|
67 |
+
response_format="json",
|
68 |
+
temperature=0.0
|
69 |
+
)
|
70 |
+
return result.text
|
71 |
+
else:
|
72 |
+
result = client.audio.transcriptions.create(
|
73 |
+
file=(audio_file, file.read()),
|
74 |
+
model="whisper-large-v3", # Use Groq Whisper Large V3
|
75 |
+
response_format="json",
|
76 |
+
temperature=0.0
|
77 |
+
)
|
78 |
+
return result.text
|
79 |
+
except Exception as e:
|
80 |
+
return f"Error processing audio: {str(e)}"
|
81 |
+
|
82 |
+
# Function to load documents and create index
|
83 |
+
def load_documents(file_objs):
|
84 |
+
global index, query_engine
|
85 |
+
try:
|
86 |
+
if not file_objs:
|
87 |
+
return "Error: No files selected."
|
88 |
+
|
89 |
+
documents = []
|
90 |
+
document_names = []
|
91 |
+
for file_obj in file_objs:
|
92 |
+
file_name = os.path.basename(file_obj.name)
|
93 |
+
document_names.append(file_name)
|
94 |
+
loaded_docs = SimpleDirectoryReader(input_files=[file_obj.name]).load_data()
|
95 |
+
for doc in loaded_docs:
|
96 |
+
doc.metadata["source"] = file_name
|
97 |
+
documents.append(doc)
|
98 |
+
|
99 |
+
if not documents:
|
100 |
+
return "No documents found in the selected files."
|
101 |
+
|
102 |
+
index = VectorStoreIndex.from_documents(documents, llm=llm, embed_model=embed_model)
|
103 |
+
query_engine = index.as_query_engine()
|
104 |
+
|
105 |
+
return f"Successfully loaded {len(documents)} documents from the files: {', '.join(document_names)}"
|
106 |
+
except Exception as e:
|
107 |
+
return f"Error loading documents: {str(e)}"
|
108 |
+
|
109 |
+
async def perform_rag(query, history, audio_file=None, translate_audio=False):
|
110 |
+
global query_engine
|
111 |
+
if query_engine is None:
|
112 |
+
return history + [("Please load documents first.", None)], None # Tambahkan None untuk output audio
|
113 |
+
|
114 |
+
try:
|
115 |
+
# Handle audio input jika diberikan
|
116 |
+
if audio_file:
|
117 |
+
transcription = transcribe_or_translate_audio(audio_file, translate=translate_audio)
|
118 |
+
query = f"{query} {transcription}".strip()
|
119 |
+
|
120 |
+
response = await asyncio.to_thread(query_engine.query, query)
|
121 |
+
answer = str(response) # Dapatkan jawaban dari respons
|
122 |
+
|
123 |
+
# Jika dokumen relevan tersedia, tambahkan sumber tanpa label "Sources"
|
124 |
+
if hasattr(response, "get_documents"):
|
125 |
+
relevant_docs = response.get_documents()
|
126 |
+
if relevant_docs:
|
127 |
+
sources = "\n\n".join([f"{doc.metadata.get('source', 'No source available')}" for doc in relevant_docs])
|
128 |
+
else:
|
129 |
+
sources = ""
|
130 |
+
else:
|
131 |
+
sources = ""
|
132 |
+
|
133 |
+
# Gabungkan jawaban dengan sumber (jika ada) tanpa label tambahan
|
134 |
+
final_result = f"{answer}\n\n{sources}".strip()
|
135 |
+
|
136 |
+
# **Generate audio menggunakan MeloTTS**
|
137 |
+
output_audio_path = "output.wav"
|
138 |
+
model.tts_to_file(answer, model.hps.data.spk2id['EN-US'], output_audio_path, speed=1.0)
|
139 |
+
|
140 |
+
# Kembalikan history yang diperbarui dan file audio
|
141 |
+
return history + [(query, final_result)], output_audio_path
|
142 |
+
except Exception as e:
|
143 |
+
return history + [(query, f"Error processing query: {str(e)}")], None
|
144 |
+
|
145 |
+
# Function to clear the session and reset variables
|
146 |
+
def clear_all():
|
147 |
+
global index, query_engine
|
148 |
+
index = None
|
149 |
+
query_engine = None
|
150 |
+
return None, "", [], "" # Reset file input, load output, chatbot, and message input to default states
|
151 |
+
|
152 |
+
# Create the Gradio interface
|
153 |
+
with gr.Blocks(theme=gr.themes.Base(primary_hue="teal", secondary_hue="teal", neutral_hue="slate")) as demo:
|
154 |
+
gr.Markdown("# RAG Multi-file Chat Application with Speech-to-Text and Text-to-Speech")
|
155 |
+
|
156 |
+
chatbot = gr.Chatbot()
|
157 |
+
audio_output = gr.Audio(label="Response Audio", type="filepath")
|
158 |
+
with gr.Row():
|
159 |
+
file_input = gr.File(label="Select files to load", file_count="multiple")
|
160 |
+
load_btn = gr.Button("Load Documents")
|
161 |
+
load_output = gr.Textbox(label="Load Status")
|
162 |
+
with gr.Row():
|
163 |
+
msg = gr.Textbox(label="Enter your question")
|
164 |
+
audio_input = gr.Audio(type="filepath", label="RECORD")
|
165 |
+
translate_checkbox = gr.Checkbox(label="Translate Audio to English Text", value=False)
|
166 |
+
clear = gr.Button("Clear")
|
167 |
+
|
168 |
+
# Set up event handlers
|
169 |
+
load_btn.click(load_documents, inputs=[file_input], outputs=[load_output])
|
170 |
+
|
171 |
+
# Event handler untuk input teks (proses teks)
|
172 |
+
msg.submit(perform_rag, inputs=[msg, chatbot], outputs=[chatbot, audio_output]) # Tambahkan audio_output
|
173 |
+
|
174 |
+
# Event handler untuk input audio (proses audio)
|
175 |
+
audio_input.change(perform_rag, inputs=[msg, chatbot, audio_input, translate_checkbox], outputs=[chatbot, audio_output]) # Tambahkan audio_output
|
176 |
+
|
177 |
+
clear.click(clear_all, outputs=[file_input, load_output, chatbot, msg], queue=False)
|
178 |
+
|
179 |
+
# Run the app
|
180 |
+
if __name__ == "__main__":
|
181 |
+
demo.queue()
|
182 |
+
demo.launch()
|
melo/__init__.py
ADDED
File without changes
|
melo/__pycache__/__init__.cpython-312.pyc
ADDED
Binary file (162 Bytes). View file
|
|
melo/__pycache__/api.cpython-312.pyc
ADDED
Binary file (7.14 kB). View file
|
|
melo/__pycache__/attentions.cpython-312.pyc
ADDED
Binary file (22.1 kB). View file
|
|
melo/__pycache__/commons.cpython-312.pyc
ADDED
Binary file (9.49 kB). View file
|
|
melo/__pycache__/download_utils.cpython-312.pyc
ADDED
Binary file (3.96 kB). View file
|
|
melo/__pycache__/mel_processing.cpython-312.pyc
ADDED
Binary file (8.29 kB). View file
|
|
melo/__pycache__/models.cpython-312.pyc
ADDED
Binary file (42.6 kB). View file
|
|
melo/__pycache__/modules.cpython-312.pyc
ADDED
Binary file (25.1 kB). View file
|
|
melo/__pycache__/split_utils.cpython-312.pyc
ADDED
Binary file (8.89 kB). View file
|
|
melo/__pycache__/transforms.cpython-312.pyc
ADDED
Binary file (7.01 kB). View file
|
|
melo/__pycache__/utils.cpython-312.pyc
ADDED
Binary file (22.6 kB). View file
|
|
melo/api.py
ADDED
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import re
|
3 |
+
import json
|
4 |
+
import torch
|
5 |
+
import librosa
|
6 |
+
import soundfile
|
7 |
+
import torchaudio
|
8 |
+
import numpy as np
|
9 |
+
import torch.nn as nn
|
10 |
+
from tqdm import tqdm
|
11 |
+
import torch
|
12 |
+
|
13 |
+
from . import utils
|
14 |
+
from . import commons
|
15 |
+
from .models import SynthesizerTrn
|
16 |
+
from .split_utils import split_sentence
|
17 |
+
from .mel_processing import spectrogram_torch, spectrogram_torch_conv
|
18 |
+
from .download_utils import load_or_download_config, load_or_download_model
|
19 |
+
|
20 |
+
class TTS(nn.Module):
|
21 |
+
def __init__(self,
|
22 |
+
language,
|
23 |
+
device='auto',
|
24 |
+
use_hf=True,
|
25 |
+
config_path=None,
|
26 |
+
ckpt_path=None):
|
27 |
+
super().__init__()
|
28 |
+
if device == 'auto':
|
29 |
+
device = 'cpu'
|
30 |
+
if torch.cuda.is_available(): device = 'cuda'
|
31 |
+
if torch.backends.mps.is_available(): device = 'mps'
|
32 |
+
if 'cuda' in device:
|
33 |
+
assert torch.cuda.is_available()
|
34 |
+
|
35 |
+
# config_path =
|
36 |
+
hps = load_or_download_config(language, use_hf=use_hf, config_path=config_path)
|
37 |
+
|
38 |
+
num_languages = hps.num_languages
|
39 |
+
num_tones = hps.num_tones
|
40 |
+
symbols = hps.symbols
|
41 |
+
|
42 |
+
model = SynthesizerTrn(
|
43 |
+
len(symbols),
|
44 |
+
hps.data.filter_length // 2 + 1,
|
45 |
+
hps.train.segment_size // hps.data.hop_length,
|
46 |
+
n_speakers=hps.data.n_speakers,
|
47 |
+
num_tones=num_tones,
|
48 |
+
num_languages=num_languages,
|
49 |
+
**hps.model,
|
50 |
+
).to(device)
|
51 |
+
|
52 |
+
model.eval()
|
53 |
+
self.model = model
|
54 |
+
self.symbol_to_id = {s: i for i, s in enumerate(symbols)}
|
55 |
+
self.hps = hps
|
56 |
+
self.device = device
|
57 |
+
|
58 |
+
# load state_dict
|
59 |
+
checkpoint_dict = load_or_download_model(language, device, use_hf=use_hf, ckpt_path=ckpt_path)
|
60 |
+
self.model.load_state_dict(checkpoint_dict['model'], strict=True)
|
61 |
+
|
62 |
+
language = language.split('_')[0]
|
63 |
+
self.language = 'ZH_MIX_EN' if language == 'ZH' else language # we support a ZH_MIX_EN model
|
64 |
+
|
65 |
+
@staticmethod
|
66 |
+
def audio_numpy_concat(segment_data_list, sr, speed=1.):
|
67 |
+
audio_segments = []
|
68 |
+
for segment_data in segment_data_list:
|
69 |
+
audio_segments += segment_data.reshape(-1).tolist()
|
70 |
+
audio_segments += [0] * int((sr * 0.05) / speed)
|
71 |
+
audio_segments = np.array(audio_segments).astype(np.float32)
|
72 |
+
return audio_segments
|
73 |
+
|
74 |
+
@staticmethod
|
75 |
+
def split_sentences_into_pieces(text, language, quiet=False):
|
76 |
+
texts = split_sentence(text, language_str=language)
|
77 |
+
if not quiet:
|
78 |
+
print(" > Text split to sentences.")
|
79 |
+
print('\n'.join(texts))
|
80 |
+
print(" > ===========================")
|
81 |
+
return texts
|
82 |
+
|
83 |
+
def tts_to_file(self, text, speaker_id, output_path=None, sdp_ratio=0.2, noise_scale=0.6, noise_scale_w=0.8, speed=1.0, pbar=None, format=None, position=None, quiet=False,):
|
84 |
+
language = self.language
|
85 |
+
texts = self.split_sentences_into_pieces(text, language, quiet)
|
86 |
+
audio_list = []
|
87 |
+
if pbar:
|
88 |
+
tx = pbar(texts)
|
89 |
+
else:
|
90 |
+
if position:
|
91 |
+
tx = tqdm(texts, position=position)
|
92 |
+
elif quiet:
|
93 |
+
tx = texts
|
94 |
+
else:
|
95 |
+
tx = tqdm(texts)
|
96 |
+
for t in tx:
|
97 |
+
if language in ['EN', 'ZH_MIX_EN']:
|
98 |
+
t = re.sub(r'([a-z])([A-Z])', r'\1 \2', t)
|
99 |
+
device = self.device
|
100 |
+
bert, ja_bert, phones, tones, lang_ids = utils.get_text_for_tts_infer(t, language, self.hps, device, self.symbol_to_id)
|
101 |
+
with torch.no_grad():
|
102 |
+
x_tst = phones.to(device).unsqueeze(0)
|
103 |
+
tones = tones.to(device).unsqueeze(0)
|
104 |
+
lang_ids = lang_ids.to(device).unsqueeze(0)
|
105 |
+
bert = bert.to(device).unsqueeze(0)
|
106 |
+
ja_bert = ja_bert.to(device).unsqueeze(0)
|
107 |
+
x_tst_lengths = torch.LongTensor([phones.size(0)]).to(device)
|
108 |
+
del phones
|
109 |
+
speakers = torch.LongTensor([speaker_id]).to(device)
|
110 |
+
audio = self.model.infer(
|
111 |
+
x_tst,
|
112 |
+
x_tst_lengths,
|
113 |
+
speakers,
|
114 |
+
tones,
|
115 |
+
lang_ids,
|
116 |
+
bert,
|
117 |
+
ja_bert,
|
118 |
+
sdp_ratio=sdp_ratio,
|
119 |
+
noise_scale=noise_scale,
|
120 |
+
noise_scale_w=noise_scale_w,
|
121 |
+
length_scale=1. / speed,
|
122 |
+
)[0][0, 0].data.cpu().float().numpy()
|
123 |
+
del x_tst, tones, lang_ids, bert, ja_bert, x_tst_lengths, speakers
|
124 |
+
#
|
125 |
+
audio_list.append(audio)
|
126 |
+
torch.cuda.empty_cache()
|
127 |
+
audio = self.audio_numpy_concat(audio_list, sr=self.hps.data.sampling_rate, speed=speed)
|
128 |
+
|
129 |
+
if output_path is None:
|
130 |
+
return audio
|
131 |
+
else:
|
132 |
+
if format:
|
133 |
+
soundfile.write(output_path, audio, self.hps.data.sampling_rate, format=format)
|
134 |
+
else:
|
135 |
+
soundfile.write(output_path, audio, self.hps.data.sampling_rate)
|
melo/app.py
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# WebUI by mrfakename <X @realmrfakename / HF @mrfakename>
|
2 |
+
# Demo also available on HF Spaces: https://huggingface.co/spaces/mrfakename/MeloTTS
|
3 |
+
import gradio as gr
|
4 |
+
import os, torch, io
|
5 |
+
# os.system('python -m unidic download')
|
6 |
+
print("Make sure you've downloaded unidic (python -m unidic download) for this WebUI to work.")
|
7 |
+
from melo.api import TTS
|
8 |
+
speed = 1.0
|
9 |
+
import tempfile
|
10 |
+
import click
|
11 |
+
device = 'auto'
|
12 |
+
models = {
|
13 |
+
'EN': TTS(language='EN', device=device),
|
14 |
+
'ES': TTS(language='ES', device=device),
|
15 |
+
'FR': TTS(language='FR', device=device),
|
16 |
+
'ZH': TTS(language='ZH', device=device),
|
17 |
+
'JP': TTS(language='JP', device=device),
|
18 |
+
'KR': TTS(language='KR', device=device),
|
19 |
+
}
|
20 |
+
speaker_ids = models['EN'].hps.data.spk2id
|
21 |
+
|
22 |
+
default_text_dict = {
|
23 |
+
'EN': 'The field of text-to-speech has seen rapid development recently.',
|
24 |
+
'ES': 'El campo de la conversión de texto a voz ha experimentado un rápido desarrollo recientemente.',
|
25 |
+
'FR': 'Le domaine de la synthèse vocale a connu un développement rapide récemment',
|
26 |
+
'ZH': 'text-to-speech 领域近年来发展迅速',
|
27 |
+
'JP': 'テキスト読み上げの分野は最近急速な発展を遂げています',
|
28 |
+
'KR': '최근 텍스트 음성 변환 분야가 급속도로 발전하고 있습니다.',
|
29 |
+
}
|
30 |
+
|
31 |
+
def synthesize(speaker, text, speed, language, progress=gr.Progress()):
|
32 |
+
bio = io.BytesIO()
|
33 |
+
models[language].tts_to_file(text, models[language].hps.data.spk2id[speaker], bio, speed=speed, pbar=progress.tqdm, format='wav')
|
34 |
+
return bio.getvalue()
|
35 |
+
def load_speakers(language, text):
|
36 |
+
if text in list(default_text_dict.values()):
|
37 |
+
newtext = default_text_dict[language]
|
38 |
+
else:
|
39 |
+
newtext = text
|
40 |
+
return gr.update(value=list(models[language].hps.data.spk2id.keys())[0], choices=list(models[language].hps.data.spk2id.keys())), newtext
|
41 |
+
with gr.Blocks() as demo:
|
42 |
+
gr.Markdown('# MeloTTS WebUI\n\nA WebUI for MeloTTS.')
|
43 |
+
with gr.Group():
|
44 |
+
speaker = gr.Dropdown(speaker_ids.keys(), interactive=True, value='EN-US', label='Speaker')
|
45 |
+
language = gr.Radio(['EN', 'ES', 'FR', 'ZH', 'JP', 'KR'], label='Language', value='EN')
|
46 |
+
speed = gr.Slider(label='Speed', minimum=0.1, maximum=10.0, value=1.0, interactive=True, step=0.1)
|
47 |
+
text = gr.Textbox(label="Text to speak", value=default_text_dict['EN'])
|
48 |
+
language.input(load_speakers, inputs=[language, text], outputs=[speaker, text])
|
49 |
+
btn = gr.Button('Synthesize', variant='primary')
|
50 |
+
aud = gr.Audio(interactive=False)
|
51 |
+
btn.click(synthesize, inputs=[speaker, text, speed, language], outputs=[aud])
|
52 |
+
gr.Markdown('WebUI by [mrfakename](https://twitter.com/realmrfakename).')
|
53 |
+
@click.command()
|
54 |
+
@click.option('--share', '-s', is_flag=True, show_default=True, default=False, help="Expose a publicly-accessible shared Gradio link usable by anyone with the link. Only share the link with people you trust.")
|
55 |
+
@click.option('--host', '-h', default=None)
|
56 |
+
@click.option('--port', '-p', type=int, default=None)
|
57 |
+
def main(share, host, port):
|
58 |
+
demo.queue(api_open=False).launch(show_api=False, share=share, server_name=host, server_port=port)
|
59 |
+
|
60 |
+
if __name__ == "__main__":
|
61 |
+
main()
|
melo/attentions.py
ADDED
@@ -0,0 +1,459 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import math
|
2 |
+
import torch
|
3 |
+
from torch import nn
|
4 |
+
from torch.nn import functional as F
|
5 |
+
|
6 |
+
from . import commons
|
7 |
+
import logging
|
8 |
+
|
9 |
+
logger = logging.getLogger(__name__)
|
10 |
+
|
11 |
+
|
12 |
+
class LayerNorm(nn.Module):
|
13 |
+
def __init__(self, channels, eps=1e-5):
|
14 |
+
super().__init__()
|
15 |
+
self.channels = channels
|
16 |
+
self.eps = eps
|
17 |
+
|
18 |
+
self.gamma = nn.Parameter(torch.ones(channels))
|
19 |
+
self.beta = nn.Parameter(torch.zeros(channels))
|
20 |
+
|
21 |
+
def forward(self, x):
|
22 |
+
x = x.transpose(1, -1)
|
23 |
+
x = F.layer_norm(x, (self.channels,), self.gamma, self.beta, self.eps)
|
24 |
+
return x.transpose(1, -1)
|
25 |
+
|
26 |
+
|
27 |
+
@torch.jit.script
|
28 |
+
def fused_add_tanh_sigmoid_multiply(input_a, input_b, n_channels):
|
29 |
+
n_channels_int = n_channels[0]
|
30 |
+
in_act = input_a + input_b
|
31 |
+
t_act = torch.tanh(in_act[:, :n_channels_int, :])
|
32 |
+
s_act = torch.sigmoid(in_act[:, n_channels_int:, :])
|
33 |
+
acts = t_act * s_act
|
34 |
+
return acts
|
35 |
+
|
36 |
+
|
37 |
+
class Encoder(nn.Module):
|
38 |
+
def __init__(
|
39 |
+
self,
|
40 |
+
hidden_channels,
|
41 |
+
filter_channels,
|
42 |
+
n_heads,
|
43 |
+
n_layers,
|
44 |
+
kernel_size=1,
|
45 |
+
p_dropout=0.0,
|
46 |
+
window_size=4,
|
47 |
+
isflow=True,
|
48 |
+
**kwargs
|
49 |
+
):
|
50 |
+
super().__init__()
|
51 |
+
self.hidden_channels = hidden_channels
|
52 |
+
self.filter_channels = filter_channels
|
53 |
+
self.n_heads = n_heads
|
54 |
+
self.n_layers = n_layers
|
55 |
+
self.kernel_size = kernel_size
|
56 |
+
self.p_dropout = p_dropout
|
57 |
+
self.window_size = window_size
|
58 |
+
|
59 |
+
self.cond_layer_idx = self.n_layers
|
60 |
+
if "gin_channels" in kwargs:
|
61 |
+
self.gin_channels = kwargs["gin_channels"]
|
62 |
+
if self.gin_channels != 0:
|
63 |
+
self.spk_emb_linear = nn.Linear(self.gin_channels, self.hidden_channels)
|
64 |
+
self.cond_layer_idx = (
|
65 |
+
kwargs["cond_layer_idx"] if "cond_layer_idx" in kwargs else 2
|
66 |
+
)
|
67 |
+
assert (
|
68 |
+
self.cond_layer_idx < self.n_layers
|
69 |
+
), "cond_layer_idx should be less than n_layers"
|
70 |
+
self.drop = nn.Dropout(p_dropout)
|
71 |
+
self.attn_layers = nn.ModuleList()
|
72 |
+
self.norm_layers_1 = nn.ModuleList()
|
73 |
+
self.ffn_layers = nn.ModuleList()
|
74 |
+
self.norm_layers_2 = nn.ModuleList()
|
75 |
+
|
76 |
+
for i in range(self.n_layers):
|
77 |
+
self.attn_layers.append(
|
78 |
+
MultiHeadAttention(
|
79 |
+
hidden_channels,
|
80 |
+
hidden_channels,
|
81 |
+
n_heads,
|
82 |
+
p_dropout=p_dropout,
|
83 |
+
window_size=window_size,
|
84 |
+
)
|
85 |
+
)
|
86 |
+
self.norm_layers_1.append(LayerNorm(hidden_channels))
|
87 |
+
self.ffn_layers.append(
|
88 |
+
FFN(
|
89 |
+
hidden_channels,
|
90 |
+
hidden_channels,
|
91 |
+
filter_channels,
|
92 |
+
kernel_size,
|
93 |
+
p_dropout=p_dropout,
|
94 |
+
)
|
95 |
+
)
|
96 |
+
self.norm_layers_2.append(LayerNorm(hidden_channels))
|
97 |
+
|
98 |
+
def forward(self, x, x_mask, g=None):
|
99 |
+
attn_mask = x_mask.unsqueeze(2) * x_mask.unsqueeze(-1)
|
100 |
+
x = x * x_mask
|
101 |
+
for i in range(self.n_layers):
|
102 |
+
if i == self.cond_layer_idx and g is not None:
|
103 |
+
g = self.spk_emb_linear(g.transpose(1, 2))
|
104 |
+
g = g.transpose(1, 2)
|
105 |
+
x = x + g
|
106 |
+
x = x * x_mask
|
107 |
+
y = self.attn_layers[i](x, x, attn_mask)
|
108 |
+
y = self.drop(y)
|
109 |
+
x = self.norm_layers_1[i](x + y)
|
110 |
+
|
111 |
+
y = self.ffn_layers[i](x, x_mask)
|
112 |
+
y = self.drop(y)
|
113 |
+
x = self.norm_layers_2[i](x + y)
|
114 |
+
x = x * x_mask
|
115 |
+
return x
|
116 |
+
|
117 |
+
|
118 |
+
class Decoder(nn.Module):
|
119 |
+
def __init__(
|
120 |
+
self,
|
121 |
+
hidden_channels,
|
122 |
+
filter_channels,
|
123 |
+
n_heads,
|
124 |
+
n_layers,
|
125 |
+
kernel_size=1,
|
126 |
+
p_dropout=0.0,
|
127 |
+
proximal_bias=False,
|
128 |
+
proximal_init=True,
|
129 |
+
**kwargs
|
130 |
+
):
|
131 |
+
super().__init__()
|
132 |
+
self.hidden_channels = hidden_channels
|
133 |
+
self.filter_channels = filter_channels
|
134 |
+
self.n_heads = n_heads
|
135 |
+
self.n_layers = n_layers
|
136 |
+
self.kernel_size = kernel_size
|
137 |
+
self.p_dropout = p_dropout
|
138 |
+
self.proximal_bias = proximal_bias
|
139 |
+
self.proximal_init = proximal_init
|
140 |
+
|
141 |
+
self.drop = nn.Dropout(p_dropout)
|
142 |
+
self.self_attn_layers = nn.ModuleList()
|
143 |
+
self.norm_layers_0 = nn.ModuleList()
|
144 |
+
self.encdec_attn_layers = nn.ModuleList()
|
145 |
+
self.norm_layers_1 = nn.ModuleList()
|
146 |
+
self.ffn_layers = nn.ModuleList()
|
147 |
+
self.norm_layers_2 = nn.ModuleList()
|
148 |
+
for i in range(self.n_layers):
|
149 |
+
self.self_attn_layers.append(
|
150 |
+
MultiHeadAttention(
|
151 |
+
hidden_channels,
|
152 |
+
hidden_channels,
|
153 |
+
n_heads,
|
154 |
+
p_dropout=p_dropout,
|
155 |
+
proximal_bias=proximal_bias,
|
156 |
+
proximal_init=proximal_init,
|
157 |
+
)
|
158 |
+
)
|
159 |
+
self.norm_layers_0.append(LayerNorm(hidden_channels))
|
160 |
+
self.encdec_attn_layers.append(
|
161 |
+
MultiHeadAttention(
|
162 |
+
hidden_channels, hidden_channels, n_heads, p_dropout=p_dropout
|
163 |
+
)
|
164 |
+
)
|
165 |
+
self.norm_layers_1.append(LayerNorm(hidden_channels))
|
166 |
+
self.ffn_layers.append(
|
167 |
+
FFN(
|
168 |
+
hidden_channels,
|
169 |
+
hidden_channels,
|
170 |
+
filter_channels,
|
171 |
+
kernel_size,
|
172 |
+
p_dropout=p_dropout,
|
173 |
+
causal=True,
|
174 |
+
)
|
175 |
+
)
|
176 |
+
self.norm_layers_2.append(LayerNorm(hidden_channels))
|
177 |
+
|
178 |
+
def forward(self, x, x_mask, h, h_mask):
|
179 |
+
"""
|
180 |
+
x: decoder input
|
181 |
+
h: encoder output
|
182 |
+
"""
|
183 |
+
self_attn_mask = commons.subsequent_mask(x_mask.size(2)).to(
|
184 |
+
device=x.device, dtype=x.dtype
|
185 |
+
)
|
186 |
+
encdec_attn_mask = h_mask.unsqueeze(2) * x_mask.unsqueeze(-1)
|
187 |
+
x = x * x_mask
|
188 |
+
for i in range(self.n_layers):
|
189 |
+
y = self.self_attn_layers[i](x, x, self_attn_mask)
|
190 |
+
y = self.drop(y)
|
191 |
+
x = self.norm_layers_0[i](x + y)
|
192 |
+
|
193 |
+
y = self.encdec_attn_layers[i](x, h, encdec_attn_mask)
|
194 |
+
y = self.drop(y)
|
195 |
+
x = self.norm_layers_1[i](x + y)
|
196 |
+
|
197 |
+
y = self.ffn_layers[i](x, x_mask)
|
198 |
+
y = self.drop(y)
|
199 |
+
x = self.norm_layers_2[i](x + y)
|
200 |
+
x = x * x_mask
|
201 |
+
return x
|
202 |
+
|
203 |
+
|
204 |
+
class MultiHeadAttention(nn.Module):
|
205 |
+
def __init__(
|
206 |
+
self,
|
207 |
+
channels,
|
208 |
+
out_channels,
|
209 |
+
n_heads,
|
210 |
+
p_dropout=0.0,
|
211 |
+
window_size=None,
|
212 |
+
heads_share=True,
|
213 |
+
block_length=None,
|
214 |
+
proximal_bias=False,
|
215 |
+
proximal_init=False,
|
216 |
+
):
|
217 |
+
super().__init__()
|
218 |
+
assert channels % n_heads == 0
|
219 |
+
|
220 |
+
self.channels = channels
|
221 |
+
self.out_channels = out_channels
|
222 |
+
self.n_heads = n_heads
|
223 |
+
self.p_dropout = p_dropout
|
224 |
+
self.window_size = window_size
|
225 |
+
self.heads_share = heads_share
|
226 |
+
self.block_length = block_length
|
227 |
+
self.proximal_bias = proximal_bias
|
228 |
+
self.proximal_init = proximal_init
|
229 |
+
self.attn = None
|
230 |
+
|
231 |
+
self.k_channels = channels // n_heads
|
232 |
+
self.conv_q = nn.Conv1d(channels, channels, 1)
|
233 |
+
self.conv_k = nn.Conv1d(channels, channels, 1)
|
234 |
+
self.conv_v = nn.Conv1d(channels, channels, 1)
|
235 |
+
self.conv_o = nn.Conv1d(channels, out_channels, 1)
|
236 |
+
self.drop = nn.Dropout(p_dropout)
|
237 |
+
|
238 |
+
if window_size is not None:
|
239 |
+
n_heads_rel = 1 if heads_share else n_heads
|
240 |
+
rel_stddev = self.k_channels**-0.5
|
241 |
+
self.emb_rel_k = nn.Parameter(
|
242 |
+
torch.randn(n_heads_rel, window_size * 2 + 1, self.k_channels)
|
243 |
+
* rel_stddev
|
244 |
+
)
|
245 |
+
self.emb_rel_v = nn.Parameter(
|
246 |
+
torch.randn(n_heads_rel, window_size * 2 + 1, self.k_channels)
|
247 |
+
* rel_stddev
|
248 |
+
)
|
249 |
+
|
250 |
+
nn.init.xavier_uniform_(self.conv_q.weight)
|
251 |
+
nn.init.xavier_uniform_(self.conv_k.weight)
|
252 |
+
nn.init.xavier_uniform_(self.conv_v.weight)
|
253 |
+
if proximal_init:
|
254 |
+
with torch.no_grad():
|
255 |
+
self.conv_k.weight.copy_(self.conv_q.weight)
|
256 |
+
self.conv_k.bias.copy_(self.conv_q.bias)
|
257 |
+
|
258 |
+
def forward(self, x, c, attn_mask=None):
|
259 |
+
q = self.conv_q(x)
|
260 |
+
k = self.conv_k(c)
|
261 |
+
v = self.conv_v(c)
|
262 |
+
|
263 |
+
x, self.attn = self.attention(q, k, v, mask=attn_mask)
|
264 |
+
|
265 |
+
x = self.conv_o(x)
|
266 |
+
return x
|
267 |
+
|
268 |
+
def attention(self, query, key, value, mask=None):
|
269 |
+
# reshape [b, d, t] -> [b, n_h, t, d_k]
|
270 |
+
b, d, t_s, t_t = (*key.size(), query.size(2))
|
271 |
+
query = query.view(b, self.n_heads, self.k_channels, t_t).transpose(2, 3)
|
272 |
+
key = key.view(b, self.n_heads, self.k_channels, t_s).transpose(2, 3)
|
273 |
+
value = value.view(b, self.n_heads, self.k_channels, t_s).transpose(2, 3)
|
274 |
+
|
275 |
+
scores = torch.matmul(query / math.sqrt(self.k_channels), key.transpose(-2, -1))
|
276 |
+
if self.window_size is not None:
|
277 |
+
assert (
|
278 |
+
t_s == t_t
|
279 |
+
), "Relative attention is only available for self-attention."
|
280 |
+
key_relative_embeddings = self._get_relative_embeddings(self.emb_rel_k, t_s)
|
281 |
+
rel_logits = self._matmul_with_relative_keys(
|
282 |
+
query / math.sqrt(self.k_channels), key_relative_embeddings
|
283 |
+
)
|
284 |
+
scores_local = self._relative_position_to_absolute_position(rel_logits)
|
285 |
+
scores = scores + scores_local
|
286 |
+
if self.proximal_bias:
|
287 |
+
assert t_s == t_t, "Proximal bias is only available for self-attention."
|
288 |
+
scores = scores + self._attention_bias_proximal(t_s).to(
|
289 |
+
device=scores.device, dtype=scores.dtype
|
290 |
+
)
|
291 |
+
if mask is not None:
|
292 |
+
scores = scores.masked_fill(mask == 0, -1e4)
|
293 |
+
if self.block_length is not None:
|
294 |
+
assert (
|
295 |
+
t_s == t_t
|
296 |
+
), "Local attention is only available for self-attention."
|
297 |
+
block_mask = (
|
298 |
+
torch.ones_like(scores)
|
299 |
+
.triu(-self.block_length)
|
300 |
+
.tril(self.block_length)
|
301 |
+
)
|
302 |
+
scores = scores.masked_fill(block_mask == 0, -1e4)
|
303 |
+
p_attn = F.softmax(scores, dim=-1) # [b, n_h, t_t, t_s]
|
304 |
+
p_attn = self.drop(p_attn)
|
305 |
+
output = torch.matmul(p_attn, value)
|
306 |
+
if self.window_size is not None:
|
307 |
+
relative_weights = self._absolute_position_to_relative_position(p_attn)
|
308 |
+
value_relative_embeddings = self._get_relative_embeddings(
|
309 |
+
self.emb_rel_v, t_s
|
310 |
+
)
|
311 |
+
output = output + self._matmul_with_relative_values(
|
312 |
+
relative_weights, value_relative_embeddings
|
313 |
+
)
|
314 |
+
output = (
|
315 |
+
output.transpose(2, 3).contiguous().view(b, d, t_t)
|
316 |
+
) # [b, n_h, t_t, d_k] -> [b, d, t_t]
|
317 |
+
return output, p_attn
|
318 |
+
|
319 |
+
def _matmul_with_relative_values(self, x, y):
|
320 |
+
"""
|
321 |
+
x: [b, h, l, m]
|
322 |
+
y: [h or 1, m, d]
|
323 |
+
ret: [b, h, l, d]
|
324 |
+
"""
|
325 |
+
ret = torch.matmul(x, y.unsqueeze(0))
|
326 |
+
return ret
|
327 |
+
|
328 |
+
def _matmul_with_relative_keys(self, x, y):
|
329 |
+
"""
|
330 |
+
x: [b, h, l, d]
|
331 |
+
y: [h or 1, m, d]
|
332 |
+
ret: [b, h, l, m]
|
333 |
+
"""
|
334 |
+
ret = torch.matmul(x, y.unsqueeze(0).transpose(-2, -1))
|
335 |
+
return ret
|
336 |
+
|
337 |
+
def _get_relative_embeddings(self, relative_embeddings, length):
|
338 |
+
2 * self.window_size + 1
|
339 |
+
# Pad first before slice to avoid using cond ops.
|
340 |
+
pad_length = max(length - (self.window_size + 1), 0)
|
341 |
+
slice_start_position = max((self.window_size + 1) - length, 0)
|
342 |
+
slice_end_position = slice_start_position + 2 * length - 1
|
343 |
+
if pad_length > 0:
|
344 |
+
padded_relative_embeddings = F.pad(
|
345 |
+
relative_embeddings,
|
346 |
+
commons.convert_pad_shape([[0, 0], [pad_length, pad_length], [0, 0]]),
|
347 |
+
)
|
348 |
+
else:
|
349 |
+
padded_relative_embeddings = relative_embeddings
|
350 |
+
used_relative_embeddings = padded_relative_embeddings[
|
351 |
+
:, slice_start_position:slice_end_position
|
352 |
+
]
|
353 |
+
return used_relative_embeddings
|
354 |
+
|
355 |
+
def _relative_position_to_absolute_position(self, x):
|
356 |
+
"""
|
357 |
+
x: [b, h, l, 2*l-1]
|
358 |
+
ret: [b, h, l, l]
|
359 |
+
"""
|
360 |
+
batch, heads, length, _ = x.size()
|
361 |
+
# Concat columns of pad to shift from relative to absolute indexing.
|
362 |
+
x = F.pad(x, commons.convert_pad_shape([[0, 0], [0, 0], [0, 0], [0, 1]]))
|
363 |
+
|
364 |
+
# Concat extra elements so to add up to shape (len+1, 2*len-1).
|
365 |
+
x_flat = x.view([batch, heads, length * 2 * length])
|
366 |
+
x_flat = F.pad(
|
367 |
+
x_flat, commons.convert_pad_shape([[0, 0], [0, 0], [0, length - 1]])
|
368 |
+
)
|
369 |
+
|
370 |
+
# Reshape and slice out the padded elements.
|
371 |
+
x_final = x_flat.view([batch, heads, length + 1, 2 * length - 1])[
|
372 |
+
:, :, :length, length - 1 :
|
373 |
+
]
|
374 |
+
return x_final
|
375 |
+
|
376 |
+
def _absolute_position_to_relative_position(self, x):
|
377 |
+
"""
|
378 |
+
x: [b, h, l, l]
|
379 |
+
ret: [b, h, l, 2*l-1]
|
380 |
+
"""
|
381 |
+
batch, heads, length, _ = x.size()
|
382 |
+
# pad along column
|
383 |
+
x = F.pad(
|
384 |
+
x, commons.convert_pad_shape([[0, 0], [0, 0], [0, 0], [0, length - 1]])
|
385 |
+
)
|
386 |
+
x_flat = x.view([batch, heads, length**2 + length * (length - 1)])
|
387 |
+
# add 0's in the beginning that will skew the elements after reshape
|
388 |
+
x_flat = F.pad(x_flat, commons.convert_pad_shape([[0, 0], [0, 0], [length, 0]]))
|
389 |
+
x_final = x_flat.view([batch, heads, length, 2 * length])[:, :, :, 1:]
|
390 |
+
return x_final
|
391 |
+
|
392 |
+
def _attention_bias_proximal(self, length):
|
393 |
+
"""Bias for self-attention to encourage attention to close positions.
|
394 |
+
Args:
|
395 |
+
length: an integer scalar.
|
396 |
+
Returns:
|
397 |
+
a Tensor with shape [1, 1, length, length]
|
398 |
+
"""
|
399 |
+
r = torch.arange(length, dtype=torch.float32)
|
400 |
+
diff = torch.unsqueeze(r, 0) - torch.unsqueeze(r, 1)
|
401 |
+
return torch.unsqueeze(torch.unsqueeze(-torch.log1p(torch.abs(diff)), 0), 0)
|
402 |
+
|
403 |
+
|
404 |
+
class FFN(nn.Module):
|
405 |
+
def __init__(
|
406 |
+
self,
|
407 |
+
in_channels,
|
408 |
+
out_channels,
|
409 |
+
filter_channels,
|
410 |
+
kernel_size,
|
411 |
+
p_dropout=0.0,
|
412 |
+
activation=None,
|
413 |
+
causal=False,
|
414 |
+
):
|
415 |
+
super().__init__()
|
416 |
+
self.in_channels = in_channels
|
417 |
+
self.out_channels = out_channels
|
418 |
+
self.filter_channels = filter_channels
|
419 |
+
self.kernel_size = kernel_size
|
420 |
+
self.p_dropout = p_dropout
|
421 |
+
self.activation = activation
|
422 |
+
self.causal = causal
|
423 |
+
|
424 |
+
if causal:
|
425 |
+
self.padding = self._causal_padding
|
426 |
+
else:
|
427 |
+
self.padding = self._same_padding
|
428 |
+
|
429 |
+
self.conv_1 = nn.Conv1d(in_channels, filter_channels, kernel_size)
|
430 |
+
self.conv_2 = nn.Conv1d(filter_channels, out_channels, kernel_size)
|
431 |
+
self.drop = nn.Dropout(p_dropout)
|
432 |
+
|
433 |
+
def forward(self, x, x_mask):
|
434 |
+
x = self.conv_1(self.padding(x * x_mask))
|
435 |
+
if self.activation == "gelu":
|
436 |
+
x = x * torch.sigmoid(1.702 * x)
|
437 |
+
else:
|
438 |
+
x = torch.relu(x)
|
439 |
+
x = self.drop(x)
|
440 |
+
x = self.conv_2(self.padding(x * x_mask))
|
441 |
+
return x * x_mask
|
442 |
+
|
443 |
+
def _causal_padding(self, x):
|
444 |
+
if self.kernel_size == 1:
|
445 |
+
return x
|
446 |
+
pad_l = self.kernel_size - 1
|
447 |
+
pad_r = 0
|
448 |
+
padding = [[0, 0], [0, 0], [pad_l, pad_r]]
|
449 |
+
x = F.pad(x, commons.convert_pad_shape(padding))
|
450 |
+
return x
|
451 |
+
|
452 |
+
def _same_padding(self, x):
|
453 |
+
if self.kernel_size == 1:
|
454 |
+
return x
|
455 |
+
pad_l = (self.kernel_size - 1) // 2
|
456 |
+
pad_r = self.kernel_size // 2
|
457 |
+
padding = [[0, 0], [0, 0], [pad_l, pad_r]]
|
458 |
+
x = F.pad(x, commons.convert_pad_shape(padding))
|
459 |
+
return x
|
melo/commons.py
ADDED
@@ -0,0 +1,160 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import math
|
2 |
+
import torch
|
3 |
+
from torch.nn import functional as F
|
4 |
+
|
5 |
+
|
6 |
+
def init_weights(m, mean=0.0, std=0.01):
|
7 |
+
classname = m.__class__.__name__
|
8 |
+
if classname.find("Conv") != -1:
|
9 |
+
m.weight.data.normal_(mean, std)
|
10 |
+
|
11 |
+
|
12 |
+
def get_padding(kernel_size, dilation=1):
|
13 |
+
return int((kernel_size * dilation - dilation) / 2)
|
14 |
+
|
15 |
+
|
16 |
+
def convert_pad_shape(pad_shape):
|
17 |
+
layer = pad_shape[::-1]
|
18 |
+
pad_shape = [item for sublist in layer for item in sublist]
|
19 |
+
return pad_shape
|
20 |
+
|
21 |
+
|
22 |
+
def intersperse(lst, item):
|
23 |
+
result = [item] * (len(lst) * 2 + 1)
|
24 |
+
result[1::2] = lst
|
25 |
+
return result
|
26 |
+
|
27 |
+
|
28 |
+
def kl_divergence(m_p, logs_p, m_q, logs_q):
|
29 |
+
"""KL(P||Q)"""
|
30 |
+
kl = (logs_q - logs_p) - 0.5
|
31 |
+
kl += (
|
32 |
+
0.5 * (torch.exp(2.0 * logs_p) + ((m_p - m_q) ** 2)) * torch.exp(-2.0 * logs_q)
|
33 |
+
)
|
34 |
+
return kl
|
35 |
+
|
36 |
+
|
37 |
+
def rand_gumbel(shape):
|
38 |
+
"""Sample from the Gumbel distribution, protect from overflows."""
|
39 |
+
uniform_samples = torch.rand(shape) * 0.99998 + 0.00001
|
40 |
+
return -torch.log(-torch.log(uniform_samples))
|
41 |
+
|
42 |
+
|
43 |
+
def rand_gumbel_like(x):
|
44 |
+
g = rand_gumbel(x.size()).to(dtype=x.dtype, device=x.device)
|
45 |
+
return g
|
46 |
+
|
47 |
+
|
48 |
+
def slice_segments(x, ids_str, segment_size=4):
|
49 |
+
ret = torch.zeros_like(x[:, :, :segment_size])
|
50 |
+
for i in range(x.size(0)):
|
51 |
+
idx_str = ids_str[i]
|
52 |
+
idx_end = idx_str + segment_size
|
53 |
+
ret[i] = x[i, :, idx_str:idx_end]
|
54 |
+
return ret
|
55 |
+
|
56 |
+
|
57 |
+
def rand_slice_segments(x, x_lengths=None, segment_size=4):
|
58 |
+
b, d, t = x.size()
|
59 |
+
if x_lengths is None:
|
60 |
+
x_lengths = t
|
61 |
+
ids_str_max = x_lengths - segment_size + 1
|
62 |
+
ids_str = (torch.rand([b]).to(device=x.device) * ids_str_max).to(dtype=torch.long)
|
63 |
+
ret = slice_segments(x, ids_str, segment_size)
|
64 |
+
return ret, ids_str
|
65 |
+
|
66 |
+
|
67 |
+
def get_timing_signal_1d(length, channels, min_timescale=1.0, max_timescale=1.0e4):
|
68 |
+
position = torch.arange(length, dtype=torch.float)
|
69 |
+
num_timescales = channels // 2
|
70 |
+
log_timescale_increment = math.log(float(max_timescale) / float(min_timescale)) / (
|
71 |
+
num_timescales - 1
|
72 |
+
)
|
73 |
+
inv_timescales = min_timescale * torch.exp(
|
74 |
+
torch.arange(num_timescales, dtype=torch.float) * -log_timescale_increment
|
75 |
+
)
|
76 |
+
scaled_time = position.unsqueeze(0) * inv_timescales.unsqueeze(1)
|
77 |
+
signal = torch.cat([torch.sin(scaled_time), torch.cos(scaled_time)], 0)
|
78 |
+
signal = F.pad(signal, [0, 0, 0, channels % 2])
|
79 |
+
signal = signal.view(1, channels, length)
|
80 |
+
return signal
|
81 |
+
|
82 |
+
|
83 |
+
def add_timing_signal_1d(x, min_timescale=1.0, max_timescale=1.0e4):
|
84 |
+
b, channels, length = x.size()
|
85 |
+
signal = get_timing_signal_1d(length, channels, min_timescale, max_timescale)
|
86 |
+
return x + signal.to(dtype=x.dtype, device=x.device)
|
87 |
+
|
88 |
+
|
89 |
+
def cat_timing_signal_1d(x, min_timescale=1.0, max_timescale=1.0e4, axis=1):
|
90 |
+
b, channels, length = x.size()
|
91 |
+
signal = get_timing_signal_1d(length, channels, min_timescale, max_timescale)
|
92 |
+
return torch.cat([x, signal.to(dtype=x.dtype, device=x.device)], axis)
|
93 |
+
|
94 |
+
|
95 |
+
def subsequent_mask(length):
|
96 |
+
mask = torch.tril(torch.ones(length, length)).unsqueeze(0).unsqueeze(0)
|
97 |
+
return mask
|
98 |
+
|
99 |
+
|
100 |
+
@torch.jit.script
|
101 |
+
def fused_add_tanh_sigmoid_multiply(input_a, input_b, n_channels):
|
102 |
+
n_channels_int = n_channels[0]
|
103 |
+
in_act = input_a + input_b
|
104 |
+
t_act = torch.tanh(in_act[:, :n_channels_int, :])
|
105 |
+
s_act = torch.sigmoid(in_act[:, n_channels_int:, :])
|
106 |
+
acts = t_act * s_act
|
107 |
+
return acts
|
108 |
+
|
109 |
+
|
110 |
+
def convert_pad_shape(pad_shape):
|
111 |
+
layer = pad_shape[::-1]
|
112 |
+
pad_shape = [item for sublist in layer for item in sublist]
|
113 |
+
return pad_shape
|
114 |
+
|
115 |
+
|
116 |
+
def shift_1d(x):
|
117 |
+
x = F.pad(x, convert_pad_shape([[0, 0], [0, 0], [1, 0]]))[:, :, :-1]
|
118 |
+
return x
|
119 |
+
|
120 |
+
|
121 |
+
def sequence_mask(length, max_length=None):
|
122 |
+
if max_length is None:
|
123 |
+
max_length = length.max()
|
124 |
+
x = torch.arange(max_length, dtype=length.dtype, device=length.device)
|
125 |
+
return x.unsqueeze(0) < length.unsqueeze(1)
|
126 |
+
|
127 |
+
|
128 |
+
def generate_path(duration, mask):
|
129 |
+
"""
|
130 |
+
duration: [b, 1, t_x]
|
131 |
+
mask: [b, 1, t_y, t_x]
|
132 |
+
"""
|
133 |
+
|
134 |
+
b, _, t_y, t_x = mask.shape
|
135 |
+
cum_duration = torch.cumsum(duration, -1)
|
136 |
+
|
137 |
+
cum_duration_flat = cum_duration.view(b * t_x)
|
138 |
+
path = sequence_mask(cum_duration_flat, t_y).to(mask.dtype)
|
139 |
+
path = path.view(b, t_x, t_y)
|
140 |
+
path = path - F.pad(path, convert_pad_shape([[0, 0], [1, 0], [0, 0]]))[:, :-1]
|
141 |
+
path = path.unsqueeze(1).transpose(2, 3) * mask
|
142 |
+
return path
|
143 |
+
|
144 |
+
|
145 |
+
def clip_grad_value_(parameters, clip_value, norm_type=2):
|
146 |
+
if isinstance(parameters, torch.Tensor):
|
147 |
+
parameters = [parameters]
|
148 |
+
parameters = list(filter(lambda p: p.grad is not None, parameters))
|
149 |
+
norm_type = float(norm_type)
|
150 |
+
if clip_value is not None:
|
151 |
+
clip_value = float(clip_value)
|
152 |
+
|
153 |
+
total_norm = 0
|
154 |
+
for p in parameters:
|
155 |
+
param_norm = p.grad.data.norm(norm_type)
|
156 |
+
total_norm += param_norm.item() ** norm_type
|
157 |
+
if clip_value is not None:
|
158 |
+
p.grad.data.clamp_(min=-clip_value, max=clip_value)
|
159 |
+
total_norm = total_norm ** (1.0 / norm_type)
|
160 |
+
return total_norm
|
melo/configs/config.json
ADDED
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"train": {
|
3 |
+
"log_interval": 200,
|
4 |
+
"eval_interval": 1000,
|
5 |
+
"seed": 52,
|
6 |
+
"epochs": 10000,
|
7 |
+
"learning_rate": 0.0003,
|
8 |
+
"betas": [
|
9 |
+
0.8,
|
10 |
+
0.99
|
11 |
+
],
|
12 |
+
"eps": 1e-09,
|
13 |
+
"batch_size": 6,
|
14 |
+
"fp16_run": false,
|
15 |
+
"lr_decay": 0.999875,
|
16 |
+
"segment_size": 16384,
|
17 |
+
"init_lr_ratio": 1,
|
18 |
+
"warmup_epochs": 0,
|
19 |
+
"c_mel": 45,
|
20 |
+
"c_kl": 1.0,
|
21 |
+
"skip_optimizer": true
|
22 |
+
},
|
23 |
+
"data": {
|
24 |
+
"training_files": "",
|
25 |
+
"validation_files": "",
|
26 |
+
"max_wav_value": 32768.0,
|
27 |
+
"sampling_rate": 44100,
|
28 |
+
"filter_length": 2048,
|
29 |
+
"hop_length": 512,
|
30 |
+
"win_length": 2048,
|
31 |
+
"n_mel_channels": 128,
|
32 |
+
"mel_fmin": 0.0,
|
33 |
+
"mel_fmax": null,
|
34 |
+
"add_blank": true,
|
35 |
+
"n_speakers": 256,
|
36 |
+
"cleaned_text": true,
|
37 |
+
"spk2id": {}
|
38 |
+
},
|
39 |
+
"model": {
|
40 |
+
"use_spk_conditioned_encoder": true,
|
41 |
+
"use_noise_scaled_mas": true,
|
42 |
+
"use_mel_posterior_encoder": false,
|
43 |
+
"use_duration_discriminator": true,
|
44 |
+
"inter_channels": 192,
|
45 |
+
"hidden_channels": 192,
|
46 |
+
"filter_channels": 768,
|
47 |
+
"n_heads": 2,
|
48 |
+
"n_layers": 6,
|
49 |
+
"n_layers_trans_flow": 3,
|
50 |
+
"kernel_size": 3,
|
51 |
+
"p_dropout": 0.1,
|
52 |
+
"resblock": "1",
|
53 |
+
"resblock_kernel_sizes": [
|
54 |
+
3,
|
55 |
+
7,
|
56 |
+
11
|
57 |
+
],
|
58 |
+
"resblock_dilation_sizes": [
|
59 |
+
[
|
60 |
+
1,
|
61 |
+
3,
|
62 |
+
5
|
63 |
+
],
|
64 |
+
[
|
65 |
+
1,
|
66 |
+
3,
|
67 |
+
5
|
68 |
+
],
|
69 |
+
[
|
70 |
+
1,
|
71 |
+
3,
|
72 |
+
5
|
73 |
+
]
|
74 |
+
],
|
75 |
+
"upsample_rates": [
|
76 |
+
8,
|
77 |
+
8,
|
78 |
+
2,
|
79 |
+
2,
|
80 |
+
2
|
81 |
+
],
|
82 |
+
"upsample_initial_channel": 512,
|
83 |
+
"upsample_kernel_sizes": [
|
84 |
+
16,
|
85 |
+
16,
|
86 |
+
8,
|
87 |
+
2,
|
88 |
+
2
|
89 |
+
],
|
90 |
+
"n_layers_q": 3,
|
91 |
+
"use_spectral_norm": false,
|
92 |
+
"gin_channels": 256
|
93 |
+
}
|
94 |
+
}
|
melo/data/example/metadata.list
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
data/example/wavs/000.wav|EN-default|EN|Well, there are always new trends and styles emerging in the fashion world, but I think some of the biggest trends at the moment include sustainability and ethical fashion, streetwear and athleisure, and oversized and deconstructed silhouettes.
|
2 |
+
data/example/wavs/001.wav|EN-default|EN|Many designers and brands are focusing on creating more environmentally-friendly and socially responsible clothing, while others are incorporating elements of sportswear and casual wear into their collections.
|
3 |
+
data/example/wavs/002.wav|EN-default|EN|And there's a growing interest in looser, more relaxed shapes and unconventional materials and finishes.
|
4 |
+
data/example/wavs/003.wav|EN-default|EN|That's really insightful.
|
5 |
+
data/example/wavs/004.wav|EN-default|EN|What do you think are some of the benefits of following fashion trends?
|
6 |
+
data/example/wavs/005.wav|EN-default|EN|Well, I think one of the main benefits of following fashion trends is that it can be a way to express your creativity, personality, and individuality.
|
7 |
+
data/example/wavs/006.wav|EN-default|EN|Fashion can be a powerful tool for self-expression and can help you feel more confident and comfortable in your own skin.
|
8 |
+
data/example/wavs/007.wav|EN-default|EN|Additionally, staying up-to-date with fashion trends can help you develop your own sense of style and learn how to put together outfits that make you look and feel great.
|
9 |
+
data/example/wavs/008.wav|EN-default|EN|That's a great point.
|
10 |
+
data/example/wavs/009.wav|EN-default|EN|Do you think it's important to stay on top of the latest fashion trends, or is it more important to focus on timeless style?
|
11 |
+
data/example/wavs/010.wav|EN-default|EN|I think it's really up to each individual to decide what approach to fashion works best for them.
|
12 |
+
data/example/wavs/011.wav|EN-default|EN|Some people prefer to stick with classic, timeless styles that never go out of fashion, while others enjoy experimenting with new and innovative trends.
|
13 |
+
data/example/wavs/012.wav|EN-default|EN|Ultimately, fashion is about personal expression and there's no right or wrong way to approach it.
|
14 |
+
data/example/wavs/013.wav|EN-default|EN|The most important thing is to wear what makes you feel good and confident.
|
15 |
+
data/example/wavs/014.wav|EN-default|EN|I completely agree.
|
16 |
+
data/example/wavs/015.wav|EN-default|EN|Some popular ones that come to mind are oversized blazers, statement sleeves, printed maxi dresses, and chunky sneakers.
|
17 |
+
data/example/wavs/016.wav|EN-default|EN|It's been really interesting chatting with you about fashion.
|
18 |
+
data/example/wavs/017.wav|EN-default|EN|That's a good point.
|
19 |
+
data/example/wavs/018.wav|EN-default|EN|What do you think are some current fashion trends that are popular right now?
|
20 |
+
data/example/wavs/019.wav|EN-default|EN|There are so many trends happening right now, it's hard to keep track of them all!
|
melo/data_utils.py
ADDED
@@ -0,0 +1,413 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import random
|
3 |
+
import torch
|
4 |
+
import torch.utils.data
|
5 |
+
from tqdm import tqdm
|
6 |
+
from loguru import logger
|
7 |
+
import commons
|
8 |
+
from mel_processing import spectrogram_torch, mel_spectrogram_torch
|
9 |
+
from utils import load_filepaths_and_text
|
10 |
+
from utils import load_wav_to_torch_librosa as load_wav_to_torch
|
11 |
+
from text import cleaned_text_to_sequence, get_bert
|
12 |
+
import numpy as np
|
13 |
+
|
14 |
+
"""Multi speaker version"""
|
15 |
+
|
16 |
+
|
17 |
+
class TextAudioSpeakerLoader(torch.utils.data.Dataset):
|
18 |
+
"""
|
19 |
+
1) loads audio, speaker_id, text pairs
|
20 |
+
2) normalizes text and converts them to sequences of integers
|
21 |
+
3) computes spectrograms from audio files.
|
22 |
+
"""
|
23 |
+
|
24 |
+
def __init__(self, audiopaths_sid_text, hparams):
|
25 |
+
self.audiopaths_sid_text = load_filepaths_and_text(audiopaths_sid_text)
|
26 |
+
self.max_wav_value = hparams.max_wav_value
|
27 |
+
self.sampling_rate = hparams.sampling_rate
|
28 |
+
self.filter_length = hparams.filter_length
|
29 |
+
self.hop_length = hparams.hop_length
|
30 |
+
self.win_length = hparams.win_length
|
31 |
+
self.sampling_rate = hparams.sampling_rate
|
32 |
+
self.spk_map = hparams.spk2id
|
33 |
+
self.hparams = hparams
|
34 |
+
self.disable_bert = getattr(hparams, "disable_bert", False)
|
35 |
+
|
36 |
+
self.use_mel_spec_posterior = getattr(
|
37 |
+
hparams, "use_mel_posterior_encoder", False
|
38 |
+
)
|
39 |
+
if self.use_mel_spec_posterior:
|
40 |
+
self.n_mel_channels = getattr(hparams, "n_mel_channels", 80)
|
41 |
+
|
42 |
+
self.cleaned_text = getattr(hparams, "cleaned_text", False)
|
43 |
+
|
44 |
+
self.add_blank = hparams.add_blank
|
45 |
+
self.min_text_len = getattr(hparams, "min_text_len", 1)
|
46 |
+
self.max_text_len = getattr(hparams, "max_text_len", 300)
|
47 |
+
|
48 |
+
random.seed(1234)
|
49 |
+
random.shuffle(self.audiopaths_sid_text)
|
50 |
+
self._filter()
|
51 |
+
|
52 |
+
|
53 |
+
def _filter(self):
|
54 |
+
"""
|
55 |
+
Filter text & store spec lengths
|
56 |
+
"""
|
57 |
+
# Store spectrogram lengths for Bucketing
|
58 |
+
# wav_length ~= file_size / (wav_channels * Bytes per dim) = file_size / (1 * 2)
|
59 |
+
# spec_length = wav_length // hop_length
|
60 |
+
|
61 |
+
audiopaths_sid_text_new = []
|
62 |
+
lengths = []
|
63 |
+
skipped = 0
|
64 |
+
logger.info("Init dataset...")
|
65 |
+
for item in tqdm(
|
66 |
+
self.audiopaths_sid_text
|
67 |
+
):
|
68 |
+
try:
|
69 |
+
_id, spk, language, text, phones, tone, word2ph = item
|
70 |
+
except:
|
71 |
+
print(item)
|
72 |
+
raise
|
73 |
+
audiopath = f"{_id}"
|
74 |
+
if self.min_text_len <= len(phones) and len(phones) <= self.max_text_len:
|
75 |
+
phones = phones.split(" ")
|
76 |
+
tone = [int(i) for i in tone.split(" ")]
|
77 |
+
word2ph = [int(i) for i in word2ph.split(" ")]
|
78 |
+
audiopaths_sid_text_new.append(
|
79 |
+
[audiopath, spk, language, text, phones, tone, word2ph]
|
80 |
+
)
|
81 |
+
lengths.append(os.path.getsize(audiopath) // (2 * self.hop_length))
|
82 |
+
else:
|
83 |
+
skipped += 1
|
84 |
+
logger.info(f'min: {min(lengths)}; max: {max(lengths)}' )
|
85 |
+
logger.info(
|
86 |
+
"skipped: "
|
87 |
+
+ str(skipped)
|
88 |
+
+ ", total: "
|
89 |
+
+ str(len(self.audiopaths_sid_text))
|
90 |
+
)
|
91 |
+
self.audiopaths_sid_text = audiopaths_sid_text_new
|
92 |
+
self.lengths = lengths
|
93 |
+
|
94 |
+
def get_audio_text_speaker_pair(self, audiopath_sid_text):
|
95 |
+
# separate filename, speaker_id and text
|
96 |
+
audiopath, sid, language, text, phones, tone, word2ph = audiopath_sid_text
|
97 |
+
|
98 |
+
bert, ja_bert, phones, tone, language = self.get_text(
|
99 |
+
text, word2ph, phones, tone, language, audiopath
|
100 |
+
)
|
101 |
+
|
102 |
+
spec, wav = self.get_audio(audiopath)
|
103 |
+
sid = int(getattr(self.spk_map, sid, '0'))
|
104 |
+
sid = torch.LongTensor([sid])
|
105 |
+
return (phones, spec, wav, sid, tone, language, bert, ja_bert)
|
106 |
+
|
107 |
+
def get_audio(self, filename):
|
108 |
+
audio_norm, sampling_rate = load_wav_to_torch(filename, self.sampling_rate)
|
109 |
+
if sampling_rate != self.sampling_rate:
|
110 |
+
raise ValueError(
|
111 |
+
"{} {} SR doesn't match target {} SR".format(
|
112 |
+
filename, sampling_rate, self.sampling_rate
|
113 |
+
)
|
114 |
+
)
|
115 |
+
# NOTE: normalize has been achieved by torchaudio
|
116 |
+
# audio_norm = audio / self.max_wav_value
|
117 |
+
audio_norm = audio_norm.unsqueeze(0)
|
118 |
+
spec_filename = filename.replace(".wav", ".spec.pt")
|
119 |
+
if self.use_mel_spec_posterior:
|
120 |
+
spec_filename = spec_filename.replace(".spec.pt", ".mel.pt")
|
121 |
+
try:
|
122 |
+
spec = torch.load(spec_filename)
|
123 |
+
assert False
|
124 |
+
except:
|
125 |
+
if self.use_mel_spec_posterior:
|
126 |
+
spec = mel_spectrogram_torch(
|
127 |
+
audio_norm,
|
128 |
+
self.filter_length,
|
129 |
+
self.n_mel_channels,
|
130 |
+
self.sampling_rate,
|
131 |
+
self.hop_length,
|
132 |
+
self.win_length,
|
133 |
+
self.hparams.mel_fmin,
|
134 |
+
self.hparams.mel_fmax,
|
135 |
+
center=False,
|
136 |
+
)
|
137 |
+
else:
|
138 |
+
spec = spectrogram_torch(
|
139 |
+
audio_norm,
|
140 |
+
self.filter_length,
|
141 |
+
self.sampling_rate,
|
142 |
+
self.hop_length,
|
143 |
+
self.win_length,
|
144 |
+
center=False,
|
145 |
+
)
|
146 |
+
spec = torch.squeeze(spec, 0)
|
147 |
+
torch.save(spec, spec_filename)
|
148 |
+
return spec, audio_norm
|
149 |
+
|
150 |
+
def get_text(self, text, word2ph, phone, tone, language_str, wav_path):
|
151 |
+
phone, tone, language = cleaned_text_to_sequence(phone, tone, language_str)
|
152 |
+
if self.add_blank:
|
153 |
+
phone = commons.intersperse(phone, 0)
|
154 |
+
tone = commons.intersperse(tone, 0)
|
155 |
+
language = commons.intersperse(language, 0)
|
156 |
+
for i in range(len(word2ph)):
|
157 |
+
word2ph[i] = word2ph[i] * 2
|
158 |
+
word2ph[0] += 1
|
159 |
+
bert_path = wav_path.replace(".wav", ".bert.pt")
|
160 |
+
try:
|
161 |
+
bert = torch.load(bert_path)
|
162 |
+
assert bert.shape[-1] == len(phone)
|
163 |
+
except Exception as e:
|
164 |
+
print(e, wav_path, bert_path, bert.shape, len(phone))
|
165 |
+
bert = get_bert(text, word2ph, language_str)
|
166 |
+
torch.save(bert, bert_path)
|
167 |
+
assert bert.shape[-1] == len(phone), phone
|
168 |
+
|
169 |
+
if self.disable_bert:
|
170 |
+
bert = torch.zeros(1024, len(phone))
|
171 |
+
ja_bert = torch.zeros(768, len(phone))
|
172 |
+
else:
|
173 |
+
if language_str in ["ZH"]:
|
174 |
+
bert = bert
|
175 |
+
ja_bert = torch.zeros(768, len(phone))
|
176 |
+
elif language_str in ["JP", "EN", "ZH_MIX_EN", "KR", 'SP', 'ES', 'FR', 'DE', 'RU']:
|
177 |
+
ja_bert = bert
|
178 |
+
bert = torch.zeros(1024, len(phone))
|
179 |
+
else:
|
180 |
+
raise
|
181 |
+
bert = torch.zeros(1024, len(phone))
|
182 |
+
ja_bert = torch.zeros(768, len(phone))
|
183 |
+
assert bert.shape[-1] == len(phone)
|
184 |
+
phone = torch.LongTensor(phone)
|
185 |
+
tone = torch.LongTensor(tone)
|
186 |
+
language = torch.LongTensor(language)
|
187 |
+
return bert, ja_bert, phone, tone, language
|
188 |
+
|
189 |
+
def get_sid(self, sid):
|
190 |
+
sid = torch.LongTensor([int(sid)])
|
191 |
+
return sid
|
192 |
+
|
193 |
+
def __getitem__(self, index):
|
194 |
+
return self.get_audio_text_speaker_pair(self.audiopaths_sid_text[index])
|
195 |
+
|
196 |
+
def __len__(self):
|
197 |
+
return len(self.audiopaths_sid_text)
|
198 |
+
|
199 |
+
|
200 |
+
class TextAudioSpeakerCollate:
|
201 |
+
"""Zero-pads model inputs and targets"""
|
202 |
+
|
203 |
+
def __init__(self, return_ids=False):
|
204 |
+
self.return_ids = return_ids
|
205 |
+
|
206 |
+
def __call__(self, batch):
|
207 |
+
"""Collate's training batch from normalized text, audio and speaker identities
|
208 |
+
PARAMS
|
209 |
+
------
|
210 |
+
batch: [text_normalized, spec_normalized, wav_normalized, sid]
|
211 |
+
"""
|
212 |
+
# Right zero-pad all one-hot text sequences to max input length
|
213 |
+
_, ids_sorted_decreasing = torch.sort(
|
214 |
+
torch.LongTensor([x[1].size(1) for x in batch]), dim=0, descending=True
|
215 |
+
)
|
216 |
+
|
217 |
+
max_text_len = max([len(x[0]) for x in batch])
|
218 |
+
max_spec_len = max([x[1].size(1) for x in batch])
|
219 |
+
max_wav_len = max([x[2].size(1) for x in batch])
|
220 |
+
|
221 |
+
text_lengths = torch.LongTensor(len(batch))
|
222 |
+
spec_lengths = torch.LongTensor(len(batch))
|
223 |
+
wav_lengths = torch.LongTensor(len(batch))
|
224 |
+
sid = torch.LongTensor(len(batch))
|
225 |
+
|
226 |
+
text_padded = torch.LongTensor(len(batch), max_text_len)
|
227 |
+
tone_padded = torch.LongTensor(len(batch), max_text_len)
|
228 |
+
language_padded = torch.LongTensor(len(batch), max_text_len)
|
229 |
+
bert_padded = torch.FloatTensor(len(batch), 1024, max_text_len)
|
230 |
+
ja_bert_padded = torch.FloatTensor(len(batch), 768, max_text_len)
|
231 |
+
|
232 |
+
spec_padded = torch.FloatTensor(len(batch), batch[0][1].size(0), max_spec_len)
|
233 |
+
wav_padded = torch.FloatTensor(len(batch), 1, max_wav_len)
|
234 |
+
text_padded.zero_()
|
235 |
+
tone_padded.zero_()
|
236 |
+
language_padded.zero_()
|
237 |
+
spec_padded.zero_()
|
238 |
+
wav_padded.zero_()
|
239 |
+
bert_padded.zero_()
|
240 |
+
ja_bert_padded.zero_()
|
241 |
+
for i in range(len(ids_sorted_decreasing)):
|
242 |
+
row = batch[ids_sorted_decreasing[i]]
|
243 |
+
|
244 |
+
text = row[0]
|
245 |
+
text_padded[i, : text.size(0)] = text
|
246 |
+
text_lengths[i] = text.size(0)
|
247 |
+
|
248 |
+
spec = row[1]
|
249 |
+
spec_padded[i, :, : spec.size(1)] = spec
|
250 |
+
spec_lengths[i] = spec.size(1)
|
251 |
+
|
252 |
+
wav = row[2]
|
253 |
+
wav_padded[i, :, : wav.size(1)] = wav
|
254 |
+
wav_lengths[i] = wav.size(1)
|
255 |
+
|
256 |
+
sid[i] = row[3]
|
257 |
+
|
258 |
+
tone = row[4]
|
259 |
+
tone_padded[i, : tone.size(0)] = tone
|
260 |
+
|
261 |
+
language = row[5]
|
262 |
+
language_padded[i, : language.size(0)] = language
|
263 |
+
|
264 |
+
bert = row[6]
|
265 |
+
bert_padded[i, :, : bert.size(1)] = bert
|
266 |
+
|
267 |
+
ja_bert = row[7]
|
268 |
+
ja_bert_padded[i, :, : ja_bert.size(1)] = ja_bert
|
269 |
+
|
270 |
+
return (
|
271 |
+
text_padded,
|
272 |
+
text_lengths,
|
273 |
+
spec_padded,
|
274 |
+
spec_lengths,
|
275 |
+
wav_padded,
|
276 |
+
wav_lengths,
|
277 |
+
sid,
|
278 |
+
tone_padded,
|
279 |
+
language_padded,
|
280 |
+
bert_padded,
|
281 |
+
ja_bert_padded,
|
282 |
+
)
|
283 |
+
|
284 |
+
|
285 |
+
class DistributedBucketSampler(torch.utils.data.distributed.DistributedSampler):
|
286 |
+
"""
|
287 |
+
Maintain similar input lengths in a batch.
|
288 |
+
Length groups are specified by boundaries.
|
289 |
+
Ex) boundaries = [b1, b2, b3] -> any batch is included either {x | b1 < length(x) <=b2} or {x | b2 < length(x) <= b3}.
|
290 |
+
|
291 |
+
It removes samples which are not included in the boundaries.
|
292 |
+
Ex) boundaries = [b1, b2, b3] -> any x s.t. length(x) <= b1 or length(x) > b3 are discarded.
|
293 |
+
"""
|
294 |
+
|
295 |
+
def __init__(
|
296 |
+
self,
|
297 |
+
dataset,
|
298 |
+
batch_size,
|
299 |
+
boundaries,
|
300 |
+
num_replicas=None,
|
301 |
+
rank=None,
|
302 |
+
shuffle=True,
|
303 |
+
):
|
304 |
+
super().__init__(dataset, num_replicas=num_replicas, rank=rank, shuffle=shuffle)
|
305 |
+
self.lengths = dataset.lengths
|
306 |
+
self.batch_size = batch_size
|
307 |
+
self.boundaries = boundaries
|
308 |
+
|
309 |
+
self.buckets, self.num_samples_per_bucket = self._create_buckets()
|
310 |
+
self.total_size = sum(self.num_samples_per_bucket)
|
311 |
+
self.num_samples = self.total_size // self.num_replicas
|
312 |
+
print('buckets:', self.num_samples_per_bucket)
|
313 |
+
|
314 |
+
def _create_buckets(self):
|
315 |
+
buckets = [[] for _ in range(len(self.boundaries) - 1)]
|
316 |
+
for i in range(len(self.lengths)):
|
317 |
+
length = self.lengths[i]
|
318 |
+
idx_bucket = self._bisect(length)
|
319 |
+
if idx_bucket != -1:
|
320 |
+
buckets[idx_bucket].append(i)
|
321 |
+
|
322 |
+
try:
|
323 |
+
for i in range(len(buckets) - 1, 0, -1):
|
324 |
+
if len(buckets[i]) == 0:
|
325 |
+
buckets.pop(i)
|
326 |
+
self.boundaries.pop(i + 1)
|
327 |
+
assert all(len(bucket) > 0 for bucket in buckets)
|
328 |
+
# When one bucket is not traversed
|
329 |
+
except Exception as e:
|
330 |
+
print("Bucket warning ", e)
|
331 |
+
for i in range(len(buckets) - 1, -1, -1):
|
332 |
+
if len(buckets[i]) == 0:
|
333 |
+
buckets.pop(i)
|
334 |
+
self.boundaries.pop(i + 1)
|
335 |
+
|
336 |
+
num_samples_per_bucket = []
|
337 |
+
for i in range(len(buckets)):
|
338 |
+
len_bucket = len(buckets[i])
|
339 |
+
total_batch_size = self.num_replicas * self.batch_size
|
340 |
+
rem = (
|
341 |
+
total_batch_size - (len_bucket % total_batch_size)
|
342 |
+
) % total_batch_size
|
343 |
+
num_samples_per_bucket.append(len_bucket + rem)
|
344 |
+
return buckets, num_samples_per_bucket
|
345 |
+
|
346 |
+
def __iter__(self):
|
347 |
+
# deterministically shuffle based on epoch
|
348 |
+
g = torch.Generator()
|
349 |
+
g.manual_seed(self.epoch)
|
350 |
+
|
351 |
+
indices = []
|
352 |
+
if self.shuffle:
|
353 |
+
for bucket in self.buckets:
|
354 |
+
indices.append(torch.randperm(len(bucket), generator=g).tolist())
|
355 |
+
else:
|
356 |
+
for bucket in self.buckets:
|
357 |
+
indices.append(list(range(len(bucket))))
|
358 |
+
|
359 |
+
batches = []
|
360 |
+
for i in range(len(self.buckets)):
|
361 |
+
bucket = self.buckets[i]
|
362 |
+
len_bucket = len(bucket)
|
363 |
+
if len_bucket == 0:
|
364 |
+
continue
|
365 |
+
ids_bucket = indices[i]
|
366 |
+
num_samples_bucket = self.num_samples_per_bucket[i]
|
367 |
+
|
368 |
+
# add extra samples to make it evenly divisible
|
369 |
+
rem = num_samples_bucket - len_bucket
|
370 |
+
ids_bucket = (
|
371 |
+
ids_bucket
|
372 |
+
+ ids_bucket * (rem // len_bucket)
|
373 |
+
+ ids_bucket[: (rem % len_bucket)]
|
374 |
+
)
|
375 |
+
|
376 |
+
# subsample
|
377 |
+
ids_bucket = ids_bucket[self.rank :: self.num_replicas]
|
378 |
+
|
379 |
+
# batching
|
380 |
+
for j in range(len(ids_bucket) // self.batch_size):
|
381 |
+
batch = [
|
382 |
+
bucket[idx]
|
383 |
+
for idx in ids_bucket[
|
384 |
+
j * self.batch_size : (j + 1) * self.batch_size
|
385 |
+
]
|
386 |
+
]
|
387 |
+
batches.append(batch)
|
388 |
+
|
389 |
+
if self.shuffle:
|
390 |
+
batch_ids = torch.randperm(len(batches), generator=g).tolist()
|
391 |
+
batches = [batches[i] for i in batch_ids]
|
392 |
+
self.batches = batches
|
393 |
+
|
394 |
+
assert len(self.batches) * self.batch_size == self.num_samples
|
395 |
+
return iter(self.batches)
|
396 |
+
|
397 |
+
def _bisect(self, x, lo=0, hi=None):
|
398 |
+
if hi is None:
|
399 |
+
hi = len(self.boundaries) - 1
|
400 |
+
|
401 |
+
if hi > lo:
|
402 |
+
mid = (hi + lo) // 2
|
403 |
+
if self.boundaries[mid] < x and x <= self.boundaries[mid + 1]:
|
404 |
+
return mid
|
405 |
+
elif x <= self.boundaries[mid]:
|
406 |
+
return self._bisect(x, lo, mid)
|
407 |
+
else:
|
408 |
+
return self._bisect(x, mid + 1, hi)
|
409 |
+
else:
|
410 |
+
return -1
|
411 |
+
|
412 |
+
def __len__(self):
|
413 |
+
return self.num_samples // self.batch_size
|
melo/download_utils.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import os
|
3 |
+
from . import utils
|
4 |
+
from cached_path import cached_path
|
5 |
+
from huggingface_hub import hf_hub_download
|
6 |
+
|
7 |
+
DOWNLOAD_CKPT_URLS = {
|
8 |
+
'EN': 'https://myshell-public-repo-host.s3.amazonaws.com/openvoice/basespeakers/EN/checkpoint.pth',
|
9 |
+
'EN_V2': 'https://myshell-public-repo-host.s3.amazonaws.com/openvoice/basespeakers/EN_V2/checkpoint.pth',
|
10 |
+
'FR': 'https://myshell-public-repo-host.s3.amazonaws.com/openvoice/basespeakers/FR/checkpoint.pth',
|
11 |
+
'JP': 'https://myshell-public-repo-host.s3.amazonaws.com/openvoice/basespeakers/JP/checkpoint.pth',
|
12 |
+
'ES': 'https://myshell-public-repo-host.s3.amazonaws.com/openvoice/basespeakers/ES/checkpoint.pth',
|
13 |
+
'ZH': 'https://myshell-public-repo-host.s3.amazonaws.com/openvoice/basespeakers/ZH/checkpoint.pth',
|
14 |
+
'KR': 'https://myshell-public-repo-host.s3.amazonaws.com/openvoice/basespeakers/KR/checkpoint.pth',
|
15 |
+
}
|
16 |
+
|
17 |
+
DOWNLOAD_CONFIG_URLS = {
|
18 |
+
'EN': 'https://myshell-public-repo-host.s3.amazonaws.com/openvoice/basespeakers/EN/config.json',
|
19 |
+
'EN_V2': 'https://myshell-public-repo-host.s3.amazonaws.com/openvoice/basespeakers/EN_V2/config.json',
|
20 |
+
'FR': 'https://myshell-public-repo-host.s3.amazonaws.com/openvoice/basespeakers/FR/config.json',
|
21 |
+
'JP': 'https://myshell-public-repo-host.s3.amazonaws.com/openvoice/basespeakers/JP/config.json',
|
22 |
+
'ES': 'https://myshell-public-repo-host.s3.amazonaws.com/openvoice/basespeakers/ES/config.json',
|
23 |
+
'ZH': 'https://myshell-public-repo-host.s3.amazonaws.com/openvoice/basespeakers/ZH/config.json',
|
24 |
+
'KR': 'https://myshell-public-repo-host.s3.amazonaws.com/openvoice/basespeakers/KR/config.json',
|
25 |
+
}
|
26 |
+
|
27 |
+
PRETRAINED_MODELS = {
|
28 |
+
'G.pth': 'https://myshell-public-repo-host.s3.amazonaws.com/openvoice/basespeakers/pretrained/G.pth',
|
29 |
+
'D.pth': 'https://myshell-public-repo-host.s3.amazonaws.com/openvoice/basespeakers/pretrained/D.pth',
|
30 |
+
'DUR.pth': 'https://myshell-public-repo-host.s3.amazonaws.com/openvoice/basespeakers/pretrained/DUR.pth',
|
31 |
+
}
|
32 |
+
|
33 |
+
LANG_TO_HF_REPO_ID = {
|
34 |
+
'EN': 'myshell-ai/MeloTTS-English',
|
35 |
+
'EN_V2': 'myshell-ai/MeloTTS-English-v2',
|
36 |
+
'EN_NEWEST': 'myshell-ai/MeloTTS-English-v3',
|
37 |
+
'FR': 'myshell-ai/MeloTTS-French',
|
38 |
+
'JP': 'myshell-ai/MeloTTS-Japanese',
|
39 |
+
'ES': 'myshell-ai/MeloTTS-Spanish',
|
40 |
+
'ZH': 'myshell-ai/MeloTTS-Chinese',
|
41 |
+
'KR': 'myshell-ai/MeloTTS-Korean',
|
42 |
+
}
|
43 |
+
|
44 |
+
def load_or_download_config(locale, use_hf=True, config_path=None):
|
45 |
+
if config_path is None:
|
46 |
+
language = locale.split('-')[0].upper()
|
47 |
+
if use_hf:
|
48 |
+
assert language in LANG_TO_HF_REPO_ID
|
49 |
+
config_path = hf_hub_download(repo_id=LANG_TO_HF_REPO_ID[language], filename="config.json")
|
50 |
+
else:
|
51 |
+
assert language in DOWNLOAD_CONFIG_URLS
|
52 |
+
config_path = cached_path(DOWNLOAD_CONFIG_URLS[language])
|
53 |
+
return utils.get_hparams_from_file(config_path)
|
54 |
+
|
55 |
+
def load_or_download_model(locale, device, use_hf=True, ckpt_path=None):
|
56 |
+
if ckpt_path is None:
|
57 |
+
language = locale.split('-')[0].upper()
|
58 |
+
if use_hf:
|
59 |
+
assert language in LANG_TO_HF_REPO_ID
|
60 |
+
ckpt_path = hf_hub_download(repo_id=LANG_TO_HF_REPO_ID[language], filename="checkpoint.pth")
|
61 |
+
else:
|
62 |
+
assert language in DOWNLOAD_CKPT_URLS
|
63 |
+
ckpt_path = cached_path(DOWNLOAD_CKPT_URLS[language])
|
64 |
+
return torch.load(ckpt_path, map_location=device)
|
65 |
+
|
66 |
+
def load_pretrain_model():
|
67 |
+
return [cached_path(url) for url in PRETRAINED_MODELS.values()]
|
melo/infer.py
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import click
|
3 |
+
from melo.api import TTS
|
4 |
+
|
5 |
+
|
6 |
+
|
7 |
+
@click.command()
|
8 |
+
@click.option('--ckpt_path', '-m', type=str, default=None, help="Path to the checkpoint file")
|
9 |
+
@click.option('--text', '-t', type=str, default=None, help="Text to speak")
|
10 |
+
@click.option('--language', '-l', type=str, default="EN", help="Language of the model")
|
11 |
+
@click.option('--output_dir', '-o', type=str, default="outputs", help="Path to the output")
|
12 |
+
def main(ckpt_path, text, language, output_dir):
|
13 |
+
if ckpt_path is None:
|
14 |
+
raise ValueError("The model_path must be specified")
|
15 |
+
|
16 |
+
config_path = os.path.join(os.path.dirname(ckpt_path), 'config.json')
|
17 |
+
model = TTS(language=language, config_path=config_path, ckpt_path=ckpt_path)
|
18 |
+
|
19 |
+
for spk_name, spk_id in model.hps.data.spk2id.items():
|
20 |
+
save_path = f'{output_dir}/{spk_name}/output.wav'
|
21 |
+
os.makedirs(os.path.dirname(save_path), exist_ok=True)
|
22 |
+
model.tts_to_file(text, spk_id, save_path)
|
23 |
+
|
24 |
+
if __name__ == "__main__":
|
25 |
+
main()
|
melo/init_downloads.py
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
|
3 |
+
if __name__ == '__main__':
|
4 |
+
|
5 |
+
from melo.api import TTS
|
6 |
+
device = 'auto'
|
7 |
+
models = {
|
8 |
+
'EN': TTS(language='EN', device=device),
|
9 |
+
'ES': TTS(language='ES', device=device),
|
10 |
+
'FR': TTS(language='FR', device=device),
|
11 |
+
'ZH': TTS(language='ZH', device=device),
|
12 |
+
'JP': TTS(language='JP', device=device),
|
13 |
+
'KR': TTS(language='KR', device=device),
|
14 |
+
}
|
melo/losses.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
|
3 |
+
|
4 |
+
def feature_loss(fmap_r, fmap_g):
|
5 |
+
loss = 0
|
6 |
+
for dr, dg in zip(fmap_r, fmap_g):
|
7 |
+
for rl, gl in zip(dr, dg):
|
8 |
+
rl = rl.float().detach()
|
9 |
+
gl = gl.float()
|
10 |
+
loss += torch.mean(torch.abs(rl - gl))
|
11 |
+
|
12 |
+
return loss * 2
|
13 |
+
|
14 |
+
|
15 |
+
def discriminator_loss(disc_real_outputs, disc_generated_outputs):
|
16 |
+
loss = 0
|
17 |
+
r_losses = []
|
18 |
+
g_losses = []
|
19 |
+
for dr, dg in zip(disc_real_outputs, disc_generated_outputs):
|
20 |
+
dr = dr.float()
|
21 |
+
dg = dg.float()
|
22 |
+
r_loss = torch.mean((1 - dr) ** 2)
|
23 |
+
g_loss = torch.mean(dg**2)
|
24 |
+
loss += r_loss + g_loss
|
25 |
+
r_losses.append(r_loss.item())
|
26 |
+
g_losses.append(g_loss.item())
|
27 |
+
|
28 |
+
return loss, r_losses, g_losses
|
29 |
+
|
30 |
+
|
31 |
+
def generator_loss(disc_outputs):
|
32 |
+
loss = 0
|
33 |
+
gen_losses = []
|
34 |
+
for dg in disc_outputs:
|
35 |
+
dg = dg.float()
|
36 |
+
l = torch.mean((1 - dg) ** 2)
|
37 |
+
gen_losses.append(l)
|
38 |
+
loss += l
|
39 |
+
|
40 |
+
return loss, gen_losses
|
41 |
+
|
42 |
+
|
43 |
+
def kl_loss(z_p, logs_q, m_p, logs_p, z_mask):
|
44 |
+
"""
|
45 |
+
z_p, logs_q: [b, h, t_t]
|
46 |
+
m_p, logs_p: [b, h, t_t]
|
47 |
+
"""
|
48 |
+
z_p = z_p.float()
|
49 |
+
logs_q = logs_q.float()
|
50 |
+
m_p = m_p.float()
|
51 |
+
logs_p = logs_p.float()
|
52 |
+
z_mask = z_mask.float()
|
53 |
+
|
54 |
+
kl = logs_p - logs_q - 0.5
|
55 |
+
kl += 0.5 * ((z_p - m_p) ** 2) * torch.exp(-2.0 * logs_p)
|
56 |
+
kl = torch.sum(kl * z_mask)
|
57 |
+
l = kl / torch.sum(z_mask)
|
58 |
+
return l
|
melo/main.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import click
|
2 |
+
import warnings
|
3 |
+
import os
|
4 |
+
|
5 |
+
|
6 |
+
@click.command
|
7 |
+
@click.argument('text')
|
8 |
+
@click.argument('output_path')
|
9 |
+
@click.option("--file", '-f', is_flag=True, show_default=True, default=False, help="Text is a file")
|
10 |
+
@click.option('--language', '-l', default='EN', help='Language, defaults to English', type=click.Choice(['EN', 'ES', 'FR', 'ZH', 'JP', 'KR'], case_sensitive=False))
|
11 |
+
@click.option('--speaker', '-spk', default='EN-Default', help='Speaker ID, only for English, leave empty for default, ignored if not English. If English, defaults to "EN-Default"', type=click.Choice(['EN-Default', 'EN-US', 'EN-BR', 'EN_INDIA', 'EN-AU']))
|
12 |
+
@click.option('--speed', '-s', default=1.0, help='Speed, defaults to 1.0', type=float)
|
13 |
+
@click.option('--device', '-d', default='auto', help='Device, defaults to auto')
|
14 |
+
def main(text, file, output_path, language, speaker, speed, device):
|
15 |
+
if file:
|
16 |
+
if not os.path.exists(text):
|
17 |
+
raise FileNotFoundError(f'Trying to load text from file due to --file/-f flag, but file not found. Remove the --file/-f flag to pass a string.')
|
18 |
+
else:
|
19 |
+
with open(text) as f:
|
20 |
+
text = f.read().strip()
|
21 |
+
if text == '':
|
22 |
+
raise ValueError('You entered empty text or the file you passed was empty.')
|
23 |
+
language = language.upper()
|
24 |
+
if language == '': language = 'EN'
|
25 |
+
if speaker == '': speaker = None
|
26 |
+
if (not language == 'EN') and speaker:
|
27 |
+
warnings.warn('You specified a speaker but the language is English.')
|
28 |
+
from melo.api import TTS
|
29 |
+
model = TTS(language=language, device=device)
|
30 |
+
speaker_ids = model.hps.data.spk2id
|
31 |
+
if language == 'EN':
|
32 |
+
if not speaker: speaker = 'EN-Default'
|
33 |
+
spkr = speaker_ids[speaker]
|
34 |
+
else:
|
35 |
+
spkr = speaker_ids[list(speaker_ids.keys())[0]]
|
36 |
+
model.tts_to_file(text, spkr, output_path, speed=speed)
|
melo/mel_processing.py
ADDED
@@ -0,0 +1,174 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torch.utils.data
|
3 |
+
import librosa
|
4 |
+
from librosa.filters import mel as librosa_mel_fn
|
5 |
+
|
6 |
+
MAX_WAV_VALUE = 32768.0
|
7 |
+
|
8 |
+
|
9 |
+
def dynamic_range_compression_torch(x, C=1, clip_val=1e-5):
|
10 |
+
"""
|
11 |
+
PARAMS
|
12 |
+
------
|
13 |
+
C: compression factor
|
14 |
+
"""
|
15 |
+
return torch.log(torch.clamp(x, min=clip_val) * C)
|
16 |
+
|
17 |
+
|
18 |
+
def dynamic_range_decompression_torch(x, C=1):
|
19 |
+
"""
|
20 |
+
PARAMS
|
21 |
+
------
|
22 |
+
C: compression factor used to compress
|
23 |
+
"""
|
24 |
+
return torch.exp(x) / C
|
25 |
+
|
26 |
+
|
27 |
+
def spectral_normalize_torch(magnitudes):
|
28 |
+
output = dynamic_range_compression_torch(magnitudes)
|
29 |
+
return output
|
30 |
+
|
31 |
+
|
32 |
+
def spectral_de_normalize_torch(magnitudes):
|
33 |
+
output = dynamic_range_decompression_torch(magnitudes)
|
34 |
+
return output
|
35 |
+
|
36 |
+
|
37 |
+
mel_basis = {}
|
38 |
+
hann_window = {}
|
39 |
+
|
40 |
+
|
41 |
+
def spectrogram_torch(y, n_fft, sampling_rate, hop_size, win_size, center=False):
|
42 |
+
if torch.min(y) < -1.1:
|
43 |
+
print("min value is ", torch.min(y))
|
44 |
+
if torch.max(y) > 1.1:
|
45 |
+
print("max value is ", torch.max(y))
|
46 |
+
|
47 |
+
global hann_window
|
48 |
+
dtype_device = str(y.dtype) + "_" + str(y.device)
|
49 |
+
wnsize_dtype_device = str(win_size) + "_" + dtype_device
|
50 |
+
if wnsize_dtype_device not in hann_window:
|
51 |
+
hann_window[wnsize_dtype_device] = torch.hann_window(win_size).to(
|
52 |
+
dtype=y.dtype, device=y.device
|
53 |
+
)
|
54 |
+
|
55 |
+
y = torch.nn.functional.pad(
|
56 |
+
y.unsqueeze(1),
|
57 |
+
(int((n_fft - hop_size) / 2), int((n_fft - hop_size) / 2)),
|
58 |
+
mode="reflect",
|
59 |
+
)
|
60 |
+
y = y.squeeze(1)
|
61 |
+
|
62 |
+
spec = torch.stft(
|
63 |
+
y,
|
64 |
+
n_fft,
|
65 |
+
hop_length=hop_size,
|
66 |
+
win_length=win_size,
|
67 |
+
window=hann_window[wnsize_dtype_device],
|
68 |
+
center=center,
|
69 |
+
pad_mode="reflect",
|
70 |
+
normalized=False,
|
71 |
+
onesided=True,
|
72 |
+
return_complex=False,
|
73 |
+
)
|
74 |
+
|
75 |
+
spec = torch.sqrt(spec.pow(2).sum(-1) + 1e-6)
|
76 |
+
return spec
|
77 |
+
|
78 |
+
|
79 |
+
def spectrogram_torch_conv(y, n_fft, sampling_rate, hop_size, win_size, center=False):
|
80 |
+
global hann_window
|
81 |
+
dtype_device = str(y.dtype) + '_' + str(y.device)
|
82 |
+
wnsize_dtype_device = str(win_size) + '_' + dtype_device
|
83 |
+
if wnsize_dtype_device not in hann_window:
|
84 |
+
hann_window[wnsize_dtype_device] = torch.hann_window(win_size).to(dtype=y.dtype, device=y.device)
|
85 |
+
|
86 |
+
y = torch.nn.functional.pad(y.unsqueeze(1), (int((n_fft-hop_size)/2), int((n_fft-hop_size)/2)), mode='reflect')
|
87 |
+
|
88 |
+
# ******************** original ************************#
|
89 |
+
# y = y.squeeze(1)
|
90 |
+
# spec1 = torch.stft(y, n_fft, hop_length=hop_size, win_length=win_size, window=hann_window[wnsize_dtype_device],
|
91 |
+
# center=center, pad_mode='reflect', normalized=False, onesided=True, return_complex=False)
|
92 |
+
|
93 |
+
# ******************** ConvSTFT ************************#
|
94 |
+
freq_cutoff = n_fft // 2 + 1
|
95 |
+
fourier_basis = torch.view_as_real(torch.fft.fft(torch.eye(n_fft)))
|
96 |
+
forward_basis = fourier_basis[:freq_cutoff].permute(2, 0, 1).reshape(-1, 1, fourier_basis.shape[1])
|
97 |
+
forward_basis = forward_basis * torch.as_tensor(librosa.util.pad_center(torch.hann_window(win_size), size=n_fft)).float()
|
98 |
+
|
99 |
+
import torch.nn.functional as F
|
100 |
+
|
101 |
+
# if center:
|
102 |
+
# signal = F.pad(y[:, None, None, :], (n_fft // 2, n_fft // 2, 0, 0), mode = 'reflect').squeeze(1)
|
103 |
+
assert center is False
|
104 |
+
|
105 |
+
forward_transform_squared = F.conv1d(y, forward_basis.to(y.device), stride = hop_size)
|
106 |
+
spec2 = torch.stack([forward_transform_squared[:, :freq_cutoff, :], forward_transform_squared[:, freq_cutoff:, :]], dim = -1)
|
107 |
+
|
108 |
+
|
109 |
+
# ******************** Verification ************************#
|
110 |
+
spec1 = torch.stft(y.squeeze(1), n_fft, hop_length=hop_size, win_length=win_size, window=hann_window[wnsize_dtype_device],
|
111 |
+
center=center, pad_mode='reflect', normalized=False, onesided=True, return_complex=False)
|
112 |
+
assert torch.allclose(spec1, spec2, atol=1e-4)
|
113 |
+
|
114 |
+
spec = torch.sqrt(spec2.pow(2).sum(-1) + 1e-6)
|
115 |
+
return spec
|
116 |
+
|
117 |
+
|
118 |
+
def spec_to_mel_torch(spec, n_fft, num_mels, sampling_rate, fmin, fmax):
|
119 |
+
global mel_basis
|
120 |
+
dtype_device = str(spec.dtype) + "_" + str(spec.device)
|
121 |
+
fmax_dtype_device = str(fmax) + "_" + dtype_device
|
122 |
+
if fmax_dtype_device not in mel_basis:
|
123 |
+
mel = librosa_mel_fn(sr=sampling_rate, n_fft=n_fft, n_mels=num_mels, fmin=fmin, fmax=fmax)
|
124 |
+
mel_basis[fmax_dtype_device] = torch.from_numpy(mel).to(
|
125 |
+
dtype=spec.dtype, device=spec.device
|
126 |
+
)
|
127 |
+
spec = torch.matmul(mel_basis[fmax_dtype_device], spec)
|
128 |
+
spec = spectral_normalize_torch(spec)
|
129 |
+
return spec
|
130 |
+
|
131 |
+
|
132 |
+
def mel_spectrogram_torch(
|
133 |
+
y, n_fft, num_mels, sampling_rate, hop_size, win_size, fmin, fmax, center=False
|
134 |
+
):
|
135 |
+
global mel_basis, hann_window
|
136 |
+
dtype_device = str(y.dtype) + "_" + str(y.device)
|
137 |
+
fmax_dtype_device = str(fmax) + "_" + dtype_device
|
138 |
+
wnsize_dtype_device = str(win_size) + "_" + dtype_device
|
139 |
+
if fmax_dtype_device not in mel_basis:
|
140 |
+
mel = librosa_mel_fn(sr=sampling_rate, n_fft=n_fft, n_mels=num_mels, fmin=fmin, fmax=fmax)
|
141 |
+
mel_basis[fmax_dtype_device] = torch.from_numpy(mel).to(
|
142 |
+
dtype=y.dtype, device=y.device
|
143 |
+
)
|
144 |
+
if wnsize_dtype_device not in hann_window:
|
145 |
+
hann_window[wnsize_dtype_device] = torch.hann_window(win_size).to(
|
146 |
+
dtype=y.dtype, device=y.device
|
147 |
+
)
|
148 |
+
|
149 |
+
y = torch.nn.functional.pad(
|
150 |
+
y.unsqueeze(1),
|
151 |
+
(int((n_fft - hop_size) / 2), int((n_fft - hop_size) / 2)),
|
152 |
+
mode="reflect",
|
153 |
+
)
|
154 |
+
y = y.squeeze(1)
|
155 |
+
|
156 |
+
spec = torch.stft(
|
157 |
+
y,
|
158 |
+
n_fft,
|
159 |
+
hop_length=hop_size,
|
160 |
+
win_length=win_size,
|
161 |
+
window=hann_window[wnsize_dtype_device],
|
162 |
+
center=center,
|
163 |
+
pad_mode="reflect",
|
164 |
+
normalized=False,
|
165 |
+
onesided=True,
|
166 |
+
return_complex=False,
|
167 |
+
)
|
168 |
+
|
169 |
+
spec = torch.sqrt(spec.pow(2).sum(-1) + 1e-6)
|
170 |
+
|
171 |
+
spec = torch.matmul(mel_basis[fmax_dtype_device], spec)
|
172 |
+
spec = spectral_normalize_torch(spec)
|
173 |
+
|
174 |
+
return spec
|
melo/models.py
ADDED
@@ -0,0 +1,1030 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import math
|
2 |
+
import torch
|
3 |
+
from torch import nn
|
4 |
+
from torch.nn import functional as F
|
5 |
+
|
6 |
+
from melo import commons
|
7 |
+
from melo import modules
|
8 |
+
from melo import attentions
|
9 |
+
|
10 |
+
from torch.nn import Conv1d, ConvTranspose1d, Conv2d
|
11 |
+
from torch.nn.utils import weight_norm, remove_weight_norm, spectral_norm
|
12 |
+
|
13 |
+
from melo.commons import init_weights, get_padding
|
14 |
+
import melo.monotonic_align as monotonic_align
|
15 |
+
|
16 |
+
|
17 |
+
class DurationDiscriminator(nn.Module): # vits2
|
18 |
+
def __init__(
|
19 |
+
self, in_channels, filter_channels, kernel_size, p_dropout, gin_channels=0
|
20 |
+
):
|
21 |
+
super().__init__()
|
22 |
+
self.in_channels = in_channels
|
23 |
+
self.filter_channels = filter_channels
|
24 |
+
self.kernel_size = kernel_size
|
25 |
+
self.p_dropout = p_dropout
|
26 |
+
self.gin_channels = gin_channels
|
27 |
+
|
28 |
+
self.drop = nn.Dropout(p_dropout)
|
29 |
+
self.conv_1 = nn.Conv1d(
|
30 |
+
in_channels, filter_channels, kernel_size, padding=kernel_size // 2
|
31 |
+
)
|
32 |
+
self.norm_1 = modules.LayerNorm(filter_channels)
|
33 |
+
self.conv_2 = nn.Conv1d(
|
34 |
+
filter_channels, filter_channels, kernel_size, padding=kernel_size // 2
|
35 |
+
)
|
36 |
+
self.norm_2 = modules.LayerNorm(filter_channels)
|
37 |
+
self.dur_proj = nn.Conv1d(1, filter_channels, 1)
|
38 |
+
|
39 |
+
self.pre_out_conv_1 = nn.Conv1d(
|
40 |
+
2 * filter_channels, filter_channels, kernel_size, padding=kernel_size // 2
|
41 |
+
)
|
42 |
+
self.pre_out_norm_1 = modules.LayerNorm(filter_channels)
|
43 |
+
self.pre_out_conv_2 = nn.Conv1d(
|
44 |
+
filter_channels, filter_channels, kernel_size, padding=kernel_size // 2
|
45 |
+
)
|
46 |
+
self.pre_out_norm_2 = modules.LayerNorm(filter_channels)
|
47 |
+
|
48 |
+
if gin_channels != 0:
|
49 |
+
self.cond = nn.Conv1d(gin_channels, in_channels, 1)
|
50 |
+
|
51 |
+
self.output_layer = nn.Sequential(nn.Linear(filter_channels, 1), nn.Sigmoid())
|
52 |
+
|
53 |
+
def forward_probability(self, x, x_mask, dur, g=None):
|
54 |
+
dur = self.dur_proj(dur)
|
55 |
+
x = torch.cat([x, dur], dim=1)
|
56 |
+
x = self.pre_out_conv_1(x * x_mask)
|
57 |
+
x = torch.relu(x)
|
58 |
+
x = self.pre_out_norm_1(x)
|
59 |
+
x = self.drop(x)
|
60 |
+
x = self.pre_out_conv_2(x * x_mask)
|
61 |
+
x = torch.relu(x)
|
62 |
+
x = self.pre_out_norm_2(x)
|
63 |
+
x = self.drop(x)
|
64 |
+
x = x * x_mask
|
65 |
+
x = x.transpose(1, 2)
|
66 |
+
output_prob = self.output_layer(x)
|
67 |
+
return output_prob
|
68 |
+
|
69 |
+
def forward(self, x, x_mask, dur_r, dur_hat, g=None):
|
70 |
+
x = torch.detach(x)
|
71 |
+
if g is not None:
|
72 |
+
g = torch.detach(g)
|
73 |
+
x = x + self.cond(g)
|
74 |
+
x = self.conv_1(x * x_mask)
|
75 |
+
x = torch.relu(x)
|
76 |
+
x = self.norm_1(x)
|
77 |
+
x = self.drop(x)
|
78 |
+
x = self.conv_2(x * x_mask)
|
79 |
+
x = torch.relu(x)
|
80 |
+
x = self.norm_2(x)
|
81 |
+
x = self.drop(x)
|
82 |
+
|
83 |
+
output_probs = []
|
84 |
+
for dur in [dur_r, dur_hat]:
|
85 |
+
output_prob = self.forward_probability(x, x_mask, dur, g)
|
86 |
+
output_probs.append(output_prob)
|
87 |
+
|
88 |
+
return output_probs
|
89 |
+
|
90 |
+
|
91 |
+
class TransformerCouplingBlock(nn.Module):
|
92 |
+
def __init__(
|
93 |
+
self,
|
94 |
+
channels,
|
95 |
+
hidden_channels,
|
96 |
+
filter_channels,
|
97 |
+
n_heads,
|
98 |
+
n_layers,
|
99 |
+
kernel_size,
|
100 |
+
p_dropout,
|
101 |
+
n_flows=4,
|
102 |
+
gin_channels=0,
|
103 |
+
share_parameter=False,
|
104 |
+
):
|
105 |
+
super().__init__()
|
106 |
+
self.channels = channels
|
107 |
+
self.hidden_channels = hidden_channels
|
108 |
+
self.kernel_size = kernel_size
|
109 |
+
self.n_layers = n_layers
|
110 |
+
self.n_flows = n_flows
|
111 |
+
self.gin_channels = gin_channels
|
112 |
+
|
113 |
+
self.flows = nn.ModuleList()
|
114 |
+
|
115 |
+
self.wn = (
|
116 |
+
attentions.FFT(
|
117 |
+
hidden_channels,
|
118 |
+
filter_channels,
|
119 |
+
n_heads,
|
120 |
+
n_layers,
|
121 |
+
kernel_size,
|
122 |
+
p_dropout,
|
123 |
+
isflow=True,
|
124 |
+
gin_channels=self.gin_channels,
|
125 |
+
)
|
126 |
+
if share_parameter
|
127 |
+
else None
|
128 |
+
)
|
129 |
+
|
130 |
+
for i in range(n_flows):
|
131 |
+
self.flows.append(
|
132 |
+
modules.TransformerCouplingLayer(
|
133 |
+
channels,
|
134 |
+
hidden_channels,
|
135 |
+
kernel_size,
|
136 |
+
n_layers,
|
137 |
+
n_heads,
|
138 |
+
p_dropout,
|
139 |
+
filter_channels,
|
140 |
+
mean_only=True,
|
141 |
+
wn_sharing_parameter=self.wn,
|
142 |
+
gin_channels=self.gin_channels,
|
143 |
+
)
|
144 |
+
)
|
145 |
+
self.flows.append(modules.Flip())
|
146 |
+
|
147 |
+
def forward(self, x, x_mask, g=None, reverse=False):
|
148 |
+
if not reverse:
|
149 |
+
for flow in self.flows:
|
150 |
+
x, _ = flow(x, x_mask, g=g, reverse=reverse)
|
151 |
+
else:
|
152 |
+
for flow in reversed(self.flows):
|
153 |
+
x = flow(x, x_mask, g=g, reverse=reverse)
|
154 |
+
return x
|
155 |
+
|
156 |
+
|
157 |
+
class StochasticDurationPredictor(nn.Module):
|
158 |
+
def __init__(
|
159 |
+
self,
|
160 |
+
in_channels,
|
161 |
+
filter_channels,
|
162 |
+
kernel_size,
|
163 |
+
p_dropout,
|
164 |
+
n_flows=4,
|
165 |
+
gin_channels=0,
|
166 |
+
):
|
167 |
+
super().__init__()
|
168 |
+
filter_channels = in_channels # it needs to be removed from future version.
|
169 |
+
self.in_channels = in_channels
|
170 |
+
self.filter_channels = filter_channels
|
171 |
+
self.kernel_size = kernel_size
|
172 |
+
self.p_dropout = p_dropout
|
173 |
+
self.n_flows = n_flows
|
174 |
+
self.gin_channels = gin_channels
|
175 |
+
|
176 |
+
self.log_flow = modules.Log()
|
177 |
+
self.flows = nn.ModuleList()
|
178 |
+
self.flows.append(modules.ElementwiseAffine(2))
|
179 |
+
for i in range(n_flows):
|
180 |
+
self.flows.append(
|
181 |
+
modules.ConvFlow(2, filter_channels, kernel_size, n_layers=3)
|
182 |
+
)
|
183 |
+
self.flows.append(modules.Flip())
|
184 |
+
|
185 |
+
self.post_pre = nn.Conv1d(1, filter_channels, 1)
|
186 |
+
self.post_proj = nn.Conv1d(filter_channels, filter_channels, 1)
|
187 |
+
self.post_convs = modules.DDSConv(
|
188 |
+
filter_channels, kernel_size, n_layers=3, p_dropout=p_dropout
|
189 |
+
)
|
190 |
+
self.post_flows = nn.ModuleList()
|
191 |
+
self.post_flows.append(modules.ElementwiseAffine(2))
|
192 |
+
for i in range(4):
|
193 |
+
self.post_flows.append(
|
194 |
+
modules.ConvFlow(2, filter_channels, kernel_size, n_layers=3)
|
195 |
+
)
|
196 |
+
self.post_flows.append(modules.Flip())
|
197 |
+
|
198 |
+
self.pre = nn.Conv1d(in_channels, filter_channels, 1)
|
199 |
+
self.proj = nn.Conv1d(filter_channels, filter_channels, 1)
|
200 |
+
self.convs = modules.DDSConv(
|
201 |
+
filter_channels, kernel_size, n_layers=3, p_dropout=p_dropout
|
202 |
+
)
|
203 |
+
if gin_channels != 0:
|
204 |
+
self.cond = nn.Conv1d(gin_channels, filter_channels, 1)
|
205 |
+
|
206 |
+
def forward(self, x, x_mask, w=None, g=None, reverse=False, noise_scale=1.0):
|
207 |
+
x = torch.detach(x)
|
208 |
+
x = self.pre(x)
|
209 |
+
if g is not None:
|
210 |
+
g = torch.detach(g)
|
211 |
+
x = x + self.cond(g)
|
212 |
+
x = self.convs(x, x_mask)
|
213 |
+
x = self.proj(x) * x_mask
|
214 |
+
|
215 |
+
if not reverse:
|
216 |
+
flows = self.flows
|
217 |
+
assert w is not None
|
218 |
+
|
219 |
+
logdet_tot_q = 0
|
220 |
+
h_w = self.post_pre(w)
|
221 |
+
h_w = self.post_convs(h_w, x_mask)
|
222 |
+
h_w = self.post_proj(h_w) * x_mask
|
223 |
+
e_q = (
|
224 |
+
torch.randn(w.size(0), 2, w.size(2)).to(device=x.device, dtype=x.dtype)
|
225 |
+
* x_mask
|
226 |
+
)
|
227 |
+
z_q = e_q
|
228 |
+
for flow in self.post_flows:
|
229 |
+
z_q, logdet_q = flow(z_q, x_mask, g=(x + h_w))
|
230 |
+
logdet_tot_q += logdet_q
|
231 |
+
z_u, z1 = torch.split(z_q, [1, 1], 1)
|
232 |
+
u = torch.sigmoid(z_u) * x_mask
|
233 |
+
z0 = (w - u) * x_mask
|
234 |
+
logdet_tot_q += torch.sum(
|
235 |
+
(F.logsigmoid(z_u) + F.logsigmoid(-z_u)) * x_mask, [1, 2]
|
236 |
+
)
|
237 |
+
logq = (
|
238 |
+
torch.sum(-0.5 * (math.log(2 * math.pi) + (e_q**2)) * x_mask, [1, 2])
|
239 |
+
- logdet_tot_q
|
240 |
+
)
|
241 |
+
|
242 |
+
logdet_tot = 0
|
243 |
+
z0, logdet = self.log_flow(z0, x_mask)
|
244 |
+
logdet_tot += logdet
|
245 |
+
z = torch.cat([z0, z1], 1)
|
246 |
+
for flow in flows:
|
247 |
+
z, logdet = flow(z, x_mask, g=x, reverse=reverse)
|
248 |
+
logdet_tot = logdet_tot + logdet
|
249 |
+
nll = (
|
250 |
+
torch.sum(0.5 * (math.log(2 * math.pi) + (z**2)) * x_mask, [1, 2])
|
251 |
+
- logdet_tot
|
252 |
+
)
|
253 |
+
return nll + logq # [b]
|
254 |
+
else:
|
255 |
+
flows = list(reversed(self.flows))
|
256 |
+
flows = flows[:-2] + [flows[-1]] # remove a useless vflow
|
257 |
+
z = (
|
258 |
+
torch.randn(x.size(0), 2, x.size(2)).to(device=x.device, dtype=x.dtype)
|
259 |
+
* noise_scale
|
260 |
+
)
|
261 |
+
for flow in flows:
|
262 |
+
z = flow(z, x_mask, g=x, reverse=reverse)
|
263 |
+
z0, z1 = torch.split(z, [1, 1], 1)
|
264 |
+
logw = z0
|
265 |
+
return logw
|
266 |
+
|
267 |
+
|
268 |
+
class DurationPredictor(nn.Module):
|
269 |
+
def __init__(
|
270 |
+
self, in_channels, filter_channels, kernel_size, p_dropout, gin_channels=0
|
271 |
+
):
|
272 |
+
super().__init__()
|
273 |
+
|
274 |
+
self.in_channels = in_channels
|
275 |
+
self.filter_channels = filter_channels
|
276 |
+
self.kernel_size = kernel_size
|
277 |
+
self.p_dropout = p_dropout
|
278 |
+
self.gin_channels = gin_channels
|
279 |
+
|
280 |
+
self.drop = nn.Dropout(p_dropout)
|
281 |
+
self.conv_1 = nn.Conv1d(
|
282 |
+
in_channels, filter_channels, kernel_size, padding=kernel_size // 2
|
283 |
+
)
|
284 |
+
self.norm_1 = modules.LayerNorm(filter_channels)
|
285 |
+
self.conv_2 = nn.Conv1d(
|
286 |
+
filter_channels, filter_channels, kernel_size, padding=kernel_size // 2
|
287 |
+
)
|
288 |
+
self.norm_2 = modules.LayerNorm(filter_channels)
|
289 |
+
self.proj = nn.Conv1d(filter_channels, 1, 1)
|
290 |
+
|
291 |
+
if gin_channels != 0:
|
292 |
+
self.cond = nn.Conv1d(gin_channels, in_channels, 1)
|
293 |
+
|
294 |
+
def forward(self, x, x_mask, g=None):
|
295 |
+
x = torch.detach(x)
|
296 |
+
if g is not None:
|
297 |
+
g = torch.detach(g)
|
298 |
+
x = x + self.cond(g)
|
299 |
+
x = self.conv_1(x * x_mask)
|
300 |
+
x = torch.relu(x)
|
301 |
+
x = self.norm_1(x)
|
302 |
+
x = self.drop(x)
|
303 |
+
x = self.conv_2(x * x_mask)
|
304 |
+
x = torch.relu(x)
|
305 |
+
x = self.norm_2(x)
|
306 |
+
x = self.drop(x)
|
307 |
+
x = self.proj(x * x_mask)
|
308 |
+
return x * x_mask
|
309 |
+
|
310 |
+
|
311 |
+
class TextEncoder(nn.Module):
|
312 |
+
def __init__(
|
313 |
+
self,
|
314 |
+
n_vocab,
|
315 |
+
out_channels,
|
316 |
+
hidden_channels,
|
317 |
+
filter_channels,
|
318 |
+
n_heads,
|
319 |
+
n_layers,
|
320 |
+
kernel_size,
|
321 |
+
p_dropout,
|
322 |
+
gin_channels=0,
|
323 |
+
num_languages=None,
|
324 |
+
num_tones=None,
|
325 |
+
):
|
326 |
+
super().__init__()
|
327 |
+
if num_languages is None:
|
328 |
+
from text import num_languages
|
329 |
+
if num_tones is None:
|
330 |
+
from text import num_tones
|
331 |
+
self.n_vocab = n_vocab
|
332 |
+
self.out_channels = out_channels
|
333 |
+
self.hidden_channels = hidden_channels
|
334 |
+
self.filter_channels = filter_channels
|
335 |
+
self.n_heads = n_heads
|
336 |
+
self.n_layers = n_layers
|
337 |
+
self.kernel_size = kernel_size
|
338 |
+
self.p_dropout = p_dropout
|
339 |
+
self.gin_channels = gin_channels
|
340 |
+
self.emb = nn.Embedding(n_vocab, hidden_channels)
|
341 |
+
nn.init.normal_(self.emb.weight, 0.0, hidden_channels**-0.5)
|
342 |
+
self.tone_emb = nn.Embedding(num_tones, hidden_channels)
|
343 |
+
nn.init.normal_(self.tone_emb.weight, 0.0, hidden_channels**-0.5)
|
344 |
+
self.language_emb = nn.Embedding(num_languages, hidden_channels)
|
345 |
+
nn.init.normal_(self.language_emb.weight, 0.0, hidden_channels**-0.5)
|
346 |
+
self.bert_proj = nn.Conv1d(1024, hidden_channels, 1)
|
347 |
+
self.ja_bert_proj = nn.Conv1d(768, hidden_channels, 1)
|
348 |
+
|
349 |
+
self.encoder = attentions.Encoder(
|
350 |
+
hidden_channels,
|
351 |
+
filter_channels,
|
352 |
+
n_heads,
|
353 |
+
n_layers,
|
354 |
+
kernel_size,
|
355 |
+
p_dropout,
|
356 |
+
gin_channels=self.gin_channels,
|
357 |
+
)
|
358 |
+
self.proj = nn.Conv1d(hidden_channels, out_channels * 2, 1)
|
359 |
+
|
360 |
+
def forward(self, x, x_lengths, tone, language, bert, ja_bert, g=None):
|
361 |
+
bert_emb = self.bert_proj(bert).transpose(1, 2)
|
362 |
+
ja_bert_emb = self.ja_bert_proj(ja_bert).transpose(1, 2)
|
363 |
+
x = (
|
364 |
+
self.emb(x)
|
365 |
+
+ self.tone_emb(tone)
|
366 |
+
+ self.language_emb(language)
|
367 |
+
+ bert_emb
|
368 |
+
+ ja_bert_emb
|
369 |
+
) * math.sqrt(
|
370 |
+
self.hidden_channels
|
371 |
+
) # [b, t, h]
|
372 |
+
x = torch.transpose(x, 1, -1) # [b, h, t]
|
373 |
+
x_mask = torch.unsqueeze(commons.sequence_mask(x_lengths, x.size(2)), 1).to(
|
374 |
+
x.dtype
|
375 |
+
)
|
376 |
+
|
377 |
+
x = self.encoder(x * x_mask, x_mask, g=g)
|
378 |
+
stats = self.proj(x) * x_mask
|
379 |
+
|
380 |
+
m, logs = torch.split(stats, self.out_channels, dim=1)
|
381 |
+
return x, m, logs, x_mask
|
382 |
+
|
383 |
+
|
384 |
+
class ResidualCouplingBlock(nn.Module):
|
385 |
+
def __init__(
|
386 |
+
self,
|
387 |
+
channels,
|
388 |
+
hidden_channels,
|
389 |
+
kernel_size,
|
390 |
+
dilation_rate,
|
391 |
+
n_layers,
|
392 |
+
n_flows=4,
|
393 |
+
gin_channels=0,
|
394 |
+
):
|
395 |
+
super().__init__()
|
396 |
+
self.channels = channels
|
397 |
+
self.hidden_channels = hidden_channels
|
398 |
+
self.kernel_size = kernel_size
|
399 |
+
self.dilation_rate = dilation_rate
|
400 |
+
self.n_layers = n_layers
|
401 |
+
self.n_flows = n_flows
|
402 |
+
self.gin_channels = gin_channels
|
403 |
+
|
404 |
+
self.flows = nn.ModuleList()
|
405 |
+
for i in range(n_flows):
|
406 |
+
self.flows.append(
|
407 |
+
modules.ResidualCouplingLayer(
|
408 |
+
channels,
|
409 |
+
hidden_channels,
|
410 |
+
kernel_size,
|
411 |
+
dilation_rate,
|
412 |
+
n_layers,
|
413 |
+
gin_channels=gin_channels,
|
414 |
+
mean_only=True,
|
415 |
+
)
|
416 |
+
)
|
417 |
+
self.flows.append(modules.Flip())
|
418 |
+
|
419 |
+
def forward(self, x, x_mask, g=None, reverse=False):
|
420 |
+
if not reverse:
|
421 |
+
for flow in self.flows:
|
422 |
+
x, _ = flow(x, x_mask, g=g, reverse=reverse)
|
423 |
+
else:
|
424 |
+
for flow in reversed(self.flows):
|
425 |
+
x = flow(x, x_mask, g=g, reverse=reverse)
|
426 |
+
return x
|
427 |
+
|
428 |
+
|
429 |
+
class PosteriorEncoder(nn.Module):
|
430 |
+
def __init__(
|
431 |
+
self,
|
432 |
+
in_channels,
|
433 |
+
out_channels,
|
434 |
+
hidden_channels,
|
435 |
+
kernel_size,
|
436 |
+
dilation_rate,
|
437 |
+
n_layers,
|
438 |
+
gin_channels=0,
|
439 |
+
):
|
440 |
+
super().__init__()
|
441 |
+
self.in_channels = in_channels
|
442 |
+
self.out_channels = out_channels
|
443 |
+
self.hidden_channels = hidden_channels
|
444 |
+
self.kernel_size = kernel_size
|
445 |
+
self.dilation_rate = dilation_rate
|
446 |
+
self.n_layers = n_layers
|
447 |
+
self.gin_channels = gin_channels
|
448 |
+
|
449 |
+
self.pre = nn.Conv1d(in_channels, hidden_channels, 1)
|
450 |
+
self.enc = modules.WN(
|
451 |
+
hidden_channels,
|
452 |
+
kernel_size,
|
453 |
+
dilation_rate,
|
454 |
+
n_layers,
|
455 |
+
gin_channels=gin_channels,
|
456 |
+
)
|
457 |
+
self.proj = nn.Conv1d(hidden_channels, out_channels * 2, 1)
|
458 |
+
|
459 |
+
def forward(self, x, x_lengths, g=None, tau=1.0):
|
460 |
+
x_mask = torch.unsqueeze(commons.sequence_mask(x_lengths, x.size(2)), 1).to(
|
461 |
+
x.dtype
|
462 |
+
)
|
463 |
+
x = self.pre(x) * x_mask
|
464 |
+
x = self.enc(x, x_mask, g=g)
|
465 |
+
stats = self.proj(x) * x_mask
|
466 |
+
m, logs = torch.split(stats, self.out_channels, dim=1)
|
467 |
+
z = (m + torch.randn_like(m) * tau * torch.exp(logs)) * x_mask
|
468 |
+
return z, m, logs, x_mask
|
469 |
+
|
470 |
+
|
471 |
+
class Generator(torch.nn.Module):
|
472 |
+
def __init__(
|
473 |
+
self,
|
474 |
+
initial_channel,
|
475 |
+
resblock,
|
476 |
+
resblock_kernel_sizes,
|
477 |
+
resblock_dilation_sizes,
|
478 |
+
upsample_rates,
|
479 |
+
upsample_initial_channel,
|
480 |
+
upsample_kernel_sizes,
|
481 |
+
gin_channels=0,
|
482 |
+
):
|
483 |
+
super(Generator, self).__init__()
|
484 |
+
self.num_kernels = len(resblock_kernel_sizes)
|
485 |
+
self.num_upsamples = len(upsample_rates)
|
486 |
+
self.conv_pre = Conv1d(
|
487 |
+
initial_channel, upsample_initial_channel, 7, 1, padding=3
|
488 |
+
)
|
489 |
+
resblock = modules.ResBlock1 if resblock == "1" else modules.ResBlock2
|
490 |
+
|
491 |
+
self.ups = nn.ModuleList()
|
492 |
+
for i, (u, k) in enumerate(zip(upsample_rates, upsample_kernel_sizes)):
|
493 |
+
self.ups.append(
|
494 |
+
weight_norm(
|
495 |
+
ConvTranspose1d(
|
496 |
+
upsample_initial_channel // (2**i),
|
497 |
+
upsample_initial_channel // (2 ** (i + 1)),
|
498 |
+
k,
|
499 |
+
u,
|
500 |
+
padding=(k - u) // 2,
|
501 |
+
)
|
502 |
+
)
|
503 |
+
)
|
504 |
+
|
505 |
+
self.resblocks = nn.ModuleList()
|
506 |
+
for i in range(len(self.ups)):
|
507 |
+
ch = upsample_initial_channel // (2 ** (i + 1))
|
508 |
+
for j, (k, d) in enumerate(
|
509 |
+
zip(resblock_kernel_sizes, resblock_dilation_sizes)
|
510 |
+
):
|
511 |
+
self.resblocks.append(resblock(ch, k, d))
|
512 |
+
|
513 |
+
self.conv_post = Conv1d(ch, 1, 7, 1, padding=3, bias=False)
|
514 |
+
self.ups.apply(init_weights)
|
515 |
+
|
516 |
+
if gin_channels != 0:
|
517 |
+
self.cond = nn.Conv1d(gin_channels, upsample_initial_channel, 1)
|
518 |
+
|
519 |
+
def forward(self, x, g=None):
|
520 |
+
x = self.conv_pre(x)
|
521 |
+
if g is not None:
|
522 |
+
x = x + self.cond(g)
|
523 |
+
|
524 |
+
for i in range(self.num_upsamples):
|
525 |
+
x = F.leaky_relu(x, modules.LRELU_SLOPE)
|
526 |
+
x = self.ups[i](x)
|
527 |
+
xs = None
|
528 |
+
for j in range(self.num_kernels):
|
529 |
+
if xs is None:
|
530 |
+
xs = self.resblocks[i * self.num_kernels + j](x)
|
531 |
+
else:
|
532 |
+
xs += self.resblocks[i * self.num_kernels + j](x)
|
533 |
+
x = xs / self.num_kernels
|
534 |
+
x = F.leaky_relu(x)
|
535 |
+
x = self.conv_post(x)
|
536 |
+
x = torch.tanh(x)
|
537 |
+
|
538 |
+
return x
|
539 |
+
|
540 |
+
def remove_weight_norm(self):
|
541 |
+
print("Removing weight norm...")
|
542 |
+
for layer in self.ups:
|
543 |
+
remove_weight_norm(layer)
|
544 |
+
for layer in self.resblocks:
|
545 |
+
layer.remove_weight_norm()
|
546 |
+
|
547 |
+
|
548 |
+
class DiscriminatorP(torch.nn.Module):
|
549 |
+
def __init__(self, period, kernel_size=5, stride=3, use_spectral_norm=False):
|
550 |
+
super(DiscriminatorP, self).__init__()
|
551 |
+
self.period = period
|
552 |
+
self.use_spectral_norm = use_spectral_norm
|
553 |
+
norm_f = weight_norm if use_spectral_norm is False else spectral_norm
|
554 |
+
self.convs = nn.ModuleList(
|
555 |
+
[
|
556 |
+
norm_f(
|
557 |
+
Conv2d(
|
558 |
+
1,
|
559 |
+
32,
|
560 |
+
(kernel_size, 1),
|
561 |
+
(stride, 1),
|
562 |
+
padding=(get_padding(kernel_size, 1), 0),
|
563 |
+
)
|
564 |
+
),
|
565 |
+
norm_f(
|
566 |
+
Conv2d(
|
567 |
+
32,
|
568 |
+
128,
|
569 |
+
(kernel_size, 1),
|
570 |
+
(stride, 1),
|
571 |
+
padding=(get_padding(kernel_size, 1), 0),
|
572 |
+
)
|
573 |
+
),
|
574 |
+
norm_f(
|
575 |
+
Conv2d(
|
576 |
+
128,
|
577 |
+
512,
|
578 |
+
(kernel_size, 1),
|
579 |
+
(stride, 1),
|
580 |
+
padding=(get_padding(kernel_size, 1), 0),
|
581 |
+
)
|
582 |
+
),
|
583 |
+
norm_f(
|
584 |
+
Conv2d(
|
585 |
+
512,
|
586 |
+
1024,
|
587 |
+
(kernel_size, 1),
|
588 |
+
(stride, 1),
|
589 |
+
padding=(get_padding(kernel_size, 1), 0),
|
590 |
+
)
|
591 |
+
),
|
592 |
+
norm_f(
|
593 |
+
Conv2d(
|
594 |
+
1024,
|
595 |
+
1024,
|
596 |
+
(kernel_size, 1),
|
597 |
+
1,
|
598 |
+
padding=(get_padding(kernel_size, 1), 0),
|
599 |
+
)
|
600 |
+
),
|
601 |
+
]
|
602 |
+
)
|
603 |
+
self.conv_post = norm_f(Conv2d(1024, 1, (3, 1), 1, padding=(1, 0)))
|
604 |
+
|
605 |
+
def forward(self, x):
|
606 |
+
fmap = []
|
607 |
+
|
608 |
+
# 1d to 2d
|
609 |
+
b, c, t = x.shape
|
610 |
+
if t % self.period != 0: # pad first
|
611 |
+
n_pad = self.period - (t % self.period)
|
612 |
+
x = F.pad(x, (0, n_pad), "reflect")
|
613 |
+
t = t + n_pad
|
614 |
+
x = x.view(b, c, t // self.period, self.period)
|
615 |
+
|
616 |
+
for layer in self.convs:
|
617 |
+
x = layer(x)
|
618 |
+
x = F.leaky_relu(x, modules.LRELU_SLOPE)
|
619 |
+
fmap.append(x)
|
620 |
+
x = self.conv_post(x)
|
621 |
+
fmap.append(x)
|
622 |
+
x = torch.flatten(x, 1, -1)
|
623 |
+
|
624 |
+
return x, fmap
|
625 |
+
|
626 |
+
|
627 |
+
class DiscriminatorS(torch.nn.Module):
|
628 |
+
def __init__(self, use_spectral_norm=False):
|
629 |
+
super(DiscriminatorS, self).__init__()
|
630 |
+
norm_f = weight_norm if use_spectral_norm is False else spectral_norm
|
631 |
+
self.convs = nn.ModuleList(
|
632 |
+
[
|
633 |
+
norm_f(Conv1d(1, 16, 15, 1, padding=7)),
|
634 |
+
norm_f(Conv1d(16, 64, 41, 4, groups=4, padding=20)),
|
635 |
+
norm_f(Conv1d(64, 256, 41, 4, groups=16, padding=20)),
|
636 |
+
norm_f(Conv1d(256, 1024, 41, 4, groups=64, padding=20)),
|
637 |
+
norm_f(Conv1d(1024, 1024, 41, 4, groups=256, padding=20)),
|
638 |
+
norm_f(Conv1d(1024, 1024, 5, 1, padding=2)),
|
639 |
+
]
|
640 |
+
)
|
641 |
+
self.conv_post = norm_f(Conv1d(1024, 1, 3, 1, padding=1))
|
642 |
+
|
643 |
+
def forward(self, x):
|
644 |
+
fmap = []
|
645 |
+
|
646 |
+
for layer in self.convs:
|
647 |
+
x = layer(x)
|
648 |
+
x = F.leaky_relu(x, modules.LRELU_SLOPE)
|
649 |
+
fmap.append(x)
|
650 |
+
x = self.conv_post(x)
|
651 |
+
fmap.append(x)
|
652 |
+
x = torch.flatten(x, 1, -1)
|
653 |
+
|
654 |
+
return x, fmap
|
655 |
+
|
656 |
+
|
657 |
+
class MultiPeriodDiscriminator(torch.nn.Module):
|
658 |
+
def __init__(self, use_spectral_norm=False):
|
659 |
+
super(MultiPeriodDiscriminator, self).__init__()
|
660 |
+
periods = [2, 3, 5, 7, 11]
|
661 |
+
|
662 |
+
discs = [DiscriminatorS(use_spectral_norm=use_spectral_norm)]
|
663 |
+
discs = discs + [
|
664 |
+
DiscriminatorP(i, use_spectral_norm=use_spectral_norm) for i in periods
|
665 |
+
]
|
666 |
+
self.discriminators = nn.ModuleList(discs)
|
667 |
+
|
668 |
+
def forward(self, y, y_hat):
|
669 |
+
y_d_rs = []
|
670 |
+
y_d_gs = []
|
671 |
+
fmap_rs = []
|
672 |
+
fmap_gs = []
|
673 |
+
for i, d in enumerate(self.discriminators):
|
674 |
+
y_d_r, fmap_r = d(y)
|
675 |
+
y_d_g, fmap_g = d(y_hat)
|
676 |
+
y_d_rs.append(y_d_r)
|
677 |
+
y_d_gs.append(y_d_g)
|
678 |
+
fmap_rs.append(fmap_r)
|
679 |
+
fmap_gs.append(fmap_g)
|
680 |
+
|
681 |
+
return y_d_rs, y_d_gs, fmap_rs, fmap_gs
|
682 |
+
|
683 |
+
|
684 |
+
class ReferenceEncoder(nn.Module):
|
685 |
+
"""
|
686 |
+
inputs --- [N, Ty/r, n_mels*r] mels
|
687 |
+
outputs --- [N, ref_enc_gru_size]
|
688 |
+
"""
|
689 |
+
|
690 |
+
def __init__(self, spec_channels, gin_channels=0, layernorm=False):
|
691 |
+
super().__init__()
|
692 |
+
self.spec_channels = spec_channels
|
693 |
+
ref_enc_filters = [32, 32, 64, 64, 128, 128]
|
694 |
+
K = len(ref_enc_filters)
|
695 |
+
filters = [1] + ref_enc_filters
|
696 |
+
convs = [
|
697 |
+
weight_norm(
|
698 |
+
nn.Conv2d(
|
699 |
+
in_channels=filters[i],
|
700 |
+
out_channels=filters[i + 1],
|
701 |
+
kernel_size=(3, 3),
|
702 |
+
stride=(2, 2),
|
703 |
+
padding=(1, 1),
|
704 |
+
)
|
705 |
+
)
|
706 |
+
for i in range(K)
|
707 |
+
]
|
708 |
+
self.convs = nn.ModuleList(convs)
|
709 |
+
# self.wns = nn.ModuleList([weight_norm(num_features=ref_enc_filters[i]) for i in range(K)]) # noqa: E501
|
710 |
+
|
711 |
+
out_channels = self.calculate_channels(spec_channels, 3, 2, 1, K)
|
712 |
+
self.gru = nn.GRU(
|
713 |
+
input_size=ref_enc_filters[-1] * out_channels,
|
714 |
+
hidden_size=256 // 2,
|
715 |
+
batch_first=True,
|
716 |
+
)
|
717 |
+
self.proj = nn.Linear(128, gin_channels)
|
718 |
+
if layernorm:
|
719 |
+
self.layernorm = nn.LayerNorm(self.spec_channels)
|
720 |
+
print('[Ref Enc]: using layer norm')
|
721 |
+
else:
|
722 |
+
self.layernorm = None
|
723 |
+
|
724 |
+
def forward(self, inputs, mask=None):
|
725 |
+
N = inputs.size(0)
|
726 |
+
|
727 |
+
out = inputs.view(N, 1, -1, self.spec_channels) # [N, 1, Ty, n_freqs]
|
728 |
+
if self.layernorm is not None:
|
729 |
+
out = self.layernorm(out)
|
730 |
+
|
731 |
+
for conv in self.convs:
|
732 |
+
out = conv(out)
|
733 |
+
# out = wn(out)
|
734 |
+
out = F.relu(out) # [N, 128, Ty//2^K, n_mels//2^K]
|
735 |
+
|
736 |
+
out = out.transpose(1, 2) # [N, Ty//2^K, 128, n_mels//2^K]
|
737 |
+
T = out.size(1)
|
738 |
+
N = out.size(0)
|
739 |
+
out = out.contiguous().view(N, T, -1) # [N, Ty//2^K, 128*n_mels//2^K]
|
740 |
+
|
741 |
+
self.gru.flatten_parameters()
|
742 |
+
memory, out = self.gru(out) # out --- [1, N, 128]
|
743 |
+
|
744 |
+
return self.proj(out.squeeze(0))
|
745 |
+
|
746 |
+
def calculate_channels(self, L, kernel_size, stride, pad, n_convs):
|
747 |
+
for i in range(n_convs):
|
748 |
+
L = (L - kernel_size + 2 * pad) // stride + 1
|
749 |
+
return L
|
750 |
+
|
751 |
+
|
752 |
+
class SynthesizerTrn(nn.Module):
|
753 |
+
"""
|
754 |
+
Synthesizer for Training
|
755 |
+
"""
|
756 |
+
|
757 |
+
def __init__(
|
758 |
+
self,
|
759 |
+
n_vocab,
|
760 |
+
spec_channels,
|
761 |
+
segment_size,
|
762 |
+
inter_channels,
|
763 |
+
hidden_channels,
|
764 |
+
filter_channels,
|
765 |
+
n_heads,
|
766 |
+
n_layers,
|
767 |
+
kernel_size,
|
768 |
+
p_dropout,
|
769 |
+
resblock,
|
770 |
+
resblock_kernel_sizes,
|
771 |
+
resblock_dilation_sizes,
|
772 |
+
upsample_rates,
|
773 |
+
upsample_initial_channel,
|
774 |
+
upsample_kernel_sizes,
|
775 |
+
n_speakers=256,
|
776 |
+
gin_channels=256,
|
777 |
+
use_sdp=True,
|
778 |
+
n_flow_layer=4,
|
779 |
+
n_layers_trans_flow=6,
|
780 |
+
flow_share_parameter=False,
|
781 |
+
use_transformer_flow=True,
|
782 |
+
use_vc=False,
|
783 |
+
num_languages=None,
|
784 |
+
num_tones=None,
|
785 |
+
norm_refenc=False,
|
786 |
+
**kwargs
|
787 |
+
):
|
788 |
+
super().__init__()
|
789 |
+
self.n_vocab = n_vocab
|
790 |
+
self.spec_channels = spec_channels
|
791 |
+
self.inter_channels = inter_channels
|
792 |
+
self.hidden_channels = hidden_channels
|
793 |
+
self.filter_channels = filter_channels
|
794 |
+
self.n_heads = n_heads
|
795 |
+
self.n_layers = n_layers
|
796 |
+
self.kernel_size = kernel_size
|
797 |
+
self.p_dropout = p_dropout
|
798 |
+
self.resblock = resblock
|
799 |
+
self.resblock_kernel_sizes = resblock_kernel_sizes
|
800 |
+
self.resblock_dilation_sizes = resblock_dilation_sizes
|
801 |
+
self.upsample_rates = upsample_rates
|
802 |
+
self.upsample_initial_channel = upsample_initial_channel
|
803 |
+
self.upsample_kernel_sizes = upsample_kernel_sizes
|
804 |
+
self.segment_size = segment_size
|
805 |
+
self.n_speakers = n_speakers
|
806 |
+
self.gin_channels = gin_channels
|
807 |
+
self.n_layers_trans_flow = n_layers_trans_flow
|
808 |
+
self.use_spk_conditioned_encoder = kwargs.get(
|
809 |
+
"use_spk_conditioned_encoder", True
|
810 |
+
)
|
811 |
+
self.use_sdp = use_sdp
|
812 |
+
self.use_noise_scaled_mas = kwargs.get("use_noise_scaled_mas", False)
|
813 |
+
self.mas_noise_scale_initial = kwargs.get("mas_noise_scale_initial", 0.01)
|
814 |
+
self.noise_scale_delta = kwargs.get("noise_scale_delta", 2e-6)
|
815 |
+
self.current_mas_noise_scale = self.mas_noise_scale_initial
|
816 |
+
if self.use_spk_conditioned_encoder and gin_channels > 0:
|
817 |
+
self.enc_gin_channels = gin_channels
|
818 |
+
else:
|
819 |
+
self.enc_gin_channels = 0
|
820 |
+
self.enc_p = TextEncoder(
|
821 |
+
n_vocab,
|
822 |
+
inter_channels,
|
823 |
+
hidden_channels,
|
824 |
+
filter_channels,
|
825 |
+
n_heads,
|
826 |
+
n_layers,
|
827 |
+
kernel_size,
|
828 |
+
p_dropout,
|
829 |
+
gin_channels=self.enc_gin_channels,
|
830 |
+
num_languages=num_languages,
|
831 |
+
num_tones=num_tones,
|
832 |
+
)
|
833 |
+
self.dec = Generator(
|
834 |
+
inter_channels,
|
835 |
+
resblock,
|
836 |
+
resblock_kernel_sizes,
|
837 |
+
resblock_dilation_sizes,
|
838 |
+
upsample_rates,
|
839 |
+
upsample_initial_channel,
|
840 |
+
upsample_kernel_sizes,
|
841 |
+
gin_channels=gin_channels,
|
842 |
+
)
|
843 |
+
self.enc_q = PosteriorEncoder(
|
844 |
+
spec_channels,
|
845 |
+
inter_channels,
|
846 |
+
hidden_channels,
|
847 |
+
5,
|
848 |
+
1,
|
849 |
+
16,
|
850 |
+
gin_channels=gin_channels,
|
851 |
+
)
|
852 |
+
if use_transformer_flow:
|
853 |
+
self.flow = TransformerCouplingBlock(
|
854 |
+
inter_channels,
|
855 |
+
hidden_channels,
|
856 |
+
filter_channels,
|
857 |
+
n_heads,
|
858 |
+
n_layers_trans_flow,
|
859 |
+
5,
|
860 |
+
p_dropout,
|
861 |
+
n_flow_layer,
|
862 |
+
gin_channels=gin_channels,
|
863 |
+
share_parameter=flow_share_parameter,
|
864 |
+
)
|
865 |
+
else:
|
866 |
+
self.flow = ResidualCouplingBlock(
|
867 |
+
inter_channels,
|
868 |
+
hidden_channels,
|
869 |
+
5,
|
870 |
+
1,
|
871 |
+
n_flow_layer,
|
872 |
+
gin_channels=gin_channels,
|
873 |
+
)
|
874 |
+
self.sdp = StochasticDurationPredictor(
|
875 |
+
hidden_channels, 192, 3, 0.5, 4, gin_channels=gin_channels
|
876 |
+
)
|
877 |
+
self.dp = DurationPredictor(
|
878 |
+
hidden_channels, 256, 3, 0.5, gin_channels=gin_channels
|
879 |
+
)
|
880 |
+
|
881 |
+
if n_speakers > 0:
|
882 |
+
self.emb_g = nn.Embedding(n_speakers, gin_channels)
|
883 |
+
else:
|
884 |
+
self.ref_enc = ReferenceEncoder(spec_channels, gin_channels, layernorm=norm_refenc)
|
885 |
+
self.use_vc = use_vc
|
886 |
+
|
887 |
+
|
888 |
+
def forward(self, x, x_lengths, y, y_lengths, sid, tone, language, bert, ja_bert):
|
889 |
+
if self.n_speakers > 0:
|
890 |
+
g = self.emb_g(sid).unsqueeze(-1) # [b, h, 1]
|
891 |
+
else:
|
892 |
+
g = self.ref_enc(y.transpose(1, 2)).unsqueeze(-1)
|
893 |
+
if self.use_vc:
|
894 |
+
g_p = None
|
895 |
+
else:
|
896 |
+
g_p = g
|
897 |
+
x, m_p, logs_p, x_mask = self.enc_p(
|
898 |
+
x, x_lengths, tone, language, bert, ja_bert, g=g_p
|
899 |
+
)
|
900 |
+
z, m_q, logs_q, y_mask = self.enc_q(y, y_lengths, g=g)
|
901 |
+
z_p = self.flow(z, y_mask, g=g)
|
902 |
+
|
903 |
+
with torch.no_grad():
|
904 |
+
# negative cross-entropy
|
905 |
+
s_p_sq_r = torch.exp(-2 * logs_p) # [b, d, t]
|
906 |
+
neg_cent1 = torch.sum(
|
907 |
+
-0.5 * math.log(2 * math.pi) - logs_p, [1], keepdim=True
|
908 |
+
) # [b, 1, t_s]
|
909 |
+
neg_cent2 = torch.matmul(
|
910 |
+
-0.5 * (z_p**2).transpose(1, 2), s_p_sq_r
|
911 |
+
) # [b, t_t, d] x [b, d, t_s] = [b, t_t, t_s]
|
912 |
+
neg_cent3 = torch.matmul(
|
913 |
+
z_p.transpose(1, 2), (m_p * s_p_sq_r)
|
914 |
+
) # [b, t_t, d] x [b, d, t_s] = [b, t_t, t_s]
|
915 |
+
neg_cent4 = torch.sum(
|
916 |
+
-0.5 * (m_p**2) * s_p_sq_r, [1], keepdim=True
|
917 |
+
) # [b, 1, t_s]
|
918 |
+
neg_cent = neg_cent1 + neg_cent2 + neg_cent3 + neg_cent4
|
919 |
+
if self.use_noise_scaled_mas:
|
920 |
+
epsilon = (
|
921 |
+
torch.std(neg_cent)
|
922 |
+
* torch.randn_like(neg_cent)
|
923 |
+
* self.current_mas_noise_scale
|
924 |
+
)
|
925 |
+
neg_cent = neg_cent + epsilon
|
926 |
+
|
927 |
+
attn_mask = torch.unsqueeze(x_mask, 2) * torch.unsqueeze(y_mask, -1)
|
928 |
+
attn = (
|
929 |
+
monotonic_align.maximum_path(neg_cent, attn_mask.squeeze(1))
|
930 |
+
.unsqueeze(1)
|
931 |
+
.detach()
|
932 |
+
)
|
933 |
+
|
934 |
+
w = attn.sum(2)
|
935 |
+
|
936 |
+
l_length_sdp = self.sdp(x, x_mask, w, g=g)
|
937 |
+
l_length_sdp = l_length_sdp / torch.sum(x_mask)
|
938 |
+
|
939 |
+
logw_ = torch.log(w + 1e-6) * x_mask
|
940 |
+
logw = self.dp(x, x_mask, g=g)
|
941 |
+
l_length_dp = torch.sum((logw - logw_) ** 2, [1, 2]) / torch.sum(
|
942 |
+
x_mask
|
943 |
+
) # for averaging
|
944 |
+
|
945 |
+
l_length = l_length_dp + l_length_sdp
|
946 |
+
|
947 |
+
# expand prior
|
948 |
+
m_p = torch.matmul(attn.squeeze(1), m_p.transpose(1, 2)).transpose(1, 2)
|
949 |
+
logs_p = torch.matmul(attn.squeeze(1), logs_p.transpose(1, 2)).transpose(1, 2)
|
950 |
+
|
951 |
+
z_slice, ids_slice = commons.rand_slice_segments(
|
952 |
+
z, y_lengths, self.segment_size
|
953 |
+
)
|
954 |
+
o = self.dec(z_slice, g=g)
|
955 |
+
return (
|
956 |
+
o,
|
957 |
+
l_length,
|
958 |
+
attn,
|
959 |
+
ids_slice,
|
960 |
+
x_mask,
|
961 |
+
y_mask,
|
962 |
+
(z, z_p, m_p, logs_p, m_q, logs_q),
|
963 |
+
(x, logw, logw_),
|
964 |
+
)
|
965 |
+
|
966 |
+
def infer(
|
967 |
+
self,
|
968 |
+
x,
|
969 |
+
x_lengths,
|
970 |
+
sid,
|
971 |
+
tone,
|
972 |
+
language,
|
973 |
+
bert,
|
974 |
+
ja_bert,
|
975 |
+
noise_scale=0.667,
|
976 |
+
length_scale=1,
|
977 |
+
noise_scale_w=0.8,
|
978 |
+
max_len=None,
|
979 |
+
sdp_ratio=0,
|
980 |
+
y=None,
|
981 |
+
g=None,
|
982 |
+
):
|
983 |
+
# x, m_p, logs_p, x_mask = self.enc_p(x, x_lengths, tone, language, bert)
|
984 |
+
# g = self.gst(y)
|
985 |
+
if g is None:
|
986 |
+
if self.n_speakers > 0:
|
987 |
+
g = self.emb_g(sid).unsqueeze(-1) # [b, h, 1]
|
988 |
+
else:
|
989 |
+
g = self.ref_enc(y.transpose(1, 2)).unsqueeze(-1)
|
990 |
+
if self.use_vc:
|
991 |
+
g_p = None
|
992 |
+
else:
|
993 |
+
g_p = g
|
994 |
+
x, m_p, logs_p, x_mask = self.enc_p(
|
995 |
+
x, x_lengths, tone, language, bert, ja_bert, g=g_p
|
996 |
+
)
|
997 |
+
logw = self.sdp(x, x_mask, g=g, reverse=True, noise_scale=noise_scale_w) * (
|
998 |
+
sdp_ratio
|
999 |
+
) + self.dp(x, x_mask, g=g) * (1 - sdp_ratio)
|
1000 |
+
w = torch.exp(logw) * x_mask * length_scale
|
1001 |
+
|
1002 |
+
w_ceil = torch.ceil(w)
|
1003 |
+
y_lengths = torch.clamp_min(torch.sum(w_ceil, [1, 2]), 1).long()
|
1004 |
+
y_mask = torch.unsqueeze(commons.sequence_mask(y_lengths, None), 1).to(
|
1005 |
+
x_mask.dtype
|
1006 |
+
)
|
1007 |
+
attn_mask = torch.unsqueeze(x_mask, 2) * torch.unsqueeze(y_mask, -1)
|
1008 |
+
attn = commons.generate_path(w_ceil, attn_mask)
|
1009 |
+
|
1010 |
+
m_p = torch.matmul(attn.squeeze(1), m_p.transpose(1, 2)).transpose(
|
1011 |
+
1, 2
|
1012 |
+
) # [b, t', t], [b, t, d] -> [b, d, t']
|
1013 |
+
logs_p = torch.matmul(attn.squeeze(1), logs_p.transpose(1, 2)).transpose(
|
1014 |
+
1, 2
|
1015 |
+
) # [b, t', t], [b, t, d] -> [b, d, t']
|
1016 |
+
|
1017 |
+
z_p = m_p + torch.randn_like(m_p) * torch.exp(logs_p) * noise_scale
|
1018 |
+
z = self.flow(z_p, y_mask, g=g, reverse=True)
|
1019 |
+
o = self.dec((z * y_mask)[:, :, :max_len], g=g)
|
1020 |
+
# print('max/min of o:', o.max(), o.min())
|
1021 |
+
return o, attn, y_mask, (z, z_p, m_p, logs_p)
|
1022 |
+
|
1023 |
+
def voice_conversion(self, y, y_lengths, sid_src, sid_tgt, tau=1.0):
|
1024 |
+
g_src = sid_src
|
1025 |
+
g_tgt = sid_tgt
|
1026 |
+
z, m_q, logs_q, y_mask = self.enc_q(y, y_lengths, g=g_src, tau=tau)
|
1027 |
+
z_p = self.flow(z, y_mask, g=g_src)
|
1028 |
+
z_hat = self.flow(z_p, y_mask, g=g_tgt, reverse=True)
|
1029 |
+
o_hat = self.dec(z_hat * y_mask, g=g_tgt)
|
1030 |
+
return o_hat, y_mask, (z, z_p, z_hat)
|
melo/modules.py
ADDED
@@ -0,0 +1,598 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import math
|
2 |
+
import torch
|
3 |
+
from torch import nn
|
4 |
+
from torch.nn import functional as F
|
5 |
+
|
6 |
+
from torch.nn import Conv1d
|
7 |
+
from torch.nn.utils import weight_norm, remove_weight_norm
|
8 |
+
|
9 |
+
from . import commons
|
10 |
+
from .commons import init_weights, get_padding
|
11 |
+
from .transforms import piecewise_rational_quadratic_transform
|
12 |
+
from .attentions import Encoder
|
13 |
+
|
14 |
+
LRELU_SLOPE = 0.1
|
15 |
+
|
16 |
+
|
17 |
+
class LayerNorm(nn.Module):
|
18 |
+
def __init__(self, channels, eps=1e-5):
|
19 |
+
super().__init__()
|
20 |
+
self.channels = channels
|
21 |
+
self.eps = eps
|
22 |
+
|
23 |
+
self.gamma = nn.Parameter(torch.ones(channels))
|
24 |
+
self.beta = nn.Parameter(torch.zeros(channels))
|
25 |
+
|
26 |
+
def forward(self, x):
|
27 |
+
x = x.transpose(1, -1)
|
28 |
+
x = F.layer_norm(x, (self.channels,), self.gamma, self.beta, self.eps)
|
29 |
+
return x.transpose(1, -1)
|
30 |
+
|
31 |
+
|
32 |
+
class ConvReluNorm(nn.Module):
|
33 |
+
def __init__(
|
34 |
+
self,
|
35 |
+
in_channels,
|
36 |
+
hidden_channels,
|
37 |
+
out_channels,
|
38 |
+
kernel_size,
|
39 |
+
n_layers,
|
40 |
+
p_dropout,
|
41 |
+
):
|
42 |
+
super().__init__()
|
43 |
+
self.in_channels = in_channels
|
44 |
+
self.hidden_channels = hidden_channels
|
45 |
+
self.out_channels = out_channels
|
46 |
+
self.kernel_size = kernel_size
|
47 |
+
self.n_layers = n_layers
|
48 |
+
self.p_dropout = p_dropout
|
49 |
+
assert n_layers > 1, "Number of layers should be larger than 0."
|
50 |
+
|
51 |
+
self.conv_layers = nn.ModuleList()
|
52 |
+
self.norm_layers = nn.ModuleList()
|
53 |
+
self.conv_layers.append(
|
54 |
+
nn.Conv1d(
|
55 |
+
in_channels, hidden_channels, kernel_size, padding=kernel_size // 2
|
56 |
+
)
|
57 |
+
)
|
58 |
+
self.norm_layers.append(LayerNorm(hidden_channels))
|
59 |
+
self.relu_drop = nn.Sequential(nn.ReLU(), nn.Dropout(p_dropout))
|
60 |
+
for _ in range(n_layers - 1):
|
61 |
+
self.conv_layers.append(
|
62 |
+
nn.Conv1d(
|
63 |
+
hidden_channels,
|
64 |
+
hidden_channels,
|
65 |
+
kernel_size,
|
66 |
+
padding=kernel_size // 2,
|
67 |
+
)
|
68 |
+
)
|
69 |
+
self.norm_layers.append(LayerNorm(hidden_channels))
|
70 |
+
self.proj = nn.Conv1d(hidden_channels, out_channels, 1)
|
71 |
+
self.proj.weight.data.zero_()
|
72 |
+
self.proj.bias.data.zero_()
|
73 |
+
|
74 |
+
def forward(self, x, x_mask):
|
75 |
+
x_org = x
|
76 |
+
for i in range(self.n_layers):
|
77 |
+
x = self.conv_layers[i](x * x_mask)
|
78 |
+
x = self.norm_layers[i](x)
|
79 |
+
x = self.relu_drop(x)
|
80 |
+
x = x_org + self.proj(x)
|
81 |
+
return x * x_mask
|
82 |
+
|
83 |
+
|
84 |
+
class DDSConv(nn.Module):
|
85 |
+
"""
|
86 |
+
Dialted and Depth-Separable Convolution
|
87 |
+
"""
|
88 |
+
|
89 |
+
def __init__(self, channels, kernel_size, n_layers, p_dropout=0.0):
|
90 |
+
super().__init__()
|
91 |
+
self.channels = channels
|
92 |
+
self.kernel_size = kernel_size
|
93 |
+
self.n_layers = n_layers
|
94 |
+
self.p_dropout = p_dropout
|
95 |
+
|
96 |
+
self.drop = nn.Dropout(p_dropout)
|
97 |
+
self.convs_sep = nn.ModuleList()
|
98 |
+
self.convs_1x1 = nn.ModuleList()
|
99 |
+
self.norms_1 = nn.ModuleList()
|
100 |
+
self.norms_2 = nn.ModuleList()
|
101 |
+
for i in range(n_layers):
|
102 |
+
dilation = kernel_size**i
|
103 |
+
padding = (kernel_size * dilation - dilation) // 2
|
104 |
+
self.convs_sep.append(
|
105 |
+
nn.Conv1d(
|
106 |
+
channels,
|
107 |
+
channels,
|
108 |
+
kernel_size,
|
109 |
+
groups=channels,
|
110 |
+
dilation=dilation,
|
111 |
+
padding=padding,
|
112 |
+
)
|
113 |
+
)
|
114 |
+
self.convs_1x1.append(nn.Conv1d(channels, channels, 1))
|
115 |
+
self.norms_1.append(LayerNorm(channels))
|
116 |
+
self.norms_2.append(LayerNorm(channels))
|
117 |
+
|
118 |
+
def forward(self, x, x_mask, g=None):
|
119 |
+
if g is not None:
|
120 |
+
x = x + g
|
121 |
+
for i in range(self.n_layers):
|
122 |
+
y = self.convs_sep[i](x * x_mask)
|
123 |
+
y = self.norms_1[i](y)
|
124 |
+
y = F.gelu(y)
|
125 |
+
y = self.convs_1x1[i](y)
|
126 |
+
y = self.norms_2[i](y)
|
127 |
+
y = F.gelu(y)
|
128 |
+
y = self.drop(y)
|
129 |
+
x = x + y
|
130 |
+
return x * x_mask
|
131 |
+
|
132 |
+
|
133 |
+
class WN(torch.nn.Module):
|
134 |
+
def __init__(
|
135 |
+
self,
|
136 |
+
hidden_channels,
|
137 |
+
kernel_size,
|
138 |
+
dilation_rate,
|
139 |
+
n_layers,
|
140 |
+
gin_channels=0,
|
141 |
+
p_dropout=0,
|
142 |
+
):
|
143 |
+
super(WN, self).__init__()
|
144 |
+
assert kernel_size % 2 == 1
|
145 |
+
self.hidden_channels = hidden_channels
|
146 |
+
self.kernel_size = (kernel_size,)
|
147 |
+
self.dilation_rate = dilation_rate
|
148 |
+
self.n_layers = n_layers
|
149 |
+
self.gin_channels = gin_channels
|
150 |
+
self.p_dropout = p_dropout
|
151 |
+
|
152 |
+
self.in_layers = torch.nn.ModuleList()
|
153 |
+
self.res_skip_layers = torch.nn.ModuleList()
|
154 |
+
self.drop = nn.Dropout(p_dropout)
|
155 |
+
|
156 |
+
if gin_channels != 0:
|
157 |
+
cond_layer = torch.nn.Conv1d(
|
158 |
+
gin_channels, 2 * hidden_channels * n_layers, 1
|
159 |
+
)
|
160 |
+
self.cond_layer = torch.nn.utils.weight_norm(cond_layer, name="weight")
|
161 |
+
|
162 |
+
for i in range(n_layers):
|
163 |
+
dilation = dilation_rate**i
|
164 |
+
padding = int((kernel_size * dilation - dilation) / 2)
|
165 |
+
in_layer = torch.nn.Conv1d(
|
166 |
+
hidden_channels,
|
167 |
+
2 * hidden_channels,
|
168 |
+
kernel_size,
|
169 |
+
dilation=dilation,
|
170 |
+
padding=padding,
|
171 |
+
)
|
172 |
+
in_layer = torch.nn.utils.weight_norm(in_layer, name="weight")
|
173 |
+
self.in_layers.append(in_layer)
|
174 |
+
|
175 |
+
# last one is not necessary
|
176 |
+
if i < n_layers - 1:
|
177 |
+
res_skip_channels = 2 * hidden_channels
|
178 |
+
else:
|
179 |
+
res_skip_channels = hidden_channels
|
180 |
+
|
181 |
+
res_skip_layer = torch.nn.Conv1d(hidden_channels, res_skip_channels, 1)
|
182 |
+
res_skip_layer = torch.nn.utils.weight_norm(res_skip_layer, name="weight")
|
183 |
+
self.res_skip_layers.append(res_skip_layer)
|
184 |
+
|
185 |
+
def forward(self, x, x_mask, g=None, **kwargs):
|
186 |
+
output = torch.zeros_like(x)
|
187 |
+
n_channels_tensor = torch.IntTensor([self.hidden_channels])
|
188 |
+
|
189 |
+
if g is not None:
|
190 |
+
g = self.cond_layer(g)
|
191 |
+
|
192 |
+
for i in range(self.n_layers):
|
193 |
+
x_in = self.in_layers[i](x)
|
194 |
+
if g is not None:
|
195 |
+
cond_offset = i * 2 * self.hidden_channels
|
196 |
+
g_l = g[:, cond_offset : cond_offset + 2 * self.hidden_channels, :]
|
197 |
+
else:
|
198 |
+
g_l = torch.zeros_like(x_in)
|
199 |
+
|
200 |
+
acts = commons.fused_add_tanh_sigmoid_multiply(x_in, g_l, n_channels_tensor)
|
201 |
+
acts = self.drop(acts)
|
202 |
+
|
203 |
+
res_skip_acts = self.res_skip_layers[i](acts)
|
204 |
+
if i < self.n_layers - 1:
|
205 |
+
res_acts = res_skip_acts[:, : self.hidden_channels, :]
|
206 |
+
x = (x + res_acts) * x_mask
|
207 |
+
output = output + res_skip_acts[:, self.hidden_channels :, :]
|
208 |
+
else:
|
209 |
+
output = output + res_skip_acts
|
210 |
+
return output * x_mask
|
211 |
+
|
212 |
+
def remove_weight_norm(self):
|
213 |
+
if self.gin_channels != 0:
|
214 |
+
torch.nn.utils.remove_weight_norm(self.cond_layer)
|
215 |
+
for l in self.in_layers:
|
216 |
+
torch.nn.utils.remove_weight_norm(l)
|
217 |
+
for l in self.res_skip_layers:
|
218 |
+
torch.nn.utils.remove_weight_norm(l)
|
219 |
+
|
220 |
+
|
221 |
+
class ResBlock1(torch.nn.Module):
|
222 |
+
def __init__(self, channels, kernel_size=3, dilation=(1, 3, 5)):
|
223 |
+
super(ResBlock1, self).__init__()
|
224 |
+
self.convs1 = nn.ModuleList(
|
225 |
+
[
|
226 |
+
weight_norm(
|
227 |
+
Conv1d(
|
228 |
+
channels,
|
229 |
+
channels,
|
230 |
+
kernel_size,
|
231 |
+
1,
|
232 |
+
dilation=dilation[0],
|
233 |
+
padding=get_padding(kernel_size, dilation[0]),
|
234 |
+
)
|
235 |
+
),
|
236 |
+
weight_norm(
|
237 |
+
Conv1d(
|
238 |
+
channels,
|
239 |
+
channels,
|
240 |
+
kernel_size,
|
241 |
+
1,
|
242 |
+
dilation=dilation[1],
|
243 |
+
padding=get_padding(kernel_size, dilation[1]),
|
244 |
+
)
|
245 |
+
),
|
246 |
+
weight_norm(
|
247 |
+
Conv1d(
|
248 |
+
channels,
|
249 |
+
channels,
|
250 |
+
kernel_size,
|
251 |
+
1,
|
252 |
+
dilation=dilation[2],
|
253 |
+
padding=get_padding(kernel_size, dilation[2]),
|
254 |
+
)
|
255 |
+
),
|
256 |
+
]
|
257 |
+
)
|
258 |
+
self.convs1.apply(init_weights)
|
259 |
+
|
260 |
+
self.convs2 = nn.ModuleList(
|
261 |
+
[
|
262 |
+
weight_norm(
|
263 |
+
Conv1d(
|
264 |
+
channels,
|
265 |
+
channels,
|
266 |
+
kernel_size,
|
267 |
+
1,
|
268 |
+
dilation=1,
|
269 |
+
padding=get_padding(kernel_size, 1),
|
270 |
+
)
|
271 |
+
),
|
272 |
+
weight_norm(
|
273 |
+
Conv1d(
|
274 |
+
channels,
|
275 |
+
channels,
|
276 |
+
kernel_size,
|
277 |
+
1,
|
278 |
+
dilation=1,
|
279 |
+
padding=get_padding(kernel_size, 1),
|
280 |
+
)
|
281 |
+
),
|
282 |
+
weight_norm(
|
283 |
+
Conv1d(
|
284 |
+
channels,
|
285 |
+
channels,
|
286 |
+
kernel_size,
|
287 |
+
1,
|
288 |
+
dilation=1,
|
289 |
+
padding=get_padding(kernel_size, 1),
|
290 |
+
)
|
291 |
+
),
|
292 |
+
]
|
293 |
+
)
|
294 |
+
self.convs2.apply(init_weights)
|
295 |
+
|
296 |
+
def forward(self, x, x_mask=None):
|
297 |
+
for c1, c2 in zip(self.convs1, self.convs2):
|
298 |
+
xt = F.leaky_relu(x, LRELU_SLOPE)
|
299 |
+
if x_mask is not None:
|
300 |
+
xt = xt * x_mask
|
301 |
+
xt = c1(xt)
|
302 |
+
xt = F.leaky_relu(xt, LRELU_SLOPE)
|
303 |
+
if x_mask is not None:
|
304 |
+
xt = xt * x_mask
|
305 |
+
xt = c2(xt)
|
306 |
+
x = xt + x
|
307 |
+
if x_mask is not None:
|
308 |
+
x = x * x_mask
|
309 |
+
return x
|
310 |
+
|
311 |
+
def remove_weight_norm(self):
|
312 |
+
for l in self.convs1:
|
313 |
+
remove_weight_norm(l)
|
314 |
+
for l in self.convs2:
|
315 |
+
remove_weight_norm(l)
|
316 |
+
|
317 |
+
|
318 |
+
class ResBlock2(torch.nn.Module):
|
319 |
+
def __init__(self, channels, kernel_size=3, dilation=(1, 3)):
|
320 |
+
super(ResBlock2, self).__init__()
|
321 |
+
self.convs = nn.ModuleList(
|
322 |
+
[
|
323 |
+
weight_norm(
|
324 |
+
Conv1d(
|
325 |
+
channels,
|
326 |
+
channels,
|
327 |
+
kernel_size,
|
328 |
+
1,
|
329 |
+
dilation=dilation[0],
|
330 |
+
padding=get_padding(kernel_size, dilation[0]),
|
331 |
+
)
|
332 |
+
),
|
333 |
+
weight_norm(
|
334 |
+
Conv1d(
|
335 |
+
channels,
|
336 |
+
channels,
|
337 |
+
kernel_size,
|
338 |
+
1,
|
339 |
+
dilation=dilation[1],
|
340 |
+
padding=get_padding(kernel_size, dilation[1]),
|
341 |
+
)
|
342 |
+
),
|
343 |
+
]
|
344 |
+
)
|
345 |
+
self.convs.apply(init_weights)
|
346 |
+
|
347 |
+
def forward(self, x, x_mask=None):
|
348 |
+
for c in self.convs:
|
349 |
+
xt = F.leaky_relu(x, LRELU_SLOPE)
|
350 |
+
if x_mask is not None:
|
351 |
+
xt = xt * x_mask
|
352 |
+
xt = c(xt)
|
353 |
+
x = xt + x
|
354 |
+
if x_mask is not None:
|
355 |
+
x = x * x_mask
|
356 |
+
return x
|
357 |
+
|
358 |
+
def remove_weight_norm(self):
|
359 |
+
for l in self.convs:
|
360 |
+
remove_weight_norm(l)
|
361 |
+
|
362 |
+
|
363 |
+
class Log(nn.Module):
|
364 |
+
def forward(self, x, x_mask, reverse=False, **kwargs):
|
365 |
+
if not reverse:
|
366 |
+
y = torch.log(torch.clamp_min(x, 1e-5)) * x_mask
|
367 |
+
logdet = torch.sum(-y, [1, 2])
|
368 |
+
return y, logdet
|
369 |
+
else:
|
370 |
+
x = torch.exp(x) * x_mask
|
371 |
+
return x
|
372 |
+
|
373 |
+
|
374 |
+
class Flip(nn.Module):
|
375 |
+
def forward(self, x, *args, reverse=False, **kwargs):
|
376 |
+
x = torch.flip(x, [1])
|
377 |
+
if not reverse:
|
378 |
+
logdet = torch.zeros(x.size(0)).to(dtype=x.dtype, device=x.device)
|
379 |
+
return x, logdet
|
380 |
+
else:
|
381 |
+
return x
|
382 |
+
|
383 |
+
|
384 |
+
class ElementwiseAffine(nn.Module):
|
385 |
+
def __init__(self, channels):
|
386 |
+
super().__init__()
|
387 |
+
self.channels = channels
|
388 |
+
self.m = nn.Parameter(torch.zeros(channels, 1))
|
389 |
+
self.logs = nn.Parameter(torch.zeros(channels, 1))
|
390 |
+
|
391 |
+
def forward(self, x, x_mask, reverse=False, **kwargs):
|
392 |
+
if not reverse:
|
393 |
+
y = self.m + torch.exp(self.logs) * x
|
394 |
+
y = y * x_mask
|
395 |
+
logdet = torch.sum(self.logs * x_mask, [1, 2])
|
396 |
+
return y, logdet
|
397 |
+
else:
|
398 |
+
x = (x - self.m) * torch.exp(-self.logs) * x_mask
|
399 |
+
return x
|
400 |
+
|
401 |
+
|
402 |
+
class ResidualCouplingLayer(nn.Module):
|
403 |
+
def __init__(
|
404 |
+
self,
|
405 |
+
channels,
|
406 |
+
hidden_channels,
|
407 |
+
kernel_size,
|
408 |
+
dilation_rate,
|
409 |
+
n_layers,
|
410 |
+
p_dropout=0,
|
411 |
+
gin_channels=0,
|
412 |
+
mean_only=False,
|
413 |
+
):
|
414 |
+
assert channels % 2 == 0, "channels should be divisible by 2"
|
415 |
+
super().__init__()
|
416 |
+
self.channels = channels
|
417 |
+
self.hidden_channels = hidden_channels
|
418 |
+
self.kernel_size = kernel_size
|
419 |
+
self.dilation_rate = dilation_rate
|
420 |
+
self.n_layers = n_layers
|
421 |
+
self.half_channels = channels // 2
|
422 |
+
self.mean_only = mean_only
|
423 |
+
|
424 |
+
self.pre = nn.Conv1d(self.half_channels, hidden_channels, 1)
|
425 |
+
self.enc = WN(
|
426 |
+
hidden_channels,
|
427 |
+
kernel_size,
|
428 |
+
dilation_rate,
|
429 |
+
n_layers,
|
430 |
+
p_dropout=p_dropout,
|
431 |
+
gin_channels=gin_channels,
|
432 |
+
)
|
433 |
+
self.post = nn.Conv1d(hidden_channels, self.half_channels * (2 - mean_only), 1)
|
434 |
+
self.post.weight.data.zero_()
|
435 |
+
self.post.bias.data.zero_()
|
436 |
+
|
437 |
+
def forward(self, x, x_mask, g=None, reverse=False):
|
438 |
+
x0, x1 = torch.split(x, [self.half_channels] * 2, 1)
|
439 |
+
h = self.pre(x0) * x_mask
|
440 |
+
h = self.enc(h, x_mask, g=g)
|
441 |
+
stats = self.post(h) * x_mask
|
442 |
+
if not self.mean_only:
|
443 |
+
m, logs = torch.split(stats, [self.half_channels] * 2, 1)
|
444 |
+
else:
|
445 |
+
m = stats
|
446 |
+
logs = torch.zeros_like(m)
|
447 |
+
|
448 |
+
if not reverse:
|
449 |
+
x1 = m + x1 * torch.exp(logs) * x_mask
|
450 |
+
x = torch.cat([x0, x1], 1)
|
451 |
+
logdet = torch.sum(logs, [1, 2])
|
452 |
+
return x, logdet
|
453 |
+
else:
|
454 |
+
x1 = (x1 - m) * torch.exp(-logs) * x_mask
|
455 |
+
x = torch.cat([x0, x1], 1)
|
456 |
+
return x
|
457 |
+
|
458 |
+
|
459 |
+
class ConvFlow(nn.Module):
|
460 |
+
def __init__(
|
461 |
+
self,
|
462 |
+
in_channels,
|
463 |
+
filter_channels,
|
464 |
+
kernel_size,
|
465 |
+
n_layers,
|
466 |
+
num_bins=10,
|
467 |
+
tail_bound=5.0,
|
468 |
+
):
|
469 |
+
super().__init__()
|
470 |
+
self.in_channels = in_channels
|
471 |
+
self.filter_channels = filter_channels
|
472 |
+
self.kernel_size = kernel_size
|
473 |
+
self.n_layers = n_layers
|
474 |
+
self.num_bins = num_bins
|
475 |
+
self.tail_bound = tail_bound
|
476 |
+
self.half_channels = in_channels // 2
|
477 |
+
|
478 |
+
self.pre = nn.Conv1d(self.half_channels, filter_channels, 1)
|
479 |
+
self.convs = DDSConv(filter_channels, kernel_size, n_layers, p_dropout=0.0)
|
480 |
+
self.proj = nn.Conv1d(
|
481 |
+
filter_channels, self.half_channels * (num_bins * 3 - 1), 1
|
482 |
+
)
|
483 |
+
self.proj.weight.data.zero_()
|
484 |
+
self.proj.bias.data.zero_()
|
485 |
+
|
486 |
+
def forward(self, x, x_mask, g=None, reverse=False):
|
487 |
+
x0, x1 = torch.split(x, [self.half_channels] * 2, 1)
|
488 |
+
h = self.pre(x0)
|
489 |
+
h = self.convs(h, x_mask, g=g)
|
490 |
+
h = self.proj(h) * x_mask
|
491 |
+
|
492 |
+
b, c, t = x0.shape
|
493 |
+
h = h.reshape(b, c, -1, t).permute(0, 1, 3, 2) # [b, cx?, t] -> [b, c, t, ?]
|
494 |
+
|
495 |
+
unnormalized_widths = h[..., : self.num_bins] / math.sqrt(self.filter_channels)
|
496 |
+
unnormalized_heights = h[..., self.num_bins : 2 * self.num_bins] / math.sqrt(
|
497 |
+
self.filter_channels
|
498 |
+
)
|
499 |
+
unnormalized_derivatives = h[..., 2 * self.num_bins :]
|
500 |
+
|
501 |
+
x1, logabsdet = piecewise_rational_quadratic_transform(
|
502 |
+
x1,
|
503 |
+
unnormalized_widths,
|
504 |
+
unnormalized_heights,
|
505 |
+
unnormalized_derivatives,
|
506 |
+
inverse=reverse,
|
507 |
+
tails="linear",
|
508 |
+
tail_bound=self.tail_bound,
|
509 |
+
)
|
510 |
+
|
511 |
+
x = torch.cat([x0, x1], 1) * x_mask
|
512 |
+
logdet = torch.sum(logabsdet * x_mask, [1, 2])
|
513 |
+
if not reverse:
|
514 |
+
return x, logdet
|
515 |
+
else:
|
516 |
+
return x
|
517 |
+
|
518 |
+
|
519 |
+
class TransformerCouplingLayer(nn.Module):
|
520 |
+
def __init__(
|
521 |
+
self,
|
522 |
+
channels,
|
523 |
+
hidden_channels,
|
524 |
+
kernel_size,
|
525 |
+
n_layers,
|
526 |
+
n_heads,
|
527 |
+
p_dropout=0,
|
528 |
+
filter_channels=0,
|
529 |
+
mean_only=False,
|
530 |
+
wn_sharing_parameter=None,
|
531 |
+
gin_channels=0,
|
532 |
+
):
|
533 |
+
assert n_layers == 3, n_layers
|
534 |
+
assert channels % 2 == 0, "channels should be divisible by 2"
|
535 |
+
super().__init__()
|
536 |
+
self.channels = channels
|
537 |
+
self.hidden_channels = hidden_channels
|
538 |
+
self.kernel_size = kernel_size
|
539 |
+
self.n_layers = n_layers
|
540 |
+
self.half_channels = channels // 2
|
541 |
+
self.mean_only = mean_only
|
542 |
+
|
543 |
+
self.pre = nn.Conv1d(self.half_channels, hidden_channels, 1)
|
544 |
+
self.enc = (
|
545 |
+
Encoder(
|
546 |
+
hidden_channels,
|
547 |
+
filter_channels,
|
548 |
+
n_heads,
|
549 |
+
n_layers,
|
550 |
+
kernel_size,
|
551 |
+
p_dropout,
|
552 |
+
isflow=True,
|
553 |
+
gin_channels=gin_channels,
|
554 |
+
)
|
555 |
+
if wn_sharing_parameter is None
|
556 |
+
else wn_sharing_parameter
|
557 |
+
)
|
558 |
+
self.post = nn.Conv1d(hidden_channels, self.half_channels * (2 - mean_only), 1)
|
559 |
+
self.post.weight.data.zero_()
|
560 |
+
self.post.bias.data.zero_()
|
561 |
+
|
562 |
+
def forward(self, x, x_mask, g=None, reverse=False):
|
563 |
+
x0, x1 = torch.split(x, [self.half_channels] * 2, 1)
|
564 |
+
h = self.pre(x0) * x_mask
|
565 |
+
h = self.enc(h, x_mask, g=g)
|
566 |
+
stats = self.post(h) * x_mask
|
567 |
+
if not self.mean_only:
|
568 |
+
m, logs = torch.split(stats, [self.half_channels] * 2, 1)
|
569 |
+
else:
|
570 |
+
m = stats
|
571 |
+
logs = torch.zeros_like(m)
|
572 |
+
|
573 |
+
if not reverse:
|
574 |
+
x1 = m + x1 * torch.exp(logs) * x_mask
|
575 |
+
x = torch.cat([x0, x1], 1)
|
576 |
+
logdet = torch.sum(logs, [1, 2])
|
577 |
+
return x, logdet
|
578 |
+
else:
|
579 |
+
x1 = (x1 - m) * torch.exp(-logs) * x_mask
|
580 |
+
x = torch.cat([x0, x1], 1)
|
581 |
+
return x
|
582 |
+
|
583 |
+
x1, logabsdet = piecewise_rational_quadratic_transform(
|
584 |
+
x1,
|
585 |
+
unnormalized_widths,
|
586 |
+
unnormalized_heights,
|
587 |
+
unnormalized_derivatives,
|
588 |
+
inverse=reverse,
|
589 |
+
tails="linear",
|
590 |
+
tail_bound=self.tail_bound,
|
591 |
+
)
|
592 |
+
|
593 |
+
x = torch.cat([x0, x1], 1) * x_mask
|
594 |
+
logdet = torch.sum(logabsdet * x_mask, [1, 2])
|
595 |
+
if not reverse:
|
596 |
+
return x, logdet
|
597 |
+
else:
|
598 |
+
return x
|
melo/monotonic_align/__init__.py
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from numpy import zeros, int32, float32
|
2 |
+
from torch import from_numpy
|
3 |
+
|
4 |
+
from .core import maximum_path_jit
|
5 |
+
|
6 |
+
|
7 |
+
def maximum_path(neg_cent, mask):
|
8 |
+
device = neg_cent.device
|
9 |
+
dtype = neg_cent.dtype
|
10 |
+
neg_cent = neg_cent.data.cpu().numpy().astype(float32)
|
11 |
+
path = zeros(neg_cent.shape, dtype=int32)
|
12 |
+
|
13 |
+
t_t_max = mask.sum(1)[:, 0].data.cpu().numpy().astype(int32)
|
14 |
+
t_s_max = mask.sum(2)[:, 0].data.cpu().numpy().astype(int32)
|
15 |
+
maximum_path_jit(path, neg_cent, t_t_max, t_s_max)
|
16 |
+
return from_numpy(path).to(device=device, dtype=dtype)
|
melo/monotonic_align/__pycache__/__init__.cpython-312.pyc
ADDED
Binary file (1.47 kB). View file
|
|
melo/monotonic_align/__pycache__/core.cpython-312.pyc
ADDED
Binary file (1.79 kB). View file
|
|
melo/monotonic_align/core.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numba
|
2 |
+
|
3 |
+
|
4 |
+
@numba.jit(
|
5 |
+
numba.void(
|
6 |
+
numba.int32[:, :, ::1],
|
7 |
+
numba.float32[:, :, ::1],
|
8 |
+
numba.int32[::1],
|
9 |
+
numba.int32[::1],
|
10 |
+
),
|
11 |
+
nopython=True,
|
12 |
+
nogil=True,
|
13 |
+
)
|
14 |
+
def maximum_path_jit(paths, values, t_ys, t_xs):
|
15 |
+
b = paths.shape[0]
|
16 |
+
max_neg_val = -1e9
|
17 |
+
for i in range(int(b)):
|
18 |
+
path = paths[i]
|
19 |
+
value = values[i]
|
20 |
+
t_y = t_ys[i]
|
21 |
+
t_x = t_xs[i]
|
22 |
+
|
23 |
+
v_prev = v_cur = 0.0
|
24 |
+
index = t_x - 1
|
25 |
+
|
26 |
+
for y in range(t_y):
|
27 |
+
for x in range(max(0, t_x + y - t_y), min(t_x, y + 1)):
|
28 |
+
if x == y:
|
29 |
+
v_cur = max_neg_val
|
30 |
+
else:
|
31 |
+
v_cur = value[y - 1, x]
|
32 |
+
if x == 0:
|
33 |
+
if y == 0:
|
34 |
+
v_prev = 0.0
|
35 |
+
else:
|
36 |
+
v_prev = max_neg_val
|
37 |
+
else:
|
38 |
+
v_prev = value[y - 1, x - 1]
|
39 |
+
value[y, x] += max(v_prev, v_cur)
|
40 |
+
|
41 |
+
for y in range(t_y - 1, -1, -1):
|
42 |
+
path[y, index] = 1
|
43 |
+
if index != 0 and (
|
44 |
+
index == y or value[y - 1, index] < value[y - 1, index - 1]
|
45 |
+
):
|
46 |
+
index = index - 1
|
melo/preprocess_text.py
ADDED
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
from collections import defaultdict
|
3 |
+
from random import shuffle
|
4 |
+
from typing import Optional
|
5 |
+
|
6 |
+
from tqdm import tqdm
|
7 |
+
import click
|
8 |
+
from text.cleaner import clean_text_bert
|
9 |
+
import os
|
10 |
+
import torch
|
11 |
+
from text.symbols import symbols, num_languages, num_tones
|
12 |
+
|
13 |
+
@click.command()
|
14 |
+
@click.option(
|
15 |
+
"--metadata",
|
16 |
+
default="data/example/metadata.list",
|
17 |
+
type=click.Path(exists=True, file_okay=True, dir_okay=False),
|
18 |
+
)
|
19 |
+
@click.option("--cleaned-path", default=None)
|
20 |
+
@click.option("--train-path", default=None)
|
21 |
+
@click.option("--val-path", default=None)
|
22 |
+
@click.option(
|
23 |
+
"--config_path",
|
24 |
+
default="configs/config.json",
|
25 |
+
type=click.Path(exists=True, file_okay=True, dir_okay=False),
|
26 |
+
)
|
27 |
+
@click.option("--val-per-spk", default=4)
|
28 |
+
@click.option("--max-val-total", default=8)
|
29 |
+
@click.option("--clean/--no-clean", default=True)
|
30 |
+
def main(
|
31 |
+
metadata: str,
|
32 |
+
cleaned_path: Optional[str],
|
33 |
+
train_path: str,
|
34 |
+
val_path: str,
|
35 |
+
config_path: str,
|
36 |
+
val_per_spk: int,
|
37 |
+
max_val_total: int,
|
38 |
+
clean: bool,
|
39 |
+
):
|
40 |
+
if train_path is None:
|
41 |
+
train_path = os.path.join(os.path.dirname(metadata), 'train.list')
|
42 |
+
if val_path is None:
|
43 |
+
val_path = os.path.join(os.path.dirname(metadata), 'val.list')
|
44 |
+
out_config_path = os.path.join(os.path.dirname(metadata), 'config.json')
|
45 |
+
|
46 |
+
if cleaned_path is None:
|
47 |
+
cleaned_path = metadata + ".cleaned"
|
48 |
+
|
49 |
+
if clean:
|
50 |
+
out_file = open(cleaned_path, "w", encoding="utf-8")
|
51 |
+
new_symbols = []
|
52 |
+
for line in tqdm(open(metadata, encoding="utf-8").readlines()):
|
53 |
+
try:
|
54 |
+
utt, spk, language, text = line.strip().split("|")
|
55 |
+
norm_text, phones, tones, word2ph, bert = clean_text_bert(text, language, device='cuda:0')
|
56 |
+
for ph in phones:
|
57 |
+
if ph not in symbols and ph not in new_symbols:
|
58 |
+
new_symbols.append(ph)
|
59 |
+
print('update!, now symbols:')
|
60 |
+
print(new_symbols)
|
61 |
+
with open(f'{language}_symbol.txt', 'w') as f:
|
62 |
+
f.write(f'{new_symbols}')
|
63 |
+
|
64 |
+
assert len(phones) == len(tones)
|
65 |
+
assert len(phones) == sum(word2ph)
|
66 |
+
out_file.write(
|
67 |
+
"{}|{}|{}|{}|{}|{}|{}\n".format(
|
68 |
+
utt,
|
69 |
+
spk,
|
70 |
+
language,
|
71 |
+
norm_text,
|
72 |
+
" ".join(phones),
|
73 |
+
" ".join([str(i) for i in tones]),
|
74 |
+
" ".join([str(i) for i in word2ph]),
|
75 |
+
)
|
76 |
+
)
|
77 |
+
bert_path = utt.replace(".wav", ".bert.pt")
|
78 |
+
os.makedirs(os.path.dirname(bert_path), exist_ok=True)
|
79 |
+
torch.save(bert.cpu(), bert_path)
|
80 |
+
except Exception as error:
|
81 |
+
print("err!", line, error)
|
82 |
+
|
83 |
+
out_file.close()
|
84 |
+
|
85 |
+
metadata = cleaned_path
|
86 |
+
|
87 |
+
spk_utt_map = defaultdict(list)
|
88 |
+
spk_id_map = {}
|
89 |
+
current_sid = 0
|
90 |
+
|
91 |
+
with open(metadata, encoding="utf-8") as f:
|
92 |
+
for line in f.readlines():
|
93 |
+
utt, spk, language, text, phones, tones, word2ph = line.strip().split("|")
|
94 |
+
spk_utt_map[spk].append(line)
|
95 |
+
|
96 |
+
if spk not in spk_id_map.keys():
|
97 |
+
spk_id_map[spk] = current_sid
|
98 |
+
current_sid += 1
|
99 |
+
|
100 |
+
train_list = []
|
101 |
+
val_list = []
|
102 |
+
|
103 |
+
for spk, utts in spk_utt_map.items():
|
104 |
+
shuffle(utts)
|
105 |
+
val_list += utts[:val_per_spk]
|
106 |
+
train_list += utts[val_per_spk:]
|
107 |
+
|
108 |
+
if len(val_list) > max_val_total:
|
109 |
+
train_list += val_list[max_val_total:]
|
110 |
+
val_list = val_list[:max_val_total]
|
111 |
+
|
112 |
+
with open(train_path, "w", encoding="utf-8") as f:
|
113 |
+
for line in train_list:
|
114 |
+
f.write(line)
|
115 |
+
|
116 |
+
with open(val_path, "w", encoding="utf-8") as f:
|
117 |
+
for line in val_list:
|
118 |
+
f.write(line)
|
119 |
+
|
120 |
+
config = json.load(open(config_path, encoding="utf-8"))
|
121 |
+
config["data"]["spk2id"] = spk_id_map
|
122 |
+
|
123 |
+
config["data"]["training_files"] = train_path
|
124 |
+
config["data"]["validation_files"] = val_path
|
125 |
+
config["data"]["n_speakers"] = len(spk_id_map)
|
126 |
+
config["num_languages"] = num_languages
|
127 |
+
config["num_tones"] = num_tones
|
128 |
+
config["symbols"] = symbols
|
129 |
+
|
130 |
+
with open(out_config_path, "w", encoding="utf-8") as f:
|
131 |
+
json.dump(config, f, indent=2, ensure_ascii=False)
|
132 |
+
|
133 |
+
|
134 |
+
if __name__ == "__main__":
|
135 |
+
main()
|
melo/split_utils.py
ADDED
@@ -0,0 +1,174 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import re
|
2 |
+
import os
|
3 |
+
import glob
|
4 |
+
import numpy as np
|
5 |
+
import soundfile as sf
|
6 |
+
import torchaudio
|
7 |
+
import re
|
8 |
+
|
9 |
+
def split_sentence(text, min_len=10, language_str='EN'):
|
10 |
+
if language_str in ['EN', 'FR', 'ES', 'SP']:
|
11 |
+
sentences = split_sentences_latin(text, min_len=min_len)
|
12 |
+
else:
|
13 |
+
sentences = split_sentences_zh(text, min_len=min_len)
|
14 |
+
return sentences
|
15 |
+
|
16 |
+
|
17 |
+
def split_sentences_latin(text, min_len=10):
|
18 |
+
text = re.sub('[。!?;]', '.', text)
|
19 |
+
text = re.sub('[,]', ',', text)
|
20 |
+
text = re.sub('[“”]', '"', text)
|
21 |
+
text = re.sub('[‘’]', "'", text)
|
22 |
+
text = re.sub(r"[\<\>\(\)\[\]\"\«\»]+", "", text)
|
23 |
+
return [item.strip() for item in txtsplit(text, 256, 512) if item.strip()]
|
24 |
+
|
25 |
+
|
26 |
+
def split_sentences_zh(text, min_len=10):
|
27 |
+
text = re.sub('[。!?;]', '.', text)
|
28 |
+
text = re.sub('[,]', ',', text)
|
29 |
+
# 将文本中的换行符、空格和制表符替换为空格
|
30 |
+
text = re.sub('[\n\t ]+', ' ', text)
|
31 |
+
# 在标点符号后添加一个空格
|
32 |
+
text = re.sub('([,.!?;])', r'\1 $#!', text)
|
33 |
+
# 分隔句子并去除前后空格
|
34 |
+
# sentences = [s.strip() for s in re.split('(。|!|?|;)', text)]
|
35 |
+
sentences = [s.strip() for s in text.split('$#!')]
|
36 |
+
if len(sentences[-1]) == 0: del sentences[-1]
|
37 |
+
|
38 |
+
new_sentences = []
|
39 |
+
new_sent = []
|
40 |
+
count_len = 0
|
41 |
+
for ind, sent in enumerate(sentences):
|
42 |
+
new_sent.append(sent)
|
43 |
+
count_len += len(sent)
|
44 |
+
if count_len > min_len or ind == len(sentences) - 1:
|
45 |
+
count_len = 0
|
46 |
+
new_sentences.append(' '.join(new_sent))
|
47 |
+
new_sent = []
|
48 |
+
return merge_short_sentences_zh(new_sentences)
|
49 |
+
|
50 |
+
|
51 |
+
def merge_short_sentences_en(sens):
|
52 |
+
"""Avoid short sentences by merging them with the following sentence.
|
53 |
+
|
54 |
+
Args:
|
55 |
+
List[str]: list of input sentences.
|
56 |
+
|
57 |
+
Returns:
|
58 |
+
List[str]: list of output sentences.
|
59 |
+
"""
|
60 |
+
sens_out = []
|
61 |
+
for s in sens:
|
62 |
+
# If the previous sentense is too short, merge them with
|
63 |
+
# the current sentence.
|
64 |
+
if len(sens_out) > 0 and len(sens_out[-1].split(" ")) <= 2:
|
65 |
+
sens_out[-1] = sens_out[-1] + " " + s
|
66 |
+
else:
|
67 |
+
sens_out.append(s)
|
68 |
+
try:
|
69 |
+
if len(sens_out[-1].split(" ")) <= 2:
|
70 |
+
sens_out[-2] = sens_out[-2] + " " + sens_out[-1]
|
71 |
+
sens_out.pop(-1)
|
72 |
+
except:
|
73 |
+
pass
|
74 |
+
return sens_out
|
75 |
+
|
76 |
+
|
77 |
+
def merge_short_sentences_zh(sens):
|
78 |
+
# return sens
|
79 |
+
"""Avoid short sentences by merging them with the following sentence.
|
80 |
+
|
81 |
+
Args:
|
82 |
+
List[str]: list of input sentences.
|
83 |
+
|
84 |
+
Returns:
|
85 |
+
List[str]: list of output sentences.
|
86 |
+
"""
|
87 |
+
sens_out = []
|
88 |
+
for s in sens:
|
89 |
+
# If the previous sentense is too short, merge them with
|
90 |
+
# the current sentence.
|
91 |
+
if len(sens_out) > 0 and len(sens_out[-1]) <= 2:
|
92 |
+
sens_out[-1] = sens_out[-1] + " " + s
|
93 |
+
else:
|
94 |
+
sens_out.append(s)
|
95 |
+
try:
|
96 |
+
if len(sens_out[-1]) <= 2:
|
97 |
+
sens_out[-2] = sens_out[-2] + " " + sens_out[-1]
|
98 |
+
sens_out.pop(-1)
|
99 |
+
except:
|
100 |
+
pass
|
101 |
+
return sens_out
|
102 |
+
|
103 |
+
|
104 |
+
|
105 |
+
def txtsplit(text, desired_length=100, max_length=200):
|
106 |
+
"""Split text it into chunks of a desired length trying to keep sentences intact."""
|
107 |
+
text = re.sub(r'\n\n+', '\n', text)
|
108 |
+
text = re.sub(r'\s+', ' ', text)
|
109 |
+
text = re.sub(r'[""]', '"', text)
|
110 |
+
text = re.sub(r'([,.?!])', r'\1 ', text)
|
111 |
+
text = re.sub(r'\s+', ' ', text)
|
112 |
+
|
113 |
+
rv = []
|
114 |
+
in_quote = False
|
115 |
+
current = ""
|
116 |
+
split_pos = []
|
117 |
+
pos = -1
|
118 |
+
end_pos = len(text) - 1
|
119 |
+
def seek(delta):
|
120 |
+
nonlocal pos, in_quote, current
|
121 |
+
is_neg = delta < 0
|
122 |
+
for _ in range(abs(delta)):
|
123 |
+
if is_neg:
|
124 |
+
pos -= 1
|
125 |
+
current = current[:-1]
|
126 |
+
else:
|
127 |
+
pos += 1
|
128 |
+
current += text[pos]
|
129 |
+
if text[pos] == '"':
|
130 |
+
in_quote = not in_quote
|
131 |
+
return text[pos]
|
132 |
+
def peek(delta):
|
133 |
+
p = pos + delta
|
134 |
+
return text[p] if p < end_pos and p >= 0 else ""
|
135 |
+
def commit():
|
136 |
+
nonlocal rv, current, split_pos
|
137 |
+
rv.append(current)
|
138 |
+
current = ""
|
139 |
+
split_pos = []
|
140 |
+
while pos < end_pos:
|
141 |
+
c = seek(1)
|
142 |
+
if len(current) >= max_length:
|
143 |
+
if len(split_pos) > 0 and len(current) > (desired_length / 2):
|
144 |
+
d = pos - split_pos[-1]
|
145 |
+
seek(-d)
|
146 |
+
else:
|
147 |
+
while c not in '!?.\n ' and pos > 0 and len(current) > desired_length:
|
148 |
+
c = seek(-1)
|
149 |
+
commit()
|
150 |
+
elif not in_quote and (c in '!?\n' or (c in '.,' and peek(1) in '\n ')):
|
151 |
+
while pos < len(text) - 1 and len(current) < max_length and peek(1) in '!?.':
|
152 |
+
c = seek(1)
|
153 |
+
split_pos.append(pos)
|
154 |
+
if len(current) >= desired_length:
|
155 |
+
commit()
|
156 |
+
elif in_quote and peek(1) == '"' and peek(2) in '\n ':
|
157 |
+
seek(2)
|
158 |
+
split_pos.append(pos)
|
159 |
+
rv.append(current)
|
160 |
+
rv = [s.strip() for s in rv]
|
161 |
+
rv = [s for s in rv if len(s) > 0 and not re.match(r'^[\s\.,;:!?]*$', s)]
|
162 |
+
return rv
|
163 |
+
|
164 |
+
|
165 |
+
if __name__ == '__main__':
|
166 |
+
zh_text = "好的,我来给你讲一个故事吧。从前有一个小姑娘,她叫做小红。小红非常喜欢在森林里玩耍,她经常会和她的小伙伴们一起去探险。有一天,小红和她的小伙伴们走到了森林深处,突然遇到了一只凶猛的野兽。小红的小伙伴们都吓得不敢动弹,但是小红并没有被吓倒,她勇敢地走向野兽,用她的智慧和勇气成功地制服了野兽,保护了她的小伙伴们。从那以后,小红变得更加勇敢和自信,成为了她小伙伴们心中的英雄。"
|
167 |
+
en_text = "I didn’t know what to do. I said please kill her because it would be better than being kidnapped,” Ben, whose surname CNN is not using for security concerns, said on Wednesday. “It’s a nightmare. I said ‘please kill her, don’t take her there.’"
|
168 |
+
sp_text = "¡Claro! ¿En qué tema te gustaría que te hable en español? Puedo proporcionarte información o conversar contigo sobre una amplia variedad de temas, desde cultura y comida hasta viajes y tecnología. ¿Tienes alguna preferencia en particular?"
|
169 |
+
fr_text = "Bien sûr ! En quelle matière voudriez-vous que je vous parle en français ? Je peux vous fournir des informations ou discuter avec vous sur une grande variété de sujets, que ce soit la culture, la nourriture, les voyages ou la technologie. Avez-vous une préférence particulière ?"
|
170 |
+
|
171 |
+
print(split_sentence(zh_text, language_str='ZH'))
|
172 |
+
print(split_sentence(en_text, language_str='EN'))
|
173 |
+
print(split_sentence(sp_text, language_str='SP'))
|
174 |
+
print(split_sentence(fr_text, language_str='FR'))
|
melo/text/__init__.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from .symbols import *
|
2 |
+
|
3 |
+
|
4 |
+
_symbol_to_id = {s: i for i, s in enumerate(symbols)}
|
5 |
+
|
6 |
+
|
7 |
+
def cleaned_text_to_sequence(cleaned_text, tones, language, symbol_to_id=None):
|
8 |
+
"""Converts a string of text to a sequence of IDs corresponding to the symbols in the text.
|
9 |
+
Args:
|
10 |
+
text: string to convert to a sequence
|
11 |
+
Returns:
|
12 |
+
List of integers corresponding to the symbols in the text
|
13 |
+
"""
|
14 |
+
symbol_to_id_map = symbol_to_id if symbol_to_id else _symbol_to_id
|
15 |
+
phones = [symbol_to_id_map[symbol] for symbol in cleaned_text]
|
16 |
+
tone_start = language_tone_start_map[language]
|
17 |
+
tones = [i + tone_start for i in tones]
|
18 |
+
lang_id = language_id_map[language]
|
19 |
+
lang_ids = [lang_id for i in phones]
|
20 |
+
return phones, tones, lang_ids
|
21 |
+
|
22 |
+
|
23 |
+
def get_bert(norm_text, word2ph, language, device):
|
24 |
+
from .chinese_bert import get_bert_feature as zh_bert
|
25 |
+
from .english_bert import get_bert_feature as en_bert
|
26 |
+
from .japanese_bert import get_bert_feature as jp_bert
|
27 |
+
from .chinese_mix import get_bert_feature as zh_mix_en_bert
|
28 |
+
from .spanish_bert import get_bert_feature as sp_bert
|
29 |
+
from .french_bert import get_bert_feature as fr_bert
|
30 |
+
from .korean import get_bert_feature as kr_bert
|
31 |
+
|
32 |
+
lang_bert_func_map = {"ZH": zh_bert, "EN": en_bert, "JP": jp_bert, 'ZH_MIX_EN': zh_mix_en_bert,
|
33 |
+
'FR': fr_bert, 'SP': sp_bert, 'ES': sp_bert, "KR": kr_bert}
|
34 |
+
bert = lang_bert_func_map[language](norm_text, word2ph, device)
|
35 |
+
return bert
|
melo/text/__pycache__/__init__.cpython-312.pyc
ADDED
Binary file (1.8 kB). View file
|
|
melo/text/__pycache__/chinese.cpython-312.pyc
ADDED
Binary file (7.56 kB). View file
|
|
melo/text/__pycache__/chinese_bert.cpython-312.pyc
ADDED
Binary file (3.04 kB). View file
|
|
melo/text/__pycache__/chinese_mix.cpython-312.pyc
ADDED
Binary file (10.1 kB). View file
|
|
melo/text/__pycache__/cleaner.cpython-312.pyc
ADDED
Binary file (1.86 kB). View file
|
|
melo/text/__pycache__/english.cpython-312.pyc
ADDED
Binary file (8.54 kB). View file
|
|
melo/text/__pycache__/english_bert.cpython-312.pyc
ADDED
Binary file (2.06 kB). View file
|
|
melo/text/__pycache__/french.cpython-312.pyc
ADDED
Binary file (3.67 kB). View file
|
|
melo/text/__pycache__/french_bert.cpython-312.pyc
ADDED
Binary file (2.08 kB). View file
|
|
melo/text/__pycache__/japanese.cpython-312.pyc
ADDED
Binary file (18.5 kB). View file
|
|
melo/text/__pycache__/japanese_bert.cpython-312.pyc
ADDED
Binary file (2.42 kB). View file
|
|
melo/text/__pycache__/ko_dictionary.cpython-312.pyc
ADDED
Binary file (1.48 kB). View file
|
|
melo/text/__pycache__/korean.cpython-312.pyc
ADDED
Binary file (7.2 kB). View file
|
|
melo/text/__pycache__/spanish.cpython-312.pyc
ADDED
Binary file (4.51 kB). View file
|
|