Datawhale AI夏令营第四期魔搭-AIGC文生图方向Task3笔记
作者: 福州大学 我是一个温柔的刀客
2024/8/16
小伙伴们在经过前两轮的学习过程中是否开始对各种各样的英文概念迷糊了呢? 接下来我将用简单的语言来说明一下这个项目到底是干啥的:
首先,我们这个项目属于人工智能领域,AIGC(人工智能自动生成)分支方向,使用的模型是Stable Diffusion(稳定扩散深度学习模型),它使用了CV(计算机视觉)中常用的DL(深度学习)算法,同时我们采用Lora(低秩矩阵调整法)方法对模型进行微调,此外,我们在一种GUI(图形用户界面)–ComfyUI平台进行微调操作,就可以不用关注底层的代码原理了!
1. 初识ComfyUI
1.1 什么是ComfyUI
ComfyUI 是GUI的一种,是基于节点工作的用户界面,主要用于操作图像的生成技术,ComfyUI 的特别之处在于它采用了一种模块化的设计,把图像生成的过程分解成了许多小的步骤,每个步骤都是一个节点。这些节点可以连接起来形成一个工作流程,这样用户就可以根据需要定制自己的图像生成过程。
GUI 是 “Graphical User Interface”(图形用户界面)的缩写。简单来说,GUI 就是你在电脑屏幕上看到的那种有图标、按钮和菜单的交互方式。与早期的命令行界面(CLI)不同,GUI让用户可以通过鼠标点击、拖拽等动作来执行命令,而不是需要记住和输入文本命令。GUI的设计涉及计算机科学、人机交互、视觉艺术和心理学等多个领域,旨在创建用户友好的界面,提高用户体验,降低使用门槛。GUI在现代操作系统(如Microsoft Windows、macOS、Linux的桌面环境)和各种软件应用中普遍应用。
这是官方对于ComfyUI的解释, 是不是依然感觉有些晦涩难懂?
没关系! 利用task2中的工具[通义千问](通义tongyi.ai_你的全能AI助手 (aliyun.com)), 我们可以获得更为通俗易懂的解释: 😉
ComfyUI 是一个开源的图形用户界面(GUI)工具,主要用于简化 Stable Diffusion 这一机器学习模型的使用过程。Stable Diffusion 是一种生成式AI模型,能够根据文本描述生成相应的图像,比如你输入“一只在海边画画的猫”,它就能尝试生成这样一幅画。
ComfyUI 的主要作用是让非技术背景的用户也能轻松地使用 Stable Diffusion 模型,而不需要深入理解背后的复杂算法或编写代码。它的界面设计得像一个工作流编辑器,用户可以通过拖拽和连接不同的节点来构建自己的图像生成流程。 |
工作原理大致如下:
节点和连线:ComfyUI 使用节点代表不同的功能,比如文本输入节点、图像生成节点、参数调整节点等。用户通过连线将这些节点连接起来,形成一个处理流程。
配置参数:每个节点都有可配置的参数,用户可以根据需要调整,比如生成图像的尺寸、使用的模型版本、生成图像的数量等。
执行流程:用户构建好流程后,点击运行,ComfyUI 就会按照设定的顺序执行各个节点的功能,最终输出结果。
结果反馈:生成的图像会在界面上显示出来,用户可以查看效果,并根据需要修改流程或参数重新生成。
通过这种方式,ComfyUI 降低了使用 AI 图像生成技术的门槛,使得更多的人能够探索和利用这一前沿技术进行创作或研究。
1.2 ComfyUI核心模块
核心模块由模型加载器、提示词管理器、采样器、解码器构成.
🚀CLIP:CLIP(Contrastive Language-Image Pre-training)(对比语言-图像预训练)是一个由OpenAI开发的深度学习模型,用于理解和生成图像与文本之间的关联。CLIP模型的核心能力在于它能够学会将图像和描述图像的文本配对,即使是在训练过程中未曾见过的数据上,也能做出准确的匹配。
基本工作原理: 多模态数据集(指包含两种或多种不同类型数据的数据集,这些数据类型可以是文本、图像、音频、视频、传感器数据等)–对比学习(优化损失函数)–零样本学习zero-shot learning(对新的图像和文本进行分类或描述)
🚢OpenAI: OpenAI是一家致力于研究、开发并应用友好型人工智能(AI)的非营利组织,后来转变为一个混合性质的营利与非营利结构,旨在平衡财务回报与科研目标。OpenAI成立于2015年12月,由一群科技企业家和投资者共同创立,其中包括埃隆·马斯克(Elon Musk)、萨姆·奥特曼(Sam Altman)、彼得·泰尔(Peter Thiel)等知名人物。
OpenAI的一些著名研究成果包括:
- GPT系列模型:Generative Pre-trained Transformer,是自然语言处理领域的重要进展,能够生成连贯且高质量的文本,以及完成各种NLP任务,如翻译、问答、摘要等。
- DALL·E和DALL·E 2:能够根据文本描述生成图像的AI系统。
- CLIP:一种用于理解和生成图像与文本之间关联的模型。
- MuZero:一种无需规则就能掌握复杂游戏的AI系统,比如围棋、国际象棋和电子游戏。
🚠Stable Diffusion: Stable Diffusion是一个深度学习模型,主要用于生成图像,它基于扩散模型(Diffusion Models)的概念。扩散模型是一种概率模型,其核心思想是在前向过程中逐渐添加噪声使数据模糊,然后在后向过程中学习如何逐步去除噪声并恢复原始数据。Stable Diffusion 特别之处在于它的高效性和可访问性,它能够在普通的GPU上运行,而不需要像某些其他生成模型那样需要大量的计算资源。
Stable Diffusion 模型最初由 Stability AI 开发,它是对潜在扩散模型的一种改进,通过使用潜在变量来加速训练过程和减少计算需求。模型在大规模的数据集上进行了预训练,能够根据文本提示生成各种复杂和逼真的图像。
Stable Diffusion 的工作流程大致如下:
- 文本编码:将文本提示转换为向量表示。
- 噪声注入:开始时,模型接收随机噪声作为输入。
- 去噪过程:模型学习如何逐步减少噪声,并在每个步骤中引入文本向量的影响,从而生成与文本相关的图像。
- 图像生成:最终输出是一个与给定文本描述相匹配的新图像。
由于其开源性质和相对较低的硬件要求,Stable Diffusion 成为了研究人员和爱好者中非常流行的选择,用于艺术创作、图像生成和其他创意项目。
1.3 ComfyUI图片生成流程
2. 20分钟速通安装ComfyUI
在这里,我们依旧选择使用魔搭社区提供的Notebook和免费的GPU算力体验来体验ComfyUI:
2.1 下载脚本代码文件
下载安装ComfyUI的执行文件
和task1中微调完成Lora文件
:
Python
git lfs install
git clone https://www.modelscope.cn/datasets/maochase/kolors_test_comfyui.git
mv kolors_test_comfyui/* ./
rm -rf kolors_test_comfyui/
mkdir -p /mnt/workspace/models/lightning_logs/version_0/checkpoints/
mv epoch=0-step=500.ckpt /mnt/workspace/models/lightning_logs/version_0/checkpoints/
2.2 进入ComfyUI的安装文件
2.3 一键执行安装程序并且进入预览界面
3. 浅尝ComfyUI工作流
3.1 不带LoRA的工作流样例
Step1: 下载工作流脚本
复制上述的代码到 Vscode或者你自己的python的 IDE中粘贴, 另存为桌面上, 命名为:Kolars_example.
3.2 带LoRA的工作流样例
复制上述的代码到 Vscode或者你自己的python的 IDE中粘贴, 另存为桌面上, 命名为 Kolars_with_lora_example.
1. 这里的Lora是我们Task1微调训练出来的文件 2. 地址是:/mnt/workspace/models/lightning_logs/version_0/checkpoints/epoch=0-step=500.ckpt 3. 大家如有有其他的Lora文件,可以在下面截图Lora文件地址区域更换成自己的地址 |
4.一些资源网站
名称 | 链接地址 |
---|---|
在魔搭使用ComfyUI,玩转AIGC! | https://modelscope.cn/headlines/article/429 |
ComfyUI的官方地址 | https://github.com/comfyanonymous/ComfyUI |
ComfyUI官方示范 | https://comfyanonymous.github.io/ComfyUI_examples/ |
别人的基础工作流示范 | https://github.com/cubiq/ComfyUI_Workflowshttps://github.com/wyrde/wyrde-comfyui-workflows |
工作流分享网站 | https://comfyworkflows.com/ |
推荐一个比较好的comfyui的github仓库网站 | https://github.com/ZHO-ZHO-ZHO/ComfyUI-Workflows-ZHO?tab=readme-ov-file |
5.Lora微调
5.1 Lora简介
5.2 Lora微调的优势
5.3 Lora详解
Python
import os
cmd = """
python DiffSynth-Studio/examples/train/kolors/train_kolors_lora.py \ # 选择使用可图的Lora训练脚本DiffSynth-Studio/examples/train/kolors/train_kolors_lora.py
--pretrained_unet_path models/kolors/Kolors/unet/diffusion_pytorch_model.safetensors \ # 选择unet模型
--pretrained_text_encoder_path models/kolors/Kolors/text_encoder \ # 选择text_encoder
--pretrained_fp16_vae_path models/sdxl-vae-fp16-fix/diffusion_pytorch_model.safetensors \ # 选择vae模型
--lora_rank 16 \ # lora_rank 16 表示在权衡模型表达能力和训练效率时,选择了使用 16 作为秩,适合在不显著降低模型性能的前提下,通过 LoRA 减少计算和内存的需求
--lora_alpha 4.0 \ # 设置 LoRA 的 alpha 值,影响调整的强度
--dataset_path data/lora_dataset_processed \ # 指定数据集路径,用于训练模型
--output_path ./models \ # 指定输出路径,用于保存模型
--max_epochs 1 \ # 设置最大训练轮数为 1
--center_crop \ # 启用中心裁剪,这通常用于图像预处理
--use_gradient_checkpointing \ # 启用梯度检查点技术,以节省内存
--precision "16-mixed" # 指定训练时的精度为混合 16 位精度(half precision),这可以加速训练并减少显存使用
""".strip()
os.system(cmd) # 执行可图Lora训练
参数详情表:
5.4 UNet、VAE和文本编码器的协作关系
- UNet:负责根据输入的噪声和文本条件生成图像。在Stable Diffusion模型中,UNet接收由VAE编码器产生的噪声和文本编码器转换的文本向量作为输入,并预测去噪后的噪声,从而生成与文本描述相符的图像.
- VAE:生成模型,用于将输入数据映射到潜在空间,并从中采样以生成新图像。在Stable Diffusion中,VAE编码器首先生成带有噪声的潜在表示,这些表示随后与文本条件一起输入到UNet中.
- 文本编码器:将文本输入转换为模型可以理解的向量表示。在Stable Diffusion模型中,文本编码器使用CLIP模型将文本提示转换为向量,这些向量与VAE生成的噪声一起输入到UNet中,指导图像的生成过程.
6. 如何准备一个高质量的数据集
当我们进行图片生成相关的工作时,选择合适的数据集是非常重要的。如何找到适合自己的数据集呢,这里给大家整理了一些重要的参考维度,希望可以帮助你快速找到适合的数据集:
6.1 明确你的需求和目标
- 关注应用场景:确定你的模型将被应用到什么样的场景中(例如,艺术风格转换、产品图像生成、医疗影像合成等)。
- 关注数据类型:你需要什么样的图片?比如是真实世界的照片还是合成图像?是黑白的还是彩色的?是高分辨率还是低分辨率?
- 关注数据量:考虑你的任务应该需要多少图片来支持训练和验证。
6.2数据集来源整理
以下渠道来源均需要考虑合规性问题,请大家在使用数据集过程中谨慎选择。
来源类型 | 推荐 |
---|---|
公开的数据平台 | 魔搭社区内开放了近3000个数据集,涉及文本、图像、音频、视频和多模态等多种场景,左侧有标签栏帮助快速导览,大家可以看看有没有自己需要的数据集。 其他数据平台推荐: ImageNet:包含数百万张图片,广泛用于分类任务,也可以用于生成任务。 Open Images:由Google维护,包含数千万张带有标签的图片。 Flickr:特别是Flickr30kK和Flickr8K数据集,常用于图像描述任务。 CelebA:专注于人脸图像的数据集。 LSUN (Large-scale Scene Understanding):包含各种场景类别的大规模数据集。 |
使用API或爬虫获取 | 如果需要特定类型的内容,可以利用API从图库网站抓取图片,如Unsplash、Pexels等。使用网络爬虫技术从互联网上抓取图片,但需要注意版权问题。 |
数据合成 | 利用现有的图形引擎(如Unity、Unreal Engine)或特定软件生成合成数据,这在训练某些类型的模型时非常有用。最近Datawhale联合阿里云天池,做了一整套多模态大模型数据合成的学习,欢迎大家一起交流。从零入门多模态大模型数据合成 |
数据增强 | 对于较小的数据集,可以通过旋转、翻转、缩放、颜色变换等方式进行数据增强。 |
购买或定制 | 如果你的应用是特定领域的,比如医学影像、卫星图像等,建议从靠谱的渠道购买一些数据集。 |
魔搭社区数据集:
附录:代码块
[这里是不带LoRA的脚本代码]:
{
"last_node_id": 15,
"last_link_id": 18,
"nodes": [
{
"id": 11,
"type": "VAELoader",
"pos": [
1323,
240
],
"size": {
"0": 315,
"1": 58
},
"flags": {},
"order": 0,
"mode": 0,
"outputs": [
{
"name": "VAE",
"type": "VAE",
"links": [
12
],
"shape": 3
}
],
"properties": {
"Node name for S&R": "VAELoader"
},
"widgets_values": [
"sdxl.vae.safetensors"
]
},
{
"id": 10,
"type": "VAEDecode",
"pos": [
1368,
369
],
"size": {
"0": 210,
"1": 46
},
"flags": {},
"order": 6,
"mode": 0,
"inputs": [
{
"name": "samples",
"type": "LATENT",
"link": 18
},
{
"name": "vae",
"type": "VAE",
"link": 12,
"slot_index": 1
}
],
"outputs": [
{
"name": "IMAGE",
"type": "IMAGE",
"links": [
13
],
"shape": 3,
"slot_index": 0
}
],
"properties": {
"Node name for S&R": "VAEDecode"
}
},
{
"id": 14,
"type": "KolorsSampler",
"pos": [
1011,
371
],
"size": {
"0": 315,
"1": 222
},
"flags": {},
"order": 5,
"mode": 0,
"inputs": [
{
"name": "kolors_model",
"type": "KOLORSMODEL",
"link": 16
},
{
"name": "kolors_embeds",
"type": "KOLORS_EMBEDS",
"link": 17
}
],
"outputs": [
{
"name": "latent",
"type": "LATENT",
"links": [
18
],
"shape": 3,
"slot_index": 0
}
],
"properties": {
"Node name for S&R": "KolorsSampler"
},
"widgets_values": [
1024,
1024,
1000102404233412,
"fixed",
25,
5,
"EulerDiscreteScheduler"
]
},
{
"id": 6,
"type": "DownloadAndLoadKolorsModel",
"pos": [
201,
368
],
"size": {
"0": 315,
"1": 82
},
"flags": {},
"order": 1,
"mode": 0,
"outputs": [
{
"name": "kolors_model",
"type": "KOLORSMODEL",
"links": [
16
],
"shape": 3,
"slot_index": 0
}
],
"properties": {
"Node name for S&R": "DownloadAndLoadKolorsModel"
},
"widgets_values": [
"Kwai-Kolors/Kolors",
"fp16"
]
},
{
"id": 3,
"type": "PreviewImage",
"pos": [
1366,
468
],
"size": [
535.4001724243165,
562.2001106262207
],
"flags": {},
"order": 7,
"mode": 0,
"inputs": [
{
"name": "images",
"type": "IMAGE",
"link": 13
}
],
"properties": {
"Node name for S&R": "PreviewImage"
}
},
{
"id": 12,
"type": "KolorsTextEncode",
"pos": [
519,
529
],
"size": [
457.2893696934723,
225.28656056301645
],
"flags": {},
"order": 4,
"mode": 0,
"inputs": [
{
"name": "chatglm3_model",
"type": "CHATGLM3MODEL",
"link": 14,
"slot_index": 0
}
],
"outputs": [
{
"name": "kolors_embeds",
"type": "KOLORS_EMBEDS",
"links": [
17
],
"shape": 3,
"slot_index": 0
}
],
"properties": {
"Node name for S&R": "KolorsTextEncode"
},
"widgets_values": [
"cinematic photograph of an astronaut riding a horse in space |\nillustration of a cat wearing a top hat and a scarf |\nphotograph of a goldfish in a bowl |\nanime screencap of a red haired girl",
"",
1
]
},
{
"id": 15,
"type": "Note",
"pos": [
200,
636
],
"size": [
273.5273818969726,
149.55464588512064
],
"flags": {},
"order": 2,
"mode": 0,
"properties": {
"text": ""
},
"widgets_values": [
"Text encoding takes the most VRAM, quantization can reduce that a lot.\n\nApproximate values I have observed:\nfp16 - 12 GB\nquant8 - 8-9 GB\nquant4 - 4-5 GB\n\nquant4 reduces the quality quite a bit, 8 seems fine"
],
"color": "#432",
"bgcolor": "#653"
},
{
"id": 13,
"type": "DownloadAndLoadChatGLM3",
"pos": [
206,
522
],
"size": [
274.5334274291992,
58
],
"flags": {},
"order": 3,
"mode": 0,
"outputs": [
{
"name": "chatglm3_model",
"type": "CHATGLM3MODEL",
"links": [
14
],
"shape": 3
}
],
"properties": {
"Node name for S&R": "DownloadAndLoadChatGLM3"
},
"widgets_values": [
"fp16"
]
}
],
"links": [
[
12,
11,
0,
10,
1,
"VAE"
],
[
13,
10,
0,
3,
0,
"IMAGE"
],
[
14,
13,
0,
12,
0,
"CHATGLM3MODEL"
],
[
16,
6,
0,
14,
0,
"KOLORSMODEL"
],
[
17,
12,
0,
14,
1,
"KOLORS_EMBEDS"
],
[
18,
14,
0,
10,
0,
"LATENT"
]
],
"groups": [],
"config": {},
"extra": {
"ds": {
"scale": 1.1,
"offset": {
"0": -114.73954010009766,
"1": -139.79705810546875
}
}
},
"version": 0.4
}
[这里是带LoRA的脚本代码]:
{
"last_node_id": 16,
"last_link_id": 20,
"nodes": [
{
"id": 11,
"type": "VAELoader",
"pos": [
1323,
240
],
"size": {
"0": 315,
"1": 58
},
"flags": {},
"order": 0,
"mode": 0,
"outputs": [
{
"name": "VAE",
"type": "VAE",
"links": [
12
],
"shape": 3
}
],
"properties": {
"Node name for S&R": "VAELoader"
},
"widgets_values": [
"sdxl.vae.safetensors"
]
},
{
"id": 10,
"type": "VAEDecode",
"pos": [
1368,
369
],
"size": {
"0": 210,
"1": 46
},
"flags": {},
"order": 7,
"mode": 0,
"inputs": [
{
"name": "samples",
"type": "LATENT",
"link": 18
},
{
"name": "vae",
"type": "VAE",
"link": 12,
"slot_index": 1
}
],
"outputs": [
{
"name": "IMAGE",
"type": "IMAGE",
"links": [
13
],
"shape": 3,
"slot_index": 0
}
],
"properties": {
"Node name for S&R": "VAEDecode"
}
},
{
"id": 15,
"type": "Note",
"pos": [
200,
636
],
"size": {
"0": 273.5273742675781,
"1": 149.5546417236328
},
"flags": {},
"order": 1,
"mode": 0,
"properties": {
"text": ""
},
"widgets_values": [
"Text encoding takes the most VRAM, quantization can reduce that a lot.\n\nApproximate values I have observed:\nfp16 - 12 GB\nquant8 - 8-9 GB\nquant4 - 4-5 GB\n\nquant4 reduces the quality quite a bit, 8 seems fine"
],
"color": "#432",
"bgcolor": "#653"
},
{
"id": 13,
"type": "DownloadAndLoadChatGLM3",
"pos": [
206,
522
],
"size": {
"0": 274.5334167480469,
"1": 58
},
"flags": {},
"order": 2,
"mode": 0,
"outputs": [
{
"name": "chatglm3_model",
"type": "CHATGLM3MODEL",
"links": [
14
],
"shape": 3
}
],
"properties": {
"Node name for S&R": "DownloadAndLoadChatGLM3"
},
"widgets_values": [
"fp16"
]
},
{
"id": 6,
"type": "DownloadAndLoadKolorsModel",
"pos": [
201,
368
],
"size": {
"0": 315,
"1": 82
},
"flags": {},
"order": 3,
"mode": 0,
"outputs": [
{
"name": "kolors_model",
"type": "KOLORSMODEL",
"links": [
19
],
"shape": 3,
"slot_index": 0
}
],
"properties": {
"Node name for S&R": "DownloadAndLoadKolorsModel"
},
"widgets_values": [
"Kwai-Kolors/Kolors",
"fp16"
]
},
{
"id": 12,
"type": "KolorsTextEncode",
"pos": [
519,
529
],
"size": {
"0": 457.28936767578125,
"1": 225.28656005859375
},
"flags": {},
"order": 4,
"mode": 0,
"inputs": [
{
"name": "chatglm3_model",
"type": "CHATGLM3MODEL",
"link": 14,
"slot_index": 0
}
],
"outputs": [
{
"name": "kolors_embeds",
"type": "KOLORS_EMBEDS",
"links": [
17
],
"shape": 3,
"slot_index": 0
}
],
"properties": {
"Node name for S&R": "KolorsTextEncode"
},
"widgets_values": [
"二次元,长发,少女,白色背景",
"",
1
]
},
{
"id": 3,
"type": "PreviewImage",
"pos": [
1366,
469
],
"size": {
"0": 535.400146484375,
"1": 562.2001342773438
},
"flags": {},
"order": 8,
"mode": 0,
"inputs": [
{
"name": "images",
"type": "IMAGE",
"link": 13
}
],
"properties": {
"Node name for S&R": "PreviewImage"
}
},
{
"id": 16,
"type": "LoadKolorsLoRA",
"pos": [
606,
368
],
"size": {
"0": 317.4000244140625,
"1": 82
},
"flags": {},
"order": 5,
"mode": 0,
"inputs": [
{
"name": "kolors_model",
"type": "KOLORSMODEL",
"link": 19
}
],
"outputs": [
{
"name": "kolors_model",
"type": "KOLORSMODEL",
"links": [
20
],
"shape": 3,
"slot_index": 0
}
],
"properties": {
"Node name for S&R": "LoadKolorsLoRA"
},
"widgets_values": [
"/mnt/workspace/models/lightning_logs/version_0/checkpoints/epoch=0-step=500.ckpt",
2
]
},
{
"id": 14,
"type": "KolorsSampler",
"pos": [
1011,
371
],
"size": {
"0": 315,
"1": 266
},
"flags": {},
"order": 6,
"mode": 0,
"inputs": [
{
"name": "kolors_model",
"type": "KOLORSMODEL",
"link": 20
},
{
"name": "kolors_embeds",
"type": "KOLORS_EMBEDS",
"link": 17
},
{
"name": "latent",
"type": "LATENT",
"link": null
}
],
"outputs": [
{
"name": "latent",
"type": "LATENT",
"links": [
18
],
"shape": 3,
"slot_index": 0
}
],
"properties": {
"Node name for S&R": "KolorsSampler"
},
"widgets_values": [
1024,
1024,
0,
"fixed",
25,
5,
"EulerDiscreteScheduler",
1
]
}
],
"links": [
[
12,
11,
0,
10,
1,
"VAE"
],
[
13,
10,
0,
3,
0,
"IMAGE"
],
[
14,
13,
0,
12,
0,
"CHATGLM3MODEL"
],
[
17,
12,
0,
14,
1,
"KOLORS_EMBEDS"
],
[
18,
14,
0,
10,
0,
"LATENT"
],
[
19,
6,
0,
16,
0,
"KOLORSMODEL"
],
[
20,
16,
0,
14,
0,
"KOLORSMODEL"
]
],
"groups": [],
"config": {},
"extra": {
"ds": {
"scale": 1.2100000000000002,
"offset": {
"0": -183.91309381910426,
"1": -202.11110769225016
}
}
},
"version": 0.4
}
|