Python 和Stable Diffusion 模型生成图像

使用 Python 和 Stable Diffusion 模型生成图像的代码示例,基于 `diffusers` 库实现:

### 环境准备

“`bash

pip install torch torchvision –extra-index-url https://download.pytorch.org/whl/cu117

pip install transformers diffusers accelerate

“`

### 代码示例

“`python

import torch

from diffusers import StableDiffusionPipeline

from PIL import Image

# 设置设备 (GPU/CPU)

device = “cuda” if torch.cuda.is_available() else “cpu”

# 加载预训练模型 (首次运行会自动下载模型)

model_id = “runwayml/stable-diffusion-v1-5”

pipe = StableDiffusionPipeline.from_pretrained(

    model_id,

    torch_dtype=torch.float16 if device == “cuda” else torch.float32

).to(device)

# 输入提示词

prompt = “A futuristic cityscape at sunset, cyberpunk style, 4k resolution”

# 生成图像

with torch.autocast(device):

    images = pipe(

        prompt,

        num_inference_steps=50,

        guidance_scale=7.5,

        height=512,

        width=512,

        num_images=1

    ).images

# 保存结果

images[0].save(“generated_image.png”)

print(“Image generated successfully!”)

“`

### 参数说明

1. `prompt`: 描述生成图像的文本提示词(英文效果更好)

2. `num_inference_steps`: 生成步数(通常 30-50)

3. `guidance_scale`: 文本相关性控制(7-13 效果较好)

4. `height/width`: 生成图像尺寸(推荐 512×512 或 768×768)

### 其他实现方式

对于更简单的实现,可以使用 `diffusers` 的 DPMSolver 加速:

“`python

from diffusers import DPMSolverMultistepScheduler

pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)

images = pipe(prompt, num_inference_steps=25).images

“`

### 注意事项

1. 需要至少 8GB VRAM 的 GPU 才能流畅运行

2. 首次运行会自动下载约 5GB 的模型文件

3. 可通过修改 `model_id` 使用不同模型:

   – “stabilityai/stable-diffusion-2-1”

   – “prompthero/openjourney”

如果需要更轻量级的解决方案,可以使用基于 GAN 的架构(需要更多手动调整):

“`python

# 简易 GAN 示例(生成手写数字)

import tensorflow as tf

from tensorflow.keras import layers

import numpy as np

# 生成器模型

def build_generator():

    model = tf.keras.Sequential([

        layers.Dense(256, input_dim=100, activation='relu'),

        layers.BatchNormalization(),

        layers.Dense(512, activation='relu'),

        layers.BatchNormalization(),

        layers.Dense(784, activation='tanh')

    ])

    return model

# 生成数字图像

generator = build_generator()

noise = tf.random.normal([1, 100])

generated_image = generator(noise).numpy().reshape(28, 28)

Image.fromarray((generated_image * 127.5 + 127.5).astype(np.uint8))

“`

根据具体需求选择合适的方法,Stable Diffusion 适合复杂场景生成,GAN 更适合风格化/特定领域的生成任务。

© 版权声明
THE END
如果内容对您有所帮助,就支持一下吧!
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容