Skip to main content

PPDiffusers: Diffusers toolbox implemented based on PaddlePaddle

Project description

PPDiffusers: Diffusers toolbox implemented based on PaddlePaddle

PPDiffusers是一款支持多种模态(如文本图像跨模态、图像、语音)扩散模型(Diffusion Model)训练和推理的国产化工具箱,依托于PaddlePaddle框架和PaddleNLP自然语言处理开发库,具体来说,PPDiffusers具有以下特性:

📦 SOTA扩散模型Pipelines集合

🔊 提供丰富的Noise Scheduler

🎛️ 提供多种扩散模型组件

📖 提供丰富的训练和推理教程

🚀 支持FastDeploy高性能部署

News 📢

  • 🔥 2022.01.18 发布 0.11.0 版本,新增Heun和Single step DPM-Solver噪声调度器,支持Karlo UnCLIP、Paint-by-example、Depth-Guided Stable Diffusion等图像生成扩散模型, 支持Audio Diffusion音频生成扩散模型;

  • 🔥 2022.12.06 发布 0.9.0 版本,支持 StableDiffusion2.0 的文生图、图生图、图像编辑及图像超分等功能;

  • 🔥 2022.11.11 发布 0.6.2 版本,支持使用FastDeploy对 StableDiffusion进行高性能部署、支持 Diffusers或原版模型->PPDiffusers权重转换

  • 🔥 2022.11.04 支持 IDEA-CCNL/Taiyi-Stable-Diffusion-1B-Chinese-v0.1 和 IDEA-CCNL/Taiyi-Stable-Diffusion-1B-Chinese-EN-v0.1 中文权重;

  • 🔥 2022.10.27 发布 PPDiffusers仓库

特性 | 安装 | 快速开始 | 模型部署

特性

SOTA扩散模型Pipelines集合

我们提供SOTA(State-of-the-Art) 的扩散模型Pipelines集合。 目前PPDiffusers已经集成了33+Pipelines,支持文图生成(Text-to-Image Generation)、文本引导的图像编辑(Text-Guided Image Inpainting)、文本指导的图像变换(Image-to-Image Text-Guided Generation)、超分(Super Superresolution)在内的10+任务,覆盖文本图像跨模态、图像、音频等多种模态。 如果想要了解当前支持的所有Pipelines以及对应的来源信息,可以阅读🔥 PPDiffusers Pipelines文档。

提供丰富的Noise Scheduler

我们提供了丰富的噪声调度器(Noise Scheduler),可以对速度质量进行权衡,用户可在推理时根据需求快速切换使用。 当前PPDiffusers已经集成了14+Scheduler,不仅支持 DDPMDDIMPNDM,还支持最新的 🔥 DPMSolver

提供多种扩散模型组件

我们提供了多种扩散模型组件,如UNet1DModelUNet2DModelUNet2DConditionModelVQModelAutoencoderKL等。

提供丰富的训练和推理教程

我们提供了丰富的训练教程,不仅支持扩散模型的二次开发微调,如基于Textual InversionDreamBooth使用3-5张图定制化训练生成图像的风格或物体,还支持使用Laion400M数据集🔥 从零训练Latent Diffusion Model 模型! 此外,我们还提供了丰富的🔥 Pipelines推理脚本

支持FastDeploy高性能部署

我们提供基于FastDeploy🔥 高性能Stable Diffusion Pipeline,更多有关FastDeploy进行多推理引擎后端高性能部署的信息请参考🔥 高性能FastDeploy推理教程

from ppdiffusers import StableDiffusionPipeline, FastDeployStableDiffusionPipeline

orig_pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
fd_pipe = FastDeployStableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5@fastdeploy")

安装

环境依赖

  • paddlepaddle-gpu>=2.4.0
  • paddlenlp>=2.5.0
  • ftfy
  • regex
  • Pillow

pip安装

pip install --upgrade ppdiffusers

更多关于PaddlePaddle安装的详细教程请查看Installation

手动安装

git clone https://github.com/PaddlePaddle/PaddleNLP
# 注意:如果clone仓库非常慢的话,可以考虑使用镜像版本
# git clone https://gitee.com/paddlepaddle/PaddleNLP
cd PaddleNLP/ppdiffusers
python setup.py install

快速开始

