飞道的博客

基于 Huggingface Diffusers 的文本图像生成

432人阅读  评论(0)

需要下载 transformers 和 diffusers

pip install --upgrade diffusers
pip install --upgrade diffusers transformers

我的版本:

diffusers: 0.6.0
transformers: 4.23.1

注册Huggingface token

官网:https://huggingface.co/

  • 右上角 settings

  • 注册token,需要邮箱链接验证


huggingface-cli login

命令行输入:huggingface-cli login,使用刚刚的token登陆

huggingface-cli login

下载预训练模型

  • text2img抱抱脸预训练全部模型:

https://huggingface.co/CompVis

  • 英文

https://huggingface.co/CompVis/stable-diffusion-v1-1

  • 中文

https://huggingface.co/svjack/Stable-Diffusion-Pokemon-zh

以 stable-diffusion-v1-1 为例:

git lfs install
git lfs clone https://huggingface.co/CompVis/stable-diffusion-v1-1
# git lfs clone https://huggingface.co/svjack/Stable-Diffusion-Pokemon-zh

demo code

import torch
import transformers
from diffusers import StableDiffusionPipeline
# from torch import autocast
from torch.cuda.amp import autocast as autocast

# device = "cuda"
device = "cpu"

start_time = time.time()

# , torch_dtype=torch.float16, revision="fp16", use_auth_token=True
pipe = StableDiffusionPipeline.from_pretrained('/XXX/stable-diffusion-v1-1')
pipe = pipe.to(device)

end_time = time.time()
print('{:.0f}分 {:.0f}秒'.format((end_time - start_time) // 60, (end_time - start_time) % 60))

start_time = time.time()
# prompt = "a photo of an astronaut riding a horse on mars"
prompt = "a rabbit eating a carrot"
with autocast(device):
    # image = pipe(prompt, guidance_scale=7.5)["sample"][0]
    image = pipe(prompt).images[0]

image.save("astronaut_rides_horse.png")

end_time = time.time()
print('{:.0f}分 {:.0f}秒'.format((end_time - start_time) // 60, (end_time - start_time) % 60))

 

cpu i9 inference time:

0分 18秒
/opt/anaconda3/lib/python3.8/site-packages/torch/cuda/amp/autocast_mode.py:120: UserWarning: torch.cuda.amp.autocast only affects CUDA ops, but CUDA is not available.  Disabling.
  warnings.warn("torch.cuda.amp.autocast only affects CUDA ops, but CUDA is not available.  Disabling.")
100%|██████████| 51/51 [19:44<00:00, 23.22s/it]
20分 18秒

结果示例:



转载:https://blog.csdn.net/qq_42363032/article/details/127637196
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场