6drf21e / ChatTTS_Speaker

ChatTTS 2000条音色稳定性打分🥇+区分男女年龄👧+在线试听🔈 ChatTTS 2K Speaker Stability Score & Categorized by Gender and Age & Audio Preview
537 stars 27 forks source link

ChatTTS不再支持加载固定音色pt解决方法 #7

Open KylinMountain opened 4 months ago

KylinMountain commented 4 months ago

添加如下压缩方法,将tensor压缩并使用pybase16384编码压缩后tensor为字符串

import lzma
import numpy as np
import pybase16384 as b14
def compress_and_encode(tensor):
    np_array = tensor.numpy().astype(np.float16)
    compressed = lzma.compress(np_array.tobytes(), format=lzma.FORMAT_RAW,
                               filters=[{"id": lzma.FILTER_LZMA2, "preset": 9 | lzma.PRESET_EXTREME}])
    encoded = b14.encode_to_string(compressed)
    return encoded

使用该压缩方法,即可加载

spk = torch.load("asset/seed_1332_restored_emb.pt", map_location=torch.device('cpu')).detach()
spk_emb_str = compress_and_encode(spk)
print(spk_emb_str)  # save it for later timbre recovery

params_infer_code = ChatTTS.Chat.InferCodeParams(
    spk_emb= spk_emb_str,  # add sampled speaker
    temperature=.0003,  # using custom temperature
    top_P=0.7,  # top P decode
    top_K=20,  # top K decode
)
FeeChain commented 4 months ago

为了保证复制粘贴即可无脑成功,建议增加: import lzma import numpy as np

Raven-D commented 4 months ago

可以提供一个直接把旧pt转换成新pt的脚本吗? 尽量不想在源码里加函数和判断:)

KylinMountain commented 4 months ago

也可以使用chat里的隐藏方法_encode_spk_emb,官方自带。脚本的话 自己拷贝过去不就得了?

    @staticmethod
    @torch.no_grad()
    def _encode_spk_emb(spk_emb: torch.Tensor) -> str:
        arr: np.ndarray = spk_emb.to(dtype=torch.float16, device="cpu").numpy()
        s = b14.encode_to_string(
            lzma.compress(
                arr.tobytes(),
                format=lzma.FORMAT_RAW,
                filters=[{"id": lzma.FILTER_LZMA2, "preset": 9 | lzma.PRESET_EXTREME}],
            ),
        )
        del arr
        return s
Raven-D commented 4 months ago

也可以使用chat里的隐藏方法_encode_spk_emb,官方自带。脚本的话 自己拷贝过去不就得了?

    @staticmethod
    @torch.no_grad()
    def _encode_spk_emb(spk_emb: torch.Tensor) -> str:
        arr: np.ndarray = spk_emb.to(dtype=torch.float16, device="cpu").numpy()
        s = b14.encode_to_string(
            lzma.compress(
                arr.tobytes(),
                format=lzma.FORMAT_RAW,
                filters=[{"id": lzma.FILTER_LZMA2, "preset": 9 | lzma.PRESET_EXTREME}],
            ),
        )
        del arr
        return s

谢谢,改代码ok的,我其实意思就是,把判断旧pt的逻辑直接写好在主分支中,这样我自己就不修改了,以后直接就update了,省的冲突,怕越改越多