为了快速上手使用该项目, 我们可以先阅读🤗 Huggingface团队提供的两个Notebook教程 Getting started with DiffusersTraining a diffusers model。(Tips:国内用户可能无法正常打开)

Stable Diffusion 1.x 是一个文本到图像(text-to-image)的潜在扩散模型(Latent Diffusion Model, LDM), 该模型是由来自 CompVis, Stability AI, LAION 的工程师以及 RunwayML一起开发而完成的。该模型使用了大小为 512x512 分辨率的 LAION-5B 数据集子集进行训练。该模型使用了 Openai 开源的 CLIP ViT-L/14 文本编码器(约123M参数)来编码提示(prompt)文本(注意该部分权重不进行训练)。该模型还使用了UNet2DCondition模型(约860M参数)来建模扩散过程。

Stable Diffusion 2.0LAIONStability AI 的支持下开发完成的,它与早期的 V1 版本相比,大大改善了生成图像的质量。该版本中的文生图模型不仅可以生成默认分辨率为 512x512 像素还可以生成 768x768 分辨率的图像。该模型作为 Stable Diffusion 1.x 的升级版, 使用了全新的 OpenCLIP-ViT/H 中的文本编码器(注意:该文本编码器一共24层,实际只使用23层)。LAION 团队首先使用 V1 版的策略512x512 像素的图片上进行训练得到了一个基础版模型 stabilityai/stable-diffusion-2-base,然后他们还使用了 v-objective 策略,在基础模型之上进一步使用 768x768 分辨率的图片进行训练,得到了一个最终版的模型 stabilityai/stable-diffusion-2

Tips: 为了方便国内用户下载使用及快速体验Stable Diffusion模型,我们在百度云(BOS)上提供了paddle版本的镜像权重。注意:为了使用该模型与权重,你必须接受该模型所要求的License,请访问huggingface的runwayml/stable-diffusion-v1-5stabilityai/stable-diffusion-2, 仔细阅读里面的License,然后签署该协议。 Stable Diffusion是基于以下的License: The CreativeML OpenRAIL M license is an Open RAIL M license, adapted from the work that BigScience and the RAIL Initiative are jointly carrying in the area of responsible AI licensing. See also the article about the BLOOM Open RAIL license on which this license is based.

下面将以最近较为火热的 🔥Stable Diffusion 模型为例,来说明如何快速使用 PPDiffusers,在开始之前我们可以点开下面的折叠按钮,查看当前 Stable Diffusion 模型所支持的权重!

PPDiffusers模型支持的权重

  Stable Diffusion 模型支持的权重(英文)

我们只需要将下面的"xxxx",替换成所需的权重名,即可快速使用!

from ppdiffusers import *

pipe_text2img = StableDiffusionPipeline.from_pretrained("xxxx")
pipe_img2img = StableDiffusionImg2ImgPipeline.from_pretrained("xxxx")
pipe_inpaint_legacy = StableDiffusionInpaintPipelineLegacy.from_pretrained("xxxx")
pipe_mega = StableDiffusionMegaPipeline.from_pretrained("xxxx")

