AIGC文生图:使用ControlNet 控制 Stable Diffusion

这篇具有很好参考价值的文章主要介绍了AIGC文生图:使用ControlNet 控制 Stable Diffusion。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

1 ControlNet介绍

1.1 ControlNet是什么?

ControlNet是斯坦福大学研究人员开发的Stable Diffusion的扩展,使创作者能够轻松地控制AI图像和视频中的对象。它将根据边缘检测、草图处理或人体姿势等各种条件来控制图像生成。

论坛地址:Adding Conditional Control to Text-to-Image Diffusion Models

ControlNet是一种通过添加额外条件来控制stable diffusion的神经网络结构。它提供了一种增强稳定扩散的方法,在文本到图像生成过程中使用条件输入,如涂鸦、边缘映射、分割映射、pose关键点等。可以让生成的图像将更接近输入图像,这比传统的图像到图像生成方法有了很大的改进。

AIGC文生图:使用ControlNet 控制 Stable Diffusion,AIGC,AIGC,stable diffusion,文生图

ControlNet 模型可以在使用小数据集进行训练。然后整合任何预训练的稳定扩散模型来增强模型,来达到微调的目的。

  • ControNet 的初始版本带有以下预训练权重。‍‍
  • Canny edge — 黑色背景上带有白色边缘的单色图像。
  • Depth/Shallow areas — 灰度图像,黑色代表深区域,白色代表浅区域。
  • Normal map — 法线贴图图像。
  • Semantic segmentation map——ADE20K 的分割图像。
  • HED edge  — 黑色背景上带有白色软边缘的单色图像。
  • Scribbles — 黑色背景上带有白色轮廓的手绘单色涂鸦图像。
  • OpenPose (姿势关键点)— OpenPose 骨骼图像。
  • M-LSD  — 仅由黑色背景上的白色直线组成的单色图像。

1.2 ControlNet原理

ControlNet是一种神经网络结构,通过添加额外的条件来控制扩散模型。将网络结构划分为:

  • 可训练“trainable”
  • 不可训练“locked”

其中可训练的部分针对可控的部分进行学习。而锁定的部分,则保留了stable-diffusion模型的原始数据,因此使用少量数据引导,可以保证能充分学习到前置约束的前提下,同时保留原始扩散模型自身的学习能力

AIGC文生图:使用ControlNet 控制 Stable Diffusion,AIGC,AIGC,stable diffusion,文生图

上图的“Zero Convolution”是带有零初始化权重和偏差的1×1卷积。在进行自己的模型训练开始之前,所有零卷积输出都是零,此时模型仍然是原始的Stable Diffusion Model。而在加入自己的训练数据之后,则会对最终数据产生影响,这里的影响,更多是对最终结果的微调,因此不会导致模型出现重大偏离的情况。整体的模型结构如下:

AIGC文生图:使用ControlNet 控制 Stable Diffusion,AIGC,AIGC,stable diffusion,文生图

从整体的模型结构上可以看出,ControlNet在Stable Diffusion模型的decode层加入了上述“0卷积层”,以实现对最终模型与训练数据的一致性。

2 ControlNet部署与模型下载

2.1 运行环境构建

git clone https://github.com/lllyasviel/ControlNet.git

cd ControlNet

conda env create -f environment.yaml

conda activate control

2.2 模型下载

(1)sd模型与detectors模型下载

模型地址:huggingface

下载完成后,将模型移动到如下目录下:

  • sd模型:models
  • detectors模型:annotator/ckpts

模型地址:ControlNetHED.pth

下载完成后,将模型移动到annotator/ckpts目录下

移动完成后,通过命令查看,显示如下:

 [root@localhost ControlNet]# ll annotator/ckpts/
