[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"similar-haofanwang--ControlNet-for-Diffusers":3,"tool-haofanwang--ControlNet-for-Diffusers":64},[4,17,27,35,43,56],{"id":5,"name":6,"github_repo":7,"description_zh":8,"stars":9,"difficulty_score":10,"last_commit_at":11,"category_tags":12,"status":16},3808,"stable-diffusion-webui","AUTOMATIC1111\u002Fstable-diffusion-webui","stable-diffusion-webui 是一个基于 Gradio 构建的网页版操作界面，旨在让用户能够轻松地在本地运行和使用强大的 Stable Diffusion 图像生成模型。它解决了原始模型依赖命令行、操作门槛高且功能分散的痛点，将复杂的 AI 绘图流程整合进一个直观易用的图形化平台。\n\n无论是希望快速上手的普通创作者、需要精细控制画面细节的设计师，还是想要深入探索模型潜力的开发者与研究人员，都能从中获益。其核心亮点在于极高的功能丰富度：不仅支持文生图、图生图、局部重绘（Inpainting）和外绘（Outpainting）等基础模式，还独创了注意力机制调整、提示词矩阵、负向提示词以及“高清修复”等高级功能。此外，它内置了 GFPGAN 和 CodeFormer 等人脸修复工具，支持多种神经网络放大算法，并允许用户通过插件系统无限扩展能力。即使是显存有限的设备，stable-diffusion-webui 也提供了相应的优化选项，让高质量的 AI 艺术创作变得触手可及。",162132,3,"2026-04-05T11:01:52",[13,14,15],"开发框架","图像","Agent","ready",{"id":18,"name":19,"github_repo":20,"description_zh":21,"stars":22,"difficulty_score":23,"last_commit_at":24,"category_tags":25,"status":16},1381,"everything-claude-code","affaan-m\u002Feverything-claude-code","everything-claude-code 是一套专为 AI 编程助手（如 Claude Code、Codex、Cursor 等）打造的高性能优化系统。它不仅仅是一组配置文件，而是一个经过长期实战打磨的完整框架，旨在解决 AI 代理在实际开发中面临的效率低下、记忆丢失、安全隐患及缺乏持续学习能力等核心痛点。\n\n通过引入技能模块化、直觉增强、记忆持久化机制以及内置的安全扫描功能，everything-claude-code 能显著提升 AI 在复杂任务中的表现，帮助开发者构建更稳定、更智能的生产级 AI 代理。其独特的“研究优先”开发理念和针对 Token 消耗的优化策略，使得模型响应更快、成本更低，同时有效防御潜在的攻击向量。\n\n这套工具特别适合软件开发者、AI 研究人员以及希望深度定制 AI 工作流的技术团队使用。无论您是在构建大型代码库，还是需要 AI 协助进行安全审计与自动化测试，everything-claude-code 都能提供强大的底层支持。作为一个曾荣获 Anthropic 黑客大奖的开源项目，它融合了多语言支持与丰富的实战钩子（hooks），让 AI 真正成长为懂上",138956,2,"2026-04-05T11:33:21",[13,15,26],"语言模型",{"id":28,"name":29,"github_repo":30,"description_zh":31,"stars":32,"difficulty_score":23,"last_commit_at":33,"category_tags":34,"status":16},2271,"ComfyUI","Comfy-Org\u002FComfyUI","ComfyUI 是一款功能强大且高度模块化的视觉 AI 引擎，专为设计和执行复杂的 Stable Diffusion 图像生成流程而打造。它摒弃了传统的代码编写模式，采用直观的节点式流程图界面，让用户通过连接不同的功能模块即可构建个性化的生成管线。\n\n这一设计巧妙解决了高级 AI 绘图工作流配置复杂、灵活性不足的痛点。用户无需具备编程背景，也能自由组合模型、调整参数并实时预览效果，轻松实现从基础文生图到多步骤高清修复等各类复杂任务。ComfyUI 拥有极佳的兼容性，不仅支持 Windows、macOS 和 Linux 全平台，还广泛适配 NVIDIA、AMD、Intel 及苹果 Silicon 等多种硬件架构，并率先支持 SDXL、Flux、SD3 等前沿模型。\n\n无论是希望深入探索算法潜力的研究人员和开发者，还是追求极致创作自由度的设计师与资深 AI 绘画爱好者，ComfyUI 都能提供强大的支持。其独特的模块化架构允许社区不断扩展新功能，使其成为当前最灵活、生态最丰富的开源扩散模型工具之一，帮助用户将创意高效转化为现实。",107662,"2026-04-03T11:11:01",[13,14,15],{"id":36,"name":37,"github_repo":38,"description_zh":39,"stars":40,"difficulty_score":23,"last_commit_at":41,"category_tags":42,"status":16},3704,"NextChat","ChatGPTNextWeb\u002FNextChat","NextChat 是一款轻量且极速的 AI 助手，旨在为用户提供流畅、跨平台的大模型交互体验。它完美解决了用户在多设备间切换时难以保持对话连续性，以及面对众多 AI 模型不知如何统一管理的痛点。无论是日常办公、学习辅助还是创意激发，NextChat 都能让用户随时随地通过网页、iOS、Android、Windows、MacOS 或 Linux 端无缝接入智能服务。\n\n这款工具非常适合普通用户、学生、职场人士以及需要私有化部署的企业团队使用。对于开发者而言，它也提供了便捷的自托管方案，支持一键部署到 Vercel 或 Zeabur 等平台。\n\nNextChat 的核心亮点在于其广泛的模型兼容性，原生支持 Claude、DeepSeek、GPT-4 及 Gemini Pro 等主流大模型，让用户在一个界面即可自由切换不同 AI 能力。此外，它还率先支持 MCP（Model Context Protocol）协议，增强了上下文处理能力。针对企业用户，NextChat 提供专业版解决方案，具备品牌定制、细粒度权限控制、内部知识库整合及安全审计等功能，满足公司对数据隐私和个性化管理的高标准要求。",87618,"2026-04-05T07:20:52",[13,26],{"id":44,"name":45,"github_repo":46,"description_zh":47,"stars":48,"difficulty_score":23,"last_commit_at":49,"category_tags":50,"status":16},2268,"ML-For-Beginners","microsoft\u002FML-For-Beginners","ML-For-Beginners 是由微软推出的一套系统化机器学习入门课程，旨在帮助零基础用户轻松掌握经典机器学习知识。这套课程将学习路径规划为 12 周，包含 26 节精炼课程和 52 道配套测验，内容涵盖从基础概念到实际应用的完整流程，有效解决了初学者面对庞大知识体系时无从下手、缺乏结构化指导的痛点。\n\n无论是希望转型的开发者、需要补充算法背景的研究人员，还是对人工智能充满好奇的普通爱好者，都能从中受益。课程不仅提供了清晰的理论讲解，还强调动手实践，让用户在循序渐进中建立扎实的技能基础。其独特的亮点在于强大的多语言支持，通过自动化机制提供了包括简体中文在内的 50 多种语言版本，极大地降低了全球不同背景用户的学习门槛。此外，项目采用开源协作模式，社区活跃且内容持续更新，确保学习者能获取前沿且准确的技术资讯。如果你正寻找一条清晰、友好且专业的机器学习入门之路，ML-For-Beginners 将是理想的起点。",84991,"2026-04-05T10:45:23",[14,51,52,53,15,54,26,13,55],"数据工具","视频","插件","其他","音频",{"id":57,"name":58,"github_repo":59,"description_zh":60,"stars":61,"difficulty_score":10,"last_commit_at":62,"category_tags":63,"status":16},3128,"ragflow","infiniflow\u002Fragflow","RAGFlow 是一款领先的开源检索增强生成（RAG）引擎，旨在为大语言模型构建更精准、可靠的上下文层。它巧妙地将前沿的 RAG 技术与智能体（Agent）能力相结合，不仅支持从各类文档中高效提取知识，还能让模型基于这些知识进行逻辑推理和任务执行。\n\n在大模型应用中，幻觉问题和知识滞后是常见痛点。RAGFlow 通过深度解析复杂文档结构（如表格、图表及混合排版），显著提升了信息检索的准确度，从而有效减少模型“胡编乱造”的现象，确保回答既有据可依又具备时效性。其内置的智能体机制更进一步，使系统不仅能回答问题，还能自主规划步骤解决复杂问题。\n\n这款工具特别适合开发者、企业技术团队以及 AI 研究人员使用。无论是希望快速搭建私有知识库问答系统，还是致力于探索大模型在垂直领域落地的创新者，都能从中受益。RAGFlow 提供了可视化的工作流编排界面和灵活的 API 接口，既降低了非算法背景用户的上手门槛，也满足了专业开发者对系统深度定制的需求。作为基于 Apache 2.0 协议开源的项目，它正成为连接通用大模型与行业专有知识之间的重要桥梁。",77062,"2026-04-04T04:44:48",[15,14,13,26,54],{"id":65,"github_repo":66,"name":67,"description_en":68,"description_zh":69,"ai_summary_zh":70,"readme_en":71,"readme_zh":72,"quickstart_zh":73,"use_case_zh":74,"hero_image_url":75,"owner_login":76,"owner_name":77,"owner_avatar_url":78,"owner_bio":79,"owner_company":80,"owner_location":81,"owner_email":82,"owner_twitter":81,"owner_website":83,"owner_url":84,"languages":85,"stars":90,"forks":91,"last_commit_at":92,"license":93,"difficulty_score":94,"env_os":95,"env_gpu":96,"env_ram":97,"env_deps":98,"category_tags":104,"github_topics":105,"view_count":23,"oss_zip_url":81,"oss_zip_packed_at":81,"status":16,"created_at":111,"updated_at":112,"faqs":113,"releases":144},2378,"haofanwang\u002FControlNet-for-Diffusers","ControlNet-for-Diffusers","Transfer the ControlNet with any basemodel in diffusers🔥","ControlNet-for-Diffusers 是一个专为开发者设计的开源项目，旨在让 ControlNet 能够灵活搭配任意基础模型（Base Model），在 Hugging Face 的 Diffusers 框架中运行。它主要解决了官方 ControlNet 最初仅绑定 Stable Diffusion 1.5 模型的局限性，让用户无需依赖 WebUI，即可将姿态控制、边缘检测等能力迁移到 Anything-v3 等社区热门模型上。\n\n该工具的核心价值在于提供了一套简洁的代码教程和权重转换逻辑。通过特定的算法公式，它能保留原有的控制网络权重，仅替换底层基础模型，从而快速生成适配新模型的 ControlNet 权重文件。虽然项目目前标记为“已弃用”（因后续计划重构），但其核心功能在兼容环境下依然有效，且作者还配套提供了 T2I-Adapter 和 LoRA 在 Diffusers 中的实现方案。\n\nControlNet-for-Diffusers 特别适合熟悉 Python 编程的 AI 开发者、研究人员以及希望深入定制生成式工作流的工程师使用。对于想要摆脱固定模型限制、探索不同","ControlNet-for-Diffusers 是一个专为开发者设计的开源项目，旨在让 ControlNet 能够灵活搭配任意基础模型（Base Model），在 Hugging Face 的 Diffusers 框架中运行。它主要解决了官方 ControlNet 最初仅绑定 Stable Diffusion 1.5 模型的局限性，让用户无需依赖 WebUI，即可将姿态控制、边缘检测等能力迁移到 Anything-v3 等社区热门模型上。\n\n该工具的核心价值在于提供了一套简洁的代码教程和权重转换逻辑。通过特定的算法公式，它能保留原有的控制网络权重，仅替换底层基础模型，从而快速生成适配新模型的 ControlNet 权重文件。虽然项目目前标记为“已弃用”（因后续计划重构），但其核心功能在兼容环境下依然有效，且作者还配套提供了 T2I-Adapter 和 LoRA 在 Diffusers 中的实现方案。\n\nControlNet-for-Diffusers 特别适合熟悉 Python 编程的 AI 开发者、研究人员以及希望深入定制生成式工作流的工程师使用。对于想要摆脱固定模型限制、探索不同风格与 ControlNet 组合效果的技术人员来说，这是一个极具参考价值的实践指南。需要注意的是，由于涉及底层权重操作，普通用户或非技术背景的设计师可能需要一定的学习成本或借助 Colab 演示环境来体验。","# ControlNet-for-Any-Basemodel [![Open In Colab](https:\u002F\u002Fcolab.research.google.com\u002Fassets\u002Fcolab-badge.svg)](https:\u002F\u002Fcolab.research.google.com\u002Fdrive\u002F1BI0TobTdjTI1VBSTjLXKOfh6Ps7uj6Ye?usp=sharing)\n\n### This project is deprecated, it should still work, but may not be compatible with the latest packages. I will rebuild this tool soon, but if you have any urgent problem, please contact me via haofanwang.ai@gmail.com directly.\n\nThis repository provides the simplest tutorial code for developers using ControlNet with basemodel in the diffuser framework instead of WebUI. Our work builds highly on other excellent works. Although theses works have made some attemptes, there is no tutorial for supporting diverse ControlNet in diffusers.\n\n\u003Ccenter>\u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fhaofanwang_ControlNet-for-Diffusers_readme_77ab84967053.png\" width=\"100%\" height=\"100%\">\u003C\u002Fcenter> \n\nWe have also supported [T2I-Adapter-for-Diffusers](https:\u002F\u002Fgithub.com\u002Fhaofanwang\u002FT2I-Adapter-for-Diffusers), [Lora-for-Diffusers](https:\u002F\u002Fgithub.com\u002Fhaofanwang\u002FLora-for-Diffusers). Don't be mean to give us a star if it is helful to you.\n\n# ControlNet + Anything-v3\nOur goal is to replace the basemodel of ControlNet and infer in diffusers framework. The original [ControlNet](https:\u002F\u002Fgithub.com\u002Flllyasviel\u002FControlNet) is trained in pytorch_lightning, and the [released weights](https:\u002F\u002Fhuggingface.co\u002Flllyasviel\u002FControlNet\u002Ftree\u002Fmain\u002Fmodels) with only [stable-diffusion-1.5](https:\u002F\u002Fhuggingface.co\u002Frunwayml\u002Fstable-diffusion-v1-5) as basemodel. However, it is more flexible for users to adopt their own basemodel instead of sd-1.5. Now, let's take [anything-v3](https:\u002F\u002Fhuggingface.co\u002FLinaqruf\u002Fanything-v3.0\u002Ftree\u002Fmain) as an example. We will show you how to achieve this (ControlNet-AnythingV3) step by step. We do provide a Colab demo [![Open In Colab](https:\u002F\u002Fcolab.research.google.com\u002Fassets\u002Fcolab-badge.svg)](https:\u002F\u002Fcolab.research.google.com\u002Fdrive\u002F1BI0TobTdjTI1VBSTjLXKOfh6Ps7uj6Ye?usp=sharing), but it only works for Colab Pro users with larger RAM.\n\n### (1) The first step is to replace basemodel. \n\nFortunately, ControlNet has already provided a [guideline](https:\u002F\u002Fgithub.com\u002Flllyasviel\u002FControlNet\u002Fdiscussions\u002F12) to transfer the ControlNet to any other community model. The logic behind is as below, where we keep the added control weights and only replace the basemodel. Note that this may not work always, as ControlNet may has some trainble weights in basemodel.\n \n ```bash\n NewBaseModel-ControlHint = NewBaseModel + OriginalBaseModel-ControlHint - OriginalBaseModel\n ```\n\nFirst, we clone this repo from ControlNet.\n ```bash\n git clone https:\u002F\u002Fgithub.com\u002Flllyasviel\u002FControlNet.git\n cd ControlNet\n ```\n\nThen, we have to prepared required weights for [OriginalBaseModel](https:\u002F\u002Fhuggingface.co\u002Frunwayml\u002Fstable-diffusion-v1-5\u002Ftree\u002Fmain) (path_sd15), [OriginalBaseModel-ControlHint](https:\u002F\u002Fhuggingface.co\u002Flllyasviel\u002FControlNet\u002Ftree\u002Fmain\u002Fmodels) (path_sd15_with_control), [NewBaseModel](https:\u002F\u002Fhuggingface.co\u002FLinaqruf\u002Fanything-v3.0\u002Ftree\u002Fmain) (path_input). You only need to download following weights, and we use pose as ControlHint and anything-v3 as our new basemodel for instance. We put all weights inside .\u002Fmodels.\n\n ```bash\n path_sd15 = '.\u002Fmodels\u002Fv1-5-pruned.ckpt'\n path_sd15_with_control = '.\u002Fmodels\u002Fcontrol_sd15_openpose.pth'\n path_input = '.\u002Fmodels\u002Fanything-v3-full.safetensors'\n path_output = '.\u002Fmodels\u002Fcontrol_any3_openpose.pth'\n ```\n \n Finally, we can directly run\n ```bash\n python tool_transfer_control.py\n ```\n\nIf successful, you will get the new model. This model can already be used in ControlNet codebase.\n\n```bash\nmodels\u002Fcontrol_any3_openpose.pth\n ```\n\nIf you want to try with other models, you can just define your own path_sd15_with_control and path_input. If the path_input is trained with diffusers, you can use [convert_diffusers_to_original_stable_diffusion.py](https:\u002F\u002Fgithub.com\u002Fhuggingface\u002Fdiffusers\u002Fblob\u002Feadf0e2555cfa19b033e02de53553f71ac33536f\u002Fscripts\u002Fconvert_diffusers_to_original_stable_diffusion.py#L264) to convert it into safetensors first.\n\n### (2) The second step is to convert into diffusers\n\nGratefully, [Takuma Mori](https:\u002F\u002Fgithub.com\u002Ftakuma104) has supported it in this recent [PR](https:\u002F\u002Fgithub.com\u002Fhuggingface\u002Fdiffusers\u002Fpull\u002F2407), so that we can easily achieve this. As it is still under-devlopement, so it may be unstable, thus we have to use a specific commit version. We notice that diffusers has merged the PR in 3\u002F2\u002F2023, we will reformat our tutorial soon.\n\n```bash\ngit clone https:\u002F\u002Fgithub.com\u002Ftakuma104\u002Fdiffusers.git\ncd diffusers\ngit checkout 9a37409663a53f775fa380db332d37d7ea75c915\npip install .\n```\n\nGiven the path of the generated model in step (1), run\n```bash\npython .\u002Fscripts\u002Fconvert_controlnet_to_diffusers.py --checkpoint_path control_any3_openpose.pth  --dump_path control_any3_openpose --device cpu\n```\n\nWe have the saved model in control_any3_openpose. Now we can test it as regularly.\n\n```python\nfrom diffusers import StableDiffusionControlNetPipeline\nfrom diffusers.utils import load_image\n\npose_image = load_image('https:\u002F\u002Fhuggingface.co\u002Ftakuma104\u002Fcontrolnet_dev\u002Fresolve\u002Fmain\u002Fpose.png')\npipe = StableDiffusionControlNetPipeline.from_pretrained(\"control_any3_openpose\").to(\"cuda\")\n\npipe.safety_checker = lambda images, clip_input: (images, False)\n\nimage = pipe(prompt=\"1gril,masterpiece,graden\", controlnet_hint=pose_image).images[0]\nimage.save(\"generated.png\")\n```\n\nThe generated result may not be good enough as the pose is kind of hard. So to make sure everything goes well, we suggest to generate a normal pose via [PoseMaker](https:\u002F\u002Fhuggingface.co\u002Fspaces\u002Fjonigata\u002FPoseMaker) or use our provided pose image in .\u002Fimages\u002Fpose.png.\n\n\u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fhaofanwang_ControlNet-for-Diffusers_readme_3c889df61b47.png\" width=\"25%\" height=\"25%\"> \u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fhaofanwang_ControlNet-for-Diffusers_readme_9ef7f7cbf314.png\" width=\"25%\" height=\"25%\">\n\n\n# ControlNet + Inpainting\n\nThis is to support ControlNet with the ability to only modify a target region instead of full image just like [stable-diffusion-inpainting](https:\u002F\u002Fhuggingface.co\u002Frunwayml\u002Fstable-diffusion-inpainting). For now, we provide the condition (pose, segmentation map) beforehands, but you can use adopt pre-trained detector used in ControlNet.\n\nWe have provided the [required pipeline](https:\u002F\u002Fgithub.com\u002Fhaofanwang\u002FControlNet-for-Diffusers\u002Fblob\u002Fmain\u002Fpipeline_stable_diffusion_controlnet_inpaint.py) for usage. But please note that this file is fragile without complete testing, we will consider support it in diffusers framework formally later. Also, we find that ControlNet (sd1.5 based) is not compatible to [stable-diffusion-2-inpainting](https:\u002F\u002Fhuggingface.co\u002Fstabilityai\u002Fstable-diffusion-2-inpainting), as some layers have different modules and dimension, if you forcibly load the weights and skip those unmatching layers, the result will be bad\n\n```bash\n# assume you already know the absolute path of installed diffusers\ncp pipeline_stable_diffusion_controlnet_inpaint.py  PATH\u002Fpipelines\u002Fstable_diffusion\n```\n\nThen, you need to import this new added pipeline in corresponding files\n```\nPATH\u002Fpipelines\u002F__init__.py\nPATH\u002F__init__.py\n```\n\nNow, we can run\n\n```python\nimport torch\nfrom diffusers.utils import load_image\nfrom diffusers import StableDiffusionInpaintPipeline, StableDiffusionControlNetInpaintPipeline\n\n# we have downloaded models locally, you can also load from huggingface\n# control_sd15_seg is converted from control_sd15_seg.safetensors using instructions above\npipe_control = StableDiffusionControlNetInpaintPipeline.from_pretrained(\".\u002Fdiffusers\u002Fcontrol_sd15_seg\",torch_dtype=torch.float16).to('cuda')\npipe_inpaint = StableDiffusionInpaintPipeline.from_pretrained(\".\u002Fdiffusers\u002Fstable-diffusion-inpainting\",torch_dtype=torch.float16).to('cuda')\n\n# yes, we can directly replace the UNet\npipe_control.unet = pipe_inpaint.unet\npipe_control.unet.in_channels = 4\n\n# we also the same example as stable-diffusion-inpainting\nimage = load_image(\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fhaofanwang_ControlNet-for-Diffusers_readme_ebee4664a2ef.png\")\nmask = load_image(\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fhaofanwang_ControlNet-for-Diffusers_readme_00d96a10bfe2.png\")\n\n# the segmentation result is generated from https:\u002F\u002Fhuggingface.co\u002Fspaces\u002Fhysts\u002FControlNet\ncontrol_image = load_image('tmptvkkr0tg.png')\n\nimage = pipe_control(prompt=\"Face of a yellow cat, high resolution, sitting on a park bench\", \n                     negative_prompt=\"lowres, bad anatomy, worst quality, low quality\",\n                     controlnet_hint=control_image, \n                     image=image,\n                     mask_image=mask,\n                     num_inference_steps=100).images[0]\n\nimage.save(\"inpaint_seg.jpg\")\n```\n\nThe following images are original image, mask image, segmentation (control hint) and generated new image.\n\n\u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fhaofanwang_ControlNet-for-Diffusers_readme_ebee4664a2ef.png\" width=\"20%\" height=\"20%\"> \u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fhaofanwang_ControlNet-for-Diffusers_readme_00d96a10bfe2.png\" width=\"20%\" height=\"20%\"> \u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fhaofanwang_ControlNet-for-Diffusers_readme_9d3fe7171f54.png\" width=\"20%\" height=\"20%\"> \u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fhaofanwang_ControlNet-for-Diffusers_readme_708d31c9daf0.jpg\" width=\"20%\" height=\"20%\">\n\nYou can also use pose as control hint. But please note that it is suggested to use OpenPose format, which is consistent to the training process. If you just want to test a few images without install OpenPose locally, you can directly use [online demo of ControlNet](https:\u002F\u002Fhuggingface.co\u002Fspaces\u002Fhysts\u002FControlNet) to generate pose image given the resized 512x512 input.\n\n```python\nimage = load_image(\".\u002Fimages\u002Fpose_image.jpg\")\nmask = load_image(\".\u002Fimages\u002Fpose_mask.jpg\")\npose_image = load_image('.\u002Fimages\u002Fpose_hint.png')\n\nimage = pipe_control(prompt=\"Face of a young boy smiling\", \n                     negative_prompt=\"lowres, bad anatomy, worst quality, low quality\",\n                     controlnet_hint=pose_image, \n                     image=image,\n                     mask_image=mask,\n                     num_inference_steps=100).images[0]\n\nimage.save(\"inpaint_pos.jpg\")\n```\n\n\u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fhaofanwang_ControlNet-for-Diffusers_readme_3a53427d2d9f.jpg\" width=\"20%\" height=\"20%\"> \u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fhaofanwang_ControlNet-for-Diffusers_readme_3e3f357d58ec.jpg\" width=\"20%\" height=\"20%\"> \u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fhaofanwang_ControlNet-for-Diffusers_readme_9b32dfe346c1.png\" width=\"20%\" height=\"20%\"> \u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fhaofanwang_ControlNet-for-Diffusers_readme_ed71137f20f4.jpg\" width=\"20%\" height=\"20%\">\n\n# ControlNet + Inpainting + Img2Img\nWe have uploaded [pipeline_stable_diffusion_controlnet_inpaint_img2img.py](https:\u002F\u002Fgithub.com\u002Fhaofanwang\u002FControlNet-for-Diffusers\u002Fblob\u002Fmain\u002Fpipeline_stable_diffusion_controlnet_inpaint_img2img.py) to support img2img. You can follow the same instruction as [this section](https:\u002F\u002Fgithub.com\u002Fhaofanwang\u002FControlNet-for-Diffusers#controlnet--inpainting).\n\n# Multi-ControlNet (experimental)\nSimilar to [T2I-Adapter](https:\u002F\u002Fgithub.com\u002FTencentARC\u002FT2I-Adapter), ControlNet also supports multiple control images as input. The idea behind is simple, as the base model is frozen, we can combine the outputs from ControlNet1 and ControlNet2, and use it as input to UNet. Here, we provide pseudocode for reference. You need to modify the pipeline as below.\n\n```python\ncontrol1 = controlnet1(latent_model_input, t, encoder_hidden_states=prompt_embeds, controlnet_hint=controlnet_hint1)\ncontrol2 = controlnet2(latent_model_input, t, encoder_hidden_states=prompt_embeds, controlnet_hint=controlnet_hint2)\n\n# please note that the weights should be adjusted accordingly\ncontrol1_weight = 1.00 # control_any3_openpose\ncontrol2_weight = 0.50 # control_sd15_depth\n\nmerged_control = []\nfor i in range(len(control1)):\n    merged_control.append(control1_weight*control[i]+control2_weight*control_1[i])\ncontrol = merged_control\n\nnoise_pred = unet(latent_model_input, t, encoder_hidden_states=prompt_embeds, cross_attention_kwargs=cross_attention_kwargs, control=control).sample\n```\n\nHere is an example of Multi-ControlNet, where we use pose and depth map are control hints. The test images are both credited to [T2I-Adapter](https:\u002F\u002Fgithub.com\u002FTencentARC\u002FT2I-Adapter).\n\n\u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fhaofanwang_ControlNet-for-Diffusers_readme_b6233080eb49.png\" width=\"20%\" height=\"20%\"> \u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fhaofanwang_ControlNet-for-Diffusers_readme_e7930bc214fd.png\" width=\"20%\" height=\"20%\"> \u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fhaofanwang_ControlNet-for-Diffusers_readme_167bccd9e8ca.jpeg\" width=\"20%\" height=\"20%\">\n\n# Train your own ControlNet\nIn order to avoid this repo is too bloated, we provide tutorial about training in [Train-ControlNet-in-Diffusers](https:\u002F\u002Fgithub.com\u002Fhaofanwang\u002FTrain-ControlNet-in-Diffusers).\n\n# Acknowledgement\nWe first thanks the author of [ControlNet](https:\u002F\u002Fgithub.com\u002Flllyasviel\u002FControlNet) for such a great work, our converting code is borrowed from [here](https:\u002F\u002Fgithub.com\u002Flllyasviel\u002FControlNet\u002Fdiscussions\u002F12). We are also appreciated the contributions from this [pull request](https:\u002F\u002Fgithub.com\u002Fhuggingface\u002Fdiffusers\u002Fpull\u002F2407) in diffusers, so that we can load ControlNet into diffusers.\n\n# Contact\nThe repo is still under active development, if you have any issue when using it, feel free to open an issue.\n","# ControlNet适用于任何基础模型 [![在Colab中打开](https:\u002F\u002Fcolab.research.google.com\u002Fassets\u002Fcolab-badge.svg)](https:\u002F\u002Fcolab.research.google.com\u002Fdrive\u002F1BI0TobTdjTI1VBSTjLXKOfh6Ps7uj6Ye?usp=sharing)\n\n### 本项目已弃用，虽然仍可运行，但可能与最新版本的软件包不兼容。我将很快重构此工具，如有紧急问题，请直接通过 haofanwang.ai@gmail.com 联系我。\n\n本仓库为开发者提供了在 diffusers 框架中使用 ControlNet 和基础模型的最简教程，而非通过 WebUI。我们的工作高度依赖于其他优秀的工作成果。尽管这些工作已经做出了一些尝试，但目前仍缺乏支持 diffusers 中多种 ControlNet 的教程。\n\n\u003Ccenter>\u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fhaofanwang_ControlNet-for-Diffusers_readme_77ab84967053.png\" width=\"100%\" height=\"100%\">\u003C\u002Fcenter> \n\n我们还支持了 [T2I-Adapter-for-Diffusers](https:\u002F\u002Fgithub.com\u002Fhaofanwang\u002FT2I-Adapter-for-Diffusers)、[Lora-for-Diffusers](https:\u002F\u002Fgithub.com\u002Fhaofanwang\u002FLora-for-Diffusers)。如果对您有所帮助，请不要吝啬给我们点个赞哦。\n\n# ControlNet + Anything-v3\n我们的目标是替换 ControlNet 的基础模型，并在 diffusers 框架中进行推理。原始的 [ControlNet](https:\u002F\u002Fgithub.com\u002Flllyasviel\u002FControlNet) 是在 pytorch_lightning 中训练的，而其发布的权重仅以 [stable-diffusion-1.5](https:\u002F\u002Fhuggingface.co\u002Frunwayml\u002Fstable-diffusion-v1-5) 作为基础模型。然而，用户采用自己的基础模型而非 sd-1.5 会更加灵活。现在，我们以 [anything-v3](https:\u002F\u002Fhuggingface.co\u002FLinaqruf\u002Fanything-v3.0\u002Ftree\u002Fmain) 为例，逐步展示如何实现这一目标（ControlNet-AnythingV3）。我们提供了一个 Colab 演示 [![在Colab中打开](https:\u002F\u002Fcolab.research.google.com\u002Fassets\u002Fcolab-badge.svg)](https:\u002F\u002Fcolab.research.google.com\u002Fdrive\u002F1BI0TobTdjTI1VBSTjLXKOfh6Ps7uj6Ye?usp=sharing)，但它仅适用于拥有更大内存的 Colab Pro 用户。\n\n### (1) 第一步是替换基础模型。\n\n幸运的是，ControlNet 已经提供了一份 [指南](https:\u002F\u002Fgithub.com\u002Flllyasviel\u002FControlNet\u002Fdiscussions\u002F12)，用于将 ControlNet 转移到任何其他社区模型。其逻辑如下：我们保留添加的控制权重，只替换基础模型。请注意，这并不总是可行的，因为 ControlNet 可能在基础模型中包含一些可训练的权重。\n \n ```bash\n 新基础模型-控制提示 = 新基础模型 + 原始基础模型-控制提示 - 原始基础模型\n ```\n\n首先，我们从 ControlNet 克隆这个仓库。\n ```bash\n git clone https:\u002F\u002Fgithub.com\u002Flllyasviel\u002FControlNet.git\n cd ControlNet\n ```\n\n然后，我们需要准备好所需的权重，包括 [原始基础模型](https:\u002F\u002Fhuggingface.co\u002Frunwayml\u002Fstable-diffusion-v1-5\u002Ftree\u002Fmain)（path_sd15）、[原始基础模型-控制提示](https:\u002F\u002Fhuggingface.co\u002Flllyasviel\u002FControlNet\u002Ftree\u002Fmain\u002Fmodels)（path_sd15_with_control）、[新基础模型](https:\u002F\u002Fhuggingface.co\u002FLinaqruf\u002Fanything-v3.0\u002Ftree\u002Fmain)（path_input）。你只需下载以下权重，我们以 pose 作为控制提示，以 anything-v3 作为新的基础模型为例。我们将所有权重放在 .\u002Fmodels 目录下。\n\n ```bash\n path_sd15 = '.\u002Fmodels\u002Fv1-5-pruned.ckpt'\n path_sd15_with_control = '.\u002Fmodels\u002Fcontrol_sd15_openpose.pth'\n path_input = '.\u002Fmodels\u002Fanything-v3-full.safetensors'\n path_output = '.\u002Fmodels\u002Fcontrol_any3_openpose.pth'\n ```\n \n 最后，我们可以直接运行\n ```bash\n python tool_transfer_control.py\n ```\n\n如果成功，你将得到新的模型。该模型已经可以在 ControlNet 的代码库中使用。\n\n```bash\nmodels\u002Fcontrol_any3_openpose.pth\n ```\n\n如果你想尝试其他模型，只需定义你自己的 path_sd15_with_control 和 path_input。如果 path_input 是用 diffusers 训练的，你可以先使用 [convert_diffusers_to_original_stable_diffusion.py](https:\u002F\u002Fgithub.com\u002Fhuggingface\u002Fdiffusers\u002Fblob\u002Feadf0e2555cfa19b033e02de53553f71ac33536f\u002Fscripts\u002Fconvert_diffusers_to_original_stable_diffusion.py#L264) 将其转换为 safetensors 格式。\n\n### (2) 第二步是转换为 diffusers\n\n值得庆幸的是，[Takuma Mori](https:\u002F\u002Fgithub.com\u002Ftakuma104) 在最近的 [PR](https:\u002F\u002Fgithub.com\u002Fhuggingface\u002Fdiffusers\u002Fpull\u002F2407) 中对此进行了支持，因此我们可以轻松实现这一目标。由于该功能仍在开发中，可能会不稳定，因此我们必须使用特定的提交版本。我们注意到 diffusers 已于 2023 年 3 月 2 日合并了该 PR，我们将很快重新整理我们的教程。\n\n```bash\ngit clone https:\u002F\u002Fgithub.com\u002Ftakuma104\u002Fdiffusers.git\ncd diffusers\ngit checkout 9a37409663a53f775fa380db332d37d7ea75c915\npip install .\n```\n\n给定步骤 (1) 中生成的模型路径，运行\n```bash\npython .\u002Fscripts\u002Fconvert_controlnet_to_diffusers.py --checkpoint_path control_any3_openpose.pth  --dump_path control_any3_openpose --device cpu\n```\n\n我们得到了保存的模型 control_any3_openpose。现在我们可以像往常一样对其进行测试。\n\n```python\nfrom diffusers import StableDiffusionControlNetPipeline\nfrom diffusers.utils import load_image\n\npose_image = load_image('https:\u002F\u002Fhuggingface.co\u002Ftakuma104\u002Fcontrolnet_dev\u002Fresolve\u002Fmain\u002Fpose.png')\npipe = StableDiffusionControlNetPipeline.from_pretrained(\"control_any3_openpose\").to(\"cuda\")\n\npipe.safety_checker = lambda images, clip_input: (images, False)\n\nimage = pipe(prompt=\"1gril,masterpiece,graden\", controlnet_hint=pose_image).images[0]\nimage.save(\"generated.png\")\n```\n\n生成的结果可能不够理想，因为 pose 图像较为复杂。为了确保一切顺利，建议通过 [PoseMaker](https:\u002F\u002Fhuggingface.co\u002Fspaces\u002Fjonigata\u002FPoseMaker) 生成一个标准的 pose，或者使用我们在 .\u002Fimages\u002Fpose.png 中提供的 pose 图像。\n\n\u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fhaofanwang_ControlNet-for-Diffusers_readme_3c889df61b47.png\" width=\"25%\" height=\"25%\"> \u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fhaofanwang_ControlNet-for-Diffusers_readme_9ef7f7cbf314.png\" width=\"25%\" height=\"25%\">\n\n\n# ControlNet + 图像修复\n\n这是为了支持 ControlNet 具备仅修改目标区域的能力，而不是像 [stable-diffusion-inpainting](https:\u002F\u002Fhuggingface.co\u002Frunwayml\u002Fstable-diffusion-inpainting) 那样修改整张图像。目前，我们提前提供了条件（pose、分割图），但你也可以使用 ControlNet 中预训练的检测器。\n\n我们提供了用于使用的 [所需管道](https:\u002F\u002Fgithub.com\u002Fhaofanwang\u002FControlNet-for-Diffusers\u002Fblob\u002Fmain\u002Fpipeline_stable_diffusion_controlnet_inpaint.py)。但请注意，该文件尚未经过全面测试，比较脆弱，我们将在后续正式考虑将其集成到 diffusers 框架中。此外，我们发现 ControlNet（基于 sd1.5）与 [stable-diffusion-2-inpainting](https:\u002F\u002Fhuggingface.co\u002Fstabilityai\u002Fstable-diffusion-2-inpainting) 不兼容，因为某些层的模块和维度不同。如果你强行加载权重并跳过那些不匹配的层，结果将会很糟糕。\n\n```bash\n\n# 假设你已经知道已安装 diffusers 的绝对路径\ncp pipeline_stable_diffusion_controlnet_inpaint.py  PATH\u002Fpipelines\u002Fstable_diffusion\n```\n\n然后，你需要在相应的文件中导入这个新添加的管道：\n```\nPATH\u002Fpipelines\u002F__init__.py\nPATH\u002F__init__.py\n```\n\n现在，我们可以运行以下代码：\n\n```python\nimport torch\nfrom diffusers.utils import load_image\nfrom diffusers import StableDiffusionInpaintPipeline, StableDiffusionControlNetInpaintPipeline\n\n# 我们已经将模型下载到本地，你也可以从 Hugging Face 加载\n# control_sd15_seg 是根据上述说明，由 control_sd15_seg.safetensors 转换而来的\npipe_control = StableDiffusionControlNetInpaintPipeline.from_pretrained(\".\u002Fdiffusers\u002Fcontrol_sd15_seg\",torch_dtype=torch.float16).to('cuda')\npipe_inpaint = StableDiffusionInpaintPipeline.from_pretrained(\".\u002Fdiffusers\u002Fstable-diffusion-inpainting\",torch_dtype=torch.float16).to('cuda')\n\n# 是的，我们可以直接替换 UNet\npipe_control.unet = pipe_inpaint.unet\npipe_control.unet.in_channels = 4\n\n# 我们也使用与 stable-diffusion-inpainting 相同的示例\nimage = load_image(\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fhaofanwang_ControlNet-for-Diffusers_readme_ebee4664a2ef.png\")\nmask = load_image(\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fhaofanwang_ControlNet-for-Diffusers_readme_00d96a10bfe2.png\")\n\n# 分割结果来自 https:\u002F\u002Fhuggingface.co\u002Fspaces\u002Fhysts\u002FControlNet\ncontrol_image = load_image('tmptvkkr0tg.png')\n\nimage = pipe_control(prompt=\"一只黄色猫的脸，高分辨率，坐在公园长椅上\", \n                     negative_prompt=\"低分辨率，解剖结构错误，最差质量，低质量\",\n                     controlnet_hint=control_image, \n                     image=image,\n                     mask_image=mask,\n                     num_inference_steps=100).images[0]\n\nimage.save(\"inpaint_seg.jpg\")\n```\n\n以下图片分别是原始图像、掩码图像、分割图（控制提示）以及生成的新图像。\n\n\u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fhaofanwang_ControlNet-for-Diffusers_readme_ebee4664a2ef.png\" width=\"20%\" height=\"20%\"> \u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fhaofanwang_ControlNet-for-Diffusers_readme_00d96a10bfe2.png\" width=\"20%\" height=\"20%\"> \u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fhaofanwang_ControlNet-for-Diffusers_readme_9d3fe7171f54.png\" width=\"20%\" height=\"20%\"> \u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fhaofanwang_ControlNet-for-Diffusers_readme_708d31c9daf0.jpg\" width=\"20%\" height=\"20%\">\n\n你也可以使用姿态作为控制提示。但请注意，建议使用 OpenPose 格式，这与训练过程一致。如果你只是想测试几张图片而不想在本地安装 OpenPose，可以直接使用 [ControlNet 的在线演示](https:\u002F\u002Fhuggingface.co\u002Fspaces\u002Fhysts\u002FControlNet)，输入调整为 512x512 后生成姿态图。\n\n```python\nimage = load_image(\".\u002Fimages\u002Fpose_image.jpg\")\nmask = load_image(\".\u002Fimages\u002Fpose_mask.jpg\")\npose_image = load_image('.\u002Fimages\u002Fpose_hint.png')\n\nimage = pipe_control(prompt=\"一个微笑的年轻男孩的脸\", \n                     negative_prompt=\"低分辨率，解剖结构错误，最差质量，低质量\",\n                     controlnet_hint=pose_image, \n                     image=image,\n                     mask_image=mask,\n                     num_inference_steps=100).images[0]\n\nimage.save(\"inpaint_pos.jpg\")\n```\n\n\u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fhaofanwang_ControlNet-for-Diffusers_readme_3a53427d2d9f.jpg\" width=\"20%\" height=\"20%\"> \u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fhaofanwang_ControlNet-for-Diffusers_readme_3e3f357d58ec.jpg\" width=\"20%\" height=\"20%\"> \u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fhaofanwang_ControlNet-for-Diffusers_readme_9b32dfe346c1.png\" width=\"20%\" height=\"20%\"> \u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fhaofanwang_ControlNet-for-Diffusers_readme_ed71137f20f4.jpg\" width=\"20%\" height=\"20%\">\n\n# ControlNet + 图像修复 + 图像到图像转换\n我们已经上传了 [pipeline_stable_diffusion_controlnet_inpaint_img2img.py](https:\u002F\u002Fgithub.com\u002Fhaofanwang\u002FControlNet-for-Diffusers\u002Fblob\u002Fmain\u002Fpipeline_stable_diffusion_controlnet_inpaint_img2img.py)，以支持图像到图像转换。你可以按照与 [这一节](https:\u002F\u002Fgithub.com\u002Fhaofanwang\u002FControlNet-for-Diffusers#controlnet--inpainting) 相同的步骤操作。\n\n# 多重 ControlNet（实验性）\n类似于 [T2I-Adapter](https:\u002F\u002Fgithub.com\u002FTencentARC\u002FT2I-Adapter)，ControlNet 也支持多个控制图像作为输入。其原理很简单：由于基础模型是固定的，我们可以将 ControlNet1 和 ControlNet2 的输出结合起来，作为 UNet 的输入。这里我们提供伪代码供参考。你需要按如下方式修改管道：\n\n```python\ncontrol1 = controlnet1(latent_model_input, t, encoder_hidden_states=prompt_embeds, controlnet_hint=controlnet_hint1)\ncontrol2 = controlnet2(latent_model_input, t, encoder_hidden_states=prompt_embeds, controlnet_hint=controlnet_hint2)\n\n# 请注意，权重需要相应调整\ncontrol1_weight = 1.00 # control_any3_openpose\ncontrol2_weight = 0.50 # control_sd15_depth\n\nmerged_control = []\nfor i in range(len(control1)):\n    merged_control.append(control1_weight*control[i]+control2_weight*control_1[i])\ncontrol = merged_control\n\nnoise_pred = unet(latent_model_input, t, encoder_hidden_states=prompt_embeds, cross_attention_kwargs=cross_attention_kwargs, control=control).sample\n```\n\n以下是一个多重 ControlNet 的示例，我们使用姿态和深度图作为控制提示。测试图片均归功于 [T2I-Adapter](https:\u002F\u002Fgithub.com\u002FTencentARC\u002FT2I-Adapter)。\n\n\u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fhaofanwang_ControlNet-for-Diffusers_readme_b6233080eb49.png\" width=\"20%\" height=\"20%\"> \u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fhaofanwang_ControlNet-for-Diffusers_readme_e7930bc214fd.png\" width=\"20%\" height=\"20%\"> \u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fhaofanwang_ControlNet-for-Diffusers_readme_167bccd9e8ca.jpeg\" width=\"20%\" height=\"20%\">\n\n# 训练你自己的 ControlNet\n为了避免本仓库过于臃肿，我们在 [Train-ControlNet-in-Diffusers](https:\u002F\u002Fgithub.com\u002Fhaofanwang\u002FTrain-ControlNet-in-Diffusers) 中提供了训练教程。\n\n# 致谢\n首先感谢 [ControlNet](https:\u002F\u002Fgithub.com\u002Flllyasviel\u002FControlNet) 的作者所做出的杰出工作，我们的转换代码借鉴自 [此处](https:\u002F\u002Fgithub.com\u002Flllyasviel\u002FControlNet\u002Fdiscussions\u002F12)。我们也非常感谢 diffusers 中的 [此 Pull Request](https:\u002F\u002Fgithub.com\u002Fhuggingface\u002Fdiffusers\u002Fpull\u002F2407) 的贡献，正是它使得我们能够将 ControlNet 加载到 diffusers 中。\n\n# 联系方式\n本仓库仍在积极开发中，如果你在使用过程中遇到任何问题，请随时提交 issue。","# ControlNet-for-Diffusers 快速上手指南\n\n> **⚠️ 重要提示**：本项目已标记为废弃（Deprecated）。虽然目前仍可使用，但可能不兼容最新的依赖包。作者计划近期重构该工具。如遇紧急问题，建议直接联系作者或关注官方 Diffusers 库的最新集成方案。\n\n本指南旨在帮助开发者在 `diffusers` 框架下（而非 WebUI），将 ControlNet 应用于任意基座模型（如 Anything-v3）或实现局部重绘（Inpainting）功能。\n\n## 1. 环境准备\n\n### 系统要求\n- **操作系统**: Linux \u002F macOS \u002F Windows (WSL 推荐)\n- **GPU**: 支持 CUDA 的 NVIDIA 显卡（显存建议 8GB 以上，Colab Pro 用户体验更佳）\n- **Python**: 3.8 - 3.10\n\n### 前置依赖\n由于项目依赖特定版本的 `diffusers` 以支持 ControlNet 转换，需使用指定 commit 版本安装。\n\n```bash\n# 克隆特定版本的 diffusers 仓库\ngit clone https:\u002F\u002Fgithub.com\u002Ftakuma104\u002Fdiffusers.git\ncd diffusers\n\n# 切换到支持 ControlNet 转换的特定提交版本\ngit checkout 9a37409663a53f775fa380db332d37d7ea75c915\n\n# 安装依赖\npip install .\n```\n\n*注：国内用户若克隆速度慢，可尝试使用 Gitee 镜像或配置 git proxy。*\n\n## 2. 安装与模型转换步骤\n\n本工具的核心逻辑是将 PyTorch Lightning 训练的 ControlNet 权重转换为 `diffusers` 格式，并替换基座模型。\n\n### 第一步：准备权重文件\n你需要下载以下三类权重并放入 `.\u002Fmodels` 目录：\n1. **原始基座模型 (OriginalBaseModel)**: 例如 `stable-diffusion-v1-5`。\n2. **原始 ControlNet 权重 (OriginalBaseModel-ControlHint)**: 例如 `control_sd15_openpose.pth`。\n3. **新基座模型 (NewBaseModel)**: 例如 `anything-v3-full.safetensors`。\n\n如果新基座模型是 `diffusers` 格式，需先使用 HuggingFace 脚本将其转换为 `.ckpt` 或 `.safetensors` 格式。\n\n### 第二步：替换基座模型\n利用官方提供的脚本，将 ControlNet 的控制权重迁移到新基座模型上。\n\n```bash\n# 克隆 ControlNet 主仓库\ngit clone https:\u002F\u002Fgithub.com\u002Flllyasviel\u002FControlNet.git\ncd ControlNet\n\n# 运行转换脚本 (需确保 models 目录下文件路径正确)\npython tool_transfer_control.py\n```\n*成功后将在 `.\u002Fmodels` 生成新模型，例如 `control_any3_openpose.pth`。*\n\n### 第三步：转换为 Diffusers 格式\n将上一步生成的 `.pth` 模型转换为 `diffusers` 可用的文件夹结构。\n\n```bash\n# 回到之前安装的 diffusers 目录\ncd path\u002Fto\u002Fdiffusers\n\n# 执行转换命令\npython .\u002Fscripts\u002Fconvert_controlnet_to_diffusers.py --checkpoint_path ..\u002FControlNet\u002Fmodels\u002Fcontrol_any3_openpose.pth --dump_path .\u002Fcontrol_any3_openpose --device cpu\n```\n\n## 3. 基本使用示例\n\n### 场景一：基础生成 (ControlNet + Anything-v3)\n加载转换后的模型进行姿态控制生成。\n\n```python\nfrom diffusers import StableDiffusionControlNetPipeline\nfrom diffusers.utils import load_image\nimport torch\n\n# 加载姿态参考图\npose_image = load_image('https:\u002F\u002Fhuggingface.co\u002Ftakuma104\u002Fcontrolnet_dev\u002Fresolve\u002Fmain\u002Fpose.png')\n\n# 加载管道 (指向转换后的模型文件夹)\npipe = StableDiffusionControlNetPipeline.from_pretrained(\n    \".\u002Fcontrol_any3_openpose\", \n    torch_dtype=torch.float16\n).to(\"cuda\")\n\n# 关闭安全检测 (可选，根据需求调整)\npipe.safety_checker = lambda images, clip_input: (images, False)\n\n# 生成图像\nimage = pipe(\n    prompt=\"1girl, masterpiece, garden\", \n    controlnet_hint=pose_image,\n    num_inference_steps=20\n).images[0]\n\nimage.save(\"generated.png\")\n```\n\n### 场景二：局部重绘 (ControlNet + Inpainting)\n此功能需要手动替换 Pipeline 文件。\n\n1. **复制 Pipeline 文件**：\n   将项目中的 `pipeline_stable_diffusion_controlnet_inpaint.py` 复制到你的 diffusers 安装目录：\n   ```bash\n   cp pipeline_stable_diffusion_controlnet_inpaint.py $(python -c \"import diffusers, os; print(os.path.join(os.path.dirname(diffusers.__file__), 'pipelines', 'stable_diffusion'))\")\n   ```\n   *注意：可能需要手动在 `diffusers\u002Fpipelines\u002F__init__.py` 中注册导入该新类。*\n\n2. **代码调用示例**：\n\n```python\nimport torch\nfrom diffusers.utils import load_image\n# 假设已正确导入自定义 Pipeline\nfrom diffusers import StableDiffusionInpaintPipeline, StableDiffusionControlNetInpaintPipeline\n\n# 加载 ControlNet 管道 (基于 seg 模型示例)\npipe_control = StableDiffusionControlNetInpaintPipeline.from_pretrained(\n    \".\u002Fdiffusers\u002Fcontrol_sd15_seg\",\n    torch_dtype=torch.float16\n).to('cuda')\n\n# 加载标准 Inpainting 管道\npipe_inpaint = StableDiffusionInpaintPipeline.from_pretrained(\n    \".\u002Fdiffusers\u002Fstable-diffusion-inpainting\",\n    torch_dtype=torch.float16\n).to('cuda')\n\n# 关键步骤：替换 UNet 以支持 Inpainting\npipe_control.unet = pipe_inpaint.unet\npipe_control.unet.in_channels = 4\n\n# 加载图片、掩码和控制图 (分割图或姿态图)\nimage = load_image(\"overture-creations-5sI6fQgYIuo.png\")\nmask = load_image(\"overture-creations-5sI6fQgYIuo_mask.png\")\ncontrol_image = load_image('tmptvkkr0tg.png') # 控制提示图\n\n# 执行生成\nresult = pipe_control(\n    prompt=\"Face of a yellow cat, high resolution, sitting on a park bench\", \n    negative_prompt=\"lowres, bad anatomy, worst quality, low quality\",\n    controlnet_hint=control_image, \n    image=image,\n    mask_image=mask,\n    num_inference_steps=100\n).images[0]\n\nresult.save(\"inpaint_seg.jpg\")\n```\n\n### 场景三：多 ControlNet 联合控制 (实验性)\n通过组合多个 ControlNet 的输出作为 UNet 输入，可实现多重条件控制（如同时控制姿态和深度）。需在 Pipeline 源码中修改前向传播逻辑，叠加 `control1` 和 `control2` 的输出结果，并适当调整权重系数。","一位游戏美术开发者希望将基于 Stable Diffusion 1.5 训练的姿势控制能力，迁移到更擅长二次元风格生成的 Anything-V3 模型上，以构建定制化的角色生成管线。\n\n### 没有 ControlNet-for-Diffusers 时\n- **模型绑定僵化**：官方 ControlNet 权重仅锁定在 SD-1.5 基座上，无法直接兼容 Anything-V3 等社区热门模型，导致风格与控制力不可兼得。\n- **手动融合高风险**：开发者需自行编写复杂的权重加减脚本（$NewBase + ControlHint - OriginalBase$），极易因维度不匹配或参数错位导致推理崩溃。\n- **框架适配困难**：缺乏针对 Diffusers 框架的原生支持教程，若要从 PyTorch Lightning 迁移代码，需耗费大量时间重构底层逻辑。\n- **试错成本高昂**：每次尝试新基座模型都需重复上述繁琐过程，严重拖慢原型验证和迭代速度。\n\n### 使用 ControlNet-for-Diffusers 后\n- **无缝基座替换**：只需配置少量路径参数，即可通过内置算法自动将 ControlNet 的控制权重“移植”到 Anything-V3 等新基座上。\n- **原生框架支持**：提供专为 Diffusers 设计的精简代码示例，无需修改底层架构即可直接加载转换后的模型进行推理。\n- **流程标准化**：将复杂的权重运算封装为 `tool_transfer_control.py` 一键脚本，大幅降低人为操作失误率，确保输出模型可用。\n- **灵活扩展生态**：不仅支持姿势控制，还可轻松适配 T2I-Adapter 和 LoRA，让开发者能自由组合各类社区模型构建专属工作流。\n\nControlNet-for-Diffusers 打破了基座模型与控制插件间的壁垒，让开发者能以最低成本实现“任意模型 + 精准控制”的自由组合。","https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fhaofanwang_ControlNet-for-Diffusers_77ab8496.png","haofanwang","Frank (Haofan) Wang","https:\u002F\u002Foss.gittoolsai.com\u002Favatars\u002Fhaofanwang_0d05f6d4.jpg","I love human more than AI.","Carnegie Mellon University",null,"haofanwang.ai@gmail.com","https:\u002F\u002Fhaofanwang.github.io\u002F","https:\u002F\u002Fgithub.com\u002Fhaofanwang",[86],{"name":87,"color":88,"percentage":89},"Python","#3572A5",100,850,53,"2026-03-25T01:38:40","MIT",4,"未说明","需要 NVIDIA GPU (代码示例使用 .to('cuda')，且 Colab 演示仅适用于拥有较大显存的 Colab Pro 用户)","Colab 演示提示需要较大 RAM (具体数值未说明，建议 16GB+)",{"notes":99,"python":95,"dependencies":100},"该项目已弃用，可能不兼容最新版本的包。安装 diffusers 时需克隆特定仓库并检出特定提交版本 (2023-03-02 之前的版本)，因为官方合并的支持代码当时尚不稳定。需要将自定义管道文件 (pipeline_stable_diffusion_controlnet_inpaint.py) 手动复制到已安装的 diffusers 目录中才能使用修复功能。原始 ControlNet 权重基于 Stable Diffusion 1.5，若与其他基座模型 (如 SD 2.0 Inpainting) 混用可能导致层级维度不匹配。",[101,102,103],"diffusers (特定 commit: 9a37409663a53f775fa380db332d37d7ea75c915)","torch","pytorch_lightning (用于原始 ControlNet 权重)",[13],[106,107,108,109,110],"diffusers","controlnet","aigc","pytorch","tutorials","2026-03-27T02:49:30.150509","2026-04-06T05:15:45.407571",[114,119,124,129,134,139],{"id":115,"question_zh":116,"answer_zh":117,"source_url":118},10934,"如何在 Diffusers 中让 ControlNet 支持 SD1.5 的 img2img（图生图）功能？","可以通过替换 UNet 来实现。具体步骤如下：\n1. 加载 ControlNet Inpaint\u002FImg2Img Pipeline 和标准的 Inpaint Pipeline。\n2. 将 Inpaint Pipeline 的 unet 赋值给 ControlNet Pipeline 的 unet。\n3. 修改 in_channels 为 4。\n\n代码示例：\n```python\npipe_control = StableDiffusionControlNetInpaintImg2ImgPipeline.from_pretrained(control_model_path, torch_dtype=torch.float16).to('cuda')\npipe_inpaint = StableDiffusionInpaintPipeline.from_pretrained(inpaint_model_path, torch_dtype=torch.float16).to('cuda')\n\npipe_control.unet = pipe_inpaint.unet\npipe_control.unet.in_channels = 4\n```\n注意：此方法目前仅支持 SD1.5，且需确保 diffusers 版本兼容（如 0.14.0.dev0 或更高）。","https:\u002F\u002Fgithub.com\u002Fhaofanwang\u002FControlNet-for-Diffusers\u002Fissues\u002F15",{"id":120,"question_zh":121,"answer_zh":122,"source_url":123},10935,"如何将 ControlNet 模型从 .pth 格式转换为 Diffusers 格式？遇到 'time_embed' 相关的 KeyError 怎么办？","转换时若遇到 `KeyError: 'time_embed.0.weight'`，通常是因为转换脚本中的部分代码被注释了。请检查 `convert_to_ckpt.py` 或相关转换脚本，确保以下两行代码未被注释：\n```python\nnew_checkpoint[\"time_embedding.linear_1.weight\"] = unet_state_dict[\"time_embed.0.weight\"]\nnew_checkpoint[\"time_embedding.linear_1.bias\"] = unet_state_dict[\"time_embed.0.bias\"]\n```\n如果问题依旧，可能是模型权重结构差异导致。建议重新安装 ControlNet 环境，或确认提取 state_dict 时键名是否正确（例如检查 `time_embed.0.weight` 是否存在于 unet_state_dict 中）。此外，确保使用的是正确的 diffusers 分支或提交版本（如 `git checkout 9a37409663a53f775fa380db332d37d7ea75c915`）。","https:\u002F\u002Fgithub.com\u002Fhaofanwang\u002FControlNet-for-Diffusers\u002Fissues\u002F11",{"id":125,"question_zh":126,"answer_zh":127,"source_url":128},10936,"如何从输入图像生成姿态图（Pose Image）以供 ControlNet 使用？","ControlNet 对姿态格式敏感，因为它是在 OpenPose 样本上训练的，因此推荐使用官方建议的姿态估计器。\n对于开发者，可以加载任何预训练的姿态检测器（如 OpenPose 或 MMPose）。\n如果只是想测试几张图片，可以直接使用 Hugging Face 上的在线 Demo（https:\u002F\u002Fhuggingface.co\u002Fspaces\u002Fhysts\u002FControlNet）生成姿态图，然后下载结果即可，无需自己部署模型。","https:\u002F\u002Fgithub.com\u002Fhaofanwang\u002FControlNet-for-Diffusers\u002Fissues\u002F2",{"id":130,"question_zh":131,"answer_zh":132,"source_url":133},10937,"尝试将基础模型替换为 SD1.5 Inpainting 模型时出现形状不匹配（Shape Mismatch）错误，如何解决？","不能直接使用标准的 Inpainting 模型替换，因为输入维度不同：标准 SD1.5 只有 4 个输入通道，而 Inpainting 模型有 9 个输入通道（包含掩码和潜在图像）。\n若要赋予 ControlNet 修复（Inpainting）能力，请遵循仓库 README 中 \"ControlNet + Inpainting\" 部分的专门教程进行配置，而不是简单地替换路径或加载权重。直接替换会导致运行时维度错误。","https:\u002F\u002Fgithub.com\u002Fhaofanwang\u002FControlNet-for-Diffusers\u002Fissues\u002F29",{"id":135,"question_zh":136,"answer_zh":137,"source_url":138},10938,"StableDiffusionControlNetPipeline 是否支持多图像批量推理？","早期版本可能仅支持单张图像推理。若需支持多图像，可修改 diffusers 包源码中 `StableDiffusionControlNetPipeline` 的预处理部分（约第 600 行）。\n原代码：\n```python\nsample += self.controlnet_input_hint_block(controlnet_hint)\n```\n修改为：\n```python\nhint = self.controlnet_input_hint_block(controlnet_hint)\nif hint.shape[0] > 1:\n    hint = hint.repeat(2, 1, 1, 1) # 根据实际 batch size 调整重复逻辑，或直接相加\nsample += hint\n```\n注意：目前 `StableDiffusionControlNetPipeline` 已在官方 diffusers 库中得到原生支持，建议更新到最新版 diffusers 以获取更好的多图像支持，无需手动修改源码。","https:\u002F\u002Fgithub.com\u002Fhaofanwang\u002FControlNet-for-Diffusers\u002Fissues\u002F26",{"id":140,"question_zh":141,"answer_zh":142,"source_url":143},10939,"在 diffusers 仓库的 controlnet 分支中找不到 `convert_controlnet_to_diffusers.py` 脚本怎么办？","该脚本可能在最新的分支提交中被移除或移动。解决方法是检出包含该脚本的特定历史提交版本。\n请在 diffusers 仓库目录下执行以下命令：\n```bash\ngit checkout 9a37409663a53f775fa380db332d37d7ea75c915\n```\n检出后，你应该能在 `scripts` 目录下找到 `convert_controlnet_to_diffusers.py` 脚本。","https:\u002F\u002Fgithub.com\u002Fhaofanwang\u002FControlNet-for-Diffusers\u002Fissues\u002F25",[]]