功能
- stable-diffusion(文本生成图片)
- PaddleHub,HuggingFace两种调用方式
PaddleHub
环境
pip install paddlepaddle-gpu
pip install paddlehub
代码
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
环境
pip install diffusers transformers accelerate scipy safetensors
代码
import torch
from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler
import matplotlib.pyplot as plt
import matplotlib.image as mpimgdef 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')
结果