总用量 1125948
-rw-r--r-- 1 root root 209267595 7月  14 14:19 body_pose_model.pth
-rw-r--r-- 1 root root        13 7月  13 15:27 ckpts.txt
-rw-r--r-- 1 root root  29444406 7月  14 16:52 ControlNetHED.pth
-rw-r--r-- 1 root root 492757791 7月  14 14:20 dpt_hybrid-midas-501f0c75.pt
-rw-r--r-- 1 root root 147341049 7月  14 14:20 hand_pose_model.pth
-rw-r--r-- 1 root root   6341481 7月  14 14:20 mlsd_large_512_fp32.pth
-rw-r--r-- 1 root root   2613835 7月  14 14:20 mlsd_tiny_512_fp32.pth
-rw-r--r-- 1 root root  58871680 7月  14 14:20 network-bsds500.pth
-rw-r--r-- 1 root root 206313115 7月  14 14:21 upernet_global_small.pth
[root@localhost ControlNet]# ll annotator/ckpts/
总用量 1097192
-rw-r--r-- 1 root root 209267595 7月  14 14:19 body_pose_model.pth
-rw-r--r-- 1 root root        13 7月  13 15:27 ckpts.txt
-rw-r--r-- 1 root root 492757791 7月  14 14:20 dpt_hybrid-midas-501f0c75.pt
-rw-r--r-- 1 root root 147341049 7月  14 14:20 hand_pose_model.pth
-rw-r--r-- 1 root root   6341481 7月  14 14:20 mlsd_large_512_fp32.pth
-rw-r--r-- 1 root root   2613835 7月  14 14:20 mlsd_tiny_512_fp32.pth
-rw-r--r-- 1 root root  58871680 7月  14 14:20 network-bsds500.pth
-rw-r--r-- 1 root root 206313115 7月  14 14:21 upernet_global_small.pth

(2) clip-vit模型下载

模型地址:clip-vit-large-patch14

下载完成后,在models目录下创建文件夹clip-vit-large-patch14,将模型移动此文件夹下,通过命令,查看显示如下信息:

[root@localhost ControlNet]# ll models/clip-vit-large-patch14/
总用量 5015648
-rw-r--r-- 1 root root       4519 7月  14 16:18 config.json
-rw-r--r-- 1 root root 1710486359 7月  14 16:21 flax_model.msgpack
-rw-r--r-- 1 root root     524619 7月  14 16:21 merges.txt
-rw-r--r-- 1 root root        316 7月  14 16:21 preprocessor_config.json
-rw-r--r-- 1 root root 1710671599 7月  14 16:23 pytorch_model.bin
-rw-r--r-- 1 root root       7947 7月  14 16:23 README.md
-rw-r--r-- 1 root root        389 7月  14 16:23 special_tokens_map.json
-rw-r--r-- 1 root root 1711114176 7月  14 16:26 tf_model.h5
-rw-r--r-- 1 root root        905 7月  14 16:26 tokenizer_config.json
-rw-r--r-- 1 root root    2224003 7月  14 16:26 tokenizer.json
-rw-r--r-- 1 root root     961143 7月  14 16:26 vocab.json

 更待代码,避免通过网络自动下载(下载慢且经常失败)

vi ldm/modules/encoders/modules.py
 def __init__(self, version="openai/clip-vit-large-patch14", device="cuda", max_length=77,
                 freeze=True, layer="last", layer_idx=None):  # clip-vit-base-patch32
        super().__init__()
        assert layer in self.LAYERS
        self.tokenizer = CLIPTokenizer.from_pretrained('models/clip-vit-large-patch14')
        self.transformer = CLIPTextModel.from_pretrained('models/clip-vit-large-patch14')
        self.device = device
        self.max_length = max_length

3 ControlNet运行与效果展示

3.1 运行canny2image

python gradio_canny2image.py

效果展示:

AIGC文生图:使用ControlNet 控制 Stable Diffusion,AIGC,AIGC,stable diffusion,文生图

3.2 运行hough2image

python gradio_hough2image.py

效果展示:

AIGC文生图:使用ControlNet 控制 Stable Diffusion,AIGC,AIGC,stable diffusion,文生图

3.3 运行hed2image

python gradio_hed2image.py

