Step-by-Step Guide to Create AI Images Using Stable Diffusion in Google Colab
Prerequisites
Before starting, ensure you have the following:
- Google Account: You need a Google account to access Google Colab.
- Google Colab: Go to Google Colab and create a new notebook.
- GPU Runtime: Enable GPU for faster processing:
- Go to
Runtime > Change runtime type > Hardware accelerator > GPU
.
- Go to
- Stable Diffusion Model: We’ll use the
runwayml/stable-diffusion-v1-5
model, which is pre-trained and available via thediffusers
library. - Python Libraries: Install the required libraries using
pip
(covered in Step 2).
Step 1: Open Google Colab
Go to Google Colab and create a new notebook.
Ensure you are using a GPU runtime for faster processing:
- Go to
Runtime > Change runtime type > Hardware accelerator > GPU
.
Step 2: Install Required Libraries
Install the necessary libraries using pip
:
!pip install diffusers transformers accelerate torch
Step 3: Import Required Libraries
Import the libraries into your notebook:
import torch
from diffusers import StableDiffusionPipeline
from PIL import Image
Step 4: Load the Stable Diffusion Model
Load the Stable Diffusion model:
# Load the Stable Diffusion pipeline
pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16)
pipe = pipe.to("cuda")
Step 5: Define Your Text Prompt
Define the text prompt for the image you want to generate:
prompt = "A futuristic cityscape at sunset with flying cars and neon lights, highly detailed, 4k resolution"
Step 6: Generate the Image
Generate the image using the prompt:
# Generate the image
with torch.autocast("cuda"):
image = pipe(prompt).images[0]
Step 7: Display the Image
Display the generated image:
image.show()
Step 8: Save the Image
Save the image to your local machine or Google Drive:
Option 1: Save to Local Machine
image.save("generated_image.png")
Option 2: Save to Google Drive
from google.colab import drive
drive.mount('/content/drive')
image.save("/content/drive/My Drive/generated_image.png")
Step 9: Experiment with Different Prompts
Try different prompts to generate various types of images. For example:
"A magical forest with glowing mushrooms and fairies"
"A steampunk airship flying over a desert"
"A realistic portrait of a lion with a majestic mane"
Full Code for Reference
# Step 1: Install libraries
!pip install diffusers transformers accelerate torch
# Step 2: Import libraries
import torch
from diffusers import StableDiffusionPipeline
from PIL import Image
# Step 3: Load the model
pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16)
pipe = pipe.to("cuda")
# Step 4: Define the prompt
prompt = "A futuristic cityscape at sunset with flying cars and neon lights, highly detailed, 4k resolution"
# Step 5: Generate the image
with torch.autocast("cuda"):
image = pipe(prompt).images[0]
# Step 6: Display the image
image.show()
# Step 7: Save the image (optional)
image.save("generated_image.png")
# Step 8: Save to Google Drive (optional)
from google.colab import drive
drive.mount('/content/drive')
image.save("/content/drive/My Drive/generated_image.png")
Tips for Better Results
- Be Descriptive: Use detailed prompts to guide the model.
- Adjust Image Size: Modify the image size using
height
andwidth
parameters. - Experiment with Styles: Add style keywords like “realistic,” “cartoon,” or “cyberpunk.”
Troubleshooting
- Out of Memory Error: Reduce the image size or use a simpler prompt.
- Slow Performance: Ensure you are using a GPU runtime in Colab.