当前位置:   article > 正文

【大模型系列 03】Stable Diffusion 2.1 for PyTorch 昇腾迁移_stable diffusion 昇腾

stable diffusion 昇腾

源码链接

https://gitee.com/ascend/ModelZoo-PyTorch/blob/master/PyTorch/built-in/diffusion/stablediffusion-2.1

StableDiffusion-2.1 for PyTorch

概述

简述

StableDiffusion 是 StabilityAI公司于2022年提出的图片生成的预训练模型,论文和代码均已开源,下游任务包括文生图、图生图、图片压缩等等

  • 参考实现:

    url=https://github.com/Stability-AI/stablediffusion 
    commit_id=535d370de05c0e55a6bcbdd32933e549e125b946
    
    • 1
    • 2
  • 适配昇腾 AI 处理器的实现:

    url=https://gitee.com/ascend/ModelZoo-PyTorch.git
    code_path=PyTorch/built-in/diffusion/stablediffusion-2.1
    
    • 1
    • 2

准备环境

准备环境

  • 当前模型支持的 PyTorch 版本和已知三方库依赖如下表所示。

    表 1 版本支持表

    Torch_Version三方库依赖版本
    PyTorch 1.11torchvision== 0.12.0; transformers==4.19.2
  • 环境准备指导。

    请参考《Pytorch框架训练环境准备》。

  • 安装依赖。

    在模型源码包根目录下执行命令,安装模型对应PyTorch版本需要的依赖。

    
    pip install -r requirements.txt
    
    • 1
    • 2

图像生成任务推理

  • 下载预训练的ckpt文件

  • 先source环境变量:

    source ./test/env_npu.sh
    
    • 1
  • 执行图像生成脚本:

 python scripts/txt2img.py \
        --prompt "a professional photograph of an astronaut riding a horse" \
        --ckpt /xxx/xxx.ckpt \
        --config configs/stable-diffusion/v2-inference-v.yaml \
        --H 768 \
        --W 768 \
        --device_id 4 \
        --precision full \
        --bf16 \
        --n_samples 1 \
        --n_iter 1 \
        --dpm \
        --steps 15 

# 或者使用shell脚本启动:
bash ./test/run_infer_full_1p.sh  #单卡推理,请将脚本里的ckpt权重路径更换成实际的路径

# 注:生成的图像在outputs目录下 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 参数说明如下:
--ckpt_path #模型文件目录,需要指定到具体的ckpt文件

--n_samples #每次生成图片的batch数

--n_iter #每个prompt生成的图片数,每个prompt生成的图片总数为:(n_samples * n_iter)

--devive #除`cpu`外,默认为`cuda`

--device_id #运行的设备ID
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

版本说明

变更

FAQ

  1. 该模型当前只支持在线推理,不支持训练。

  2. requests.exceptions.SSLError:HttpSConnectionPool(host='huggingface.co', port=443)错误

    可以修改当前环境的requests包下的sessions.py文件的684行,增加kwargs["verify"] = False,一般路径在conda_path/envs/conda_name/lib/python*/site-packages/requests/sessions.py,查看requests包的所在路径可以通过pip show requests查看

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

闽ICP备14008679号