功能
- stable-diffusion(文本生成图片)
- PaddleHub,HuggingFace两种调用方式
PaddleHub
环境
bash
pip install paddlepaddle-gpu
pip install paddlehub
代码
bash
from PIL import Image
import paddlehub as hub
module = hub.Module(name='stable_diffusion')
## 保存在demo目录
result = module.generate_image(text_prompts="clouds surround the mountains and Chinese palaces,sunshine,lake,overlook,overlook,unreal engine,light effect,Dream,Greg Rutkowski,James Gurney,artstation", output_dir='demo')
结果
HuggingFace
环境
bash
pip install diffusers transformers accelerate scipy safetensors
代码
bash
import torch
from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
def show(image_path):
# 使用 Matplotlib 加载图片文件
image = mpimg.imread(image_path)
# 显示图片
plt.imshow(image)
plt.axis('off') # 关闭坐标轴
plt.show()
model_id = "stabilityai/stable-diffusion-2-1"
# Use the DPMSolverMultistepScheduler (DPM-Solver++) scheduler here instead
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
pipe = pipe.to("cuda")
prompt = "clouds surround the mountains and Chinese palaces,sunshine,lake,overlook,overlook,unreal engine,light effect,Dream,Greg Rutkowski,James Gurney,artstation"
image = pipe(prompt).images[0]
image.save("test.png")
show('test.png')