Stable Diffusion#
设置#
请按照这些 说明安装 skypilot 包。
运行
git clone https://github.com/skypilot-org/skypilot.git && cd examples/stable_diffusion
运行
sky launch -c stable-diffusion stable_diffusion_docker.yaml
运行
ssh -L 7860:localhost:7860 stable-diffusion
在浏览器中打开
http://localhost:7860/
。如果页面未加载,请等待几分钟让容器启动后再试。输入文本提示并点击“生成”。
完成后,运行
sky stop stable-diffusion
以停止虚拟机。要重启虚拟机,请重复步骤 3 和 4。
使用技巧#
由于 GPU 内存限制,请避免图像分辨率超过 900x900
您可以将“无分类器指导比例”(Classifier Free Guidance Scale)切换到更高的值,以增强对提示的遵循程度
以下是一些很好的文本提示示例(无分类器指导比例 = 7.5,采样步数 = 50)
“玩扑克的驴子”
“加州大学伯克利分校学生在笔记本电脑上写代码”
“漫威大战 DC”
“金门大桥上的柯基犬”
“沙漠高尔夫”
“印度麦当劳”
“埃隆·马斯克机器人”
“机械心脏”
“旧金山的蝙蝠侠”
“天空和云层中的未来城市”
“跳交谊舞的熊”
“瓦力终结者”
“迷幻优胜美地”
“说唱歌曲专辑封面”
“华尔街公牛牛仔竞技”
“我的世界里的特朗普”
包含的文件#
Dockerfile
# Note: this file is adapted from https://github.com/AbdBarho/stable-diffusion-webui-docker/blob/master/build/Dockerfile
# <<EOF heredoc was removed from RUN git clone https://github.com/hlky/sd-enable-textual-inversion.git line
# This was to fix an error when the instance parses the Dockerfile.
# syntax=docker/dockerfile:1
FROM continuumio/miniconda3:4.12.0
RUN conda install python=3.8.5 && conda clean -a -y
RUN conda install pytorch==1.11.0 torchvision==0.12.0 cudatoolkit=11.3 -c pytorch && conda clean -a -y
RUN git clone https://github.com/hlky/stable-diffusion.git && cd stable-diffusion && git reset --hard ff8c2d0b709f1e4180fb19fa5c27ec28c414cedd
RUN conda env update --file stable-diffusion/environment.yaml --name base && conda clean -a -y
# fonts for generating the grid
RUN apt-get update && apt install fonts-dejavu-core rsync -y && apt-get clean
# Note: don't update the sha of previous versions because the install will take forever
# instead, update the repo state in a later step
RUN cd stable-diffusion && git pull && git reset --hard c5b2c86f1479dec75b0e92dd37f9357a68594bda && \
conda env update --file environment.yaml --name base && conda clean -a -y
# Textual-inversion:
RUN git clone https://github.com/hlky/sd-enable-textual-inversion.git && \
cd /sd-enable-textual-inversion && git reset --hard 08f9b5046552d17cf7327b30a98410222741b070 && \
rsync -a /sd-enable-textual-inversion/ /stable-diffusion/
# add info
COPY info.py /info.py
RUN python /info.py /stable-diffusion/frontend/frontend.py
WORKDIR /stable-diffusion
ENV TRANSFORMERS_CACHE=/cache/transformers TORCH_HOME=/cache/torch CLI_ARGS="" \
GFPGAN_PATH=/stable-diffusion/src/gfpgan/experiments/pretrained_models/GFPGANv1.3.pth \
RealESRGAN_PATH=/stable-diffusion/src/realesrgan/experiments/pretrained_models/RealESRGAN_x4plus.pth \
RealESRGAN_ANIME_PATH=/stable-diffusion/src/realesrgan/experiments/pretrained_models/RealESRGAN_x4plus_anime_6B.pth
EXPOSE 7860
CMD \
for path in "${GFPGAN_PATH}" "${RealESRGAN_PATH}" "${RealESRGAN_ANIME_PATH}"; do \
name=$(basename "${path}"); \
base=$(dirname "${path}"); \
test -f "/models/${name}" && mkdir -p "${base}" && ln -sf "/models/${name}" "${path}" && echo "Mounted ${name}";\
done;\
# force facexlib cache
mkdir -p /cache/weights/ && rm -rf /stable-diffusion/src/facexlib/facexlib/weights && \
ln -sf /cache/weights/ /stable-diffusion/src/facexlib/facexlib/ && \
# run, -u to not buffer stdout / stderr
python3 -u scripts/webui.py --outdir /output --ckpt /models/model.ckpt --save-metadata ${CLI_ARGS}
docker-compose.yml
version: '3.9'
services:
model:
image: berkeleyskypilot/stable-diffusion
restart: on-failure
ports:
- "7860:7860"
volumes:
- ./cache:/cache
- ./output:/output
- ./models:/models
environment:
- CLI_ARGS=--extra-models-cpu --optimized-turbo
deploy:
resources:
reservations:
devices:
- driver: nvidia
device_ids: ['0']
capabilities: [gpu]
pushing_docker_image.md
推送 Docker 镜像
GCR
请按照这些 说明安装 skypilot 包。
运行
git clone https://github.com/skypilot-org/skypilot.git
。步骤 4-6 基于这些 GCR 设置说明
首先在您的项目中启用容器注册表
接下来按照这些步骤在您的 CLI 上安装并认证 Docker
确保您在项目中拥有Storage Admin权限
运行
cd skypilot/examples/stable_diffusion
和docker build Dockerfile
运行
docker tag stable-diffusion-webui-docker_model gcr.io/(my-project)/stable-diffusion
。运行
docker push gcr.io/(my-project)/stable-diffusion
并在 GCR 上验证镜像是否存在。
DockerHub
创建一个免费的 DockerHub 账号。
在您的 CLI 中,运行
docker login
并使用您的 DockerHub 账号登录凭据。运行
cd skypilot/examples/stable_diffusion
和docker build Dockerfile
运行
docker tag stable-diffusion-webui-docker_model (my-dockerhub-username)/stable-diffusion
。运行
docker push (my-dockerhub-username)/stable-diffusion
并在 Dockerhub 上验证镜像是否存在。
stable_diffusion_docker.yaml
#SkyPilot YAML to run stable diffusion web tool on 1 V100 GPU.
resources:
accelerators: V100:1
file_mounts:
/stable_diffusion: .
setup: |
cd stable-diffusion-webui-docker
sudo rm -r stable-diffusion-webui-docker
git clone https://github.com/AbdBarho/stable-diffusion-webui-docker.git
cd stable-diffusion-webui-docker
git reset --hard 0d8b7d4ac8f9ba99e041ca332547eab9d65e6360
wget https://hugging-face.cn/CompVis/stable-diffusion-v-1-4-original/resolve/main/sd-v1-4.ckpt -P models
mv models/sd-v1-4.ckpt models/model.ckpt
docker pull berkeleyskypilot/stable-diffusion
run: |
cd stable-diffusion-webui-docker
docker run -d \
--name model \
--restart on-failure \
-p 7860:7860 \
-v $(pwd)/cache:/cache \
-v $(pwd)/output:/output \
-v $(pwd)/models:/models \
-e CLI_ARGS='--extra-models-cpu --optimized-turbo' \
--gpus all \
berkeleyskypilot/stable-diffusion