# pipe_mega.text2img() 等于 pipe_text2img()
# pipe_mega.img2img() 等于 pipe_img2img()
# pipe_mega.inpaint_legacy() 等于 pipe_inpaint_legacy()
PPDiffusers支持的模型名称 支持加载的Pipeline 备注 huggingface.co地址
CompVis/stable-diffusion-v1-4 StableDiffusionPipeline、StableDiffusionImg2ImgPipeline、StableDiffusionInpaintPipelineLegacy、StableDiffusionMegaPipeline、StableDiffusionPipelineAllinOne Stable-Diffusion-v1-4 使用 Stable-Diffusion-v1-2 的权重进行初始化。随后在"laion-aesthetics v2 5+"数据集上以 512x512 分辨率微调了 225k 步数,对文本使用了 10% 的dropout(即:训练过程中文图对中的文本有 10% 的概率会变成空文本)。模型使用了CLIP ViT-L/14作为文本编码器。 地址
CompVis/ldm-text2im-large-256 LDMTextToImagePipeline LDM论文 LDM-KL-8-G* 权重。 地址
CompVis/ldm-super-resolution-4x-openimages LDMSuperResolutionPipeline LDM论文 LDM-VQ-4 权重,原始权重链接 地址
runwayml/stable-diffusion-v1-5 StableDiffusionPipeline、StableDiffusionImg2ImgPipeline、StableDiffusionInpaintPipelineLegacy、StableDiffusionMegaPipeline、StableDiffusionPipelineAllinOne Stable-Diffusion-v1-5 使用 Stable-Diffusion-v1-2 的权重进行初始化。随后在"laion-aesthetics v2 5+"数据集上以 512x512 分辨率微调了 595k 步数,对文本使用了 10% 的dropout(即:训练过程中文图对中的文本有 10% 的概率会变成空文本)。模型同样也使用了CLIP ViT-L/14作为文本编码器。 地址
runwayml/stable-diffusion-inpainting StableDiffusionInpaintPipeline Stable-Diffusion-Inpainting 使用 Stable-Diffusion-v1-2 的权重进行初始化。首先进行了 595k 步的常规训练(实际也就是 Stable-Diffusion-v1-5 的权重),然后进行了 440k 步的 inpainting 修复训练。对于 inpainting 修复训练,给 UNet 额外增加了 5 输入通道(其中 4 个用于被 Mask 遮盖住的图片,1 个用于 Mask 本身)。在训练期间,会随机生成 Mask,并有 25% 概率会将原始图片全部 Mask 掉。 地址
stabilityai/stable-diffusion-2-base StableDiffusionPipeline、StableDiffusionImg2ImgPipeline、StableDiffusionInpaintPipelineLegacy、StableDiffusionMegaPipeline、StableDiffusionPipelineAllinOne 该模型首先在 LAION-5B 256x256 子集上 (过滤条件:punsafe = 0.1 的 LAION-NSFW 分类器 和 审美分数大于等于 4.5 )从头开始训练 550k 步,然后又在分辨率 >= 512x512 的同一数据集上进一步训练 850k 步。 地址
stabilityai/stable-diffusion-2 StableDiffusionPipeline、StableDiffusionImg2ImgPipeline、StableDiffusionInpaintPipelineLegacy、StableDiffusionMegaPipeline、StableDiffusionPipelineAllinOne stable-diffusion-2 使用 stable-diffusion-2-base 权重进行初始化,首先在同一数据集上(512x512 分辨率)使用 v-objective 训练了 150k 步。然后又在 768x768 分辨率上使用 v-objective 继续训练了 140k 步。 地址
stabilityai/stable-diffusion-2-inpainting StableDiffusionInpaintPipeline stable-diffusion-2-inpainting 使用 stable-diffusion-2-base 权重初始化,并且额外训练了 200k 步。训练过程使用了 LAMA 中提出的 Mask 生成策略,并且使用 Mask 图片的 Latent 表示(经过 VAE 编码)作为附加条件。 地址
stabilityai/stable-diffusion-x4-upscaler StableDiffusionUpscalePipeline 该模型在LAION 10M 子集上(>2048x2048)训练了 1.25M 步。该模型还在分辨率为 512x512 的图像上使用 Text-guided Latent Upscaling Diffusion Model 进行了训练。除了文本输入之外,它还接收 noise_level 作为输入参数,因此我们可以使用 预定义的 Scheduler 向低分辨率的输入图片添加噪声。 地址
hakurei/waifu-diffusion StableDiffusionPipeline、StableDiffusionImg2ImgPipeline、StableDiffusionInpaintPipelineLegacy、StableDiffusionMegaPipeline、StableDiffusionPipelineAllinOne waifu-diffusion-v1-2 使用 stable-diffusion-v1-4 权重初始化,并且在高质量动漫图像数据集上进行微调后得到的模型。用于微调的数据是 680k 文本图像样本,这些样本是通过 booru 网站 下载的。 地址
hakurei/waifu-diffusion-v1-3 StableDiffusionPipeline、StableDiffusionImg2ImgPipeline、StableDiffusionInpaintPipelineLegacy、StableDiffusionMegaPipeline、StableDiffusionPipelineAllinOne waifu-diffusion-v1-3 是 waifu-diffusion-v1-2 基础上进一步训练得到的。他们对数据集进行了额外操作:(1)删除下划线;(2)删除括号;(3)用逗号分隔每个booru 标签;(4)随机化标签顺序。 地址
naclbit/trinart_stable_diffusion_v2_60k StableDiffusionPipeline、StableDiffusionImg2ImgPipeline、StableDiffusionInpaintPipelineLegacy、StableDiffusionMegaPipeline、StableDiffusionPipelineAllinOne trinart_stable_diffusion 使用 stable-diffusion-v1-4 权重初始化,在 40k 高分辨率漫画/动漫风格的图片数据集上微调了 8 个 epoch。V2 版模型使用 dropouts10k+ 图像新的标记策略训练了更长时间 地址
naclbit/trinart_stable_diffusion_v2_95k StableDiffusionPipeline、StableDiffusionImg2ImgPipeline、StableDiffusionInpaintPipelineLegacy、StableDiffusionMegaPipeline、StableDiffusionPipelineAllinOne 95k 步数的的结果,其他同上。 地址
naclbit/trinart_stable_diffusion_v2_115k StableDiffusionPipeline、StableDiffusionImg2ImgPipeline、StableDiffusionInpaintPipelineLegacy、StableDiffusionMegaPipeline、StableDiffusionPipelineAllinOne 115k 步数的的结果,其他同上。 地址
Deltaadams/Hentai-Diffusion StableDiffusionPipeline、StableDiffusionImg2ImgPipeline、StableDiffusionInpaintPipelineLegacy、StableDiffusionMegaPipeline、StableDiffusionPipelineAllinOne None 地址
ringhyacinth/nail-set-diffuser StableDiffusionPipeline、StableDiffusionImg2ImgPipeline、StableDiffusionInpaintPipelineLegacy、StableDiffusionMegaPipeline、StableDiffusionPipelineAllinOne 美甲领域的扩散模型,训练数据使用了 Weekend 地址
Linaqruf/anything-v3.0 StableDiffusionPipeline、StableDiffusionImg2ImgPipeline、StableDiffusionInpaintPipelineLegacy、StableDiffusionMegaPipeline、StableDiffusionPipelineAllinOne 该模型可通过输入几个文本提示词就能生成高质量、高度详细的动漫风格图片,该模型支持使用 danbooru 标签文本 生成图像。 地址
  Stable Diffusion 模型支持的权重(中文和多语言)
