keinerweiss.de
2022/09/03

Linux: Getting started with Stable Diffusion

Stable Diffusion is a AI image generator that runs on your own machine.

For Windows there are plenty of easily executable builds but we want to run it on Linux.

My attention was caught with this video and then I tried to figure out how to run it on Linux.

As far as I understand, it only works with pretty strong NVIDIA cards. I have a GeForce 3080 Ti with 12GB VRAM and can generate images of 256x512 Pixels in size within a few seconds but not much bigger.

Prerequisites

I use Ubuntu 22.04.

The Source Code of Stable Diffusion is not very effective without the trained AI model.

The stable-diffusion-v-1-4-original model can be obtained downloded at huggingface.co after registering for free. It's 7GB in size.

To run it you need a prepared Python environment for which Anaconda is recommended to install all dependencies without tainting the rest of you Python Environment. Check out the Ubuntu Guide how to install Anaconda):

bash Anaconda3-2022.05-Linux-x86_64.sh
# only directly after installation:
. ~/.bashrc

Anaconda manifests in the ~/.bashrc and changes how your prompt looks. If you want to get rid of it you can run this or remove the block manually.

conda init --reverse

In short once you have everything (based on the instructions on the project's github page):

# Prepare folders
mkdir ~/Stable\ Diffusion
cd ~/Stable\ Diffusion
git clone https://github.com/CompVis/stable-diffusion.git .
mkdir -p models/ldm/stable-diffusion-v1/
 
# moving the downloaded model to its destination
mv ~/Downloads/sd-v1-4-full-ema.ckpt models/ldm/stable-diffusion-v1
cd models/ldm/stable-diffusion-v1/
ln -s sd-v1-4-full-ema.ckpt model.ckpt
 
# building the Python environment and activating it
cd ~/Stable\ Diffusion
conda env create -f environment.yaml
conda activate ldm
conda install pytorch torchvision -c pytorch

When all is done you can run it via

python scripts/txt2img.py --prompt "a photograph of an astronaut riding a horse" --plms --W 512 --H 256

The images can then be found in outputs/

An elefant on a cliff looking at a sunset

Result images of: An elefant on a cliff looking at a sunset

Colorful clouds in japan

Result images of: Colorful clouds in japan

A drawing of a granny on the porch

Result images of: A drawing of a granny on the porch

Have fun!