飞桨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使用方法,基本类似(提供了一种解决问题的思路)。

相关推荐
静心问道14 分钟前
TrOCR: 基于Transformer的光学字符识别方法,使用预训练模型
人工智能·深度学习·transformer·多模态
说私域16 分钟前
基于开源AI大模型、AI智能名片与S2B2C商城小程序源码的用户价值引导与核心用户沉淀策略研究
人工智能·开源
亲持红叶17 分钟前
GLU 变种:ReGLU 、 GEGLU 、 SwiGLU
人工智能·深度学习·神经网络·激活函数
说私域17 分钟前
线上协同办公时代:以开源AI大模型等工具培养网感,拥抱职业变革
人工智能·开源
群联云防护小杜19 分钟前
深度隐匿源IP:高防+群联AI云防护防绕过实战
运维·服务器·前端·网络·人工智能·网络协议·tcp/ip
摘星编程24 分钟前
构建智能客服Agent:从需求分析到生产部署
人工智能·需求分析·智能客服·agent开发·生产部署
不爱学习的YY酱27 分钟前
信息检索革命:Perplexica+cpolar打造你的专属智能搜索中枢
人工智能
whaosoft-1432 小时前
51c自动驾驶~合集7
人工智能
刘晓倩5 小时前
Coze智能体开发实战-多Agent综合实战
人工智能·coze
石迹耿千秋6 小时前
迁移学习--基于torchvision中VGG16模型的实战
人工智能·pytorch·机器学习·迁移学习