How to Run Stable Diffusion on Your PC to Generate AI Images

0
37
How to Run Stable Diffusion on Your PC to Generate AI Images
Advertisement

An AI-generated magic gopher, artistic Egyptian vulture, and dramatic moonrise over a desert. Header image.

Artificial Intelligence (AI) art is currently all the rage, but most AI image generators run in the cloud. Stable Diffusion is different — you can run it on your very own PC and generate as many images as you want. Here’s how you can install and use Stable Diffusion on Windows.

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. It’s developed by Stability AI and was first publicly released on August 22, 2022.

Stable Diffusion doesn’t have a tidy user interface (yet) like some AI image generators, but it has an extremely permissive license, and — best of all — it is completely free to use on your own PC (or Mac.)

Update, 9/16/22: Prefer a graphical interface? Try our guide to running Stable Diffusion with a GUI on your PC.

Don’t be intimidated by the fact that Stable Diffusion currently runs in a command-line interface (CLI). Getting it up and running is pretty straight-forward. If you can double-click an executable and type in a box, you can have it running in a few minutes.

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

Stable Diffusion won’t run on your phone, or most laptops, but it will run on the average gaming PC in 2022. Here are the requirements:

If you don’t have the hardware, consider using a web-based AI image generator. You can even run a demo of Stable Diffusion on the web.

How to Install and Run Stable Diffusion on Windows

There are two pieces of software you need: Git and Miniconda3.

Note: Git and Miniconda3 are both safe programs produced by reputable organizations. You don’t need to worry about malware with them provided that you download them from the official sources linked in this article. 

Installing Git

Git is a tool that allows developers to manage different versions of the software they’re developing. They can maintain multiple versions of the software they’re working on in a central repository simultaneously and allow other developers to contribute to the project.

RELATED: How to Install Git on Windows

If you’re not a developer, Git provides a convenient way to access and download these projects, and that’s how we’ll use it in this case. Download the Windows x64 installer from the Git website, then run it to install Git.

There are several options you’ll be prompted to select while the installer runs — leave them on their default settings. One option page, “Adjusting Your PATH Environment,” is particularly important. It must be set to “Git From The Command Line And Also From 3rd-Party Software.”

Make sure that

Installing Miniconda3

Stable Diffusion draws on a few different Python libraries. If you don’t know much about Python, don’t worry too about this — suffice it to say, the libraries are just software packages that your computer can use to perform specific functions, like transform an image, or do complex math.

RELATED: What Is Python?

Miniconda3 is basically a convenience tool. It lets you download, install, and manage all of the libraries required for Stable Diffusion to function without very much manual intervention. It’ll also be how we actually use Stable Diffusion.

Head over to the Miniconda3 download page and click “Miniconda3 Windows 64-bit” to get the latest installer.

Double-click the executable once it has downloaded to start the installation. Miniconda3’s installation involves less clicking through pages than Git did, but you need to watch out for this option:

Tick the box that says

Make sure that you select “All Users” before clicking next and finishing up the installation.

You’ll be prompted to restart your computer after installing Git and Miniconda3. We didn’t find it to be necessary, but it won’t hurt if you do.

Download the Stable Diffusion GitHub Repository and the Latest Checkpoint

Now that we’ve installed the pre-requisite software, we’re ready to download and install Stable Diffusion.

Download the latest checkpoint first — version 1.4 is nearly 5GB, so it might take a while. You need to create an account to download the checkpoint, but they only require a name and email address. Everything else is optional.

Note: At the time of writing (September 2, 2022), the latest checkpoint is version 1.4. If there is a newer version, download that instead.

Click “sd-v1-4.ckpt” to start the download.

Note: The other file, “sd-v1-4-full-ema.ckpt”, might provide better results, but it is about twice the size. You can use either. 

You then need to download Stable Diffusion from GitHub. Click on the green “Code” button, then click “Download ZIP.” Alternatively, you can use this direct download link.

Now we need to prepare a few folders where we’ll unpack all of Stable Diffusion’s files. Click the Start button and type “miniconda3” into the Start Menu search bar, then click “Open” or hit Enter.

We’re going to create a folder named “stable-diffusion” using the command line. Copy and paste the code block below into the Miniconda3 window, then press Enter.

cd C:/
mkdir stable-diffusion
cd stable-diffusion

Note: Almost any time you paste a block of code into a terminal, like Miniconda3, you need to hit Enter at the end to run the last command.

If everything went well, you’ll see something like this:

Minoconda3 terminal showing commands executing successfully.

Keep the Miniconda3 window open, we’ll need it again in a minute.

Open up the ZIP file, “stable-diffusion-main.zip,” that you downloaded from GitHub in your favourite file archiving program. Alternatively, Windows can also open ZIP files by itself if you don’t have one. Keep the ZIP file open in one window, then open another File Explorer window and navigate to the “C:stable-diffusion” folder we just made.

RELATED: Get Help With File Explorer on Windows 10

Drag and drop the folder in the ZIP file, “stable-diffusion-main,” into the “stable-diffusion” folder.

Drag and drop the contents of the ZIP file into the stable-diffusion folder.

Go back to Miniconda3, then copy and paste the following commands into the window:

cd C:stable-diffusionstable-diffusion-main
conda env create -f environment.yaml
conda activate ldm
mkdir modelsldmstable-diffusion-v1

Wait for the download to finish.

Don’t interrupt this process. Some of the files are larger than a gigabyte, so it could take a bit to download. If you do interrupt the process accidentally, you’ll need to delete the environment folder and run conda env create -f environment.yaml again. If that happens, navigate to “C:Users(Your User Account).condaenvs” and delete the “ldm” folder, then run the previous command.

Note: So, what did we just do? Python lets you sort coding projects into “Environments.” Each environment is separate from other environments, so you can load different Python libraries into different environments without having to worry about conflicting versions. It is invaluable if you’re working on multiple projects on one PC.

The lines we ran created a new environment named “ldm,” downloaded and installed all of the necessary Python libraries for Stable Diffusion to work, activated the ldm environment, then changed the directory to a new folder.

We’re on the last step of the installation. Navigate to “C:stable-diffusionstable-diffusion-mainmodelsldmstable-diffusion-v1” in File Explorer, then copy and paste the checkpoint file (sd-v1-4.ckpt) into the folder.

Copy the model file into the stable-diffuse-v1 folder.

Wait for the file to finish transferring, right-click “sd-v1-4.ckpt” and then click “Rename.” Type “model.ckpt” into the highlighted box, then hit Enter to change the file name.

Note: If you’re running Windows 11, you won’t see “rename” in the right-click context menu. There is an icon that looks like a miniature text field instead.

RELATED: Windows 11’s Tiny Context Menu Buttons Will Confuse People

Rename the model file

And that’s it — we’re done. We are ready to actually use Stable Diffusion now.

How to Use Stable Diffusion

The ldm environment we created is essential, and you need to activate it any time you want to use Stable Diffusion. Enter conda activate ldm into the Miniconda3 window and hit “Enter.” The (ldm) on the left-hand side indicates that the ldm environment is active.

Note: You only need to enter that command when you open Miniconda3. The ldm environment will remain active as long as you do not close the window.

Activate the ldm environment.

Then we need to change the directory (thus the commandcd) to “C:stable-diffusionstable-diffusion-main” before we can generate any images. Paste cd C:stable-diffusionstable-diffusion-main into command line.

How to Make an Image with Stable Diffusion

We’re going to call a script, txt2img.py, that allows us to convert text prompts into 512×512 images. Here is an example. Try this out to make sure everything is working correctly:

python scripts/txt2img.py --prompt "a close-up portrait of a cat by pablo picasso, vivid, abstract art, colorful, vibrant" --plms --n_iter 5 --n_samples 1

Your console will give you a progress indicator as it produces the pictures.

Stable Diffusion generating images.

That command will produce five cat images, all located at “C:stable-diffusionstable-diffusion-mainoutputstxt2img-samplessamples”.

A cat in the style of Pablo Picasso.

It isn’t perfect, but it distinctly resembles the style of Pablo Picasso, just like we specified in the prompt. Your images should look similar but not necessarily identical.

Any time you want to change what image is generated you just need to change the text contained in the double-quotation marks following --prompt.

Tip: Don’t rewrite the entire line every time. Use the arrow keys to move the text cursor around and just replace the prompt.

python scripts/txt2img.py --prompt "YOUR, DESCRIPTIONS, GO, HERE" --plms --n_iter 5 --n_samples 1

Say we wanted to generate a realistic looking gopher in a magical forest wearing a wizard’s hat. We could try the command:

python scripts/txt2img.py --prompt "a photograph of a gopher wearing a wizard hat in a forest, vivid, photorealistic, magical, fantasy, 8K UHD, photography" --plms --n_iter 5 --n_samples 1

A gopher with a purple wizard hat.

It really is that easy — just describe what you want as specifically as you can. If you want something photorealistic, make sure to include terms relating to a realistic image. If you want something inspired by the style of a specific artist, specify the artist.

Stable Diffusion isn’t limited to portraits and animals either, it can also produce striking landscapes.

A calm lake with mountains around it, and a dramatic sky.

What Do the Arguments in the Command Mean?

Stable Diffusion has an enormous number of settings and arguments that you can provide to customize your results. The few included here are basically necessary to ensure Stable Diffusion will run on an average gaming computer.

  • –plms — Specifies how the images will be sampled. There is a paper about it, if you want to check out the math.
  • –n_iter — specifies the number of iterations you want to generate for each prompt. 5 is a decent number to see what kind of results you’re getting.
  • –n_samples — specifies the number of samples that will be generated. The default is 3, but most computers do not have enough VRAM to support that. Stick with 1 unless you have a specific reason to change it.

Of course, Stable Diffusion has a ton of different arguments that you can implement to tweak your results. Run python scripts/txt2img.py --help to get an exhaustive list of arguments that you can use.

There is a ton of trial and error involved in getting great results, but that is at least half of the fun. Make sure you write down or save arguments and descriptions that return results you like. If you don’t want to do all of the experimenting yourself, there are growing communities on Reddit (and elsewhere) dedicated to exchanging pictures and the prompts that generated them.

Read More

Advertisement

LEAVE A REPLY

Please enter your comment!
Please enter your name here