Files
sbv2-v2/safetensors.ipynb
2023-12-27 05:37:46 +09:00

85 lines
1.7 KiB
Plaintext

{
"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')}\")"
]
}
],
"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
}