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")
相关推荐
全息数据2 天前
DDPM代码讲解【详细!!!】
深度学习·stable diffusion·多模态·ddpm
老鱼说AI11 天前
当自回归模型遇上扩散模型:下一代序列预测模型详解与Pytorch实现
人工智能·pytorch·深度学习·神经网络·语言模型·自然语言处理·stable diffusion
我希望的一路生花16 天前
Nik Collection 6.2全新版Nik降噪锐化调色PS/LR插件
人工智能·计算机视觉·设计模式·stable diffusion·aigc
GetcharZp17 天前
玩转AI绘画,你只差一个节点式“魔法”工具——ComfyUI 保姆级入门指南
人工智能·stable diffusion
Seeklike19 天前
diffuxers学习--AutoPipeline
人工智能·python·stable diffusion·diffusers
游戏AI研究所19 天前
ComfyUI 里的 Prompt 插值器(prompt interpolation / text encoder 插值方式)的含义和作用!
人工智能·游戏·机器学习·stable diffusion·prompt·aigc
迈火21 天前
ComfyUI-3D-Pack:3D创作的AI神器
人工智能·gpt·3d·ai·stable diffusion·aigc·midjourney
Seeklike23 天前
diffusers学习--stable diffusion的管线解析
人工智能·stable diffusion·diffusers
马甲是掉不了一点的<.<23 天前
Stable Diffusion 环境配置详细指南
stable diffusion·环境配置
软件测试-阿涛23 天前
【AI绘画】Stable Diffusion webUI 常用功能使用技巧
人工智能·深度学习·计算机视觉·ai作画·stable diffusion