当前位置:首页>文章>Stable Diffusion>SD指南>colab运行stable diffusion以及本地部署方法

colab运行stable diffusion以及本地部署方法

colab不需要下载东西, 网页运行;

本地部署就是本地,没啥好介绍的.

也是跟着网上一些大佬分享的方法, 自己各种坑踩了一遍记录一下详尽的流程. 一开始是给鸦鸦群里朋友们做小指南的hhh搬运一下~

一、colab运行stable diffusion

0.准备: 获取Tokens和SD访问权限

前往huggingface主页注册账号,验证邮箱:https://huggingface.co/

*这里国内网路ip有可能出现400 captcha failed (就是i am not robot那个), 因为google验证被墙了,可以自行搜寻recaptcha的镜像添加使用教程. 还有一个邪道方法是用流量注册.(?)

点击下面页面获取new tokens, Role项改为write:https://huggingface.co/settings/tokens

生成后复制保存.

进入下面页面, 获取SD的model使用权限:

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

第一块勾选已阅读, 点击Access reppsitory就可以了.

1.Setup

前往stable diffusion的google colab page:https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers/stable_diffusion.ipynb

打开后,保存副本至自己的账号云端并打开. (需要注册)

在菜单栏-代码执行程序-更改运行时的类型, 修改硬件加速器为GPU.运行前三个代码块:

!nvidia-smi
!pip install diffusers==0.2.4

!pip install transformers scipy ftfy

!pip install “ipywidgets>=7,<8”
from google.colab import output

output.enable_custom_widget_manager()

运行这部分最后一个代码块:

fromhuggingface_hubimportnotebook_login

notebook_login()

随后, 提示你输入Token. 把先前生成的Token填入, 显示login successful即可.

2.开始你的调教之旅, Run the pipeline

依次运行前两格, 这可能需要一些时间.

importtorch
from diffusers import StableDiffusionPipeline

# make sure youre logged in with `huggingface-cli login`
pipe = StableDiffusionPipeline.from_pretrained(“CompVis/stable-diffusion-v1-4”, revision=“fp16”, torch_dtype=torch.float16, use_auth_token=True)
pipe=pipe.to(“cuda”)

*有兴趣的朋友可以看看每一代码块之前是如何一步步引导说明的, 当然也包括了一些参数设置是发挥了什么作用~ 可以玩得更加得心应手(●◡●)

来到第三块, 你可以修改一下prompt内容 (也就是关键词填写) 再run. 如果想生成不同的图片玩儿, 使用这一块已经足够了.

fromtorchimportautocast

#下一行修改
prompt = “a photograph of an astronaut riding a horse”
with autocast(“cuda”):
image = pipe(prompt)[“sample”][0]

#图片保存
image.save(f“astronaut_rides_horse.png”)

#或者你可以直接在这个notebook里显示查看.
image

下一块加入了Seed, 当你规定一个具体的seed值时, 每一次的图像将是一样的. 所以接着, notebook里告诉你, 调整一些参数, 观察发生了什么变化.

importtorch

generator = torch.Generator(“cuda”).manual_seed(1024)

with autocast(“cuda”):
image = pipe(prompt, num_inference_steps=15,
generator=generator)[“sample”][0]
#显示
image

num_inference_steps越大, 一般而言效果越好.

后面还有同时生成几个的设置(row和col), 以及更改默认大小(512*512, )跟着它的提示修改相应参数就可以了.

二、本地部署stable diffusion

0.准备

打开github, 下载Stable Diffusion源码 (全部文件). 解压到目录 YOUR_PATHGitHub – CompVis/stable-diffusion点击下载anaconda. *最好不要安装在C盘, 但是空间大你随意. 要装python哦https://www.anaconda.com/打开anaconda powershell prompt. 输入:

conda install git

需要先授权, 再下载huggingface提供的模型(权重):https://huggingface.co/CompVis/stable-diffusion-v-1-4-original

sd-v1-4.ckpt(4G) sd-v1-4-full-ema.ckpt(7.4G)

二者择一即可. 放到一个目录 YOUR_MODEL (要用). 下载有点久,可以继续下面的.

1.环境搭建

进入之前的源码解压目录YOUR_PATH

cd YOUR_PATH
conda env create -f environment.yaml

*setup.py 这个文件在的目录就是你要cd的文件夹. 如果不熟悉操作的朋友, 可以一行一行改好了直接粘贴进去.

此时会很大可能会报错,如果出现 openAI/CLIP和taming-transformers这两个包的安装失败提示,继续输入命令:

git clone -q https://github.com/CompVis/taming-transformers.git YOUR_PATH\src\taming-transformers

git clone -q https://github.com/openai/CLIP.git YOUR_PATH\src\clip

保险起见, 之后进入YOUR_PATH\src中找到上面那两个文件夹,复制到:Anaconda安装目录 – anaconda\envs\ldm\Lib\site-packages中.激活环境

conda activate ldm
python setup.py install

你还需要继续安装几个重要的依赖包:

conda install pytorch torchvision -c pytorch
pip install transformers==4.19.2 diffusers invisible-watermark
pip install -e .

装pytorch时可能因为网路问题会很慢, 也有挺快的, 反正很玄学…

万事俱备, 连接你的模型.

之前下载好模型放的的目录为YOUR_MODEL 还记得吧?

依次输入命令:

mkdir -p models/ldm/stable-diffusion-v1/
ln -s <path/to/model.ckpt> models/ldm/stable-diffusion-v1/model.ckpt

第2行的<path/to/model.ckpt> 改为 YOUR_MODEL\下载模型的文件名.

比如: D:/123abc/sd-v1-4-full-ema.ckpt

这里需要花一两分钟等待, 不要着急 然后就可以开始使用啦!

之后运行还是缺包或者直接一步到位不想一个个安装的话, 根绝yaml更新环境配置, 绝对好用

conda env update -n ldm –file environment.yaml

2.快乐跑图

最少配置:

python scripts/txt2img.py –prompt “a painting of Mads Mikkelsen smoking”

–prompt “键入你的调教”

一些说明–ckpt ./stable-diffusion-main/models/ldm/sd-v1-4.ckpt 权重文件, 主页提到那4种, 我们用之前下载的那个模型就可以了. 所以这里默认不用改也不用加上这个关键词和seed不变时, 生成图片不变.outdir: 输出目录. 外面加引号””init-img: 如果一开始给了指定图片, 这是你放图片的地址.下面代码块的prompt里我删掉了50词的举例, 所以尽量具体地喂它, 或许可以生成更让你惊喜的图片.不熟悉指令的朋友可以观察一下格式: –xxxx(参数) 你的值

!!非常重要!!! 根据配置要设置好batch size(即n_samples=) 和图像分辨率大小

我本地3080ti 12g 生成默认大小(512*512) 不改batch size会超过显存, 改为1~2可以.

虽说是要1.5-2G的显存但是占用率一下子从0到了97%.

step不影响.

①text2img -文字描述生成图

python scripts/txt2img.py –prompt “a painting of Mads Mikkelsen smoking”
–plms
–outdir “output/”
–ckpt
–ddim_steps 100
–H 512
–W 512
–seed 8

②img+text 2 img- 图+描述生成图

python scripts/img2img.py –prompt “a painting of Mads Mikkelsen smoking”
–init-img ./input/yourpic.jpg
–strength 0.8
–outdir ./SD/output
–ckpt ./SD/models/ldm/sd-v1-4.ckpt
–ddim_steps 100

参数格式直接copy过来了, 可以看https://github.com/CompVis/stable-diffusion 主页的命令说明::>ω<::

usage: txt2img.py [-h] [–prompt [PROMPT]] [–outdir [OUTDIR]] [–skip_grid] [–skip_save] [–ddim_steps DDIM_STEPS] [–plms] [–laion400m] [–fixed_code] [–ddim_eta DDIM_ETA]
[–n_iter N_ITER] [–H H] [–W W] [–C C] [–f F] [–n_samples N_SAMPLES] [–n_rows N_ROWS] [–scale SCALE] [–from-file FROM_FILE] [–config CONFIG] [–ckpt CKPT]
[–seed SEED] [–precision {full,autocast}]

optional arguments:
-h, –help show this help message and exit
–prompt [PROMPT] the prompt to render
–outdir [OUTDIR] dir to write results to
–skip_grid do not save a grid, only individual samples. Helpful when evaluating lots of samples
–skip_save do not save individual samples. For speed measurements.
–ddim_steps DDIM_STEPS
number of ddim sampling steps
–plms use plms sampling
–laion400m uses the LAION400M model
–fixed_code if enabled, uses the same starting code across samples
–ddim_eta DDIM_ETA ddim eta (eta=0.0 corresponds to deterministic sampling
–n_iter N_ITER sample this often
–H H image height, in pixel space
–W W image width, in pixel space
–C C latent channels
–f F downsampling factor
–n_samples N_SAMPLES
how many samples to produce for each given prompt. A.k.a. batch size
–n_rows N_ROWS rows in the grid (default: n_samples)
–scale SCALE unconditional guidance scale: eps = eps(x, empty) + scale * (eps(x, cond) – eps(x, empty))
–from-file FROM_FILE
if specified, load prompts from this file
–config CONFIG path to config which constructs model
–ckpt CKPT path to checkpoint of model
–seed SEED the seed (for reproducible sampling)
–precision {full,autocast}
evaluate at this precision

声明:学AI网所有文章,如无特殊说明或标注,均为本站作者原创发布。任何个人或组织,在未征得作者同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若此作者内容侵犯了原著者的合法权益,可联系客服或提交工单进行处理。学AI网内用户所发表的所有内容及言论仅代表其本人,并不反映任何学AI网之意见及观点。

给TA充电
共{{data.count}}人
人已充电
SD指南

模型应用—使用Stable Diffusion UI手册

2023-5-17 23:42:49

SD指南

StableDiffusion模型资源探索食用指南

2023-5-17 23:43:02

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索