本文主要是介绍AIGC-stable-diffusion(文本生成图片)+PaddleHub/HuggingFace,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
功能
- 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')
结果
这篇关于AIGC-stable-diffusion(文本生成图片)+PaddleHub/HuggingFace的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!