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:

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.

Step 5: Connect to Hugging Face

Run the fourth cell. You’ll see something like this:

Screenshot of Google Colab Requesting We Authenticate with Hugging Face Token
Screenshot of Google Colab Requesting We Authenticate with Hugging Face Token

This means we need to authenticate with Hugging Face. In that field we need to input a token, which is like a password. To do this click the link where it says your Hugging Face tokens page, which takes you to https://huggingface.co/settings/tokens.

Assuming that you created an account, as we covered earlier, you should either be able to log in or already be logged in.

Click on New token and you should see a window like the following. Give it a name (it’s just for reference, use any name you want), and set the Role to write. Click Generate a token.

Popup to create hugging face token
Create Hugging Face Token with write role.

Now your token should be created.

Token Created

Hugging Face Token CreatedJust click the icon near Show to copy it, and we’ll go back to our Google Colab to paste it.

After you pasted it into the token field and click Login. After a few seconds you’ll see something like this. Ignore the red text, if you’re seeing that then everything looks good.

Hugging Face Successful Login
Hugging Face Successful Login

Step 6: Request Access to Hugging Face Stable Diffusion Repository

We’ll need to agree to some terms to access Stable Diffusion. By this we’re agreeing to share our email and username (that we used for Hugging Face) with the authors of Stable Diffusion.

To do this go to https://huggingface.co/CompVis/stable-diffusion-v-1-4-original, scroll down a little, click the checkmark to accept the terms and click Access repository to gain access.

Agree and Access Hugging Face Repository
Agree to Terms and Access Hugging Face Repository

Now we should be good to go.

Step 7: 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 8: 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

Troubleshooting

HTTPError: 403 Client Error: Forbidden for url

You get the error:

HTTPError: 403 Client Error: Forbidden for url: https://huggingface.co/api/models/CompVis/stable-diffusion-v1-4/revision/fp16

This error happens when you try to download the models from Hugging Face, but did not agree to the terms. To fix this just follow Step 6, where we agree to the terms and request access to the repository.

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.

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

13 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Sorin
25 days 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
24 days 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
24 days 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
24 days ago
Reply to  EdXD

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

sam
sam
15 days 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
13 days 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
3 days 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
You May Also Like