Get Started With Stable Diffusion (Free) in Google Colab for AI Generated Art

A cyborg painter in a beret and apron, holding a brush and palette, in an art studio. Illustrated in a realistic and detailed style by Wei Wang, ArtStation, making heavy use of light and shadow to create a sense of mystery and foreboding
A cyborg painter in a beret and apron, holding a brush and palette, in an art studio. Illustrated in a realistic and detailed style by Wei Wang, ArtStation, making heavy use of light and shadow to create a sense of mystery and foreboding

Stable Diffusion by Stability.ai is one of the best AI text-to-image generation software, as of writing this article. A few notable things about Stable Diffusion:

  • It generates high quality, coherent, and beautiful images based very fast, with much less resources than other image generation software.
  • The team behind it seems to be extremely open and transparent. They seem to aim to give power to the people.
  • Even if Stable Diffusion is also paid, they have made it available to the public, and we can use it via Hugging Face Spaces and we can also use Stable Diffusion from Hugging Face via Google Colab, which is free, and is the method which we’ll be using.

Other very similar software include Dall-E 2, MidJourney and Disco Diffusion (which we also covered in a separate tutorial)

In this tutorial we’ll get started with Stable Diffusion on Google Colab. We won’t dive into further details, and we just want to generate our first image. Hopefully this will give you the confidence to start with image generation using Google Colab.

You don’t have to know anything about programming to follow this tutorial. We’ll simply run some code, observe the results and try to understand what’s going on.

We recommend you also check out our newer tutorial on a variant of Stable Diffusion with a web user interface. It’s easier to use, still uses Google Colab for free, and has many more features available.

Here’s a quick demo to see how fast and effortlessly you can generate images using Stable Diffusion in Google Colab:

tl;dr AI News Section Preview
tl;dr AI News Section Preview

Sidenote: AI art tools are developing so fast it’s hard to keep up.

We set up a newsletter called tl;dr AI News.

In this newsletter we distill the information that’s most valuable to you into a quick read to save you time. We cover the latest news and tutorials in the AI art world on a daily basis, so that you can stay up-to-date with the latest developments.

Check tl;dr AI News

Getting Started with Stable Diffusion (on Google Colab)

We’ll start with a quick demo of running Stable Diffusion on Google Colab from start to finish, until we generate our first images.

Quick Video Demo – Start to First Image

The video also has timestamps to help you better understand the steps taken. Hopefully it gives you an overview of what we’re about to do.

Step 1: Create an Account on Hugging Face

We’ll download Stable Diffusion from Hugging Face. For this we need an account with them. Hugging Face is, in simple terms, a repository for working with different models, similar to Stable Diffusion, other than that it has many useful functionalities. A model is basically like a computer program that can learn to do things on its own.

The process is very straightforward. Just visit https://huggingface.co/join and create an account like you’d normally do, and check your email to confirm it.

Step 2: Copy the Stable Diffusion Colab Notebook into Your Google Drive

Next, just like with any Google Doc written by someone else that we need to edit, first visit the Stable Diffusion Google Colab (https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers/stable_diffusion.ipynb) and go to File > Save a copy in Drive.

Screenshot Demonstrating How To Save Stable Diffusion to Drive
Google Colab Menu – File > Save a copy in Drive

A new tab should open with the notebook saved to your drive. Only now it’s named Copy of Stable Diffusion with 🧨 diffusers. You can rename anything you want.

Step 3: Make Sure You’re Using GPU

To run Stable Diffusion we’ll need to make sure our Google Colab is using a GPU. To do this, in the menu go to Runtime > Change runtime type.

google colab menu > runtime > change runtime type
Google Colab Menu > Runtime > Change runtime type

A small window will appear with a dropdown under Hardware accelerator. We want to see GPU there.

Screenshot of the small popup window - Hardware Accelerator > GPU
Hardware accelerator > GPU

Click save, and we can move on.

Step 4: Run The First Cells

Now we can run the first cells in the Stable Diffusion colab. Just hover with your mouse on the every one of them, and a play button will appear. Just click it and wait for it to finish. It will display a green checkmark when a cell is done.

You can see that each cell has a description above it of what it does.

Running the first 3 cells
Run the first 3 cells in Google Colab

If something like “this notebook requires high ram” appears, just click ok.

Now we should be good to go.

Step 5: Run the Fifth Cell to Download Required Files

Next we’ll run the fifth cell, under Stable Diffusion Pipeline, that will download some the necessary components.

Run Cell Under Stable Diffusion Pipeline
Stable Diffusion Pipeline

Also run the next cell, that says pipe = pipe.to("cuda"):

Click Cell to Move Pipeline to GPU
Click Sixth Cell to Move Pipeline to GPU

Step 6: Generate Our First Image

Well done. Now we can generate our first image.

In the next cell, where you’re probably already seeing an image under it, is where we generate our first image.

Just write a text in the quotes, that you want turned into an image, and run the cell.

In the following example I wrote a protoss cityscape with advanced technology, inspired by the game starcraft, making heavy use of light and shadow to create a sense of mystery and foreboding. the city sprawling below is a mix of organic and inorganic, with swirling energy currents and strange crystalline structures, illustrated in a realistic and detailed style by wei wang, artstation.

Generated First Image with Stable Diffusion on Google Colab
Generating Your First Image with Stable Diffusion on Google Colab