效果展示:

AIGC文生图:使用ControlNet 控制 Stable Diffusion,AIGC,AIGC,stable diffusion,文生图

3.4 运行scribble2image

python gradio_scribble2image.py

效果展示:

AIGC文生图:使用ControlNet 控制 Stable Diffusion,AIGC,AIGC,stable diffusion,文生图

3.5 运行交互scribble2image

python gradio_scribble2image_interactive.py

效果展示:

AIGC文生图:使用ControlNet 控制 Stable Diffusion,AIGC,AIGC,stable diffusion,文生图

3.6 运行伪造scribble2image

python gradio_fake_scribble2image.py

效果展示:

AIGC文生图:使用ControlNet 控制 Stable Diffusion,AIGC,AIGC,stable diffusion,文生图

3.7 运行pose2image

python gradio_pose2image.py

效果展示:

AIGC文生图:使用ControlNet 控制 Stable Diffusion,AIGC,AIGC,stable diffusion,文生图

3.8 运行seg2image

python gradio_seg2image.py

效果展示:

AIGC文生图:使用ControlNet 控制 Stable Diffusion,AIGC,AIGC,stable diffusion,文生图

3.9 运行depth2image

python gradio_depth2image.py

效果展示:

AIGC文生图:使用ControlNet 控制 Stable Diffusion,AIGC,AIGC,stable diffusion,文生图

3.10 运行normal2image

python gradio_normal2image.py

效果展示:

 AIGC文生图:使用ControlNet 控制 Stable Diffusion,AIGC,AIGC,stable diffusion,文生图

4 问题解决

4.1 “No module 'xformers'. Proceeding without it”问题解决

错误输出:

[root@localhost ControlNet]# python gradio_normal2image.py
logging improved.
No module 'xformers'. Proceeding without it.
ControlLDM: Running in eps-prediction mode
DiffusionWrapper has 859.52 M params.
making attention of type 'vanilla' with 512 in_channels
Working with z of shape (1, 4, 32, 32) = 4096 dimensions.
making attention of type 'vanilla' with 512 in_channels
Loaded model config from [./models/cldm_v15.yaml]
Loaded state_dict from [./models/control_sd15_normal.pth]
Running on local URL:  http://0.0.0.0:7860

To create a public link, set `share=True` in `launch()`.

 解决方法:

pip install xformers==0.0.20

5 总结

ControlNet是一个非常强大的神经网络结构,通过添加额外的条件来控制扩散模型。目前还不支持Multi-ControlNet,开源社区有消息说正在积极开发中。这个新功能提供了可以使用多个控制网络,并将他们的输出一起用于图像生成,允许更好地控制整个图像。文章来源地址https://www.toymoban.com/news/detail-563381.html

