当前位置:   article > 正文

AIGC-stable-diffusion(文本生成图片)+PaddleHub/HuggingFace

AIGC-stable-diffusion(文本生成图片)+PaddleHub/HuggingFace
功能
  1. stable-diffusion(文本生成图片)
  2. PaddleHub,HuggingFace两种调用方式
PaddleHub
环境
  1. pip install paddlepaddle-gpu
  2. pip install paddlehub
代码
  1. from PIL import Image
  2. import paddlehub as hub
  3. module = hub.Module(name='stable_diffusion')
  4. ## 保存在demo目录
  5. 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
代码
  1. import torch
  2. from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler
  3. import matplotlib.pyplot as plt
  4. import matplotlib.image as mpimg
  5. def show(image_path):
  6. # 使用 Matplotlib 加载图片文件
  7. image = mpimg.imread(image_path)
  8. # 显示图片
  9. plt.imshow(image)
  10. plt.axis('off') # 关闭坐标轴
  11. plt.show()
  12. model_id = "stabilityai/stable-diffusion-2-1"
  13. # Use the DPMSolverMultistepScheduler (DPM-Solver++) scheduler here instead
  14. pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
  15. pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
  16. pipe = pipe.to("cuda")
  17. prompt = "clouds surround the mountains and Chinese palaces,sunshine,lake,overlook,overlook,unreal engine,light effect,Dream,Greg Rutkowski,James Gurney,artstation"
  18. image = pipe(prompt).images[0]
  19. image.save("test.png")
  20. show('test.png')
结果

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/484587
推荐阅读
相关标签
  

闽ICP备14008679号