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")
相关推荐
迈火16 小时前
ComfyUI-3D-Pack:3D创作的AI神器
人工智能·gpt·3d·ai·stable diffusion·aigc·midjourney
Seeklike2 天前
diffusers学习--stable diffusion的管线解析
人工智能·stable diffusion·diffusers
马甲是掉不了一点的<.<2 天前
Stable Diffusion 环境配置详细指南
stable diffusion·环境配置
软件测试-阿涛2 天前
【AI绘画】Stable Diffusion webUI 常用功能使用技巧
人工智能·深度学习·计算机视觉·ai作画·stable diffusion
m0_603888713 天前
Stable Diffusion Models are Secretly Good at Visual In-Context Learning
人工智能·ai·stable diffusion·论文速览
爱分享的飘哥15 天前
第三十七章:文生图的炼金术:Stable Diffusion完整工作流深度解析
人工智能·pytorch·stable diffusion·文生图·ai绘画·代码实战·cfg
EndingCoder18 天前
Three.js + AI:结合 Stable Diffusion 生成纹理贴图
开发语言·前端·javascript·人工智能·stable diffusion·ecmascript·three.js
那年一路北18 天前
Deforum Stable Diffusion,轻松实现AI视频生成自由!
人工智能·stable diffusion·音视频
全宝18 天前
🎨【AI绘画实战】从零搭建Stable Diffusion环境,手把手教你生成超可爱Q版大头照!
人工智能·python·stable diffusion
sculida24 天前
秋叶sd-webui频繁出现生成后无反应的问题
stable diffusion