{"id":16269418,"url":"https://github.com/crazyboym/dreambooth-for-diffusion","last_synced_at":"2025-04-05T14:05:06.090Z","repository":{"id":65605808,"uuid":"562890302","full_name":"CrazyBoyM/dreambooth-for-diffusion","owner":"CrazyBoyM","description":"文生图大模型训练工具箱 （完整封装stable diffusion全量微调训练流程， 可训练定制自己的独特风格、人物概念，开箱即用， 含自动图像标注、权重转换、训练参数配置等）","archived":false,"fork":false,"pushed_at":"2023-05-18T12:45:37.000Z","size":135,"stargazers_count":461,"open_issues_count":8,"forks_count":67,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-03-29T13:06:07.463Z","etag":null,"topics":["dreambooth","easy-to-use","stable-diffusion-diffusers"],"latest_commit_sha":null,"homepage":"https://www.codewithgpu.com/i/CrazyBoyM/dreambooth-for-diffusion/dreambooth-for-diffusion","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/CrazyBoyM.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-11-07T13:25:53.000Z","updated_at":"2025-03-27T05:12:35.000Z","dependencies_parsed_at":"2024-12-22T15:11:45.696Z","dependency_job_id":"75a02304-7f8c-43e5-a3ba-6db8733ed7fc","html_url":"https://github.com/CrazyBoyM/dreambooth-for-diffusion","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CrazyBoyM%2Fdreambooth-for-diffusion","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CrazyBoyM%2Fdreambooth-for-diffusion/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CrazyBoyM%2Fdreambooth-for-diffusion/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CrazyBoyM%2Fdreambooth-for-diffusion/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/CrazyBoyM","download_url":"https://codeload.github.com/CrazyBoyM/dreambooth-for-diffusion/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247345850,"owners_count":20924102,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dreambooth","easy-to-use","stable-diffusion-diffusers"],"created_at":"2024-10-10T18:08:02.078Z","updated_at":"2025-04-05T14:05:06.058Z","avatar_url":"https://github.com/CrazyBoyM.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Dreambooth Stable Diffusion 集成化环境训练\n如果你是在autodl上的机器可以直接使用封装好的镜像创建实例，开箱即用  \n如果是本地或者其他服务器上也可以使用，需要手动安装一些pip包\n\n## 如何运行\n直接在autodl使用镜像运行：https://www.codewithgpu.com/i/CrazyBoyM/dreambooth-for-diffusion/dreambooth-for-diffusion  \n\n如果你不熟悉notebook代码的训练方式，也可以直接使用封装好的webui在线镜像（含稳定Dreambooth、dreamArtist训练插件，已fix）：  \nhttps://www.codewithgpu.com/i/CrazyBoyM/sd_dreambooth_extension_webui/dreambooth-dreamartist-for-webui\n\n## 注意\n本项目仅供用于学习、测试人工智能技术使用  \n请勿用于训练生成不良或侵权图片内容\n\n## 关于项目\n在autodl封装的镜像名称为：dreambooth-for-diffusion  \n可在创建实例时直接选择公开的算法镜像使用。  \n在autodl内蒙A区A5000的机器上封装，如遇到问题且无法自行解决的朋友请使用同一环境。  \n白菜写教程时做了尽可能多的测试，但仍然无法确保每一个环节都完全覆盖    \n如有小错误可尝试手动解决，或者访问git项目地址查看最新的README  \n项目地址：https://github.com/CrazyBoyM/dreambooth-for-diffusion\n\n如果遇到问题可到b站主页找该教程对应训练演示的视频：https://space.bilibili.com/291593914\n（因为现在写时视频还没做）\n\n## 强烈建议\n1.用vscode的ssh功能远程连接到本服务器，训练体验更好，autodl自带的notebook也不错，有文件上传、下载功能。  \n2.(重要)先把/root/目录下dreambooth-for-diffusion文件夹整个移动到/root/autodl-tmp/路径下(数据盘)，避免系统盘空间满\n\n## 进入工作文件夹\n```\ncd /root/autodl-tmp/dreambooth-for-diffusion\n```\n\n## 转换ckpt检查点文件为diffusers官方权重\n已经内置了两个基础模型，可以根据自己数据集的特性选择。    \n- sd_1-5.ckpt是偏真实风格  \n- nd_lastest.ckpt是偏二次元风格  \n开始转换二次元模型：\n```\n# 该步需要运行大约一分钟 \n!python tools/ckpt2diffusers.py \\\n    --checkpoint_path=./ckpt_models/nd_lastest.ckpt \\\n    --dump_path=./model \\\n    --vae_path=./ckpt_models/animevae.pt \\\n    --original_config_file=./ckpt_models/model.yaml \\\n    --scheduler_type=\"ddim\"\n```\n转换写实风格模型：\n```\n# 该步需要运行大约一分钟 \n!python tools/ckpt2diffusers.py \\\n    --checkpoint_path=./ckpt_models/sd_1-5.ckpt \\\n    --dump_path=./model \\\n    --original_config_file=./ckpt_models/model.yaml \\\n    --scheduler_type=\"ddim\"\n```\n这里后面跟的两个文件分别是你的ckpt文件和转换后的输出路径。\n\n## 转换diffusers官方权重为ckpt检查点文件\n```\npython tools/diffusers2ckpt.py ./new_model ./ckpt_models/newModel_half.ckpt --half\n```\n如需保存为float16版精度，添加--half参数，权重大小会减半。\n\n## 准备数据集\n请按照训练任务准备好对应的数据集。\n### 图像裁剪为512*512\n我在tools/handle_images.py中提供了一份批量处理的代码用于参考  \n自动center crop图像，并缩放尺寸\n```\npython tools/handle_images.py ./datasets/test ./datasets/test2 --width=512 --height=512\n```\ntest为未处理的原始图像文件夹，test2为输出处理图像的路径  \n如需处理透明背景png图为黑色/白色底jpg，可以添加--png参数。\n\n### 图像自动标注\n使用deepdanbooru生成tags label.\n```\n!python tools/label_images.py  --path=./datasets/test2 \n```\n第二个参数--path为你需要标注的图像文件夹路径   \n\n注：如提示deepdanbooru找不到，可自行参考以下仓库进行编译  \nhttps://github.com/KichangKim/DeepDanbooru  \n\n我在other文件夹下也提供了一份编译好的版本：\n```\npip install other/deepdanbooru-1.0.0-py3-none-any.whl \n```\n\n## 训练以及常用命令总结\n### 配置训练环境（可选）\n如果你不是在封装好的镜像上直接使用，则需要做以下配置：\n```\npip install accelerate\n```\n运行以下命令，并选择本地运行、NO、NO\n```\naccelerate config\n```\n\n### 开始训练 \n请打开train.sh文件，参考其中的具体参数说明。  \n如果需要训练特定人、事物： \n（推荐准备3~5张风格统一、特定对象的图片）\n\n```\nsh train_object.sh\n```\n\n如果要Finetune训练自己的大模型： \n（推荐准备3000+张图片，包含尽可能的多样性，数据决定训练出的模型质量）\n```\nsh train_style.sh\n```\nA5000的训练速度大概8分钟/1000步\n\n### 测试训练效果\n打开train/test_model.py文件修改其中的model_path和prompt，然后执行：\n```\npython test_model.py\n```\n\n### 其他常用命令\n如需后台任务训练：  \n```\nnohup sh train_style.sh \u0026\n```\n推荐晚上这样挂后台跑着，不需要担心连接中断导致的训练停止。\n白菜个人推荐的省钱训练小妙招:\n```\nnohup sh back_train.sh \u0026\n```\n(训练完直接自动关机)\n\n训练日志会输出到nohup.out文件中，可以vscode直接打开或下载查看。  \n查看日志后十行：  \n```\ntail -n 10 nohup.out\n```\n\n查看当前磁盘占有率：  \n（记得清理不要的文件，不然经常容易磁盘几十个g空间满导致模型保存失败！！）  \n```\ndf -h\n```\n\n## 如果你是在其他服务器上执行，没有使用集成环境\n提示缺少一些包可以自行安装：\n```\npip install diffusers\npip install ftfy\npip install tensorflow-gpu\npip install pytorch_lightning\npip install OmegaConf\n... 以及其他的一些\n```\n\n## 学术加速（可选）\n如果你需要拉取git上一些内容，发现速度很慢，以下内容或许对你有帮助。  \n请根据机器所在区域执行以下命令：\n```\n北京A区的实例¶\nexport http_proxy=http://100.72.64.19:12798 \u0026\u0026 export https_proxy=http://100.72.64.19:12798\n\n内蒙A区的实例¶\nexport http_proxy=http://192.168.1.174:12798 \u0026\u0026 export https_proxy=http://192.168.1.174:12798\n\n泉州A区的实例¶\nexport http_proxy=http://10.55.146.88:12798 \u0026\u0026 export https_proxy=http://10.55.146.88:12798\n```\n\n## xformers(可选)\n由于A5000实测训练和推理的速度已经很快了，就没有安装。  \n如果你使用的是其他显卡或者实在有需要，可以参考下面的地址进行编译使用：  \nhttps://github.com/facebookresearch/xformers  \n（我猜到你可能想要尝试，已经在train/other目录下放了一个提前编译好的版本啦）  \n注：需要升级pytorch版本到1.12.x以上才能安装使用（好懒）(更新：我已经升级好并帮你装好啦~！)\n\n## 升级pytorch版本到1.12.x\n```\npip install torch==1.12.0+cu113 torchvision==0.13.0+cu113 torchaudio==0.12.0 --extra-index-url https://download.pytorch.org/whl/cu113\n```\n\n# 关于autodl的使用心得\n\n## 服务器的数据迁移\n经常关机后再开机发现机器资源被占用了，这时候你只能另外开一台机器了  \n但是对于已经关机的机器在菜单上有个功能是“跨实例拷贝数据”，  \n可以很方便地同步/root/autodl-tmp文件夹下的内容到其他已开机的机器（所以推荐工作文件都放这）  \n（注意，只适用于同一区域的机器之间）\n数据迁移教程：https://www.autodl.com/docs/migrate_instance/\n\n## 传输文件的方式\n### 方式一 使用VScode\n直接从vscode拖动上传、下载文件，速度慢，也最简单。  \n\n### 方式二 使用autodl的用户网盘\n在autodl的网盘界面初始化一个同区域的网盘，然后重启一下服务器实例  \n会发现多了一个文件夹/root/autodl-nas/, 你可以在网页界面进行权重和数据的上传  \n训练完，把生成的权重文件移动到该路径下，就可以去网页上进行下载了。  \n（对应网页：https://www.autodl.com/console/netdisk）\n注意：初始化的网盘一定要和服务器处于同一区域.  \n\n### 方式三 使用对象存储\n有条件的朋友也可以尝试使用cos或oss进行文件中转，速度更快。  \n在train/tools文件夹中我也放置了一份上传到cos的代码供参考，请有经验的朋友自行使用。  \n\nautodl官网也有一些推荐的方式可以参考，https://www.autodl.com/docs/scp/\n\n# 其他内容\n感谢diffusers、deepdanbooru等开源项目  \n风格训练代码来自nbardy的PR进行修改  \n打tags标签的部分代码来自crosstyan、Nyanko Lepsoni、AUTOMATC1111  \n如果感兴趣欢迎加QQ群探讨交流，455521885  \n封装整理by - 白菜 \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcrazyboym%2Fdreambooth-for-diffusion","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcrazyboym%2Fdreambooth-for-diffusion","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcrazyboym%2Fdreambooth-for-diffusion/lists"}