Requirements
- Ubuntu 20.04 or later
- Docker
- NVIDIA GPU (tested on RTX 3090)
- NVIDIA Docker runtime (installation)
- git and git lfs
Build docker image
First of all, create a new folder to host everything that you would need as a workspace by running mkdir generateImages && cd generateImages
. Create the following setup.sh
file.
Then execute the above file bash setup.sh
to download the models into generateImages/models
directory. We download the main repository inside the docker image. Create the following Dockerfile
in the generateImages
directory.
setup.sh
file:
#!/bin/bash
# exit when any command fails
set -e
# intialize git lfs
git lfs install
mkdir models
cd models
# download models
# and remove `.git` folders to preserve some space
git clone --depth=1 https://huggingface.co/stabilityai/stable-diffusion-2-1
rm -rf stable-diffusion-2-1/.git
git clone --depth=1 https://huggingface.co/stabilityai/stable-diffusion-2-depth
rm -rf stable-diffusion-2-depth/.git
git clone --depth=1 https://huggingface.co/stabilityai/stable-diffusion-2-inpainting
rm -rf stable-diffusion-2-inpainting/.git
wget https://github.com/intel-isl/DPT/releases/download/1_0/dpt_hybrid-midas-501f0c75.pt
FROM nvcr.io/nvidia/cuda:11.3.0-devel-ubuntu20.04
RUN apt-get update \
&& DEBIAN_FRONTEND=noninteractive apt-get install -y wget
RUN wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh \
&& chmod +x Miniconda3-latest-Linux-x86_64.sh \
&& bash ./Miniconda3-latest-Linux-x86_64.sh -b -p $HOME/miniconda
RUN echo "eval \"\$(/root/miniconda/bin/conda shell.bash hook)\"">>~/.bashrc
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y git ffmpeg libsm6 libxext6
RUN wget https://github.com/git-lfs/git-lfs/releases/download/v3.3.0/git-lfs-linux-amd64-v3.3.0.tar.gz \
&& tar -xf *.tar.gz \
&& cd git-lfs-3.3.0 \
&& ./install.sh \
&& cd \
&& git lfs install
RUN git lfs install && git clone https://github.com/Stability-AI/stablediffusion.git \
&& cd /stablediffusion \
&& sed -i 's/python=3.8.5/python=3.9/g' environment.yaml \
&& eval "$(/root/miniconda/bin/conda shell.bash hook)" \
&& conda env create -f environment.yaml \
&& echo "conda activate ldm" >>~/.bashrc
RUN eval "$(/root/miniconda/bin/conda shell.bash hook)" \
&& conda activate ldm \
&& export CUDA_HOME=/usr/local/cuda \
&& pip install -U --pre triton \
&& pip install gradio timm \
&& conda install -y xformers -c xformers
WORKDIR /stablediffusion
Run the following command to build the docker image. It may take a while.
docker build -t my_stable_diffusion_image .
Using stable diffusion
Run the following to start the docker container
docker run --runtime nvidia --gpus all --rm -it \
-v `realpath imgs`:/stablediffusion/outputs \
-v `realpath models`:/models \
-v $HOME/.cache/huggingface:/root/.cache/huggingface \
-v $HOME/.cache/torch:/root/.cache/torch \
--name stabdiff \
--privileged \
--network host \
my_stable_diffusion_image bash
Generate images
You can start generating images with the following command and the images will be created in the
imgs
directory on the host.
python scripts/txt2img.py \
--ckpt /models/stable-diffusion-2-1/v2-1_768-ema-pruned.ckpt \
--config configs/stable-diffusion/v2-inference-v.yaml \
--device cuda \
--n_samples 1 \
--step 150 \
--H 1024 --W 1024 \
--n_iter 5 \
--seed 14 \
--prompt "a professional photograph of an astronaut riding a horse"
Image Modification with Stable Diffusion
pip install gradio timm \ && mkdir midas_models \ && cp /models/dpt_hybrid-midas-501f0c75.pt midas_models/ python scripts/gradio/depth2img.py \ configs/stable-diffusion/v2-midas-inference.yaml \ /models/stable-diffusion-2-depth/512-depth-ema.ckpt
Image Inpainting with Stable Diffusion
python scripts/gradio/inpainting.py \
configs/stable-diffusion/v2-inpainting-inference.yaml \
/models/stable-diffusion-2-inpainting/512-inpainting-ema.ckpt
# or
streamlit run scripts/streamlit/inpainting.py \
-- \
configs/stable-diffusion/v2-inpainting-inference.yaml \
/models/stable-diffusion-2-inpainting/512-inpainting-ema.ckpt
Enjoy.
Looks like the dockerfile is pointing to an older release of nvidia cuda that’s no longer hosted.
I tried updated to a newer release in the dockerfile, works up until the point where it tries to install pytorch and xformers, then it dies incompatible with the current releases 🙁
Anything you recommend?
Thank you,
Mike
Hi Mike,
Thanks for letting me know. I updated the Dockerfile. Please let me know if that works now.