飞桨PaddlePaddle中SDXL的常用方法

PaddlePaddle中使用SDXL的方法
controlnetvaeunetlora使用方法示例:

python 复制代码
# controlnet、vae、unet及lora使用方法
# 安装develop的ppdiffusers
# pip install "ppdiffusers>=0.24.0"
import numpy as np
import cv2
from PIL import Image
import paddle
from ppdiffusers import (
    ControlNetModel, 
    StableDiffusionXLControlNetPipeline,
    AutoencoderKL, 
	UNet2DConditionModel, 
	EulerAncestralDiscreteScheduler
)
from ppdiffusers.utils import load_image


# load unet
unet = UNet2DConditionModel.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0/unet", paddle_dtype=paddle.float16, variant="fp16")

# load controlnet
controlnet = ControlNetModel.from_pretrained("diffusers/controlnet-canny-sdxl-1.0")

# load vae
vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", paddle_dtype=paddle.float16)

pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
    "stabilityai/stable-diffusion-xl-base-1.0",
    # "SG161222/RealVisXL_V3.0", 
    paddle_dtype=paddle.float16, 
    safety_checker=None, 
    controlnet=controlnet, 
    variant="fp16",
    low_cpu_mem_usage=True, 
    vae=vae,
	unet=unet,
)

# denoise策略
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)

# 开启 xformers
pipe.enable_xformers_memory_efficient_attention()

# Initialize LoRA model and weights
# 模型路径:./lora/anime-detailer-xl.safetensors
lora_model_id = "./lora"  # 模型所在目录
lora_filename = "anime-detailer-xl.safetensors"  # 模型名字
lora_scale_slider = 2  # -2 for less detailed result

# Load and fuse LoRA weights
pipe.load_lora_weights(
    lora_model_id, 
    weight_name=lora_filename, 
    from_diffusers=True, 
    from_hf_hub=True
)
pipe.fuse_lora(lora_scale=lora_scale_slider)

# 固定随机种子
generator = paddle.Generator().manual_seed(100)

# 定义prompt
prompt = "aerial view, a futuristic research complex in a bright foggy jungle, hard lighting"
negative_prompt = 'low quality, bad quality, sketches'

# controlnet参数设置
controlnet_conditioning_scale=0.5

# controlnet参考图
image = load_image(
    "https://hf-mirror.com/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/hf-logo.png"
)
# image = image.resize((1024, 1024)) # 修改尺寸(width, height),忽略
# 参考图生成线稿
image = np.array(image)
image = cv2.Canny(image, 100, 200)
image = image[:, :, None]
image = np.concatenate([image, image, image], axis=2)
image = Image.fromarray(image)

image = pipe(
    prompt,  
    negative_prompt=negative_prompt, 
    width=1024, 
    height=1024, 
    image=image,
    num_inference_steps=30,
    guidance_scale=3,
    num_images_per_prompt=1,
    generator=generator, 
    controlnet_conditioning_scale=controlnet_conditioning_scale, 
).images[0]

image.save("text_ctl_img.png")

更多详细使用方法可以参考官方文档以及hf-mirror.com中直接搜索比较官方的对应模型查看api使用方法,基本类似(提供了一种解决问题的思路)。

相关推荐
勾股导航30 分钟前
K-means
人工智能·机器学习·kmeans
liliangcsdn31 分钟前
Diff2Flow中扩散和流匹配的对齐探索
人工智能
SmartBrain36 分钟前
战略洞察:以AI为代表的第四次工业革命
人工智能·语言模型·aigc
一个处女座的程序猿1 小时前
AI之Agent之VibeCoding:《Vibe Coding Kills Open Source》翻译与解读
人工智能·开源·vibecoding·氛围编程
Jay Kay1 小时前
GVPO:Group Variance Policy Optimization
人工智能·算法·机器学习
风指引着方向1 小时前
归约操作优化:ops-math 的 Sum/Mean/Max 实现
人工智能·wpf
机器之心1 小时前
英伟达世界模型再进化,一个模型驱动所有机器人!机器人的GPT时刻真正到来
人工智能·openai
纯爱掌门人1 小时前
终焉轮回里,藏着 AI 与人类的答案
前端·人工智能·aigc
人工智能AI技术1 小时前
Transformer:大模型的“万能骨架”
人工智能
uesowys2 小时前
Apache Spark算法开发指导-Factorization machines classifier
人工智能·算法