Feat: slice-and-transcribe
This commit is contained in:
@@ -58,6 +58,76 @@
|
||||
"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": {
|
||||
|
||||
Reference in New Issue
Block a user