|
|
import torch |
|
|
import hydra |
|
|
from hydra import compose, initialize |
|
|
from hydra.utils import instantiate |
|
|
from lightning import LightningModule |
|
|
from loguru import logger |
|
|
from omegaconf import OmegaConf |
|
|
import sys |
|
|
sys.path.insert(0,'/workspace/user_code/kuachen/projects/v2s') |
|
|
|
|
|
from fish_speech.models.v2s_tts.pretrain_model import V2S_TTS_Pretrain_Model |
|
|
from fish_speech.models.v2s_tts.flow_matching_dit import ConditionalCFM |
|
|
from fish_speech.models.v2s_tts.model.backbones.dit import DiT_Style |
|
|
from fish_speech.models.v2s_tts.transformer.encoder import ConformerEncoder |
|
|
from fish_speech.models.v2s_tts.style_bank import StyleBankExtractor |
|
|
from omegaconf import DictConfig |
|
|
|
|
|
class CFMParams: |
|
|
def __init__(self): |
|
|
self.sigma_min = 1e-06 |
|
|
self.solver = "euler" |
|
|
self.t_scheduler = "cosine" |
|
|
self.training_cfg_rate = 0.2 |
|
|
self.inference_cfg_rate = 0.7 |
|
|
self.reg_loss_type = "l1" |
|
|
|
|
|
|
|
|
def load_model(config_name, checkpoint_path, device="cpu"): |
|
|
hydra.core.global_hydra.GlobalHydra.instance().clear() |
|
|
with initialize(version_base="1.3", config_path="../fish_speech/configs"): |
|
|
cfg = compose(config_name=config_name) |
|
|
|
|
|
model: LightningModule = instantiate(cfg.model) |
|
|
state_dict = torch.load( |
|
|
checkpoint_path, |
|
|
map_location=model.device, |
|
|
) |
|
|
|
|
|
if "state_dict" in state_dict: |
|
|
state_dict = state_dict["state_dict"] |
|
|
|
|
|
model.load_state_dict(state_dict, strict=False) |
|
|
model.eval() |
|
|
model.to(device) |
|
|
logger.info("Restored model from checkpoint") |
|
|
|
|
|
return model |
|
|
|
|
|
def get_pretrain_model(checkpoint_path): |
|
|
|
|
|
encoder = ConformerEncoder( |
|
|
output_size=512, |
|
|
attention_heads=8, |
|
|
linear_units=2048, |
|
|
num_blocks=6, |
|
|
dropout_rate=0.1, |
|
|
positional_dropout_rate=0.1, |
|
|
attention_dropout_rate=0.1, |
|
|
normalize_before=True, |
|
|
input_layer='linear', |
|
|
pos_enc_layer_type='rel_pos_espnet', |
|
|
selfattention_layer_type='rel_selfattn', |
|
|
input_size=512, |
|
|
use_cnn_module=False, |
|
|
macaron_style=False |
|
|
) |
|
|
|
|
|
|
|
|
style_qformer = StyleBankExtractor( |
|
|
dim_in=1024, |
|
|
n_layers=4, |
|
|
n_emb=32, |
|
|
d_model=64, |
|
|
nhead=4 |
|
|
) |
|
|
|
|
|
|
|
|
estimator = DiT_Style( |
|
|
dim=1024, |
|
|
depth=22, |
|
|
heads=16, |
|
|
ff_mult=2, |
|
|
conv_layers=4, |
|
|
mel_dim=80, |
|
|
style_dim=64 |
|
|
) |
|
|
|
|
|
cfm_params = CFMParams() |
|
|
|
|
|
|
|
|
decoder = ConditionalCFM( |
|
|
in_channels=160, |
|
|
n_spks=0, |
|
|
spk_emb_dim=80, |
|
|
cfm_params=cfm_params, |
|
|
estimator=estimator |
|
|
) |
|
|
|
|
|
|
|
|
model = V2S_TTS_Pretrain_Model( |
|
|
input_size=512, |
|
|
output_size=80, |
|
|
output_type='mel', |
|
|
vocab_size=500, |
|
|
spk_dim=192, |
|
|
sll_checkpoint='checkpoints/wavlm_large.pt', |
|
|
output_layer=6, |
|
|
decoder=decoder, |
|
|
encoder=encoder, |
|
|
style_qformer=style_qformer |
|
|
) |
|
|
state_dict = torch.load(checkpoint_path)['state_dict'] |
|
|
new_params = {} |
|
|
for k,v in state_dict.items(): |
|
|
if k.startswith('generator'): |
|
|
new_k = k[10:] |
|
|
new_params[new_k] = v |
|
|
|
|
|
model.load_state_dict(new_params, strict=True) |
|
|
model.eval() |
|
|
return model |
|
|
|
|
|
|
|
|
def get_pretrain_model_32_dim32(checkpoint_path): |
|
|
|
|
|
encoder = ConformerEncoder( |
|
|
output_size=512, |
|
|
attention_heads=8, |
|
|
linear_units=2048, |
|
|
num_blocks=6, |
|
|
dropout_rate=0.1, |
|
|
positional_dropout_rate=0.1, |
|
|
attention_dropout_rate=0.1, |
|
|
normalize_before=True, |
|
|
input_layer='linear', |
|
|
pos_enc_layer_type='rel_pos_espnet', |
|
|
selfattention_layer_type='rel_selfattn', |
|
|
input_size=512, |
|
|
use_cnn_module=False, |
|
|
macaron_style=False |
|
|
) |
|
|
|
|
|
|
|
|
style_qformer = StyleBankExtractor( |
|
|
dim_in=1024, |
|
|
n_layers=4, |
|
|
n_emb=32, |
|
|
d_model=32, |
|
|
nhead=4 |
|
|
) |
|
|
|
|
|
|
|
|
estimator = DiT_Style( |
|
|
dim=1024, |
|
|
depth=22, |
|
|
heads=16, |
|
|
ff_mult=2, |
|
|
conv_layers=4, |
|
|
mel_dim=80, |
|
|
style_dim=32 |
|
|
) |
|
|
|
|
|
cfm_params = CFMParams() |
|
|
|
|
|
|
|
|
decoder = ConditionalCFM( |
|
|
in_channels=160, |
|
|
n_spks=0, |
|
|
spk_emb_dim=80, |
|
|
cfm_params=cfm_params, |
|
|
estimator=estimator |
|
|
) |
|
|
|
|
|
|
|
|
model = V2S_TTS_Pretrain_Model( |
|
|
input_size=512, |
|
|
output_size=80, |
|
|
output_type='mel', |
|
|
vocab_size=500, |
|
|
spk_dim=192, |
|
|
sll_checkpoint='checkpoints/wavlm_large.pt', |
|
|
output_layer=6, |
|
|
decoder=decoder, |
|
|
encoder=encoder, |
|
|
style_qformer=style_qformer |
|
|
) |
|
|
state_dict = torch.load(checkpoint_path)['state_dict'] |
|
|
new_params = {} |
|
|
for k,v in state_dict.items(): |
|
|
if k.startswith('generator'): |
|
|
new_k = k[10:] |
|
|
new_params[new_k] = v |
|
|
|
|
|
model.load_state_dict(new_params, strict=True) |
|
|
model.eval() |
|
|
return model |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|