{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4c919a7f-a4bb-4736-b927-abb0f29f2ad8",
   "metadata": {},
   "source": [
    "# kohya_ss gui训练混元DiT教程"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "176ca8ef-30a8-4aba-99c6-76af5a86d299",
   "metadata": {},
   "source": [
    "## 1. 数据集一键打标\n",
    "运行以下代码，即可为每张图片得到一个同名的txt文件，里面存放着对应图片的描述  \n",
    "\"/root/demo_images_wukong\" 可以替换为你自己上传解压后的数据集路径  \n",
    "如何解压自己上传的压缩包？命令：! unzip xxx.zip -d ./xxx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "47e1e013-1b64-4a6a-8938-e28aeb1aee72",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 演示用\n",
    "! unzip /root/xxx.zip -d ./root/xxx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "af012425-b64d-483c-87c4-53d55a775b8f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 一键打标数据集\n",
    "%cd HunyuanDiT\n",
    "! python mllm/caption_imgs_with_txt.py \\\n",
    "    --model_path \"./ckpts/captioner\" \\\n",
    "    --mode \"caption_zh\" \\\n",
    "    --image_folder \"/root/demo_images_wukong\"\n",
    "%cd /root"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ba7bc5b4-9bb8-44be-b83a-0d2b63186d42",
   "metadata": {},
   "source": [
    "## 2. 使用kohya_ss gui训练"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bccd05f6",
   "metadata": {},
   "source": [
    "首先，按照[README.md](.\\README.md)配置好基础的Kohya_ss GUI for 混元训练环境并启动web界面。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a50a4544",
   "metadata": {},
   "source": [
    "### 2.1 使用预设训练参数"
   ]
  },
  {
   "attachments": {
    "317d2fda-e6de-4ebb-a93a-79ecdafc7bbf.png": {
     "image/png": "[encoded data, 95548 characters]"
    }
   },
   "cell_type": "markdown",
   "id": "abb890cf-b906-416e-8509-40966dbbe60f",
   "metadata": {},
   "source": [
    "说明：下面的流程已经配置好，可以直接加载/root/kohya_ss/huynyuan_lora.json读取样例配置 （输入json配置路径后，点击右侧按钮进行载入）  \n",
    "注意：默认载入的样例配置不一定是最佳配置，请按照自己的数据 & 任务调整训练轮数、步数、lora rank、dropout等超参数，以获得最佳lora训练效果\n",
    "![image.png](attachment:317d2fda-e6de-4ebb-a93a-79ecdafc7bbf.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7cc311f1-63aa-4c40-a20c-15ac9086aa58",
   "metadata": {},
   "source": [
    "### 2.2 自行填写训练参数"
   ]
  },
  {
   "attachments": {
    "83bd6293-ccbb-460a-af82-7c7eac28e201.png": {
     "image/png": "[encoded data, 74988 characters]"
    }
   },
   "cell_type": "markdown",
   "id": "0017ade8-ecbf-4dce-bb55-18722e3238fc",
   "metadata": {},
   "source": [
    "提醒：在这里把lora模型的说明信息修改成你的作者信息，以便在被分享传播的lora模型文件中存入一份你的印记。\n",
    "![image.png](attachment:83bd6293-ccbb-460a-af82-7c7eac28e201.png)"
   ]
  },
  {
   "attachments": {
    "423c5053-0d57-4a50-8a08-2350650f580b.png": {
     "image/png": "[encoded data, 283304 characters]"
    }
   },
   "cell_type": "markdown",
   "id": "556fa2d7-f75e-4b8e-b66c-4d1f213c5da9",
   "metadata": {},
   "source": [
    "按如下方式配置预训练模型路径、数据集路径、输出模型路径，以及其他训练选项。\n",
    "![image.png](attachment:423c5053-0d57-4a50-8a08-2350650f580b.png)"
   ]
  },
  {
   "attachments": {
    "image.png": {
     "image/png": "[encoded data, 300784 characters]"
    }
   },
   "cell_type": "markdown",
   "id": "95fc731b",
   "metadata": {},
   "source": [
    "下面选项可以设置一些和lora rank、alpha、dropout相关的参数，可以自行调教以获得最优训练效果。\n",
    "![image.png](attachment:image.png)"
   ]
  },
  {
   "attachments": {
    "7832c841-0bc0-44e7-bcd8-68413176e5e6.png": {
     "image/png": "[encoded data, 177804 characters]"
    }
   },
   "cell_type": "markdown",
   "id": "1f582046-3b4f-454e-8f21-dba4ecabb3a0",
   "metadata": {},
   "source": [
    "勾选下面的选项以节省显存，防止显存不够用。(位置：Parameters -> Advanced -> Gradient Checkpointing）\n",
    "![image.png](attachment:7832c841-0bc0-44e7-bcd8-68413176e5e6.png)"
   ]
  },
  {
   "attachments": {
    "ef492675-d4d1-467c-bb6a-3f24cea6565a.png": {
     "image/png": "[encoded data, 274548 characters]"
    }
   },
   "cell_type": "markdown",
   "id": "0bc27816-bc31-42f0-9496-d1f22098d574",
   "metadata": {},
   "source": [
    "点击下面按钮，开始训练。\n",
    "![image.png](attachment:ef492675-d4d1-467c-bb6a-3f24cea6565a.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1fa93367-b130-45df-abef-0fed708ddb02",
   "metadata": {},
   "source": [
    "## 3. 到ComfyUI测试模型效果"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a99e189f-5320-4071-a542-d2173f565ade",
   "metadata": {},
   "source": [
    "训练好lora后，可以在模型输出路径里（这里图上填写的是/root/kohya_ss/outputs)找到对应的模型权重文件（.safetensors格式结尾看最近时间的文件），复制到ComfyUI/models/loras/目录下使用ComfyUI做lora测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "5e38f144-b084-4c8a-8d97-d69ae8d715e5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "复制完成\n"
     ]
    }
   ],
   "source": [
    "! cp /path/to/kohya_ss/outputs/last-step00001600.safetensors /path/to/ComfyUI/models/loras/ && echo \"复制完成\""
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.9.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
