手赚网 类似网站怎么建设,辽宁短视频搜索seo哪家实惠,方法seo,自己怎么设计公司标志#xff08;本文将围绕 安装Diffusers库及其依赖、理解Diffusers核心概念#xff1a;Pipeline, Model, Scheduler 、使用预训练模型进行推理#xff08;文生图、图生图等#xff09; 、 自定义模型和调度器 、训练自己的扩散模型#xff08;可选#xff0c;需要大量资源本文将围绕 安装Diffusers库及其依赖、理解Diffusers核心概念Pipeline, Model, Scheduler 、使用预训练模型进行推理文生图、图生图等 、 自定义模型和调度器 、训练自己的扩散模型可选需要大量资源、以及高级应用ControlNet、LoRA等进行展开
官网链接huggingface
镜像链接mirror
一、安装Diffusers库
Diffusers 已在 Python 3.8、PyTorch 1.7.0 和 Flax 上进行了测试。请按照以下适用于您正在使用的 Deep Learning Library 的安装说明进行作
# 使用 conda 安装
# 激活虚拟环境后使用 由社区维护conda
conda install -c conda-forge diffusers
# 从源码安装
# 在从源安装 Diffusers 之前请确保您已安装 PyTorch 和 Accelerate。
# 要安装 Accelerate
pip install accelerate
# 然后从源码安装 Diffusers
pip install githttps://github.com/huggingface/diffusers可编辑安装
git clone https://github.com/huggingface/diffusers.git
cd diffusers克隆更新到最新版本的 Diffusers
cd ~/diffusers/
git pull 此命令将安装最前沿版本而不是最新版本。 该版本有助于及时了解最新发展。 例如如果自上次正式发布以来已修复错误但尚未推出新版本。 但是这意味着版本可能并不总是稳定的。 我们努力保持版本可运行大多数问题通常会在几小时或一天内得到解决。 如果您遇到问题请打开https://github.com/huggingface/diffusers/issues/new/choose
二、模型文件和布局 扩散模型以各种文件类型保存并按不同的布局进行组织。Diffusers 将模型权重作为 safetensors 文件存储在 Diffusers-multifolder 布局中它还支持从 diffusion 生态系统中常用的单文件布局加载文件如 safetensors 和 ckpt 文件。每种布局都有自己的优点和用例本指南将向您展示如何加载不同的文件和布局以及如何转换它们。
1. Safetensors 库
Safetensors 是一种安全快速的文件格式用于安全地存储和加载张量。Safetensors 限制 header 大小以限制某些类型的攻击支持延迟加载对分布式设置很有用并且通常具有更快的加载速度。
# 确保已安装 Safetensors 库。
!pip install safetensors Diffusers 库是 Hugging Face 官方开发的 开源 Python 库专门用于简化扩散模型Diffusion Models的部署与应用。
Diffusers-multifolder 布局可能有几个单独的 safetensors 文件每个管道组件文本编码器、UNet、VAE一个组织在子文件夹中查看 stable-diffusion-v1-5/stable-diffusion-v1-5 存储库作为示例单文件布局所有模型权重都可以保存在一个文件中查看 WarriorMama777/OrangeMixs 存储库作为示例
2.LoRA 文件
LoRA 是一种轻量级适配器训练快速且易于因此在以某种方式或样式生成图像方面特别受欢迎。这些适配器通常存储在 safetensors 文件中并且在 civitai 等模型共享平台上广泛流行。LoRA 使用 load_lora_weights 方法加载到基础模型中。
from diffusers import StableDiffusionXLPipeline
import torch# base model
pipeline StableDiffusionXLPipeline.from_pretrained(Lykon/dreamshaper-xl-1-0, torch_dtypetorch.float16, variantfp16
).to(cuda)# download LoRA weights
!wget https://civitai.com/api/download/models/168776 -O blueprintify.safetensors# load LoRA weights
pipeline.load_lora_weights(., weight_nameblueprintify.safetensors)
prompt bl3uprint, a highly detailed blueprint of the empire state building, explaining how to build all parts, many txt, blueprint grid backdrop
negative_prompt lowres, cropped, worst quality, low quality, normal quality, artifacts, signature, watermark, username, blurry, more than one bridge, bad architectureimage pipeline(promptprompt,negative_promptnegative_prompt,generatortorch.manual_seed(0),
).images[0]
image 3.CKPT
PyTorch 的 torch.save 函数使用 Python 的 pickle 实用程序来序列化和保存模型。这些文件保存为 ckpt 文件并且包含整个模型的权重。
使用 from_single_file 方法直接加载 ckpt 文件。
from diffusers import StableDiffusionPipelinepipeline StableDiffusionPipeline.from_single_file(https://huggingface.co/stable-diffusion-v1-5/stable-diffusion-v1-5/blob/main/v1-5-pruned.ckpt
)
4.存储布局
有两种方式组织模型文件一种是 Diffusers-multifolder 布局另一种是单文件布局。Diffusers-multifolder 布局是默认布局每个组件文件文本编码器、UNet、VAE都存储在单独的子文件夹中。Diffusers 还支持从单文件布局加载模型其中所有组件都捆绑在一起。
Diffusers-multifolder
Diffusers-multifolder 布局是 Diffusers 的默认存储布局。每个组件文本编码器、UNet、VAE的权重都存储在单独的子文件夹中。权重可以存储为 safetensors 或 ckpt 文件。
要从 Diffusers-multifolder 布局加载请使用 from_pretrained 方法。
from diffusers import DiffusionPipelinepipeline DiffusionPipeline.from_pretrained(stabilityai/stable-diffusion-xl-base-1.0,torch_dtypetorch.float16,variantfp16,use_safetensorsTrue,
).to(cuda)
使用 Diffusers-multifolder 布局的好处包括 单独或并行加载每个组件文件的速度更快。 减少了内存使用量因为您只加载了所需的组件。例如SDXL Turbo、SDXL Lightning 和 Hyper-SD 等型号除 UNet 外具有相同的组件。您可以使用 from_pipe 方法重用它们的共享组件而无需消耗任何额外的内存请查看 重用管道指南并且只加载 UNet。这样您就不需要下载冗余组件也无需不必要地使用更多内存。
import torch
from diffusers import StableDiffusionXLPipeline, UNet2DConditionModel, EulerDiscreteScheduler# download one model
sdxl_pipeline StableDiffusionXLPipeline.from_pretrained(stabilityai/stable-diffusion-xl-base-1.0,torch_dtypetorch.float16,variantfp16,use_safetensorsTrue,
).to(cuda)# switch UNet for another model
unet UNet2DConditionModel.from_pretrained(stabilityai/sdxl-turbo,subfolderunet,torch_dtypetorch.float16,variantfp16,use_safetensorsTrue
)
# reuse all the same components in new model except for the UNet
turbo_pipeline StableDiffusionXLPipeline.from_pipe(sdxl_pipeline, unetunet,
).to(cuda)
turbo_pipeline.scheduler EulerDiscreteScheduler.from_config(turbo_pipeline.scheduler.config,timestepspacingtrailing
)
image turbo_pipeline(an astronaut riding a unicorn on mars,num_inference_steps1,guidance_scale0.0,
).images[0]
image
三、 理解Diffusers核心概念
Pipeline, Model, Scheduler 核心功能全景
功能实例代码示例文生图输入“星空下的城堡” → 生成高清图像pipe(星空下的城堡).images[0]图生图将照片转为梵高风格pipe(image输入图, prompt梵高风格)图像修复智能补全破损老照片inpaint_pipeline(mask蒙版, image原图)视频生成生成 3 秒动画片段video_pipe(跳舞的机器人, num_frames24)音频合成文本转自然语音audio_pipe(你好世界, output_typemp3) 核心概念速查表
概念说明代码示例Pipeline端到端生成流程StableDiffusionPipelineScheduler控制扩散过程EulerDiscreteSchedulerModel核心神经网络UNet2DConditionModelVAE图像编码/解码AutoencoderKLTokenizer文本处理CLIPTokenizer
3. 使用预训练模型进行推理文生图、图生图等
4. 自定义模型和调度器
5. 训练自己的扩散模型可选需要大量资源
6. 高级应用ControlNet、LoRA等