Well done! That image generation should have taken under a minute.

Generate Multiple Images at a Time

In the initial demo video you’ll see we’re also generating 3 images at a time. To do this scroll a bit further and you’ll see the following cells.

Just run the one that starts with from PIL import Image and in the following one edit the text in ["blah blah"] and run it.

Screenshot of Generating Multiple Images at a Time with Stable Diffusion
Generating Multiple Images at a Time

Conclusion

Congratulations! Hopefully this guide got you to generate your first image using Stable Diffusion from Hugging Face on Google Colab. From here you can explore the other instructions in the Google Colab notebook. Our purpose here was to just get you up and running to get through that initial barrier.

If you encountered any issues or have any questions feel free to leave a comment and we’ll get back to you as soon as we can.

15 Shares:
Subscribe
Notify of
guest
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)

34 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Sorin
5 months ago

Everything worked perfectly.

Except when I wanted to generate 3 images. I’m getting this error:
“NameError                 Traceback (most recent call last)
<ipython-input-13-03a816915603> in <module>
   5  images = pipe(prompt)[“sample”]
   6 
—-> 7 grid = image_grid(images, rows=1, cols=3)
   8 grid

NameError: name ‘image_grid’ is not defined”

Any idea why?

Thanks!

Barly
Barly
5 months ago

Hi, thanks for this guide, it was very helpful!

I have a few follow-up questions:

– When I want to go back after I closed the tab, where do I go? Simply the same URL, that is https://colab.research.google.com/drive/ and then 33 alphanumeric signs, or is that only to set it up the first time?

– When I want to create the next image, after I had closed the tab, do I have to run all the cells again? (done in just 2 min, but I’m not sure if need to or can save myself the time)

– How do I add all those modifying things like size, stylize parameters and in particular img2img? (I understand how to upload a picture and copy its path, but not how to add it to the prompt, if possible.)

Barly
Barly
5 months ago
Reply to  EdXD

Thanks again. Yes, that looks cool!

I went through the setup and it says “Download the stable diffusion model (s-d-v1-4.ckpt) file from Hugging Face Stable Diffusion”. There, there isn’t a simple file or download link, and I don’t yet know enough about models, weights and diffusers, it seems. How do I get that file, and would it include all the other files or folders (feature_extractor, safety_checker)?

Sorry for the dummy questions – that’s the sort of thing that is completely self-explanatory once you know it, and impenetrable to outsiders.

Barly
Barly
5 months ago
Reply to  EdXD

Yes, seems to work so far, I’m playing with the parameters now. Thanks a lot again!

sam
sam
4 months ago
Reply to  EdXD

I did all of this but then when I go to my URL it is asking for username and pw. I did add a PW in step 2.1 ‘share password’ but I don’t know what the username it is asking for gradio.app??

gib
gib
4 months ago

Hi thanx very much for your guide !
I tried as you recomand it the webui by altryne and it worked perfectly.
After a moment it deconnected, so i tryied to reconnect unsuccesfully.
After several attempts I finally decided to relaunch completly, and for an unknown reason when I launched the public url as i already did the first time it opens me a completly different interface much less interesting with much less choice. Impossible to choose the seed, or the size of the image for example…
It appears that i’m not anymore on the interface by Altryn, but created by CompVis and Stability AI, adapted by JPH Productions.
Have you got an idea of what happened ?
Thanx again for your very helpfull work, and excuse me for my poor english 😉

Jean
4 months ago

Hello,
In the forth cell, I don’t get the option to enter my Token and login (or the HuggingFace logo). When I press “play”, all I get is the text shown on the screenshot

Google colab Huggingface error.png
Wwarped
Wwarped
4 months ago

Step 7 fails to run.

sodii
sodii
3 months ago

How do you turn off NSFW filters for google colab with this method?

Lean
Lean
3 months ago

Hi! This guide is awesome and it worked perfectly for me. Thanks!
Do you know how i can upload a picture and edit it?

Ferran
2 months ago

How can we use Stable Diffusion v2?

Henrique
Henrique
2 months ago

Hey, thanks for the tutorial!

I’m getting an error when running the 5th cell. It seems all the files are being correctly downloaded, but something is happening with “from_pretrained” method.

Do you know what could be problem? Maybe a recent bug?

Thanks again!

Captura de tela 2022-12-02 000739.jpg
|Grave18
|Grave18
1 month ago
Reply to  Henrique

We need to accept something in repo but there is nothing to accept ont this page https://huggingface.co/CompVis/stable-diffusion-v-1-4-original

|Grave18
|Grave18
1 month ago
Reply to  EdXD

Hello, same thing

Screenshot_1.png
|Grave18
|Grave18
1 month ago
Reply to  EdXD

Hello

Screenshot_1.png
|Grave18
|Grave18
1 month ago
Reply to  EdXD

I don’t have that folder on my Google drive.

|Grave18
|Grave18
1 month ago
Reply to  EdXD

Perfect, now it works 🙂

Savannah
Savannah
1 month ago

I get to the point where I can open the link to the UI but then it asks for a username and password. My google email doesn’t work. I set my password as 1234. But the username does not work.

Bulat!
Bulat!
15 days ago

Can I upload custom models (checkpoints, etc.)?

You May Also Like