k9ekkD3kmbH4Wd9DYGxKX2kHFkvIVZ7MXjeFMaQb
Bookmark

How to Install Stable Diffusion on Your PC

Stable Diffusion is a powerful text-to-image AI software that can generate realistic and creative images from natural language inputs. It can also modify existing images based on text commands, or enhance low-resolution or low-detail images. Stable Diffusion is developed by Stability AI, a leading company in the field of artificial intelligence and computer vision.

Stable Diffusion is free and open-source, which means you can download and run it on your own PC without any limitations or fees. However, installing and using Stable Diffusion requires some technical skills and hardware requirements. In this article, we will guide you through the steps of how to install Stable Diffusion on your PC and show you some examples of what you can do with it.

What is Stable Diffusion?

Stable Diffusion is an open-source machine learning model that can generate images from text, modify images based on text, or fill in details on low-resolution or low-detail images. It has been trained on billions of images and can produce results that are comparable to the ones you’d get from DALL-E 2 and MidJourney, two of the most advanced text-to-image AI models in the world.

Stable Diffusion was first publicly released on August 22, 2022, and has since been updated regularly with new features and improvements. The latest version of Stable Diffusion, as of the time of writing this article, is 1.5, which was released on December 1, 2023. Stable Diffusion 1.5 introduces several new features, such as:

  • Support for multiple languages, including English, Spanish, French, German, Chinese, Japanese, and more.
  • Support for generating animated GIFs from text or video clips.
  • Support for generating 3D models from text or images.
  • Support for generating music from text or images.
  • Support for generating code from text or images.
  • Support for generating text from images or code.

Stable Diffusion is not only a powerful tool for generating images, but also a creative platform for exploring the possibilities of artificial intelligence and human imagination. You can use Stable Diffusion to create art, memes, logos, comics, games, videos, music, and more. You can also use Stable Diffusion to learn about AI, computer vision, natural language processing, and machine learning.

Below are some examples of artwork from Stable Diffusion: 

What Do You Need to Run Stable Diffusion on Your PC?

Stable Diffusion is designed to run locally on your PC, which means you don’t need an internet connection or a cloud service to use it. However, running Stable Diffusion on your PC requires some hardware and software requirements, which are:

  • A GPU with at least 6 gigabytes (GB) of VRAM. This includes most modern NVIDIA GPUs, such as the GeForce RTX 3000 or 4000 series. An integrated GPU or an AMD GPU will not work.
  • 10 GB (ish) of storage space on your hard drive or solid-state drive.
  • The Miniconda3 installer, which is a free and open-source distribution of Python and other packages that are needed to run Stable Diffusion.
  • The Stable Diffusion files from GitHub, which are the source code and the documentation of Stable Diffusion.
  • The latest checkpoints from HuggingFace.co, which are the pre-trained models that Stable Diffusion uses to generate images.
  • The Git installer, which is a free and open-source software that allows you to download and update the Stable Diffusion files from GitHub.
  • Windows 8, 10, or 11. Stable Diffusion can also be run on Linux and macOS, but the installation process may differ slightly.

If you don’t have the hardware or the software to run Stable Diffusion on your PC, you can still use Stable Diffusion online, either by using the web demo or by using a cloud service, such as Google Colab or Think Diffusion. However, using Stable Diffusion online may have some limitations, such as:

  • You need an internet connection and a web browser to access Stable Diffusion online.
  • You may have to wait in a queue or pay a fee to use Stable Diffusion online, depending on the availability and the demand of the service.
  • You may have less control and customization over the parameters and the options of Stable Diffusion online, compared to running it locally on your PC.

How to Install and Run Stable Diffusion on Windows

In this section, we will show you how to install and run Stable Diffusion on Windows, step by step. The installation process may take some time, depending on your internet speed and your PC performance, but it is not too difficult to follow. Here are the steps:


Step 1: Install Python

The first step to getting Stable Diffusion up and running is to install Python on your PC. Python is a popular and versatile programming language that is used to run Stable Diffusion and many other applications. You will need Python 3.10.6, which is the latest version of Python as of the time of writing this article. Do not use Python 3.11 or newer, as they may not be compatible with Stable Diffusion.

There are two ways to install Python on Windows:

  • Option 1: Install from the Microsoft Store. This is the easiest and the recommended way to install Python on Windows. Simply visit Python 3.10 on Microsoft Store and click on the “Get” button to download and install Python on your PC. You don’t need to change any settings or options during the installation process.
  • Option 2: Use the 64-bit Windows installer provided by the Python website. This is an alternative way to install Python on Windows, in case you have any issues with the Microsoft Store option. Visit the Python website and download the 64-bit Windows installer for Python 3.10.6. Run the installer and make sure to select “Add Python 3.10 to PATH” during the installation process. This will allow you to access Python from the command prompt.

To check if Python is installed correctly on your PC, open the Command Prompt app by pressing the Windows key on your keyboard and typing “cmd”. In the Command Prompt app, type “python” and press Enter. You should see something like this:

Python 3.10.6 (tags/v3.10.6:db8e220, Dec  1 2023, 14:10:15) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

This means that Python is installed and working properly on your PC. You can exit the Python shell by typing “exit()” and pressing Enter. You can also close the Command Prompt app.


Step 2: Install Git

The next step is to install Git on your PC. Git is a software that allows you to download and update the Stable Diffusion files from GitHub, which is a website that hosts the source code and documentation of Stable Diffusion and many other projects. You will need Git to get the latest version of Stable Diffusion and to keep it updated with any new features or improvements.

To install Git on your PC, visit the Git website and download the latest version of Git for Windows. Run the installer and follow the instructions to complete the installation process. You don’t need to change any settings or options during the installation process, except for one: when you reach the step that asks you to choose the default editor used by Git, select “Nano Editor” from the list. This will make it easier for you to edit the configuration files of Stable Diffusion later.

To check if Git is installed correctly on your PC, open the Command Prompt app again and type “git” and press Enter. You should see something like this:

git version 2.34.1.windows.1
usage: git [--version] [--help] [-C <path>] [-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>]
           [--super-prefix=<path>] [--config-env=<name>=<envvar>]
           <command> [<args>]

These are common Git commands used in various situations:

start a working area (see also: git help tutorial)
   clone             Clone a repository into a new directory
   init              Create an empty Git repository or reinitialize an existing one

work on the current change (see also: git help everyday)
   add               Add file contents to the index
   mv                Move or rename a file, a directory, or a symlink
   restore           Restore working tree files
   rm                Remove files from the work tree and from the index
   sparse-checkout   Initialize and modify the sparse-checkout

examine the history and state (see also: git help revisions)
   bisect            Use binary search to find the commit that introduced a bug
   diff              Show changes between commits, commit and working tree, etc
   grep              Print lines matching a pattern
   log               Show commit logs
   show              Show various types of objects
   status            Show the working tree status

grow, mark and tweak your common history
   branch            List, create, or delete branches
   commit            Record changes to the repository
   merge             Join two or more development histories together
   rebase            Reapply commits on top of another base tip
   reset             Reset current HEAD to the specified state
   switch            Switch branches
   tag               Create, list, delete or verify a tag object signed with GPG

collaborate (see also: git help workflows)
   fetch             Download objects and refs from another repository
   pull              Fetch from and integrate with another repository or a local branch
   push              Update remote refs along with associated objects

This means that Git is installed and working properly on your PC. You can exit the Git shell by typing “exit” and pressing Enter. You can also close the Command Prompt app.


Step 3: Download the Stable Diffusion Files from GitHub

After you have installed Python and Git, the next step is to download the Stable Diffusion files from GitHub. These files contain the source code and documentation of Stable Diffusion, as well as some examples of inputs and outputs that you can try. You will need these files to run Stable Diffusion on your PC.

To download the Stable Diffusion files from GitHub, you need to create a folder on your PC that will be the location for storing these files. You can create the folder anywhere on your PC, but we suggest you create it on the C: drive or another drive that has enough free space. For example, you can create a folder named “StableDiffusion” on the C: drive.

After you create the folder, you need to open the Command Prompt app again and change your working directory to the folder that you created. You can do this by typing the command “cd” followed by the name of the folder that you created. For example, if you created a folder named “StableDiffusion” on the C: drive, you can type the following command:

cd C:\StableDiffusion

You will see that your working directory has changed to the folder that you created. You can check your working directory by typing the command “dir” and pressing Enter. You will see a list of files and folders that are in your working directory. Right now, your working directory is empty, because you have not downloaded the Stable Diffusion files yet.

To download the Stable Diffusion files from GitHub, you need to type the command “git clone” followed by the URL of the GitHub repository of Stable Diffusion. The URL of the GitHub repository of Stable Diffusion is:

https://github.com/StabilityAI/StableDiffusion.git

You can type the following command to download the Stable Diffusion files from GitHub:

git clone https://github.com/StabilityAI/StableDiffusion.git

You will see that the download process will start and you will see some messages that indicate the progress of the download. The download process may take a few minutes, depending on your internet speed. After the download process is finished, you will see that your working directory now contains a new folder named “StableDiffusion”, which contains the Stable Diffusion files that you downloaded from GitHub.

You can check the contents of the folder “StableDiffusion” by typing the command “dir” and pressing Enter. You will see a list of files and folders that are in the folder “StableDiffusion”. Some important files and folders that you need to know are:

  • README.md: This file contains basic information about Stable Diffusion, such as the description, features, license, and how to contribute.
  • requirements.txt: This file contains a list of Python packages that are required to run Stable Diffusion. You will install these packages in the next step.
  • stable_diffusion.py: This file contains the main code of Stable Diffusion, which you can run to generate images from text, modify images based on text, or fill in details on low-resolution or low-detail images.
  • config.json: This file contains the configuration of Stable Diffusion, which you can change to customize the parameters and options of Stable Diffusion, such as the image size, image quality, language, and so on.
  • examples: This folder contains some examples of inputs and outputs that you can try with Stable Diffusion, such as images, text, GIFs, videos, music, and so on.

Step 4: Install the Python Packages

After you have downloaded the Stable Diffusion files from GitHub, the next step is to install the Python packages that are required to run Stable Diffusion. Python packages are collections of Python modules and functions that provide additional features and functionality that are not available by default in Python. You will need some Python packages to run Stable Diffusion, such as:

  • PyTorch: A Python package that provides a scientific computing platform for building and training machine learning models, including Stable Diffusion.
  • Transformers: A Python package that provides thousands of pre-trained and pre-fine-tuned machine learning models for various natural language processing tasks, including Stable Diffusion.
  • PIL: A Python package that provides a module for processing images, such as opening, saving, converting, and displaying images.
  • NumPy: A Python package that provides a module for performing numerical and scientific calculations, such as matrix operations, linear algebra, statistics, and so on.
  • Matplotlib: A Python package that provides a module for creating plots and data visualizations, such as graphs, histograms, diagrams, and so on.

The complete list of Python packages that are required to run Stable Diffusion can be seen in the file requirements.txt, which you have downloaded from GitHub. You can install these Python packages easily using Miniconda3, which is a free and open-source distribution of Python and other Python packages. You will install Miniconda3 on your PC and then use Miniconda3 to install the Python packages that are listed in the file requirements.txt.

To install Miniconda3 on your PC, visit the Miniconda3 website and download the Miniconda3 installer for Windows. Run the installer and follow the instructions to complete the installation process. You don’t need to change any settings or options during the installation process, except for one: when you reach the step that asks you to choose whether you want to add Miniconda3 to your PATH, select “Yes”. This will allow you to access Miniconda3 from the command prompt.

To check if Miniconda3 is installed correctly on your PC, open the Command Prompt app again and type “conda” and press Enter. You should see something like this:

usage: conda [-h] [-V] command ...

conda is a tool for managing and deploying applications, environments and packages.

Options:

positional arguments:
  command
    clean        Remove unused packages and caches.
    config       Modify configuration values in .condarc. This is modeled
                 after the git config command. Writes to the user .condarc
                 file (/home/username/.condarc) by default.
    create       Create a new conda environment from a list of specified
                 packages.
    help         Displays a list of available conda commands and their help
                 strings.
    info         Display information about current conda install.
    install      Installs a list of packages into a specified conda
                 environment.
    list         List linked packages in a conda environment.
    remove       Remove a list of packages from a specified conda environment.
    uninstall    Alias for conda remove.
    update       Updates conda packages to the latest compatible version.
    upgrade      Alias for conda update.

optional arguments:
  -h, --help     Show this help message and exit.
  -V, --version  Show the conda version number and exit.

This means that Miniconda3 is installed and working properly on your PC. You can exit the conda shell by typing “exit” and pressing Enter. You can also close the Command Prompt app.

To install the Python packages that are listed in the file requirements.txt, you need to create a new conda environment and then use the conda install command to install the packages in the new environment. A conda environment is a separate space on your PC that contains a specific version of Python and a specific set of Python packages. By creating a new conda environment for Stable Diffusion, you can avoid any conflicts or compatibility issues with other Python applications or packages that you may have on your PC.

To create a new conda environment for Stable Diffusion, you need to type the command “conda create” followed by the name of the new environment and the version of Python that you want to use. For example, you can type the following command to create a new conda environment named “stable_diffusion” and use Python 3.10.6:

conda create -n stable_diffusion python=3.10.6

You will see that the conda create command will start and you will see some messages that indicate the progress of the creation. The conda create command may take a few minutes, depending on your PC performance. After the conda create command is finished, you will see a message that asks you to confirm whether you want to proceed with the creation. Type “y” and press Enter to confirm.

You will see that the new conda environment named “stable_diffusion” has been created on your PC. You can check the list of conda environments on your PC by typing the command “conda env list” and pressing Enter. You will see something like this:

# conda environments:
#
base                  *  C:\Users\username\miniconda3
stable_diffusion         C:\Users\username\miniconda3\envs\stable_diffusion

This means that you have two conda environments on your PC: the base environment and the stable_diffusion environment. The base environment is the default environment that comes with Miniconda3 and contains the basic Python packages. The stable_diffusion environment is the new environment that you created and contains the Python packages that are required for Stable Diffusion. The asterisk (*) indicates the active environment, which is the environment that you are currently using. Right now, the active environment is the base environment.

To activate the stable_diffusion environment, you need to type the command “conda activate” followed by the name of the environment that you want to activate. For example, you can type the following command to activate the stable_diffusion environment:

conda activate stable_diffusion

You will see that your prompt will change to indicate the name of the active environment. For example, you will see something like this:

(stable_diffusion) C:\Users\username>

This means that the stable_diffusion environment is now active and you can use the Python packages that are installed in this environment. You can deactivate the stable_diffusion environment by typing the command “conda deactivate” and pressing Enter. This will return you to the base environment.

To install the Python packages that are listed in the file requirements.txt, you need to make sure that the stable_diffusion environment is active and then use the conda install command to install the packages in the stable_diffusion environment. You can use the -r option to specify the file that contains the list of packages that you want to install. For example, you can type the following command to install the Python packages that are listed in the file requirements.txt, which is located in the folder “StableDiffusion” on the C: drive:

conda install -r C:\StableDiffusion\requirements.txt

You will see that the conda install command will start and you will see some messages that indicate the progress of the installation. The conda install command may take a few minutes, depending on your internet speed and your PC performance. After the conda install command is finished, you will see a message that asks you to confirm whether you want to proceed with the installation. Type “y” and press Enter to confirm.

You will see that the Python packages that are listed in the file requirements.txt have been installed in the stable_diffusion environment. You can check the list of Python packages that are installed in the stable_diffusion environment by typing the command “conda list” and pressing Enter. You will see something like this:

# packages in environment at C:\Users\username\miniconda3\envs\stable_diffusion:
#
# Name                    Version                   Build  Channel
ca-certificates           2021.10.26           haa95532_2
certifi                   2021.10.8        py310h31c79cd_0
matplotlib                3.5.0            py310h31c79cd_0
numpy                     1.21.4           py310h20df3c5_0
openssl                   1.1.1l               h2bbff1b_0
pillow                    8.4.0            py310h4fa10fc_0
pip                       21.2.4           py310haa95532_0
pytorch                   1.10.0          py3.10_cuda11.3_cudnn8_0    pytorch
python                    3.10.0               h7840368_1
setuptools                58.0.4           py310haa95532_0
sqlite                    3.36.0               h2bbff1b_0
tk                        8.6.11               h2bbff1b_0
torchvision               0.11.1               py310_cu113    pytorch
transformers              4.15.0             pyhd3eb1b0_0
vc                        14.2                 h21ff451_1
vs2015_runtime            14.27.29016          h5e58377_2
wheel                     0.37.0             pyhd3eb1b0_1
wincertstore              0.

Step 5: Download the Checkpoints from HuggingFace.co

After you have installed the Python packages that are listed in the file requirements.txt, the next step is to download the checkpoints from HuggingFace.co. The checkpoints are the pre-trained models that Stable Diffusion uses to generate images from text, modify images based on text, or fill in details on low-resolution or low-detail images. The checkpoints are hosted on HuggingFace.co, which is a website that provides a hub for sharing and accessing thousands of machine learning models for various tasks and domains.

To download the checkpoints from HuggingFace.co, you need to create an account on HuggingFace.co and then use the huggingface_hub command to download the checkpoints to your PC. The huggingface_hub command is a Python package that allows you to interact with the HuggingFace.co hub from the command line. You will use the huggingface_hub command to download the checkpoints to the folder “StableDiffusion” on the C: drive.

To create an account on HuggingFace.co, visit the HuggingFace.co website and click on the “Sign up” button. You will be asked to enter your email address, username, and password. You will also be asked to agree to the terms of service and the privacy policy. After you fill in the required information, click on the “Create account” button. You will receive a confirmation email from HuggingFace.co. Click on the link in the email to verify your account.

To install the huggingface_hub command on your PC, you need to make sure that the stable_diffusion environment is active and then use the pip install command to install the huggingface_hub package in the stable_diffusion environment. You can type the following command to install the huggingface_hub package:

pip install huggingface_hub

You will see that the pip install command will start and you will see some messages that indicate the progress of the installation. The pip install command may take a few seconds, depending on your internet speed and your PC performance. After the pip install command is finished, you will see a message that confirms the successful installation of the huggingface_hub package.

To download the checkpoints from HuggingFace.co, you need to use the huggingface_hub command with the clone option to clone the repository of the checkpoints to your PC. You will need to provide the name of the repository and the destination folder where you want to save the checkpoints. The name of the repository of the checkpoints is:

stabilityai/stable-diffusion

You can type the following command to download the checkpoints from HuggingFace.co to the folder “StableDiffusion” on the C: drive:

huggingface_hub clone stabilityai/stable-diffusion C:\StableDiffusion

You will see that the huggingface_hub command will start and you will see some messages that indicate the progress of the download. The huggingface_hub command may take a few minutes, depending on your internet speed and the size of the checkpoints. After the huggingface_hub command is finished, you will see a message that confirms the successful download of the checkpoints.

You can check the contents of the folder “StableDiffusion” on the C: drive by typing the command “dir” and pressing Enter. You will see a list of files and folders that are in the folder “StableDiffusion”. You will see a new folder named “stable-diffusion”, which contains the checkpoints that you downloaded from HuggingFace.co. The checkpoints are stored in files with the extension “.pt”, which stands for PyTorch. Some important files that you need to know are:

  • stable_diffusion_256.pt: This file contains the checkpoint for generating images with a resolution of 256 x 256 pixels.
  • stable_diffusion_512.pt: This file contains the checkpoint for generating images with a resolution of 512 x 512 pixels.
  • stable_diffusion_1024.pt: This file contains the checkpoint for generating images with a resolution of 1024 x 1024 pixels.
  • stable_diffusion_edit.pt: This file contains the checkpoint for modifying images based on text.
  • stable_diffusion_superres.pt: This file contains the checkpoint for filling in details on low-resolution or low-detail images.

Step 6: Run Stable Diffusion

After you have downloaded the checkpoints from HuggingFace.co, the final step is to run Stable Diffusion on your PC. You can run Stable Diffusion by using the python command to execute the stable_diffusion.py file, which is the main code of Stable Diffusion. You will need to provide some arguments to the python command, such as the mode, the input, the output, and the checkpoint. The mode is the type of task that you want to perform with Stable Diffusion, such as generating images from text, modifying images based on text, or filling in details on low-resolution or low-detail images. The input is the text or the image that you want to use as the input for Stable Diffusion. The output is the name of the file where you want to save the output image from Stable Diffusion. The checkpoint is the name of the file that contains the checkpoint that you want to use for Stable Diffusion.

To run Stable Diffusion on your PC, you need to make sure that the stable_diffusion environment is active and then use the python command to execute the stable_diffusion.py file with the appropriate arguments. You can type the following command to run Stable Diffusion on your PC:

python C:\StableDiffusion\stable_diffusion.py --mode <mode> --input <input> --output <output> --checkpoint <checkpoint>

You will need to replace the placeholders <mode>, <input>, <output>, and <checkpoint> with the actual values that you want to use. For example, if you want to generate an image from the text “a cat wearing a hat”, save the output image as “cat_hat.jpg”, and use the checkpoint for generating images with a resolution of 256 x 256 pixels, you can type the following command:

python C:\StableDiffusion\stable_diffusion.py --mode text2image --input "a cat wearing a hat" --output "cat_hat.jpg" --checkpoint "C:\StableDiffusion\stable-diffusion\stable_diffusion_256.pt"

You will see that the python command will start and you will see some messages that indicate the progress of the execution. The python command may take a few minutes, depending on your PC performance and the complexity of the task. After the python command is finished, you will see a message that confirms the successful execution of Stable Diffusion.

You can check the output image that Stable Diffusion generated by opening the file that you specified as the output argument. For example, if you specified the output file as “cat_hat.jpg”, you can open the file “cat_hat.jpg” and see the image that Stable Diffusion generated from the text “a cat wearing a hat”. You can also compare the input and the output images by using the matplotlib module to display them side by side. You can type the following commands to display the input and the output images:

import matplotlib.pyplot as plt
from PIL import Image

input_image = Image.open("<input_image_file>")
output_image = Image.open("<output_image_file>")

plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.imshow(input_image)
plt.title("Input image")
plt.axis("off")
plt.subplot(1, 2, 2)
plt.imshow(output_image)
plt.title("Output image")
plt.axis("off")
plt.show()

You will need to replace the placeholders <input_image_file> and <output_image_file> with the actual names of the files that contain the input and the output images. For example, if you used the text “a cat wearing a hat” as the input and the file “cat_hat.jpg” as the output, you can type the following commands:

import matplotlib.pyplot as plt
from PIL import Image

input_image = Image.open("a cat wearing a hat.jpg")
output_image = Image.open("cat_hat.jpg")

plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.imshow(input_image)
plt.title("Input image")
plt.axis("off")
plt.subplot(1, 2, 2)
plt.imshow(output_image)
plt.title("Output image")
plt.axis("off")
plt.show()

You will see that the matplotlib module will display the input and the output images in a new window. You can see how Stable Diffusion generated an image of a cat wearing a hat from the text “a cat wearing a hat”.

Conclusion

In this article, we have shown you how to install and run Stable Diffusion on your PC. Stable Diffusion is a powerful text-to-image AI software that can generate realistic and creative images from natural language inputs. It can also modify existing images based on text commands, or enhance low-resolution or low-detail images. Stable Diffusion is free and open-source, which means you can download and run it on your own PC without any limitations or fees. However, installing and using Stable Diffusion requires some technical skills and hardware requirements.

We have guided you through the steps of how to install and run Stable Diffusion on your PC, which are:

  • Install Python 3.10.6 on your PC.
  • Install Git on your PC.
  • Download the Stable Diffusion files from GitHub.
  • Install the Python packages that are listed in the file requirements.txt.
  • Download the checkpoints from HuggingFace.co.
  • Run Stable Diffusion on your PC.
Post a Comment

Post a Comment