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")
相关推荐
语戚1 天前
Stable Diffusion 入门:架构、空间与生成流程概览
人工智能·ai·stable diffusion·aigc·模型
yumgpkpm1 天前
华为昇腾910B上用Kubernetes(K8s)部署LLM(Qwen3-32B)的详细步骤,保姆级命令及方法、下载链接等
运维·服务器·华为·stable diffusion·aigc·copilot·llama
平安的平安1 天前
Python 实现 AI 图像生成:调用 Stable Diffusion API 完整教程
人工智能·python·stable diffusion
孤岛站岗6 天前
【AI大模型入门】B02:Stable Diffusion——开源绘图,让AI绘画飞入寻常百姓家
人工智能·stable diffusion·开源
智星云算力6 天前
从创作到训练:Stable Diffusion WebUI全链路实战
人工智能·深度学习·stable diffusion·智星云·gpu算力租用
ai_xiaogui7 天前
PanelAI 最新进展:AI算力集群一键部署ComfyUI & Stable Diffusion,多节点Docker管理面板从原型到真实数据全解析
人工智能·docker·stable diffusion·ai算力集群管理·ai私有化部署工具·ai容器管理与算力调度实战·多节点docker服务器面板
scott1985128 天前
扩散模型之(二十)Stable Diffusion的运作原理
深度学习·stable diffusion·扩散模型·生成式
xingyuzhisuan8 天前
怎么快速在云上部署一个Stable Diffusion环境?(实操落地版)
人工智能·stable diffusion·ai绘画·gpu算力
scott19851211 天前
扩散模型之(十九)stable diffusion的演进与技术路线
stable diffusion
Jial-(^V^)11 天前
使用Stable-Diffusion实现文本生成图像
stable diffusion