PPDiffusers支持的模型名称 支持加载的Pipeline 备注 huggingface.co地址
BAAI/AltDiffusion AltDiffusionPipeline、AltDiffusionImg2ImgPipeline 该模型使用 AltCLIP 作为文本编码器,在 Stable Diffusion 基础上训练了双语Diffusion模型,其中训练数据来自 WuDao数据集LAION 地址
BAAI/AltDiffusion-m9 AltDiffusionPipeline、AltDiffusionImg2ImgPipeline 该模型使用9种语言的 AltCLIP-m9 作为文本编码器,其他同上。 地址
IDEA-CCNL/Taiyi-Stable-Diffusion-1B-Chinese-v0.1 StableDiffusionPipeline、StableDiffusionImg2ImgPipeline、StableDiffusionInpaintPipelineLegacy、StableDiffusionMegaPipeline、StableDiffusionPipelineAllinOne 他们将 Noah-Wukong 数据集 (100M) 和 Zero 数据集 (23M) 用作预训练的数据集,先用 IDEA-CCNL/Taiyi-CLIP-RoBERTa-102M-ViT-L-Chinese 对这两个数据集的图文对相似性进行打分,取 CLIP Score 大于 0.2 的图文对作为训练集。 他们使用 IDEA-CCNL/Taiyi-CLIP-RoBERTa-102M-ViT-L-Chinese 作为初始化的text encoder,冻住 stable-diffusion-v1-4 (论文) 模型的其他部分,只训练 text encoder,以便保留原始模型的生成能力且实现中文概念的对齐。该模型目前在0.2亿图文对上训练了一个 epoch。 在 32 x A100 上训练了大约100小时,该版本只是一个初步的版本。 地址
IDEA-CCNL/Taiyi-Stable-Diffusion-1B-Chinese-EN-v0.1 StableDiffusionPipeline、StableDiffusionImg2ImgPipeline、StableDiffusionInpaintPipelineLegacy、StableDiffusionMegaPipeline、StableDiffusionPipelineAllinOne 他们将 Noah-Wukong 数据集 (100M) 和 Zero 数据集 (23M) 用作预训练的数据集,先用 IDEA-CCNL/Taiyi-CLIP-RoBERTa-102M-ViT-L-Chinese 对这两个数据集的图文对相似性进行打分,取 CLIP Score 大于 0.2 的图文对作为训练集。 他们使用 stable-diffusion-v1-4 (论文) 模型进行继续训练,其中训练分为两个stage第一个stage 中冻住模型的其他部分,只训练 text encoder ,以便保留原始模型的生成能力且实现中文概念的对齐。第二个stage 中将全部模型解冻,一起训练 text encoder 和 diffusion model ,以便 diffusion model 更好的适配中文引导。第一个 stage 他们训练了 80 小时,第二个 stage 训练了 100 小时,两个stage都是用了8 x A100,该版本是一个初步的版本。 地址