到了这里,关于AIGC文生图:使用ControlNet 控制 Stable Diffusion的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • AIGC:文生图模型Stable Diffusion

    Stable Diffusion 是由CompVis、Stability AI和LAION共同开发的一个文本转图像模型,它通过LAION-5B子集大量的 512x512 图文模型进行训练,我们只要简单的输入一段文本,Stable Diffusion 就可以迅速将其转换为图像,同样我们也可以置入图片或视频,配合文本对其进行处理。 Stable Diffusion的

    2024年02月15日
    浏览(31)
  • AIGC 文生图及 stable diffusion webui 练习笔记

    文生图, 图生图 等 AIGC 创作大火, 也来体验一把~ 本文记录了环境搭建过程与使用心得. 如果动手能力弱或只想省心, 有现成的整合包, 见参考[1] 列出来我的环境吧: CPU, AMD Ryzen 7 5800X 8-Core Processor RAM, 32G GPU, NVIDIA GeForce RTX 2070 SUPER (8G) OS, Windows 11 专业版 开发环境, python 3.10 与 gi

    2024年02月09日
    浏览(34)
  • Stable Diffusion基础:ControlNet之人体姿势控制

    在AI绘画中精确控制图片是一件比较困难的事情,不过随着 ControlNet 的诞生,这一问题得到了很大的缓解。 今天我就给大家分享一个使用Stable Diffusion WebUI + OpenPose ControlNet 复制照片人物姿势的方法,效果可以参考上图。 OpenPose 可以控制人体的姿态、面部的表情,有时候还能够

    2024年02月13日
    浏览(28)
  • 本地开启stable diffusion web-ui体验AIGC文生图,图生图

    目录 准备工作 主机电脑配置检查 安装以下软件 Python Git 下载stable-diffusion-webui仓库 根据显卡属性安装CUDA 2.下载stable diffusion的训练模型 启动 问题处理 模型加载问题 这是启动后界面 以下是运行时的系统状态截图 需要16G内存,8G显存(网上说是6G就够,不过跑出来图片像素会

    2024年02月14日
    浏览(43)
  • Stable Diffusion WebUI 控制网络 ControlNet 插件实现精准控图-详细教程

    本文收录于《AI绘画从入门到精通》专栏,专栏总目录:点这里,订阅后可阅读专栏内所有文章。 大家好,我是水滴~~ 本文主要介绍 Stable Diffusion WebUI 一个比较重要的插件 ControlNet(控制网络),主要内容有:ControlNet 的工作原理、如果安装 ControlNet 插件、如何使用 ControlNet

    2024年04月25日
    浏览(29)
  • AIGC与AidLux互联应用——AidLux端AIGC测评(二)PC端&云端Stable Diffusion模型推理应用(文生图,图生图)

    整体运行架构 Stable Diffusion模型搭建首先下载diffusers,然后安装,命令如下: git clone https://github.com/huggingface/diffusers.git pip install diffusers cd diffusers pip install . ubuntu和win系统下都可以 文生图,图生图代码和训练好的模型见百度网盘(训练好的模型很大,十几个g) 修改txt2jpg_in

    2024年02月09日
    浏览(43)
  • (十六) AIGC、AI绘画、SD-WebUI、本地部署、Stable-Diffusion本地模型、最新Controlnet扩展

    1、简介 AI绘画的工具网站很多,大部分收费/限制数量,而且速度很慢,如果部署本地的模型,不仅免费无限制、还可以提高出图效率,随意生成自己想象中的图,部署中发现有各种各样的环境配置问题,无法正确运行。 本博整理了SD-WebUI的整合包(Windows环境下),包括启动器

    2024年02月13日
    浏览(48)
  • AIGC专栏2——Stable Diffusion结构解析-以文本生成图像(文生图,txt2img)为例

    用了很久的Stable Diffusion,但从来没有好好解析过它内部的结构,写个博客记录一下,嘿嘿。 https://github.com/bubbliiiing/stable-diffusion 喜欢的可以点个star噢。 Stable Diffusion是比较新的一个扩散模型,翻译过来是稳定扩散,虽然名字叫稳定扩散,但实际上换个seed生成的结果就完全不

    2024年02月10日
    浏览(37)
  • Stable Diffusion使用controlnet报错

    1、问题描述,使用controlnet时报错如下 error running process: G:\\0111stable-diffusion-webuiextensionssd-webui-controlnetscriptscontrolnet.py Traceback (most recent call last):   File \\\"G:\\0111stable-diffusion-webuimodulesscripts.py\\\", line 418, in process     script.process(p, *script_args)   File \\\"G:\\0111stable-diffusion-webuiextensi

    2024年02月09日
    浏览(43)
  • 【AIGC】Controlnet:基于扩散模型的文生图的可控性

    controlnet可以让stable diffusion的生图变得可控。 文章连接:https://arxiv.org/pdf/2302.05543.pdf  冻结了stable  diffusion的预训练模型并重用它的预训练编码层 神经网络结构与零初始化卷积层连接,从零开始逐渐增加参数,并确保微调过程中不会有噪声影响 Controlnet在小数据集(小于5张万

    2024年01月21日
    浏览(36)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包