一、安裝GPU驅(qū)動
1.1 購買云服務(wù)器
在騰訊云選購一臺GPU型的服務(wù)器(最好的境外的,境外的服務(wù)器后面關(guān)于鏡像加速的步驟都可以跳過)
購買成功之后騰訊的站內(nèi)信會發(fā)送初始的密碼給你,登錄成功后會自動幫我們安裝相關(guān)的驅(qū)動,提示整個過程大概需要20分鐘,我這里大概5分鐘就下完了。
This script will download and install the GPU driver, CUDA, CUDNN library automatically
安裝完成之后
// 將CUDA的bin目錄添加到PATH環(huán)境變量中
echo 'export PATH=/usr/local/cuda/bin${PATH:+:${PATH}}' >> ~/.bashrc
source ~/.bashrc
驗證,cuda版本號
[root@VM-0-7-centos local]# nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2021 NVIDIA Corporation
Built on Mon_Oct_11_21:27:02_PDT_2021
Cuda compilation tools, release 11.4, V11.4.152
Build cuda_11.4.r11.4/compiler.30521435_0
查看GPU版本驅(qū)動信息
[root@VM-0-7-centos ~]# nvidia-smi
Sun Jun 25 19:16:16 2023
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 470.82.01 Driver Version: 470.82.01 CUDA Version: 11.4 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|===============================+======================+======================|
| 0 Tesla T4 On | 00000000:00:08.0 Off | 0 |
| N/A 32C P8 11W / 70W | 0MiB / 15109MiB | 0% Default |
| | | N/A |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=============================================================================|
| No running processes found |
+-----------------------------------------------------------------------------+
1.2 安裝最新驅(qū)動
如果后續(xù)要訓(xùn)練lora模型(沒有這個需求的跳過這步),建議直接重裝系統(tǒng),重裝的時候不要勾選后臺自動安裝GPU驅(qū)動 ,我們自己安裝能夠訓(xùn)練lora模型的GPU驅(qū)動版本
驅(qū)動下載地址: https://developer.nvidia.com/cuda-downloads
wget https://developer.download.nvidia.com/compute/cuda/12.1.1/local_installers/cuda-repo-rhel7-12-1-local-12.1.1_530.30.02-1.x86_64.rpm
sudo rpm -i cuda-repo-rhel7-12-1-local-12.1.1_530.30.02-1.x86_64.rpm
sudo yum clean all
sudo yum -y install nvidia-driver-latest-dkms
sudo yum -y install cuda
// 將CUDA的bin目錄添加到PATH環(huán)境變量中
echo 'export PATH=/usr/local/cuda/bin${PATH:+:${PATH}}' >> ~/.bashrc
source ~/.bashrc
安裝完之后檢查cuda版本號和驅(qū)動信息
查看cuda版本信息
[root@VM-0-7-centos ~]# nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2023 NVIDIA Corporation
Built on Mon_Apr__3_17:16:06_PDT_2023
Cuda compilation tools, release 12.1, V12.1.105
Build cuda_12.1.r12.1/compiler.32688072_0
查看驅(qū)動信息
[root@VM-0-7-centos ~]# nvidia-smi
Sun Jun 25 20:39:56 2023
+---------------------------------------------------------------------------------------+
| NVIDIA-SMI 530.30.02 Driver Version: 530.30.02 CUDA Version: 12.1 |
|-----------------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|=========================================+======================+======================|
| 0 Tesla T4 Off| 00000000:00:08.0 Off | 0 |
| N/A 32C P8 9W / 70W| 2MiB / 15360MiB | 0% Default |
| | | N/A |
+-----------------------------------------+----------------------+----------------------+
+---------------------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=======================================================================================|
| No running processes found |
+---------------------------------------------------------------------------------------+
二、安裝基礎(chǔ)環(huán)境
2.1 更新系統(tǒng)軟件包
更新系統(tǒng)軟件包可以使系統(tǒng)中的軟件和應(yīng)用程序保持最新狀態(tài),包括修復(fù)已知的漏洞、增強(qiáng)系統(tǒng)性能和穩(wěn)定性、提供新功能
sudo yum update
2.2 安裝依賴庫
yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel asciidoc gcc perl-ExtUtils-MakeMaker epel-release
2.3 安裝git2.5.1
git版本太低有些命令會運(yùn)行失敗
[root@VM-16-9-centos ~]# yum remove git
// 獲取git安裝包
[root@VM-16-9-centos ~]# wget https://www.kernel.org/pub/software/scm/git/git-2.15.1.tar.xz
[root@VM-16-9-centos ~]# tar -vxf git-2.15.1.tar.xz
[root@VM-16-9-centos ~]# cd git-2.15.1/
// 可以先執(zhí)行make clean
[root@VM-16-9-centos git-2.15.1]# make prefix=/usr/local/git all
[root@VM-16-9-centos git-2.15.1]# make prefix=/usr/local/git install
[root@VM-16-9-centos git-2.15.1]# echo "export PATH=$PATH:/usr/local/git/bin" >> /etc/profile
[root@VM-16-9-centos git-2.15.1]# source /etc/profile
// 查看是否安裝成功
[root@VM-16-9-centos git-2.15.1]# git --version
git version 2.15.1
2.4 安裝python3.10.6
最好是這個版本的,其他版本有可能出現(xiàn)報錯的情況
2.4.1 安裝依賴
[root@VM-16-9-centos ~]# yum -y install openssl-devel libffi-devel bzip2-devel
[root@VM-16-9-centos ~]# yum -y groupinstall "Development Tools"
2.4.2 安裝openssl
[root@VM-16-9-centos ~]# wget https://www.openssl.org/source/openssl-1.1.1q.tar.gz --no-check-certificate
[root@VM-16-9-centos ~]# tar xf openssl-1.1.1q.tar.gz && cd openssl-1.1.1q
[root@VM-16-9-centos openssl-1.1.1q]# ./config --prefix=/usr/local/openssl-1.1.1
[root@VM-16-9-centos openssl-1.1.1q]# make && make install
2.4.3 安裝Python-3.10.6
如果系統(tǒng)中有其他python3的版本先卸載,沒有可以跳過這步,可用python3 --version查看
// 使用 yum 命令卸載 Python 3.6.8:
sudo yum remove python36
// 刪除 Python 3.6.8 的相關(guān)文件和目錄:
sudo rm -rf /usr/local/lib/python3.6/
sudo rm -rf /usr/local/bin/python3.6
sudo rm -rf /usr/local/bin/pip3.6
安裝3.10.6版本
[root@VM-16-9-centos ~]# wget https://www.python.org/ftp/python/3.10.6/Python-3.10.6.tgz
[root@VM-16-9-centos ~]# tar xzf Python-3.10.6.tgz && cd Python-3.10.6
[root@VM-16-9-centos Python-3.10.6]# ./configure --enable-optimizations --with-openssl=/usr/local/openssl-1.1.1 --with-openssl-rpath=auto
[root@VM-16-9-centos Python-3.10.6]# make altinstall
2.4.4 添加軟連接
[root@VM-16-9-centos Python-3.10.6]# cd /usr/bin/
[root@VM-16-9-centos bin]# mv /usr/bin/python /usr/bin/python.bak
[root@VM-16-9-centos bin]# ln -s /usr/local/bin/python3.10 /usr/bin/python3
[root@VM-16-9-centos bin]# ln -s /usr/local/bin/python3.10 /usr/bin/python
[root@VM-16-9-centos bin]# python --version
Python 3.10.6
2.4.5 更改yum的配置
yum的命令使用python2寫的,如果不更改yum命令用不了
[root@VM-16-9-centos ~]# vim /usr/bin/yum
把第一行的python改成python2
[root@VM-16-9-centos ~]# vim /usr/libexec/urlgrabber-ext-down
把第一行的python改成python2
三、項目運(yùn)行
3.1 下載stable-diffusion-webui源碼
前面加https://ghproxy.com是為了加速,不然國內(nèi)服務(wù)器很容易下載失敗
// 下載源碼
git clone https://ghproxy.com/https://github.com/AUTOMATIC1111/stable-diffusion-webui.git
3.2 下載底層模型
下載默認(rèn)的 1.5模型,將下載后的文件放到/root/stable-diffusion-webui/models/Stable-diffusion路徑下,可以用Xftp傳輸,服務(wù)器在境外的話直接通過wget命令下載
https://huggingface.co/runwayml/stable-diffusion-v1-5/resolve/main/v1-5-pruned-emaonly.safetensors
也可以用chilloutmix_NiPrunedFp32Fix模型,這個模型一般用來畫真人模型
wget https://huggingface.co/naonovn/chilloutmix_NiPrunedFp32Fix/resolve/main/chilloutmix_NiPrunedFp32Fix.safetensors
Xftp免費版下載地址
https://www.xshell.com/zh/free-for-home-school/
3.3 git鏡像加速
境外服務(wù)器直接跳過這步,修改stable-diffusion-webui目錄下的requirements.txt和requirements_versions.txt文件,將其中的gradio==3.31.0改成3.30.0,我在下載的時候31版本一直下載不了,(親測服務(wù)器在境外的時候可以下載31版本,可以先下載31版本試試看)
gradio==3.30.0
修改stable-diffusion-webui/modules目錄下的launch_utils.py文件,在https://github.com前面加上https://ghproxy.com/,不然國內(nèi)服務(wù)器很容易下載失敗
3.4 設(shè)置管理員運(yùn)行
修改webui.sh,把can_run_as_root=0改成1,不然root賬號無法運(yùn)行
3.5 禁用SSL證書
修改launch.py文件,添加代碼禁用SSL證書驗證,防止后續(xù)生成圖片的時候出現(xiàn)證書問題
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
3.6 創(chuàng)建虛擬環(huán)境
// 安裝TCMalloc 不然后面運(yùn)行webui.sh時有可能Cannot locate TCMalloc (improves CPU memory usage)
[root@VM-16-9-centos ~]# yum install gperftools
// 創(chuàng)建虛擬環(huán)境
[root@VM-16-9-centos ~]# cd stable-diffusion-webui/
// 創(chuàng)建一個名為venv的虛擬環(huán)境,后面那個venv為虛擬環(huán)境名稱
[root@VM-16-9-centos stable-diffusion-webui]# /usr/local/bin/python3.10 -m venv venv
// 進(jìn)入虛擬環(huán)境
[root@VM-16-9-centos stable-diffusion-webui]# source venv/bin/activate
// 修改pip源,境外服務(wù)器可以跳過
(venv) [root@VM-16-9-centos stable-diffusion-webui]# pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
Writing to /root/.config/pip/pip.conf
(venv) [root@VM-16-9-centos stable-diffusion-webui]# pip config set install.trusted-host mirrors.aliyun.com
Writing to /root/.config/pip/pip.conf
3.7 下載項目依賴
在虛擬環(huán)境中安裝項目所需依賴,這個過程比較慢,慢慢等就行
// 進(jìn)入虛擬環(huán)境,可以通過deactivate命令退出虛擬環(huán)境
[root@VM-16-9-centos stable-diffusion-webui]# source venv/bin/activate
// 先更新pip不然有可能報錯
(venv) [root@VM-16-9-centos stable-diffusion-webui]# pip install --upgrade pip
(venv) [root@VM-16-9-centos stable-diffusion-webui]# pip3.10 install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
(venv) [root@VM-16-9-centos stable-diffusion-webui]# pip3.10 install -r requirements_versions.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
3.8 項目運(yùn)行
最后下載完之后運(yùn)行webui.sh就行了,–no-gradio-queue是為了解決在訪問頁面的時候無法生成圖片,提示Something went wrong Connection errored out.錯誤,–gradio-auth username:password當(dāng)中的username替換成你設(shè)置的賬號,password為你登錄時的密碼
./webui.sh --no-gradio-queue --gradio-auth username:password
如果要在后臺運(yùn)行采用下面的命令運(yùn)行
nohup ./webui.sh --no-gradio-queue --gradio-auth username:password &
四、配置nginx反向代理
http://127.0.0.1:7860我們是無法直接訪問的,這里我們通過nginx的反向代理將請求轉(zhuǎn)發(fā)到7860端口,當(dāng)然你也可以在執(zhí)行./webui.sh 的時候加上 --share,這樣會自動生成公網(wǎng)可以訪問的鏈接
4.1 安裝nginx
sudo yum install nginx
4.2 配置 Nginx 代理
在 /etc/nginx/conf.d/ 目錄下創(chuàng)建一個新的配置文件,例如 proxy.conf,并將以下內(nèi)容寫入該文件:
server {
listen 8080;
server_name 公網(wǎng)IP;
location / {
proxy_pass http://127.0.0.1:7860;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
測試配置文件是否正確:
sudo nginx -t
重啟 Nginx 服務(wù):
sudo systemctl restart nginx
4.3 公網(wǎng)訪問
瀏覽器訪問公網(wǎng)Ip:8080開始生成圖片吧
五、安裝stable diffusion webui插件
5.1 安裝漢化插件
中文語言包下載地址
https://github.com/VinsonLaro/stable-diffusion-webui-chinese
要是安裝失敗直接進(jìn)入/root/stable-diffusion-webui/extensions文件夾下手動下載
git clone https://github.com/VinsonLaro/stable-diffusion-webui-chinese
重啟界面
設(shè)置中文
重啟UI之后就變成中文了
5.2 安裝controlnet
這款插件允許 Web UI 將ControlNet添加到原始 Stable Diffusion 模型中以生成可控圖像,插件安裝地址
https://github.com/Mikubill/sd-webui-controlnet
下載完之后安裝pycairo
sudo yum install cairo cairo-devel
pip install pycairo
六、問題總結(jié)
6.1 運(yùn)行webui.sh文件報錯,報錯信息如下,這個主要是git版本太低導(dǎo)致的
Using TCMalloc: libtcmalloc.so.4
Python 3.10.6 (main, Jun 4 2023, 15:28:08) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]
Version: v1.3.1
Commit hash: b6af0a3809ea869fb180633f9affcae4b199ffcf
Installing clip
Installing open_clip
Cloning Stable Diffusion into /root/stable-diffusion-webui/repositories/stable-diffusion-stability-ai...
Traceback (most recent call last):
File "/root/stable-diffusion-webui/launch.py", line 38, in <module>
main()
File "/root/stable-diffusion-webui/launch.py", line 29, in main
prepare_environment()
File "/root/stable-diffusion-webui/modules/launch_utils.py", line 288, in prepare_environment
git_clone(stable_diffusion_repo, repo_dir('stable-diffusion-stability-ai'), "Stable Diffusion", stable_diffusion_commit_hash)
File "/root/stable-diffusion-webui/modules/launch_utils.py", line 150, in git_clone
run(f'"{git}" -C "{dir}" checkout {commithash}', None, "Couldn't checkout {name}'s hash: {commithash}")
File "/root/stable-diffusion-webui/modules/launch_utils.py", line 101, in run
raise RuntimeError("\n".join(error_bits))
RuntimeError: Couldn't checkout {name}'s hash: {commithash}.
Command: "git" -C "/root/stable-diffusion-webui/repositories/stable-diffusion-stability-ai" checkout cf1d67a6fd5ea1aa600c4df58e5b47da45f6bdbf
Error code: 129
stderr: Unknown option: -C
usage: git [--version] [--help] [-c name=value]
[--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
[-p|--paginate|--no-pager] [--no-replace-objects] [--bare]
[--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
<command> [<args>]
解決方案:
進(jìn)入創(chuàng)建的虛擬環(huán)境中利用git --version命令查看git版本,如果是1.幾的版本參考前面git安裝步驟卸載重新安裝git2.5.1
6.2 訪問公網(wǎng)地址在生成圖片的時候提示以下信息
Downloading: "https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/codeformer.pth" to /root/stable-diffusion-webui/models/Codeformer/codeformer-v0.1.0.pth| 58/58 [02:01<00:00, 3.02s/it]
Unable to load codeformer model.
這個主要是國內(nèi)服務(wù)器訪問github太慢導(dǎo)致文件下載失敗,境外服務(wù)器一般不會有這個問題,我們可以復(fù)制這個鏈接手動下載然后上傳到/root/stable-diffusion-webui/models/Codeformer目錄下,并重命名為codeformer-v0.1.0.pth
6.3 生成圖片的時候碰到ssl證書問題,主要報錯信息如下
urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate(_ssl.c:997)>
解決方案:在根目錄找到launch.py,添加代碼禁用SSL證書驗證,這種方法會跳過SSL證書驗證,但存在一定的安全風(fēng)險。
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
6.4 在訪問webui頁面的時候提示 Something went wrong Connection errored out.錯誤
解決方案:在運(yùn)行./webui.sh命令時加上–no-gradio-queue
6.5 如果你的服務(wù)器在國外,訪問webui頁面的時候關(guān)掉你的代理上網(wǎng)方式,否則有可能報錯
最后分享兩個stable diffusion模型的下載網(wǎng)站
- https://huggingface.co/
- https://civitai.com/
分享一個國風(fēng)3.3模型生成的圖片參數(shù)文章來源:http://www.zghlxwxcb.cn/news/detail-538931.html
best quality, masterpiece, highres, 1girl,blush,(seductive smile:0.8),star-shaped pupils,china hanfu,hair ornament,necklace, jewelry,Beautiful face,upon_body, tyndall effect,photorealistic, dark studio, rim lighting, two tone lighting,(high detailed skin:1.2), 8k uhd, dslr, soft lighting, high quality, volumetric lighting, candid, Photograph, high resolution, 4k, 8k, Bokeh
Negative prompt: (((simple background))),monochrome ,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, lowres, bad anatomy, bad hands, text, error, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts, signature, watermark, username, blurry, ugly,pregnant,vore,duplicate,morbid,mut ilated,tran nsexual, hermaphrodite,long neck,mutated hands,poorly drawn hands,poorly drawn face,mutation,deformed,blurry,bad anatomy,bad proportions,malformed limbs,extra limbs,cloned face,disfigured,gross proportions, (((missing arms))),(((missing legs))), (((extra arms))),(((extra legs))),pubic hair, plump,bad legs,error legs,username,blurry,bad feet
ENSD: 31337, Size: 640x1024, Seed: 3556647833, Model: gf_anylora_gf3.2_anylora1.2, Steps: 30, Sampler: Euler a, CFG scale: 9, Clip skip: 2, Model hash: 4078eb4174, Hires steps: 64, Hires upscale: 2, Hires upscaler: R-ESRGAN 4x+ Anime6B, Denoising strength: 0
文章來源地址http://www.zghlxwxcb.cn/news/detail-538931.html
到了這里,關(guān)于AI繪畫指南:在CentOS7中安裝Stable Diffusion WebUI的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!