CoolFace
Modelpublic

nonoJDWAOIDAWKDA/Shiori_reviewed_ft_StyleTTS2

sourceHugging Facemitupdated 1y agoView on Hugging Face
0likes4downloads
utils.py74 linesDownload Raw Back to root
1from monotonic_align import maximum_path2from monotonic_align import mask_from_lens3from monotonic_align.core import maximum_path_c4import numpy as np5import torch6import copy7from torch import nn8import torch.nn.functional as F9import torchaudio10import librosa11import matplotlib.pyplot as plt12from munch import Munch13 14def maximum_path(neg_cent, mask):15  """ Cython optimized version.16  neg_cent: [b, t_t, t_s]17  mask: [b, t_t, t_s]18  """19  device = neg_cent.device20  dtype = neg_cent.dtype21  neg_cent =  np.ascontiguousarray(neg_cent.data.cpu().numpy().astype(np.float32))22  path =  np.ascontiguousarray(np.zeros(neg_cent.shape, dtype=np.int32))23 24  t_t_max = np.ascontiguousarray(mask.sum(1)[:, 0].data.cpu().numpy().astype(np.int32))25  t_s_max = np.ascontiguousarray(mask.sum(2)[:, 0].data.cpu().numpy().astype(np.int32))26  maximum_path_c(path, neg_cent, t_t_max, t_s_max)27  return torch.from_numpy(path).to(device=device, dtype=dtype)28 29def get_data_path_list(train_path=None, val_path=None):30    if train_path is None:31        train_path = "Data/train_list.txt"32    if val_path is None:33        val_path = "Data/val_list.txt"34 35    with open(train_path, 'r', encoding='utf-8', errors='ignore') as f:36        train_list = f.readlines()37    with open(val_path, 'r', encoding='utf-8', errors='ignore') as f:38        val_list = f.readlines()39 40    return train_list, val_list41 42def length_to_mask(lengths):43    mask = torch.arange(lengths.max()).unsqueeze(0).expand(lengths.shape[0], -1).type_as(lengths)44    mask = torch.gt(mask+1, lengths.unsqueeze(1))45    return mask46 47# for norm consistency loss48def log_norm(x, mean=-4, std=4, dim=2):49    """50    normalized log mel -> mel -> norm -> log(norm)51    """52    x = torch.log(torch.exp(x * std + mean).norm(dim=dim))53    return x54 55def get_image(arrs):56    plt.switch_backend('agg')57    fig = plt.figure()58    ax = plt.gca()59    ax.imshow(arrs)60 61    return fig62 63def recursive_munch(d):64    if isinstance(d, dict):65        return Munch((k, recursive_munch(v)) for k, v in d.items())66    elif isinstance(d, list):67        return [recursive_munch(v) for v in d]68    else:69        return d70    71def log_print(message, logger):72    logger.info(message)73    print(message)74