{"id":64145,"date":"2022-09-02T19:52:24","date_gmt":"2022-09-02T19:52:24","guid":{"rendered":"https:\/\/harchi90.com\/how-to-run-stable-diffusion-on-your-pc-to-generate-ai-images\/"},"modified":"2022-09-02T19:52:24","modified_gmt":"2022-09-02T19:52:24","slug":"how-to-run-stable-diffusion-on-your-pc-to-generate-ai-images","status":"publish","type":"post","link":"https:\/\/harchi90.com\/how-to-run-stable-diffusion-on-your-pc-to-generate-ai-images\/","title":{"rendered":"How to Run Stable Diffusion on Your PC to Generate AI Images"},"content":{"rendered":"
\n

<\/p>\n

Artificial Intelligence (AI) art is currently all the rage, but most AI image generators run in the cloud. Stable Diffusion is different \u2014 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.<\/p>\n

What Is Stable Diffusion?<\/h2>\n

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.<\/p>\n

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

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.<\/p>\n

What Do You Need to Run Stable Diffusion on Your PC?<\/h2>\n

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:<\/p>\n

How to Install and Run Stable Diffusion on Windows<\/h2>\n

There are two pieces of software you need: Git and Miniconda3.<\/p>\n

Note:<\/strong> 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. <\/p><\/blockquote>\n

Installing Go<\/h3>\n

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.<\/p>\n

RELATED:<\/strong> What Is GitHub, and What Is It Used For?<\/em><\/strong><\/p>\n

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.<\/p>\n

There are several options you’ll be prompted to select while the installer runs \u2014 leave them on their default settings. One option page, \u201cAdjusting Your PATH Environment,\u201d is particularly important. It must be set to \u201cGit From The Command Line And Also From 3rd-Party Software.\u201d<\/p>\n

\"Make<\/p>\n

Installing Miniconda3<\/h3>\n

Stable Diffusion draws on a few different Python libraries. If you don’t know much about Python, don’t worry too about this \u2014 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.<\/p>\n

RELATED:<\/strong> What Is Python?<\/em><\/strong><\/p>\n

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.<\/p>\n

Head over to the Miniconda3 download page and click \u201cMiniconda3 Windows 64-bit\u201d to get the latest installer.<\/p>\n

\"\"<\/p>\n

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:<\/p>\n

\"Tick<\/p>\n

Make sure that you select \u201cAll Users\u201d before clicking next and finishing up the installation.<\/p>\n

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.<\/p>\n

Download the Stable Diffusion GitHub Repository and the Latest Checkpoint<\/h3>\n

Now that we’ve installed the pre-requisite software, we’re ready to download and install Stable Diffusion.<\/p>\n

Download the latest checkpoint first \u2014 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.<\/p>\n

Note:<\/strong> At the time of writing (September 2, 2022), the latest checkpoint is version 1.4. If there is a newer version, download that instead.<\/p><\/blockquote>\n

\"\"<\/p>\n

Click \u201csd-v1-4.ckpt\u201d to start the download.<\/p>\n

Note:<\/strong> The other file, \u201csd-v1-4-full-ema.ckpt\u201d, might <\/em>provide better results, but it is about twice the size. You can use either. <\/p><\/blockquote>\n

\"\"<\/p>\n

You then need to download Stable Diffusion from GitHub. Click on the green \u201cCode\u201d button, then click \u201cDownload ZIP.\u201d Alternatively, you can use this direct download link.<\/p>\n

\"\"<\/p>\n

Now we need to prepare a few folders where we’ll unpack all of Stable Diffusion’s files. Click the Start button and type \u201cminiconda3\u201d into the Start Menu search bar, then click \u201cOpen\u201d or hit Enter.<\/p>\n

\"\"<\/p>\n

We’re going to create a folder named \u201cstable-diffusion\u201d using the command line. Copy and paste the code block below into the Miniconda3 window, then press Enter.<\/p>\n

cd C:\/
\nmkdir stable-diffusion
\ncd stable-diffusion<\/pre>\n

Note:<\/strong> 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.<\/p><\/blockquote>\n

If everything went well, you’ll see something like this:<\/p>\n

\"Minoconda3<\/p>\n

Keep the Miniconda3 window open, we’ll need it again in a minute.<\/p>\n

Open up the ZIP file, \u201cstable-diffusion-main.zip,\u201d that you downloaded from GitHub in your favorite file archiving program. alternative, 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 \u201cC:stable-diffusion\u201d folder we just made.<\/p>\n

RELATED:<\/strong> Get Help With File Explorer on Windows 10<\/em><\/strong><\/p>\n

Drag and drop the folder in the ZIP file, \u201cstable-diffusion-main,\u201d into the \u201cstable-diffusion\u201d folder.<\/p>\n

\"Drag<\/p>\n

Go back to Miniconda3, then copy and paste the following commands into the window:<\/p>\n

cd C:stable-diffusionstable-diffusion-main
\nconda env create -f environment.yaml
\nconda activate ldm
\nmkdir modelsldmstable-diffusion-v1<\/pre>\n

\"Wait<\/p>\n

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<\/code> again. If that happens, navigate to \u201cC:Users(Your User Account).condaenvs\u201d and delete the \u201cldm\u201d folder, then run the previous command.<\/p>\n

Note:<\/strong> So, what did we just do? Python lets you sort coding projects into \u201cEnvironments.\u201d 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.<\/p>\n

The lines we ran created a new environment named \u201cldm,\u201d 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.<\/p>\n<\/blockquote>\n

We’re on the last step of the installation. Navigate to \u201cC:stable-diffusionstable-diffusion-mainmodelsldmstable-diffusion-v1\u201d in File Explorer, then copy and paste the checkpoint file (sd-v1-4.ckpt) into the folder.<\/p>\n

\"Copy<\/p>\n

Wait for the file to finish transferring, right-click \u201csd-v1-4.ckpt\u201d and then click \u201cRename.\u201d Type \u201cmodel.ckpt\u201d into the highlighted box, then hit Enter to change the file name.<\/p>\n

Note:<\/strong> If you’re running Windows 11, you won’t see \u201crename\u201d in the right-click context menu. There is an icon that looks like a miniature text field instead.<\/p><\/blockquote>\n

RELATED:<\/strong> Windows 11’s Tiny Context Menu Buttons Will Confuse People<\/em><\/strong><\/p>\n

\"Rename<\/p>\n

And that’s it \u2014 we’re done. We are ready to actually use Stable Diffusion now.<\/p>\n

How to Use Stable Diffusion<\/h2>\n

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<\/code> into the Miniconda3 window and hit \u201cEnter.\u201d The (ldm) on the left-hand side indicates that the ldm environment is active.<\/p>\n

Note:<\/strong> 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.<\/p><\/blockquote>\n

\"Activate<\/p>\n

Then we need to change the directory (thus the commandcd<\/code>) to \u201cC:stable-diffusionstable-diffusion-main\u201d before we can generate any images. Paste cd\u00a0C:stable-diffusionstable-diffusion-main<\/code> into command line.<\/p>\n

How to Make an Image with Stable Diffusion<\/h3>\n

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

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<\/pre>\n

Your console will give you a progress indicator as it produces the pictures.<\/p>\n

\"Stable<\/p>\n

That command will produce five cat images, all located at \u201cC:stable-diffusionstable-diffusion-mainoutputstxt2img-samplessamples\u201d.<\/p>\n

\"A<\/p>\n

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.<\/p>\n

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<\/code>.<\/p>\n

Medicine:<\/strong> Don’t rewrite the entire line every time. Use the arrow keys to move the text cursor around and just replace the prompt.<\/p><\/blockquote>\n

python scripts\/txt2img.py --prompt \"YOUR, DESCRIPTIONS, GO, HERE<\/strong>\" --plms --n_iter 5 --n_samples 1<\/pre>\n

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

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<\/pre>\n

\"A<\/p>\n

It really is that easy \u2014 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.<\/p>\n

Stable Diffusion isn’t limited to portraits and animals either, it can also produce striking landscapes.<\/p>\n

\"A<\/p>\n

What Do the Arguments in the Command Mean?<\/h3>\n

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.<\/p>\n

    \n
  • \u2013plms \u2014 Specifies how the images will be sampled. There is a paper about it, if you want to check out the math.<\/li>\n
  • \u2013n_iter \u2014 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.<\/li>\n
  • \u2013n_samples \u2014 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.<\/li>\n<\/ul>\n

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

    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.<\/p>\n<\/div>\n