Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
KylinMountain opened this issue Jun 29, 2024 · 4 comments
Open

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

KylinMountain opened this issue Jun 29, 2024 · 4 comments

Comments

@KylinMountain
Copy link

KylinMountain commented Jun 29, 2024

添加如下压缩方法,将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
Copy link

FeeChain commented Jul 8, 2024

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

@Raven-D
Copy link

Raven-D commented Jul 9, 2024

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

@KylinMountain
Copy link
Author

也可以使用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
Copy link

Raven-D commented Jul 9, 2024

也可以使用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了,省的冲突,怕越改越多

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants