Stable Diffusion XL 文生图

Stable Diffusion XL 文生图

flyfish

py 复制代码
import torch
from diffusers import DiffusionPipeline, AutoencoderKL
from PIL import Image

# 定义模型路径为常量
BASE_MODEL_PATH = "/media/stable-diffusion-xl-base-1___0/"
REFINER_MODEL_PATH = "/media/stable-diffusion-xl-refiner-1___0/"
VAE_MODEL_PATH = "/media/sdxl-vae-fp16-fix/"

# 加载 VAE 模型
vae = AutoencoderKL.from_pretrained(VAE_MODEL_PATH, torch_dtype=torch.float16)

# 加载基础模型和细化器模型
base_pipe = DiffusionPipeline.from_pretrained(
    BASE_MODEL_PATH, vae=vae, torch_dtype=torch.float16, variant="fp16", use_safetensors=True
)
refiner_pipe = DiffusionPipeline.from_pretrained(
    REFINER_MODEL_PATH, vae=vae, torch_dtype=torch.float16, use_safetensors=True, variant="fp16"
)

# 将模型移动到 GPU
base_pipe.to("cuda")
refiner_pipe.to("cuda")

# 设置推理步骤和其他参数
n_steps = 50
high_noise_frac = 0.7

# 自定义生成图像的分辨率
width = 1920  # 宽度
height = 1080  # 高度

# 输入提示词
prompt = "A beautiful and dreamy world with floating islands, glowing rivers, and magical creatures"
negative_prompt = "ugly, blurry, distorted, disfigured, low quality"

# 生成图像(使用自定义分辨率)
latent_image = base_pipe(
    prompt=prompt,
    negative_prompt=negative_prompt,
    num_inference_steps=n_steps,
    denoising_end=high_noise_frac,
    output_type="latent",
    width=width,
    height=height
).images


# 细化图像并保存最终结果
final_image = refiner_pipe(
    prompt=prompt,
    negative_prompt=negative_prompt,
    num_inference_steps=n_steps,
    denoising_start=high_noise_frac,
    image=latent_image
).images[0]

final_image.save("refined_generated_image.png")
print("细化模型图像已保存为 refined_generated_image.png")
相关推荐
多恩Stone2 天前
【Stable Diffusion 1.5 】在 Unet 中每个 Cross Attention 块中的张量变化过程
stable diffusion
今夕节度使2 天前
ARM架构推理Stable Diffusiond
stable diffusion
远瞻。6 天前
【论文精读】2024 ECCV--MGLD-VSR现实世界视频超分辨率(RealWorld VSR)
人工智能·算法·stable diffusion·音视频·超分辨率重建
远瞻。7 天前
【论文精读】2024 CVPR--Upscale-A-Video现实世界视频超分辨率(RealWorld VSR)
论文阅读·人工智能·算法·stable diffusion·音视频·超分辨率重建
乱世刀疤7 天前
AI绘画:手把手带你Stable Diffusion从入门到精通(系列教程)
人工智能·ai作画·stable diffusion
layneyao9 天前
从0到1搭建AI绘画模型:Stable Diffusion微调全流程避坑指南
ai作画·stable diffusion
远瞻。9 天前
【论文精读】2024 arXiv --VEnhancer现实世界视频超分辨率(RealWorld VSR)
论文阅读·stable diffusion·音视频·超分辨率重建
立秋678911 天前
深入理解Diffusers: 从基础到Stable Diffusion
stable diffusion
Liudef0611 天前
Stable Diffusion底模对应的VAE推荐
stable diffusion
胖墩会武术11 天前
通过Auto平台与VScode搭建远程开发环境(以Stable Diffusion Web UI为例)
前端·vscode·stable diffusion