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")
相关推荐
AloneCat201211 小时前
stable Diffusion模型结构
stable diffusion
霍志杰2 天前
stable-diffusion windows本地部署
windows·stable diffusion
昨日之日20062 天前
ACE-Step - 20秒生成4分钟完整歌曲,音乐界的Stable Diffusion,支持50系显卡 本地一键整合包下载
计算机视觉·stable diffusion·音视频
白熊1882 天前
【图像大模型】Stable Diffusion Web UI:深度解析与实战指南
ui·stable diffusion
西西弗Sisyphus4 天前
基于Stable Diffusion XL模型进行文本生成图像的训练
stable diffusion
阿维的博客日记4 天前
VAE和Stable Diffusion的关系
stable diffusion·vae
我算是程序猿5 天前
【2025最新】AI绘画终极提示词库|Midjourney&Stable Diffusion通用公式大全
人工智能·ai作画·stable diffusion·aigc·midjourney
乌旭8 天前
AIGC算力消耗白皮书:Stable Diffusion vs Midjourney的架构成本差异
人工智能·深度学习·stable diffusion·架构·aigc·midjourney·gpu算力
谷雪_65811 天前
AIGC 落地实战:用 Stable Diffusion 打造企业级营销素材生成平台
python·stable diffusion·aigc