准备
硬件:有GPU的主机,显存6G+
系统:windows10 或者 ubuntu 20.04
软件: anaconda
1.下载安装anaconda
wget https://repo.anaconda.com/archive/Anaconda3-2022.10-Linux-x86_64.sh
sh Anaconda3-2022.10-Linux-x86_64.sh
验证是否安装成功
conda --version //输出 conda 4.5.11
2.创建虚拟环境
conda create -n diffusers python==3.9
3.安装需要的python库
source activate diffusers
pip install -r requirements.txt
pip install torch==2.0.0+cu117 torchvision==0.15.1+cu117 torchaudio==2.0.0+cu117 -f https://download.pytorch.org/whl/torch_stable.html
requirements.txt 内容如下
diffusers
transformers
accelerate
ftfy
spacy
filelock
charset-normalizer
tqdm
opencv-python
datasets
4.python脚本
from diffusers import StableDiffusionPipeline
import torch
import matplotlib.pyplot as plt
import cv2
import numpy as np
model_id = "runwayml/stable-diffusion-v1-5"
def generateImage(pipe, prompt):
image = pipe(prompt).images[0]
img_bgr = cv2.cvtColor(np.asarray(image), cv2.COLOR_RGB2BGR)
cv2.imshow('img_bgr', img_bgr)
cv2.waitKey(1000)
if __name__=='__main__':
print(torch.cuda.is_available())
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
pipe.safety_checker = lambda images, clip_input: (images, False)
pipe = pipe.to("cuda")
while True:
prompt= input('输入描述来生成图片:')
generateImage(pipe, prompt)
保存为stable_diffusion.py并执行
python stable_diffusion.py