文图生成 (Text-to-Image Generation)

import paddle
from ppdiffusers import StableDiffusionPipeline

# 可选模型权重
# CompVis/stable-diffusion-v1-4
# runwayml/stable-diffusion-v1-5
# stabilityai/stable-diffusion-2-base (原始策略 512x512)
# stabilityai/stable-diffusion-2 (v-objective 768x768)
# Linaqruf/anything-v3.0
# ......
pipe = StableDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2")

# 设置随机种子,我们可以复现下面的结果!
paddle.seed(5232132133)
prompt = "a portrait of shiba inu with a red cap growing on its head. intricate. lifelike. soft light. sony a 7 r iv 5 5 mm. cinematic post - processing "
image = pipe(prompt, guidance_scale=7.5, height=768, width=768).images[0]

image.save("shiba_dog_with_a_red_cap.png")
image

文本引导的图像变换(Image-to-Image Text-Guided Generation)

 Image-to-Image Text-Guided Generation Demo
import paddle
from ppdiffusers import StableDiffusionImg2ImgPipeline
from ppdiffusers.utils import load_image

# 可选模型权重
# CompVis/stable-diffusion-v1-4
# runwayml/stable-diffusion-v1-5
# stabilityai/stable-diffusion-2-base (原始策略 512x512)
# stabilityai/stable-diffusion-2 (v-objective 768x768)
# Linaqruf/anything-v3.0
# ......
pipe = StableDiffusionImg2ImgPipeline.from_pretrained("Linaqruf/anything-v3.0", safety_checker=None)

url = "https://paddlenlp.bj.bcebos.com/models/community/CompVis/data/image_Kurisu.png"
image = load_image(url).resize((512, 768))

# 设置随机种子,我们可以复现下面的结果!
paddle.seed(42)
prompt = "Kurisu Makise, looking at viewer, long hair, standing, 1girl, hair ornament, hair flower, cute, jacket, white flower, white dress"
negative_prompt = "lowres, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts, signature, watermark, username, blurry"

image = pipe(prompt=prompt, negative_prompt=negative_prompt, image=image, strength=0.75, guidance_scale=7.5).images[0]
image.save("image_Kurisu_img2img.png")
image

文本引导的图像编辑(Text-Guided Image Inpainting)

注意!当前有两种版本的图像编辑代码,一个是Legacy版本,一个是正式版本,下面将分别介绍两种代码如何使用!

 Legacy版本代码
import paddle
from ppdiffusers import StableDiffusionInpaintPipelineLegacy
from ppdiffusers.utils import load_image

# 可选模型权重
# CompVis/stable-diffusion-v1-4
# runwayml/stable-diffusion-v1-5
# stabilityai/stable-diffusion-2-base (原始策略 512x512)
# stabilityai/stable-diffusion-2 (v-objective 768x768)
# Linaqruf/anything-v3.0
# ......
img_url = "https://paddlenlp.bj.bcebos.com/models/community/CompVis/stable-diffusion-v1-4/overture-creations.png"
mask_url = "https://paddlenlp.bj.bcebos.com/models/community/CompVis/stable-diffusion-v1-4/overture-creations-mask.png"

image = load_image(img_url).resize((512, 512))
mask_image = load_image(mask_url).resize((512, 512))

pipe = StableDiffusionInpaintPipelineLegacy.from_pretrained("stabilityai/stable-diffusion-2-base", safety_checker=None)

# 设置随机种子,我们可以复现下面的结果!
paddle.seed(10245)
prompt = "a red cat sitting on a bench"
image = pipe(prompt=prompt, image=image, mask_image=mask_image, strength=0.75).images[0]

