How to run Stable Diffusion on your PC to generate AI images

0
403

[ad_1]

The art of artificial intelligence (AI) is currently all the rage, but most AI imagers run in the cloud. Stable Diffusion is different: you can run it on your own PC and generate as many images as you like. This is 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 comparable to what you would get with DALL-E 2 and MidJourney. It is developed by Stability AI and was first publicly released on August 22, 2022.

Stable Diffusion doesn’t have a neat UI (yet) like some AI imagers, but it does have an extremely permissive license, and best of all, it’s completely free to use on your own PC (or Mac).

Don’t be put off by the fact that Stable Diffusion currently runs on a command line interface (CLI). Getting it up and running is quite simple. If you can double-click an executable and type in a box, you can run it 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:

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 secure programs produced by reputable organizations. You don’t need to worry about malware with them as long as 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 are developing. They can keep multiple versions of the software they are working on in a central repository simultaneously and allow other developers to contribute to the project.

RELATED: What is GitHub and what is it for?

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, and then run it.

There are several options that you will be prompted to select while the installer is running; leave them at their default settings. One page of options, “Setting your PATH environment”, is particularly important. It should be set to “Git from the command line and also from third party software”.

Make sure it is selected "Git from the command line and also from third party software".

Installing Miniconda 3

Stable Diffusion is based on a few different Python libraries. If you don’t know much about Python, don’t worry too much about this; suffice it to say that libraries are just software packages that your computer can use to perform specific functions, such as transforming an image or doing complex mathematical operations.

RELATED: What is Python?

Miniconda3 is basically a convenience tool. It allows you to download, install and manage all the necessary libraries to get Stable Diffusion working without much manual intervention. It will also be how we use Stable Diffusion.

Go 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. Installing Miniconda3 involves fewer page clicks than Git, but be careful with this option:

Check the box that says "All users".

Be sure to select “All Users” before clicking Next and finishing the installation.

You will be prompted to restart your computer after installing Git and Miniconda3. We don’t think it’s necessary, but it won’t hurt you if you do.

Download the Stable Diffusion GitHub repository and the latest checkpoint

Now that we have installed the prerequisite software, we are ready to download and install Stable Diffusion.

Download the latest checkpoint first – version 1.4 is almost 5GB so it may take a while. You must 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 it instead.

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

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

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

Now we need to prepare some folders where we will unpack all the Stable Diffusion files. Click the Start button and type “miniconda3” in the Start menu search bar, then click “Open” or press Enter.

We are going to create a folder called “stable broadcast” 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 every time you paste a block of code into a terminal, like Miniconda3, you have to hit Enter at the end to run the last command.

If all went well, you will see something like this:

Minoconda3 terminal showing commands running successfully.

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

Open the ZIP file, “stable-diffusion-main.zip”, that you downloaded from GitHub in your favorite file archiver. Alternatively, Windows can also open ZIP files on its own 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 created.

RELATED: Get help with File Explorer in 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 broadcast 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.

Do not interrupt this process. Some of the files are larger than a gigabyte, so it might take a while to download. If you accidentally interrupt the process, you will 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 above command.

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

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

We are in the last step of the installation. Go to “C: stable-diffusion stable-diffusion-main models ldm stable-diffusion-v1” in File Explorer, then copy and paste the checkpoint file (sd-v1-4.ckpt) into the folder.

Copy the model file to 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” in the highlighted box, then press Enter to rename the file.

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

RELATED: Tiny Windows 11 Context Menu Buttons Will Confuse People

Rename the model file

And that’s it, we’re done. We are ready to use Stable Broadcast now.

How to use stable diffusion

The ldm environment we created is essential and you must activate it every time you want to use Stable Diffusion. Get intoconda activate ldm in the Miniconda3 window and press “Enter”. The (ldm) on the left 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 (hence the commandcd) to “C: stable-diffusion stable-diffusion-main” before we can generate any images. Pasta cd C:stable-diffusionstable-diffusion-main on the 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 to 512 × 512 images. Here’s an example. Try this to make sure everything is working properly:

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 images.

Stable diffusion generating images.

That command will produce five cat images, all located in “C: stable-diffusion stable-diffusion-main outputs txt2img-samples samples”.

A cat in the style of Pablo Picasso.

It’s not perfect, but it clearly resembles Pablo Picasso’s style, just as we specified in the ad. The pictures of him should look similar but not necessarily identical.

Every time you want to change the image that is generated, you just need to change the text contained between the double quotes that follow. --prompt.

Advice: Don’t rewrite the whole line every time. Use the arrow keys to move the text cursor and simply replace the indicator.

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

Let’s say we wanted to generate a realistic looking squirrel in a magical forest wearing a wizard 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 possible. If you want something photorealistic, be sure to include terms related to a realistic image. If you want something inspired by a specific artist’s style, specify the artist.

Stable Diffusion is not limited to portraits and animals, it can also produce amazing landscapes.

A calm lake with mountains around it and a spectacular sky.

What do the arguments in the command mean?

Stable Diffusion has a huge number of settings and arguments that you can provide to customize your results. The few included here are basically necessary to ensure that Stable Diffusion runs on an average gaming PC.

  • –Plms: Specifies how the images will be sampled. There’s an article about it, if you want to check the math.
  • –N_iter: Specifies the number of iterations that you want to generate for each request. 5 is a decent number to see what kind of results you’re getting.
  • –N_samples: Specifies the number of samples to be generated. The default value is 3, but most computers don’t have enough VRAM to support that. Stick with 1 unless you have a specific reason to change it.

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

There’s a lot of trial and error involved to get great results, but that’s at least half the fun. Be sure to write or save arguments and descriptions that return the results you like. If you don’t want to do the whole experiment yourself, there are growing communities on Reddit (and elsewhere) dedicated to sharing images and the prompts that generated them.

[ad_2]