forked from eigenpunk/ComfyUI-audio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtacotron_nodes.py
407 lines (320 loc) · 11.3 KB
/
tacotron_nodes.py
1
2
3
4
5
6
7
8
9
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
import json
import os
import sys
from glob import glob
import torch
base_incl_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "include")
sys.path = [
os.path.join(base_incl_path, "hifi-gan"),
] + sys.path
from denoiser import Denoiser as HifiGANDenoiser
from env import AttrDict
from meldataset import mel_spectrogram, MAX_WAV_VALUE
from models import Generator as HifiGAN
sys.path = [
os.path.join(base_incl_path, "tacotron2"),
os.path.join(base_incl_path, "tacotron2", "waveglow"),
] + sys.path
from hparams import create_hparams
from model import Tacotron2
from train import load_model
from text import text_to_sequence
from denoiser import Denoiser as WaveGlowDenoiser
from .util import do_cleanup, get_device, models_dir, object_to, obj_on_device
BIGINT = 2 ** 32
MODELS_PATH = os.path.join(models_dir, "tacotron2")
WAVEGLOW_MODELS_PATH = os.path.join(models_dir, "waveglow")
HIFIGAN_MODELS_PATH = os.path.join(models_dir, "hifigan")
os.makedirs(MODELS_PATH, exist_ok=True)
os.makedirs(WAVEGLOW_MODELS_PATH, exist_ok=True)
os.makedirs(HIFIGAN_MODELS_PATH, exist_ok=True)
MODELS = {
x.removeprefix(MODELS_PATH)[1:]: x
for x in sorted(glob(os.path.join(MODELS_PATH, "*.pt")))
}
WAVEGLOW_MODELS = {
x.removeprefix(WAVEGLOW_MODELS_PATH)[1:]: x
for x in sorted(glob(os.path.join(WAVEGLOW_MODELS_PATH, "*")))
}
HIFIGAN_MODELS = {
x.removeprefix(HIFIGAN_MODELS_PATH)[1:]: x
for x in sorted(glob(os.path.join(HIFIGAN_MODELS_PATH, "*")))
}
HIFIGAN_CONFIGS = {
os.path.basename(x): x
for x in glob(os.path.join(base_incl_path, "hifi-gan", "config_*.json"))
}
class Tacotron2Loader:
"""
loads a Tacotron2 model
"""
def __init__(self):
self.model = None
@classmethod
def INPUT_TYPES(cls):
return {
"required": {"model_name": (list(MODELS.keys()),),}
}
RETURN_NAMES = ("TT2_MODEL", "SR")
RETURN_TYPES = ("TT2_MODEL", "INT")
FUNCTION = "load"
CATEGORY = "audio"
def load(self, model_name):
if self.model is not None:
self.model = object_to(self.model, empty_cuda_cache=False)
del self.model
do_cleanup()
print("Tacotron2Loader: unloaded model")
print("Tacotron2Loader: loading model")
hparams = create_hparams()
hparams.sampling_rate = 22050
path = MODELS[model_name]
self.model = load_model(hparams)
sd = torch.load(path, map_location="cpu")["state_dict"]
self.model.load_state_dict(sd)
self.model.device = "cpu"
self.model.eval().half()
return self.model, 22050
class WaveGlowLoader:
"""
loads a WaveGlow model
"""
def __init__(self):
self.model = None
self.denoiser = None
@classmethod
def INPUT_TYPES(cls):
return {"required": {"model_name": (list(WAVEGLOW_MODELS.keys()),),}}
RETURN_TYPES = ("WG_MODEL",)
FUNCTION = "load"
CATEGORY = "audio"
def load(self, model_name):
if self.model is not None:
self.model = object_to(self.model, empty_cuda_cache=False)
self.denoiser = object_to(self.denoiser, empty_cuda_cache=False)
del self.model, self.denoiser
do_cleanup()
print("WaveGlowLoader: unloaded model")
print("WaveGlowLoader: loading model")
path = WAVEGLOW_MODELS[model_name]
self.model = torch.load(path, map_location="cpu")["model"]
self.model.eval().half()
for k in self.model.convinv:
k.float()
self.denoiser = WaveGlowDenoiser(self.model)
return (self.model, self.denoiser),
class HifiGANLoader:
"""
loads a HifiGAN model
"""
def __init__(self):
self.model = None
self.denoiser = None
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"model_name": (list(HIFIGAN_MODELS.keys()),),
"config": (list(HIFIGAN_CONFIGS.keys()),),
}
}
RETURN_TYPES = ("HG_MODEL",)
FUNCTION = "load"
CATEGORY = "audio"
def load(self, model_name, config):
if self.model is not None:
self.model = object_to(self.model, empty_cuda_cache=False)
self.denoiser = object_to(self.denoiser, empty_cuda_cache=False)
del self.model, self.denoiser
do_cleanup()
print("HifiGANLoader: unloaded model")
print("HifiGANLoader: loading model")
with open(HIFIGAN_CONFIGS[config], "r") as f:
cfg = AttrDict(json.load(f))
path = HIFIGAN_MODELS[model_name]
# model insists on choosing device itself
device = HifiGANDenoiser.device
self.model = HifiGAN(cfg).to(device)
sd = torch.load(path, map_location=device)["generator"]
self.model.load_state_dict(sd)
self.model.eval()
self.model.remove_weight_norm()
self.denoiser = HifiGANDenoiser(self.model, mode="normal")
self.model.cpu()
self.denoiser.cpu()
self.model.device = "cpu"
self.denoiser.device = "cpu"
return (self.model, self.denoiser, cfg),
class Tacotron2Generate:
"""
generates speech mels from text using Tacotron2
"""
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"model": ("TT2_MODEL",),
"text": ("STRING", {"default": "hello world", "multiline": True}),
"seed": ("INT", {"default": 0, "min": 0}),
},
}
RETURN_NAMES = ("mel_outputs", "postnet_outputs")
RETURN_TYPES = ("MEL_TENSOR", "MEL_TENSOR")
FUNCTION = "generate"
CATEGORY = "audio"
def generate(
self,
model: Tacotron2,
text: str = "",
seed: int = 0,
):
device = get_device()
sequence = text_to_sequence(text, ['basic_cleaners'])
with (
torch.no_grad(),
torch.random.fork_rng(),
obj_on_device(model, dst=device, verbose_move=True) as m
):
prev_device = m.device
m.device = device
torch.manual_seed(seed)
sequence = torch.tensor(sequence, dtype=torch.long).unsqueeze(0).to(device)
mel_outputs, mel_outputs_postnet, *_ = m.inference(sequence)
m.device = prev_device
do_cleanup()
return mel_outputs, mel_outputs_postnet
class WaveGlowApply:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"mels": ("MEL_TENSOR",),
"model": ("WG_MODEL",),
"sigma": ("FLOAT", {"default": 1.0, "min": 0.0}),
"denoiser_strength": ("FLOAT", {"default": 0.06, "min": 0}),
},
}
RETURN_NAMES = ("raw_audio",)
RETURN_TYPES = ("AUDIO_TENSOR",)
FUNCTION = "apply"
CATEGORY = "audio"
def apply(
self,
mels,
model,
sigma: float = 1.0,
denoiser_strength: float = 0.06,
):
device = get_device()
waveglow, denoiser = model
with (
torch.no_grad(),
torch.random.fork_rng(),
obj_on_device(waveglow, dst=device, verbose_move=True) as wg,
obj_on_device(denoiser, dst=device, verbose_move=True) as dn,
):
prev_device = wg.device
wg.device = dn.device = device
mels = mels.to(device)
audio = wg.infer(mels, sigma=sigma)
mels.cpu()
if denoiser_strength != 0.0:
audio = dn(audio, denoiser_strength=denoiser_strength)
audio = audio.cpu().unbind(0)
wg.device = dn.device = prev_device
do_cleanup()
return audio,
class HifiGANApply:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"mels": ("MEL_TENSOR",),
"model": ("HG_MODEL",),
"denoiser_strength": ("FLOAT", {"default": 0.06, "min": 0.0, "step": 0.001}),
},
}
RETURN_NAMES = ("raw_audio",)
RETURN_TYPES = ("AUDIO_TENSOR",)
FUNCTION = "apply"
CATEGORY = "audio"
def apply(self, mels, model, denoiser_strength: float = 0.06):
device = get_device()
hifigan, denoiser, cfg = model
with (
torch.no_grad(),
torch.random.fork_rng(),
obj_on_device(hifigan, dst=device, verbose_move=True) as hg,
obj_on_device(denoiser, dst=device, verbose_move=True) as dn,
):
prev_device = hg.device
hg.device = dn.device = device
mels = mels.to(device)
audio = hg(mels.float())
mels.cpu()
if denoiser_strength != 0.0:
audio *= MAX_WAV_VALUE
audio = dn(audio.squeeze(1), denoiser_strength)
audio /= MAX_WAV_VALUE
audio = list(audio.cpu().unbind(0))
hg.device = dn.device = prev_device
do_cleanup()
return audio,
class ToMelSpectrogram:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"audio": ("AUDIO_TENSOR",),
"sample_rate": ("INT", {"default": 22050, "min": 1, "max": BIGINT}),
"n_fft": ("INT", {"default": 1024, "min": 1, "max": BIGINT}),
"n_mels": ("INT", {"default": 80, "min": 1}),
"hop_len": ("INT", {"default": 256, "min": 1, "max": BIGINT}),
"win_len": ("INT", {"default": 1024, "min":1, "max": BIGINT}),
"fmin": ("INT", {"default": 0, "min": 0, "max": BIGINT}),
"fmax": ("INT", {"default": 8000, "min": 0, "max": BIGINT}),
},
}
RETURN_NAMES = ("mels",)
RETURN_TYPES = ("MEL_TENSOR",)
FUNCTION = "apply"
CATEGORY = "audio"
def apply(self, audio, sample_rate: int, n_fft: int, n_mels: int, hop_len: int, win_len: int, fmin: int, fmax: int):
with torch.no_grad():
mels = [mel_spectrogram(clip, n_fft, n_mels, sample_rate, hop_len, win_len, fmin, fmax) for clip in audio]
mels = torch.cat(mels)
do_cleanup()
return mels,
class HifiGANModelParams:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {"model": ("HG_MODEL",)},
}
RETURN_NAMES = ("sr", "n_mels", "n_fft", "hop_len", "win_len", "fmin", "fmax")
RETURN_TYPES = ("INT", "INT", "INT", "INT", "INT", "INT", "INT")
FUNCTION = "get"
CATEGORY = "audio"
def get(self, model):
*_, cfg = model
return cfg.sampling_rate, cfg.num_mels, cfg.n_fft, cfg.hop_size, cfg.win_size, cfg.fmin, cfg.fmax
NODE_CLASS_MAPPINGS = {
"Tacotron2Loader": Tacotron2Loader,
"Tacotron2Generate": Tacotron2Generate,
"HifiGANLoader": HifiGANLoader,
"HifiGANModelParams": HifiGANModelParams,
"HifiGANApply": HifiGANApply,
"WaveGlowLoader": WaveGlowLoader,
"WaveGlowApply": WaveGlowApply,
"ToMelSpectrogram": ToMelSpectrogram,
}
NODE_DISPLAY_NAME_MAPPINGS = {
"Tacotron2Loader": "Tacotron2 Loader",
"Tacotron2Generate": "Tacotron2 Generator",
"HifiGANLoader": "HifiGAN Loader",
"HifiGANModelParams": "Get HifiGAN Model Parameters",
"HifiGANApply": "Apply HifiGAN",
"WaveGlowLoader": "WaveGlow Loader",
"WaveGlowApply": "Apply WaveGlow",
"ToMelSpectrogram": "Audio to Mel Spectrogram",
}