image.save("a_red_cat_legacy.png")
image
 正式版本代码

Tips: 下面的使用方法是新版本的代码,也是官方推荐的代码,注意必须配合 runwayml/stable-diffusion-inpaintingstabilityai/stable-diffusion-2-inpainting 才可正常使用。

import paddle
from ppdiffusers import StableDiffusionInpaintPipeline
from ppdiffusers.utils import load_image

# 可选模型权重
# runwayml/stable-diffusion-inpainting
# stabilityai/stable-diffusion-2-inpainting
img_url = "https://paddlenlp.bj.bcebos.com/models/community/CompVis/stable-diffusion-v1-4/overture-creations.png"
mask_url = "https://paddlenlp.bj.bcebos.com/models/community/CompVis/stable-diffusion-v1-4/overture-creations-mask.png"

image = load_image(img_url).resize((512, 512))
mask_image = load_image(mask_url).resize((512, 512))

pipe = StableDiffusionInpaintPipeline.from_pretrained("stabilityai/stable-diffusion-2-inpainting")

# 设置随机种子,我们可以复现下面的结果!
paddle.seed(1024)
prompt = "Face of a yellow cat, high resolution, sitting on a park bench"
image = pipe(prompt=prompt, image=image, mask_image=mask_image).images[0]

image.save("a_yellow_cat.png")
image

文本引导的图像放大 & 超分(Text-Guided Image Upscaling & Super Superresolution)

 Text-Guided Image Upscaling Demo
import paddle
from ppdiffusers import StableDiffusionUpscalePipeline
from ppdiffusers.utils import load_image

pipe = StableDiffusionUpscalePipeline.from_pretrained("stabilityai/stable-diffusion-x4-upscaler")

url = "https://paddlenlp.bj.bcebos.com/models/community/CompVis/data/low_res_cat.png"
# 我们人工将原始图片缩小成 128x128 分辨率,最终保存的图片会放大4倍!
low_res_img = load_image(url).resize((128, 128))

prompt = "a white cat"
image = pipe(prompt=prompt, image=low_res_img).images[0]

image.save("upscaled_white_cat.png")
image image
 Super Superresolution Demo
import paddle
from ppdiffusers import LDMSuperResolutionPipeline
from ppdiffusers.utils import load_image

pipe = LDMSuperResolutionPipeline.from_pretrained("CompVis/ldm-super-resolution-4x-openimages")

url = "https://paddlenlp.bj.bcebos.com/models/community/CompVis/stable-diffusion-v1-4/overture-creations.png"

# 我们人工将原始图片缩小成 128x128 分辨率,最终保存的图片会放大4倍!
low_res_img = load_image(url).resize((128, 128))

image = pipe(image=low_res_img, num_inference_steps=100).images[0]

image.save("ldm-super-resolution-image.png")
image image

模型部署

StableDiffusion模型除了支持Paddle动态图运行,还支持将模型导出并使用推理引擎运行。我们提供在基于FastDeploy上的StableDiffusion模型部署示例,包括文生图、图生图、图像编辑等任务,用户可以按照我们提供StableDiffusion模型导出教程将模型导出,或者使用一键导出脚本导出模型,然后使用FastDeployStableDiffusionMegaPipeline进行高性能推理部署!

  已预先导出的FastDeploy版Stable Diffusion权重

注意:当前导出的vae encoder带有随机因素!随机因素代码地址

  • CompVis/stable-diffusion-v1-4@fastdeploy
  • runwayml/stable-diffusion-v1-5@fastdeploy
  • runwayml/stable-diffusion-inpainting@fastdeploy
  • stabilityai/stable-diffusion-2-base@fastdeploy
  • stabilityai/stable-diffusion-2@fastdeploy
  • stabilityai/stable-diffusion-2-inpainting@fastdeploy
  • Linaqruf/anything-v3.0@fastdeploy
  • hakurei/waifu-diffusion-v1-3@fastdeploy
import fastdeploy as fd
from ppdiffusers import FastDeployStableDiffusionMegaPipeline
from ppdiffusers.utils import load_image

