From dd94d23d320e42dc93a8ade8fbcee4052249b7f4 Mon Sep 17 00:00:00 2001 From: litagin02 Date: Tue, 9 Jan 2024 16:02:32 +0900 Subject: [PATCH] Clean --- safetensors.ipynb | 154 ---------------------------------------------- server_test.ipynb | 74 ---------------------- 2 files changed, 228 deletions(-) delete mode 100644 safetensors.ipynb delete mode 100644 server_test.ipynb diff --git a/safetensors.ipynb b/safetensors.ipynb deleted file mode 100644 index 73f04fc..0000000 --- a/safetensors.ipynb +++ /dev/null @@ -1,154 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [ - "import os\n", - "\n", - "root = \"model_assets/jvnv-F1\"\n", - "pth_files = [os.path.join(root, f) for f in os.listdir(root) if f.endswith(\".pth\")]" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [], - "source": [ - "import torch\n", - "\n", - "model_name = \"G_0.pth\"\n", - "model = torch.load(f\"pretrained/{model_name}\", map_location=torch.device(\"cpu\"))" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "enc_p.emo_proj.bias\n", - "enc_p.emo_q_proj.weight\n", - "enc_p.emo_q_proj.bias\n" - ] - } - ], - "source": [ - "from safetensors.torch import save_file\n", - "state_dict = model[\"model\"]\n", - "new_dict = {}\n", - "for k in state_dict.keys():\n", - " if k.startswith(\"enc_p.emo\"):\n", - " print(k)\n", - " else:\n", - " new_dict[k] = state_dict[k]\n" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [], - "source": [ - "save_file(new_dict, f\"pretrained/{model_name.replace('.pth', '.safetensors')}\")" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [ - "from glob import glob\n", - "root_dir = \"model_assets\"\n", - "\n", - "safetensors_files = glob(f\"{root_dir}/**/*.safetensors\", recursive=True)\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# 推論に不要なenc_qを消し忘れていたのを削除\n", - "\n", - "from safetensors import safe_open\n", - "from safetensors.torch import save_file\n", - "\n", - "for path in safetensors_files:\n", - " print(path)\n", - " tensors = {}\n", - " with safe_open(path, framework=\"pt\", device=\"cpu\") as f:\n", - " for key in f.keys():\n", - " if key.startswith(\"enc_q\"):\n", - " print(key)\n", - " continue\n", - " tensors[key] = f.get_tensor(key)\n", - " save_file(tensors, path.replace(\".safetensors\", \".new.safetensors\"))" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "enc_p.xvec_proj.weight enc_p.style_proj.weight\n", - "enc_p.xvec_proj.bias enc_p.style_proj.bias\n" - ] - } - ], - "source": [ - "# pthファイルを推論用safetensorsに変換\n", - "from safetensors.torch import save_file\n", - "from safetensors import safe_open\n", - "import torch\n", - "\n", - "pth_path = \"model_assets/jvnv-F1/release_7000.pth\"\n", - "pth_weight = torch.load(pth_path, map_location=torch.device(\"cpu\"))\n", - "new_dict = {}\n", - "for key in pth_weight[\"model\"]:\n", - " if key.startswith(\"enc_p.xvec_proj.\"): # 前のモデルの名残\n", - " print(key, key.replace(\"enc_p.xvec_proj.\", \"enc_p.style_proj.\"))\n", - " new_dict[key.replace(\"enc_p.xvec_proj.\", \"enc_p.style_proj.\")] = pth_weight[\"model\"][key].clone().contiguous() # よく分からないおまじないをしないとエラーになる\n", - " elif not key.startswith(\"enc_q\"):\n", - " new_dict[key] = pth_weight[\"model\"][key]\n", - " else:\n", - " continue\n", - "new_dict[\"iteration\"] = torch.LongTensor([pth_weight[\"iteration\"]])\n", - "save_file(new_dict, pth_path.replace(\".pth\", \".pth.safetensors\"))\n" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "venv", - "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.11" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/server_test.ipynb b/server_test.ipynb deleted file mode 100644 index 36742eb..0000000 --- a/server_test.ipynb +++ /dev/null @@ -1,74 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 15, - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "\n", - " \n", - " " - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "import requests\n", - "from IPython.display import Audio\n", - "\n", - "\n", - "def fecth_and_play_voice(params):\n", - " url = \"http://127.0.0.1:5000/voice\"\n", - " response = requests.get(url, params=params)\n", - "\n", - " if response.status_code == 200:\n", - " display(Audio(response.content))\n", - " else:\n", - " print(f\"Error: {response.status_code}\")\n", - " print(response.json())\n", - "\n", - "\n", - "params = {\n", - " \"text\": \"こんにちは、吾輩は猫である。名前はまだない。\",\n", - " \"model_id\": 10,\n", - " \"style\": \"Neutral\",\n", - " \"style_weight\": 100\n", - "}\n", - "\n", - "fecth_and_play_voice(params)" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "venv", - "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.11" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -}