找回密码
 立即注册→加入我们

QQ登录

只需一步,快速开始

搜索
热搜: 下载 VB C 实现 编写
查看: 2253|回复: 6

【AI】Windows 10 上搭建 stable-diffusion-webui 实现 AI 作画

[复制链接]

1111

主题

1651

回帖

7万

积分

用户组: 管理员

一只技术宅

UID
1
精华
244
威望
743 点
宅币
24239 个
贡献
46222 次
宅之契约
0 份
在线时间
2297 小时
注册时间
2014-1-26
发表于 2022-12-9 13:01:21 | 显示全部楼层 |阅读模式

欢迎访问技术宅的结界,请注册或者登录吧。

您需要 登录 才可以下载或查看,没有账号?立即注册→加入我们

×

Windows 10 搭建 stable-diffusion-webui 实现 AI 作画

传说中的 “AI作画取代画师职业” 就是使用 stable-diffusion 跑一个训练好了的针对二次元画风的神经网络来进行作画。
输入一个正面提示文本,一个负面提示文本,其它参数不用动,基本就能画出你要的内容。
当然毕竟是 AI 经常也会画出一些让人感到可怕和反胃的奇怪内容,所以使用的时候要注意。

准备

在准备搭建前,你需要这几个东西:

  • 梯子
  • NVIDIA 显卡,显存至少 4GB(实测 RTX 2070 生成一张图需要大约 10 秒)以及最新显卡驱动。
  • cuDNN
  • zlibwapi.dll
  • stable-diffusion-webui 源码
  • Python 版本 >= 3.8 推荐 Python 3.10
  • git(会被 Python 调用,用于下载一堆东西)
  • 文本编辑器(修改 stable-diffusion-webui 的一些 python 脚本,使其知道要翻墙)
  • 网页浏览器(用来使用 WebUI、观察生成出来的图)(都行,推荐 Chrome、Mozilla 或者 Edge)

下载 cuDNN

从 NVIDIA 下载:

https://developer.nvidia.com/rdp/cudnn-download

打开后,提示 “NVIDIA Developer Program Membership Required” 意思就是你需要注册 NVIDIA 账号。已有账号的话 Login,没有账号则点 Join now 然后注册账号。注册账号的时候需要你填写一些信息,比如名字、电话、公司、国家。这块稍微填填,不必太详细。

join.png

登录进去后,它会提示你要下载 CUDA 10.x 还是 11.x。选择 11.x,然后下载 Local Installer for Windows (Zip),你会得到一个压缩包,备用。


cudnn.png
tk4download.png

我下载的文件名 cudnn-windows-x86_64-8.6.0.163_cuda11-archive.zip,注意中间那一串版本号内容可能和你下载的不一样。

如果从 NVIDIA 下载不下来,我这里提供一个百度云盘链接

cudnn-windows-x86_64-8.9.2.26_cuda11-archive.zip
链接:https://pan.baidu.com/s/118SdClqsPq8HbnxHjQbMkg?pwd=aa55
提取码:aa55

取得 zlibwapi.dll

根据 NVIDIA 的教程,zlibwapi.dll 从这里下载:
http://www.winimage.com/zLibDll/zlib123dllx64.zip

下载后得到一个压缩包,备用。

代码编辑器

此处推荐 Sublime Text,它虽然不免费,但是使用体验合理。也可以使用 VSCODE。
不推荐 Notepad++
安装代码编辑器后,再安装 git。如果你的 git 已经配置好了,可以跳过下面的安装 git 的步骤。

安装 Git

https://git-scm.com/downloads

打开后,选 Windows,下载 Download for Windows,下载后打开安装。


git.jpg

安装的时候,注意每一个步骤,正确选择你需要的选项:

  • 代码编辑器选择你常用的代码编辑器比如 Sublime Text
    • 如果这一步你没处理的话,它有可能给你使用 Vim。如果你不知道什么是 Vim,那我可以告诉你:这玩意儿味道冲,你肯定不会用,甚至还能吓死你。
  • Adjusting the name of the initial branch in new repositories
    • 此处指的是新 repo 的初始分支的名字,以前是 master,现在据说 master这个词不太正确,要用 main
      此处可以选“Let Git decide”
  • Adjusting your PATH environment
    • 此处问的是你打算如何在命令行使用 git。为了确保 Python 脚本能够使用到 git,必须保证 git 在 PATH 里。
      此处要选“Git from the command line and also from 3rd-party software”
  • Choosing the SSH executable
    • 此处选择“Use bundled OpenSSH”
  • Choosing HTTPS transport backend
    • 一般选择 Use the OpenSSL library。这里选的是 git 使用的 HTTPS 的传输后端。
  • Configuring the line ending conversions
    • 这里控制 git 对行尾的转换。
      此处选择 Checkout as-is, commit as-is。
  • Configuring the terminal emulator to use with Git Bash
    • 此处选 Use MinTTY (the default terminal of MSYS2)
      如果选错了那就傻逼了,你的 cmd 就会变成四不像,一堆 *nix 命令在里面……而且还是用 exe 模拟的,给整怕了。
  • Choose the default behavior of git pull
    • 此处按自己的需求选择,不知道怎么选的话选默认的那个。
  • Choose a credential helper
    • 选 Git Credential Manager,以后用的时候可以省得输密码。
  • Configuring extra options
    • 勾选 Enable file system caching 和 Enable symbolic links
  • Configuring experimental options
    • 一个也别勾选,否则坑死你。

安装 Python

进 Python 官方下载页面:(此处给的链接是 3.10.9)
https://www.python.org/downloads/release/python-3109/

打开后,滚动条拉到最底部,找到 Windows installer (64-bit) 点开下载。

注意 安装的时候勾选 “Add python.exe to PATH”。


python38.PNG

取得 stable-diffusion-webui 源码

https://github.com/AUTOMATIC1111/stable-diffusion-webui
下载到本地。
这个地方考虑到它自己会运行 git pull 进行一个 repo 的自动更新,你如果用 git clone 的方式下载的话,它就会自动更新。

配置启动环境和脚本

下载 stable-diffusion-webui 后,注意确保路径里没有中文和空格,然后进入 stable-diffusion-webui 的文件夹。
正常情况下,你会看到它里面有很多的文件和文件夹,比如 webui-user.bat 这个启动脚本就是接下来需要修改的。


webui.PNG

将 cuDNN 的 bin 文件夹加入启动脚本PATH

必须经过这一步之后,才能运行 Python 启动脚本,否则你安装的 torch 将无法使用 GPU。

找到之前下载的 cuDNN 的压缩包,将其解压到 stable-diffusion-webui 的文件夹里。解压后,进入它的文件夹 (我的是 cudnn-windows-x86_64-8.6.0.163_cuda11-archive),找到 bin 文件夹,把之前下载好的 zlibwapi.dll 放进去。


zlibw.PNG

注意这个 bin 文件夹需要在 PATH 里,但是不需要修改 Windows 的全局 PATH ,只需要在 webui-user.bat 这个启动脚本里设置 PATH 即可,方法如下:

回到 stable-diffusion-webui 文件夹,找到 webui-user.bat 文件,右键、编辑(或者拖拽到一个新打开的记事本窗口里),它的内容应该是这样的:

@echo off

set PYTHON=
set GIT=
set VENV_DIR=
set COMMANDLINE_ARGS=

call webui.bat

在这个地方进行修改,使 cuDNN 的 bin 文件夹也要在 PATH 里(cuDNN ),我的代码如下:

@echo off

set PYTHON=
set GIT=
set VENV_DIR=
set COMMANDLINE_ARGS=

set PATH=%~dp0\cudnn-windows-x86_64-8.4.1.50_cuda11.6-archive\bin;%PATH%

call webui.bat

需要注意 set PATH 那一行的 cudnn 路径需要按照你下载的 cuDNN 版本来。

启用梯子

这块如果你能自由访问 github、google 等网站,那你是不需要梯子的。

如果你的梯子是 VPN 方式的,直接开启就好,不需要配置启动脚本。

而如果你的梯子是“代理”、“系统代理”模式的,那你的梯子应该是本地开个端口提供 socks5 服务的那种,此时你需要配置你的环境、修改启动脚本使其能够使用上梯子。此处假设你的梯子开的端口是 1080。

注意 如果你的梯子是按流量算的,搭建环境 需要下载至少 6 GB 的内容(包括 4 GB 的神经网络,torch,以及一堆 python 软件包等)

这块会用到梯子的命令有两个,一个是 git,一个是 python。其中,python 可以通过在命令行的结尾指定 --proxy=socks5h://localhost:1080 来使用梯子,但是必须先安装 pysocks 这个包。而 git 的设置就比较坑了:python 在调用 git 进行 clone 操作的时候,并不会把 python 它自己的梯子选项传递给 git,导致 git 下载失败。

设置 git 使用梯子

打开 CMD(git bash 也行),运行如下命令:

git config --global http.proxy localhost:1080
git config --global https.proxy localhost:1080

设置 Python 使用梯子

  • 先安装 pysocks

使用你的代码编辑器打开 launch.py 找到如下的代码:

if not is_installed("torch") or not is_installed("torchvision"):
    run(f'"{python}" -m {torch_command}', "Installing torch and torchvision", "Couldn't install torch")

这个代码是给虚拟环境 venv 的 Python 安装 torch,算是这个脚本的第一个安装软件的操作了,但是你需要在这个的前面插入代码检测 pysocks 是否安装,因此这样写:

if not is_installed("pysocks"):
    run(f'"{python}" -m pip install pysocks', "Installing pysocks", "Couldn't install pysocks")

if not is_installed("torch") or not is_installed("torchvision"):
    run(f'"{python}" -m {torch_command} --proxy=socks5h://localhost:1080', "Installing torch and torchvision", "Couldn't install torch")

这样就确保 pysocks 装上了,并且安装 torch 的过程中会使用梯子。但是还有别的包也要走梯子安装。代码往上翻,找到这个代码:

def run_pip(args, desc=None):
    index_url_line = f' --index-url {index_url}' if index_url != '' else ''
    return run(f'"{python}" -m pip {args} --prefer-binary{index_url_line}', desc=f"Installing {desc}", errdesc=f"Couldn't install {desc}")

这块,还是一样给它添加一个 --proxy=socks5h://localhost:1080 就可以使用梯子了:

def run_pip(args, desc=None):
    index_url_line = f' --index-url {index_url}' if index_url != '' else ''
    return run(f'"{python}" -m pip {args} --prefer-binary{index_url_line} --proxy=socks5h://localhost:1080', desc=f"Installing {desc}", errdesc=f"Couldn't install {desc}")

保存关闭。

初次启动运行脚本。

双击运行 webui-user.bat,它开始配置 Python 虚拟环境,安装、下载 torch 等软件,然后不出意外它会报错。

No checkpoint found. When searching for checkpoints, looked at:
 - file xxxxx\stable-diffusion-webui\model.ckpt
 - directory xxxxx\stable-diffusion-webui\models\Stable-diffusion
Can't run without a checkpoint. Find and place a .ckpt file into any of those locations. The program will exit.

报这个错是因为它要下载神经网络检查点,但是它下载到错误的位置了。我这边在 C:\Users\0xAA55\Downloads 这个路径里找到了 model.ckpt 这个文件,就离谱。我有一个朋友则是发现这个文件出现在了 C:\Users\xxxxxx\Desktop 也就是桌面上。可以对 C 盘进行一个全盘搜文件,就找 model.ckpt 这个文件。如果实在找不到,说明它可能根本就没有下载成功。这边我提供一份我自己下载到的 model.ckpt 文件的百度盘链接:

链接:https://pan.baidu.com/s/1x2_gAKgXvSOfDQbMbNIekQ?pwd=ckpt
提取码:ckpt
下载后解压。

找到(或者下载并解压了) model.ckpt 文件后,把它放到 stable-diffusion-webui 文件夹里,然后重新运行 webui-user.bat,它应该就能完成初始化和安装,然后打开一个本地端口(默认应该是 7860)、提供使用了。


good.png

使用浏览器打开本地页面 UI,进行 AI 绘图

确保 webui-user.bat 启动脚本正常运行后,用浏览器打开 http://localhost:7860 你就可以看到页面了,其中有上下两个文本框,一个是正面提示(Positive Prompt),一个是负面提示(Negative Prompt)。其中,正面提示是你想要看到的绘制的内容,负面提示是你不想看到的绘制的内容。此时你可以上网搜索 “AI 绘图关键词” 然后填写进去,点击右边的按钮进行一个 AI 绘图,并在浏览器上看到效果。


ai1.png

我常用的一个负面提示如下:

multiple breasts, lowres, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts, signature, watermark, username, blurry, bad feet, single color, ((((ugly)))), (((duplicate))), ((morbid)), ((mutilated)), (((tranny))), (((trans))), (((trannsexual))), (hermaphrodite), (out of frame), extra fingers, mutated hands, ((poorly drawn hands)), ((poorly drawn face)), (((mutation))), (((deformed))), ((ugly)), blurry, ((bad anatomy)), (((bad proportions))), ((extra limbs)), (((disfigured))), out of frame, (bad anatomy), gross proportions, (malformed limbs), ((missing arms)), ((missing legs)), (((extra arms))), (((extra legs))), mutated hands,(fused fingers), (too many fingers), (((long neck))), more than one people, more than one light, more than one head, more than one face, more than one body

回复

使用道具 举报

2

主题

10

回帖

44

积分

用户组: 初·技术宅

UID
5337
精华
0
威望
0 点
宅币
32 个
贡献
0 次
宅之契约
0 份
在线时间
3 小时
注册时间
2019-9-30
发表于 2022-12-10 20:55:52 | 显示全部楼层
现在各种运营商也在玩了,比如疼讯,那个美图修修
回复 赞! 靠!

使用道具 举报

307

主题

228

回帖

7341

积分

用户组: 真·技术宅

UID
2
精华
76
威望
291 点
宅币
5591 个
贡献
253 次
宅之契约
0 份
在线时间
948 小时
注册时间
2014-1-25
发表于 2022-12-12 17:51:55 | 显示全部楼层
使用梯子进行github的问题,这里有个小窍门可以避免使用梯子,那就是选择gitee转换,gitee支持将github项目转gitee
一般比较大的github项目,gitee上官方都会定期更新。如果真有gitee没收录的,也可以直接在gitee里创建项目,并在创建时指定github项目的url,最后只需要git clone这个gitee源即可
用gitee就别用梯子。gitee在国内clone的速度非常快。
回复 赞! 靠!

使用道具 举报

30

主题

211

回帖

2786

积分

用户组: 版主

UID
1821
精华
7
威望
69 点
宅币
2166 个
贡献
206 次
宅之契约
0 份
在线时间
481 小时
注册时间
2016-7-12
发表于 2022-12-12 21:04:15 | 显示全部楼层
分享一下反向提示词,训练的时候将生成的不满意的图重新进行预处理,并且要勾选 deepbooru 生成tag,并将需要的反面提示词添加的提示词文本里再进行训练,括号的作用可以提高优先级用的时候吧换行符替换成空格,不需要过滤的删除,偶尔会更新这个帖子
  1. (bad proportions),
  2. (bad anatomy),
  3. (bad shadow),
  4. (bad hands),
  5. bad feet,
  6. bad breasts,
  7. bad ears,
  8. bad animal ears,
  9. bad digit,
  10. bad shoes,
  11. bad gloves,
  12. bad cum,
  13. bad hairs,
  14. bad face,
  15. bad eyes,
  16. bad nipples,
  17. bad knee,
  18. bad tails,
  19. bad mouth,
  20. bad tongue,
  21. bad pantie,
  22. bad camel toe,
  23. bad asshole,
  24. bad anus,
  25. bad pussy,
  26. bad crotch,
  27. bad crotch seam fused anus,
  28. bad thigh gap,
  29. bad collarbone,
  30. bad tentacles,
  31. bad clit,

  32. missing fingers,
  33. ((missing arms)),
  34. ((missing legs)),
  35. missing breasts,
  36. missing hand,
  37. missing ears,
  38. missing animal ears,
  39. missing limb,
  40. missing digit,
  41. missing feet,
  42. missing nipples,
  43. missing thighs,
  44. missing calf,
  45. missing asshole,
  46. missing thigh gap,
  47. missing collarbone,
  48. missing clit,

  49. (fused fingers),
  50. fused breasts,
  51. fused hand,
  52. fused ears,
  53. fused animal ears,
  54. fused digit,
  55. fused feet,
  56. fused shoes,
  57. fused gloves,
  58. fused cum,
  59. fused hairs,
  60. fused face,
  61. fused nipples,
  62. fused calf,
  63. fused mouth,
  64. fused pantie,
  65. fused cloth,
  66. fused asshole,
  67. fused pussy,
  68. fused anus,
  69. fused crotch,
  70. fused seam,
  71. fused thigh gap,
  72. fused collarbone,
  73. fused tentacles,
  74. fused clit,

  75. dirty face,
  76. dirty teeth,
  77. dirty pantie,

  78. (extra fingers),
  79. (extra limbs),
  80. (extra arms),
  81. (extra legs),
  82. extra breasts,
  83. extra ears,
  84. extra animal ears,
  85. extra digit,
  86. extra feet,
  87. extra shoes,
  88. extra eyes,
  89. extra thighs,
  90. extra calf,
  91. extra knee,

  92. more than one people,
  93. more than one light,
  94. more than one head,
  95. more than one face,
  96. more than one body,
  97. one hand with more than 5 fingers,
  98. one hand with more than 5 digit,
  99. one hand with less than 5 fingers,
  100. one hand with less than 5 digit,
  101. more than two shoes,
  102. more than 2 nipples,
  103. more than 1 lefthand,
  104. more than 1 righthand,
  105. more than 2 thighs,
  106. more than 2 legs,

  107. 2girls,
  108. multiple_girls,

  109. huge breasts,
  110. huge haunch,
  111. huge thighs,
  112. huge calf,

  113. disappearing arms,
  114. disappearing thing,
  115. disappearing calf,
  116. disappearing legs,

  117. (long neck),
  118. (too long fingers),
  119. too long tongue,
  120. (long body)
  121. long neck,
  122. long face,

  123. (poorly drawn) ,
  124. (poorly drawn hands),
  125. (poorly drawn face),
  126. poorly drawn breasts,
  127. poorly drawn ears,
  128. poorly drawn animal ears,
  129. poorly drawn legs,
  130. poorly drawn feet,
  131. poorly drawn shoes,
  132. poorly drawn gloves,
  133. poorly drawn cum,
  134. poorly drawn hairs,
  135. (fused eyes poorly drawn eyes),
  136. poorly drawn nipples,
  137. poorly drawn mouth,
  138. poorly drawn pantie,
  139. poorly drawn cloth,
  140. poorly drawn asshole,
  141. poorly drawn crotch,
  142. poorly drawn anus,
  143. poorly drawn pussy,
  144. poorly drawn crotch seam,
  145. poorly drawn thigh gap,
  146. poorly drawn tentacles,

  147. liquid body,
  148. liquid digit,
  149. liquid thigh gap,
  150. liquid collarbone,
  151. liquid tentacles,
  152. liquid clit,

  153. malformed,
  154. malformed hands,
  155. malformed feet,
  156. malformed limbs,

  157. tongue within mouth,
  158. big mouth,
  159. cracked mouth,
  160. liquid tongue,
  161. colorful tongue,
  162. black tongue,


  163. cloned face,
  164. big face,
  165. big muscles,
  166. uncoordinated body,
  167. unnatural body,

  168. multiple breasts,
  169. liquid breasts,
  170. heavy breasts,
  171. liquid ears,
  172. heavy ears,
  173. liquid animal ears,
  174. heavy animal ears,

  175. different nipples,
  176. black nipples,
  177. colorful nipples,

  178. 3D,
  179. 3D game,
  180. 3D game scene,
  181. 3D character,
  182. photo_(medium),
  183. worst quality,
  184. low quality,
  185. normal quality,
  186. jpeg artifacts,
  187. single color,
  188. out of frame,

  189. lowres,
  190. text,
  191. error,
  192. (tranny),
  193. (trans),
  194. (trannsexual),
  195. (hermaphrodite),
  196. (black-white),
  197. disfigured,
  198. anatomical nonsense,
  199. blurred,
  200. lowers,
  201. low res,
  202. UI,
  203. cropped,
  204. watermark,
  205. username,
  206. signature,
  207. ugly,
  208. cloned face,
  209. gross proportions,
  210. short arm,
  211. mutation,
  212. duplicate,
  213. morbid,
  214. mutilated,
  215. deformed,
  216. chromatic_aberration,

  217. ((blurry)),
  218. (blurry_foreground),
  219. (blurry_eyes),
  220. blurry_background,
  221. (blur_censor),
  222. (motion_blur),

  223. (High contrast),
  224. (stained_glass),

  225. (bent fingers),
  226. greyscale,
  227. halftone,
  228. honeycomb (pattern),
  229. yellow teeth,
  230. yellow face,
  231. thick lips,
  232. obesity,
  233. split tentacles,
  234. colorful clit,
  235. black clit,
  236. QR code,
  237. bar code,
  238. safety panties,
  239. safety knickers,
  240. beard,
  241. furry,
  242. pony,
  243. pubic hair,
  244. monochrome,
  245. mosaic,
  246. excrement,
  247. faces,
  248. shit,
  249. Futa,
  250. polka_dot,
  251. testis,
  252. (unconventional_media),
复制代码
回复 赞! 靠!

使用道具 举报

1111

主题

1651

回帖

7万

积分

用户组: 管理员

一只技术宅

UID
1
精华
244
威望
743 点
宅币
24239 个
贡献
46222 次
宅之契约
0 份
在线时间
2297 小时
注册时间
2014-1-26
 楼主| 发表于 2023-3-13 11:16:52 | 显示全部楼层
Python 3.11 竟然不行,torch 还没做支持。(2023/3/13)
所以说一般情况下我都会同时安装 Python 3.7 3.8 3.9 3.10 3.11
回复 赞! 靠!

使用道具 举报

3

主题

25

回帖

1952

积分

用户组: 管理员

UID
4313
精华
1
威望
35 点
宅币
1828 个
贡献
21 次
宅之契约
0 份
在线时间
227 小时
注册时间
2018-9-24
发表于 2023-3-13 11:31:07 | 显示全部楼层
0xAA55 发表于 2023-3-13 11:16
Python 3.11 竟然不行,torch 还没做支持。(2023/3/13)
所以说一般情况下我都会同时安装 Python 3.7 3.8  ...


截屏2023-03-13 11.29.09.png
支持了
pytorch 1.13.1 py3.11 linux_x86_64
但没完全支持 ,指 只有Linux版本
回复 赞! 靠!

使用道具 举报

QQ|Archiver|小黑屋|技术宅的结界 ( 滇ICP备16008837号 )|网站地图

GMT+8, 2024-4-24 05:26 , Processed in 0.052637 second(s), 35 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表