def create_runtime_option(device_id=-1, backend="paddle"):
    option = fd.RuntimeOption()
    if backend == "paddle":
        option.use_paddle_backend()
    else:
        option.use_ort_backend()
    if device_id == -1:
        option.use_cpu()
    else:
        option.use_gpu(device_id)
    return option

runtime_options = {
    "text_encoder": create_runtime_option(-1, "onnx"),  # use cpu
    "vae_encoder": create_runtime_option(-1, "paddle"),  # use cpu
    "vae_decoder": create_runtime_option(-1, "paddle"),  # use cpu
    "unet": create_runtime_option(0, "paddle"),  # use gpu
}

fd_pipe = FastDeployStableDiffusionMegaPipeline.from_pretrained(
    "Linaqruf/anything-v3.0@fastdeploy", runtime_options=runtime_options
)

# text2img
prompt = "a portrait of shiba inu with a red cap growing on its head. intricate. lifelike. soft light. sony a 7 r iv 5 5 mm. cinematic post - processing "
image_text2img = fd_pipe.text2img(prompt=prompt, num_inference_steps=50).images[0]
image_text2img.save("image_text2img.png")

# img2img
url = "https://paddlenlp.bj.bcebos.com/models/community/CompVis/data/image_Kurisu.png"
image = load_image(url).resize((512, 512))
prompt = "Kurisu Makise, looking at viewer, long hair, standing, 1girl, hair ornament, hair flower, cute, jacket, white flower, white dress"
negative_prompt = "lowres, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts, signature, watermark, username, blurry"

image_img2img = fd_pipe.img2img(
    prompt=prompt, negative_prompt=negative_prompt, image=image, strength=0.75, guidance_scale=7.5
).images[0]
image_img2img.save("image_img2img.png")

# inpaint_legacy
img_url = "https://paddlenlp.bj.bcebos.com/models/community/CompVis/stable-diffusion-v1-4/overture-creations.png"
mask_url = "https://paddlenlp.bj.bcebos.com/models/community/CompVis/stable-diffusion-v1-4/overture-creations-mask.png"
image = load_image(img_url).resize((512, 512))
mask_image = load_image(mask_url).resize((512, 512))
prompt = "a red cat sitting on a bench"

image_inpaint_legacy = fd_pipe.inpaint_legacy(
    prompt=prompt, image=image, mask_image=mask_image, strength=0.75, num_inference_steps=50
).images[0]
image_inpaint_legacy.save("image_inpaint_legacy.png")
image

License

PPDiffusers 遵循 Apache-2.0开源协议

Stable Diffusion 遵循 The CreativeML OpenRAIL M 开源协议

The CreativeML OpenRAIL M is an Open RAIL M license, adapted from the work that BigScience and the RAIL Initiative are jointly carrying in the area of responsible AI licensing. See also the article about the BLOOM Open RAIL license on which this license is based.

Acknowledge

我们借鉴了🤗 Hugging Face的Diffusers关于预训练扩散模型使用的优秀设计,在此对Hugging Face作者及其开源社区表示感谢。

Credits

This library concretizes previous work by many different authors and would not have been possible without their great research and implementations. We'd like to thank, in particular, the following implementations which have helped us in our development and without which the API could not have been as polished today:

  • @huggingface' diffusers library, available here
  • @CompVis' latent diffusion models library, available here
  • @hojonathanho original DDPM implementation, available here as well as the extremely useful translation into PyTorch by @pesser, available here
  • @ermongroup's DDIM implementation, available here.
  • @yang-song's Score-VE and Score-VP implementations, available here

We also want to thank @heejkoo for the very helpful overview of papers, code and resources on diffusion models, available here as well as @crowsonkb and @rromb for useful discussions and insights.

Citation

@misc{von-platen-etal-2022-diffusers,
  author = {Patrick von Platen and Suraj Patil and Anton Lozhkov and Pedro Cuenca and Nathan Lambert and Kashif Rasul and Mishig Davaadorj and Thomas Wolf},
  title = {Diffusers: State-of-the-art diffusion models},
  year = {2022},
  publisher = {GitHub},
  journal = {GitHub repository},
  howpublished = {\url{https://github.com/huggingface/diffusers}}
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ppdiffusers-0.11.0.tar.gz (352.4 kB view hashes)

Uploaded Source

Built Distribution

ppdiffusers-0.11.0-py3-none-any.whl (595.4 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page