{ "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import torch\n", "#### 迁移gpt模型的embedding层,插入两个新的token\n", "dict = torch.load(r\"D:\\pyprojs\\GPT-SoVITSs\\fork\\GPT-SoVITS\\GPT_SoVITS\\pretrained_models\\s1bert25hz-2kh-longer-epoch=68e-step=50232-2.ckpt\",map_location=torch.device('cpu'))\n", "dict[\"weight\"][\"model.ar_text_embedding.word_embeddings.weight\"].shape\n", "# 0~94保持不动,95~向后移动两位,新的95 96随机初始化\n", "# 一共512列,embedding_dim=5\n", "first_part = dict[\"weight\"][\"model.ar_text_embedding.word_embeddings.weight\"][:95,:]\n", "second_part = dict[\"weight\"][\"model.ar_text_embedding.word_embeddings.weight\"][95:-2,:]\n", "new_weight = torch.cat([first_part,second_part, torch.randn(2, 512)],dim=0)\n", "dict[\"weight\"][\"model.ar_text_embedding.word_embeddings.weight\"] = new_weight\n", "torch.save(dict,r\"D:\\pyprojs\\GPT-SoVITSs\\fork\\GPT-SoVITS\\GPT_SoVITS\\pretrained_models\\s1bert25hz-hs_model.ckpt\")" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import torch\n", "dict2 = torch.load(r\"D:\\pyprojs\\GPT-SoVITSs\\fork\\GPT-SoVITS\\GPT_SoVITS\\pretrained_models\\s2G488k-legacy.pth\",map_location=torch.device('cpu'))\n", "dict2[\"weight\"][\"enc_p.text_embedding.weight\"].shape\n", "# 0~94保持不动,95~向后移动两位,新的95 96随机初始化\n", "first_part = dict2[\"weight\"][\"enc_p.text_embedding.weight\"][:95,:]\n", "second_part = dict2[\"weight\"][\"enc_p.text_embedding.weight\"][95:,:]\n", "new_weight = torch.cat([first_part,torch.zeros(2,192),second_part],dim=0)\n", "dict2[\"weight\"][\"enc_p.text_embedding.weight\"] = new_weight\n", "torch.save(dict2,r\"D:\\pyprojs\\GPT-SoVITSs\\fork\\GPT-SoVITS\\GPT_SoVITS\\pretrained_models\\s2G488k.pth\")" ] } ], "metadata": { "kernelspec": { "display_name": "vits", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.0" } }, "nbformat": 4, "nbformat_minor": 2 }