Image generation is a fascinating and challenging task in machine learning, with many potential applications such as art, entertainment, education, and data augmentation. However, most existing methods for image generation have some drawbacks, such as unstable training, mode collapse, slow synthesis speed, or low fidelity.
In this article, we will show you how to install and run stable diffusion locally, a Image generation of diffusion models, locally on your PC.
How to install and run stable diffusion locally on your PC
Stable Diffusion is a powerful AI image generator that can be run locally on your computer. In this tutorial, we will explore the steps required to install Stable Diffusion on your computer.
- A Linux or MacOS system with Python 3.8 or higher
- A GPU with CUDA support and at least 16 GB of memory
To install stable diffusion, follow these steps:
Install Updated Python
Let’s start by seeing if Python is already installed on your PC. To do so, launch a terminal and enter the following command:
If a Python version is returned, continue on to the next step. Otherwise, install Python with the following commands:
sudo apt-get update sudo apt-get install python3.8
Next, we need to ensure the package/environment manager conda is installed. Enter conda -version in the terminal. If a conda version is returned, move on to the next step. Otherwise, go to the conda website and download and run the appropriate Miniconda installer for your version of Python and operating system.
For Python3.8, you can download and run the installer with the following commands:
wget https://repo.anaconda.com/miniconda/Miniconda3-py38_4.12.0-Linux-x86_64.sh bash Miniconda3-py38_4.12.0-Linux-x86_64.sh
Hold press Enter to proceed through the license, then type “yes” when requested to continue. Next, click Enter to confirm the installation location, and then type “yes” when prompted to restart Miniconda.
Clone the stable-diffusion repository and enter the directory
After cloning the repository, we must install the Stable Diffusion dependencies. Run the following command in your terminal to accomplish this:
git clone https://github.com/CompVis/stable-diffusion.git cd stable-diffusion
This will install all Python dependencies needed to run Stable Diffusion locally. If you don’t have git, run sudo apt install git to get it. Before cloning the repository, read and accept the Stable Diffusion licence.
Design a Conda Environment
Now that we’ve cloned the Stable Diffusion repository, we’ll need to set up a Conda environment to hold all of the packages required to execute Stable Diffusion.
We will use the following commands to accomplish this:
conda env create -f environment.yaml conda activate ldm
The first command creates a new Conda environment called ldm. This environment will be activated by the second command line.
Once the environment has been activated, we can install the Stable Diffusion dependencies.
Download the Stable Diffusion
To download the Stable Diffusion checkpoint v1.4, you can follow the instructions below:
Make sure you have read and accepted the Stable Diffusion license.
Download the weights with the following command:
curl https://f004.backblazeb2.com/file/aai-blog-files/sd-v1-4.ckpt > sd-v1-4.ckpt
Several versions of the Stable Diffusion checkpoint have been released. Higher version numbers have been trained on more data and, as a result, perform better than lower version numbers.
Now you are ready to run stable diffusion locally and generate images from noise or text.
Image generation with stable diffusion
Open a terminal and navigate into the stable-diffusion directory. Make sure you are in the proper environment by executing the command conda activate ldm.
To generate an image, run the following command:
python scripts/txt2img.py -prompt “YOUR-PROMPT-HERE” -plms -ckpt sd-v1-4.ckpt -skip_grid -n_samples 1
The minimum image size is 256×256. This will create a directory and save the generated images as PNG files. Where YOU-PROMPT-HERE is replaced with the caption for which you want to generate an image (without the quotation marks). Running this command with the input “a photorealistic image of a penguin riding a Scatting through space” results in the image below:
Prompt: a photorealistic image of a penguin riding a Scatting through space
Prompt: a photorealistic vaporwave image of a men riding a snowboard through space
As you can see, the generated images are realistic and diverse, covering a range of natural scenes and objects.
You can also check out our blog, How to Use Stable Diffusion XL 0.9 in Clipdrop to Create Stunning Images for more tips and tutorials on How to create image using stable diffusion.
In this article, we have introduced diffusion models, a new approach for image generation that can produce high-quality images from pure noise, either unconditionally or conditionally on some input such as text. We have also shown you how to install and run stable diffusion, and Image generation of diffusion models, locally on your PC.
Please feel free to share your thoughts and feedback in the comment section below.