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")
相关推荐
localbob5 天前
2025.10.17 更新 AI绘画秋葉aaaki整合包 Stable Diffusion整合包v4.10 +ComfyUI整合包下载地址
ai作画·stable diffusion·comfyui·整合包·秋叶整合包
qsc9012345675 天前
AI绘画新手必看:Stable Diffusion与Midjourney的保姆级入门指南(附免费资源)
stable diffusion·aigc·midjourney·ai绘画
爱绘画的彤姐5 天前
【AI工具大盘点】AI绘画利器:Stable-Diffusion-ComfyUI保姆级教程
人工智能·ai作画·stable diffusion·aigc·comfyui·dall·e 2·ai工具
Keep_Trying_Go6 天前
基于stable diffusion图像生成的小小demo应用(pytorch)
人工智能·stable diffusion
weixin_480641616 天前
一款全能型 AI 设计修图工具实测:椒图 AI,Nano Banana Pro 加持,电商 / 修图 / 设计一站式解决
大数据·图像处理·人工智能·ai作画·stable diffusion·aigc·midjourney
学易25 天前
第二十节.探索新技术:如何自学SD3模型(上)(找官方资料/精读/下载/安装/3款工作流/效果测试)
人工智能·ai作画·stable diffusion·comfyui·工作流·sd3
Bachnroth1 个月前
Stable Diffusion 3.5 FP8镜像负载均衡配置建议
stable diffusion· fp8· 负载均衡
新缸中之脑1 个月前
Stable Diffusion的3个替代方案
人工智能·stable diffusion
2401_828890641 个月前
实现扩散模型 Stable Diffusion - MNIST 数据集
人工智能·python·深度学习·stable diffusion
凯子坚持 c1 个月前
在 openJiuwen 里把在线小工具搬回本地
人工智能·windows·stable diffusion·openteledb·openclaw