# Demo 01 Stable Diffusion Text to Image
course: Module 4 ā Generative AI & LLMs
module: Module-4-Generative-AI-LLMs
type: notebook
source_url: https://personal-learn.armco.dev/files/Module-4-Generative-AI-LLMs/General/21_June/Demo_01_Stable_Diffusion_Text_to_Image.ipynb
---
[cell 1 markdown]
# **Introduction to Image Generation with Stable Diffusion Pipeline**
### **Objective:**
To generate images from text prompts using the **Stable Diffusion pipeline**, demonstrating how AI models transform textual descriptions into high-quality visuals.
---
### **Note:**
- Before running any demo, ensure that the **`requirements.txt`** file is installed. This file contains all the required dependencies for **all demos and guided practices under _Image Generation Capabilities_**.
- Install the dependencies using the following command in the terminal: (Run this command twice if the packages are not installed properly)
**pip install -r requirements.txt**
- The installation process may take around 10- 15 minutes, once installed you select the kernal of that virtual environment
---
### **Steps to perform:**
1. Setup the Environment
2. Import the necessary libraries
3. Load the pretrained model
4. Move to GPU if available
5. Initialize list to store images
---
[cell 2 markdown]
### __Step 2: Import the neccessary libraries__
- Import **torch** for PyTorch functionality
- Import **StableDiffusionPipeline** from `diffusers` to generate images from text
- Import **matplotlib** for displaying generated images
[cell 3 code]
!pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
!pip install diffusers[torch] transformers accelerate safetensors xformers matplotlib
[cell 4 code]
import os
import matplotlib.pyplot as plt
import torch
from diffusers import StableDiffusionPipeline
[cell 5 markdown]
### __Step 3: Load the pretrained model__
- Define the model identifier (model_id) for the Stable Diffusion v1.5 model
- Load the pretrained model using StableDiffusionPipeline.from_pretrained, setting torch_dtype=torch.float16 for efficiency
[cell 6 code]
# Load the Stable Diffusion pipeline
model_id = "runwayml/stable-diffusion-v1-5"
pipeline = StableDiffusionPipeline.from_pretrained(
model_id,
torch_dtype=torch.float16
)
print("ā
Model loaded successfully.")
[cell 7 markdown]
### __Step 4: Move to GPU if available__
- Move the pipeline to GPU using .to("cuda") for faster processing if a GPU is available.
- Create a list of textual prompts (image_prompts) that will be used to generate images.
- Each prompt describes a scene or concept.
[cell 8 code]
# Move model to GPU (if available)
device = "cuda" if torch.cuda.is_available() else "cpu"
pipeline = pipeline.to(device)
print(f"ā
Pipeline moved to device: {device}")
# Define image prompts
image_prompts = [
"A futuristic sports car",
"A futuristic sports car, cinematic lighting",
"A futuristic sports car, cinematic lighting, ultra realistic",
"A futuristic sports car, cinematic lighting, ultra realistic, 8k, highly detailed",
"A futuristic sports car, cyberpunk city background, neon lights, cinematic, highly detailed"
]
[cell 9 markdown]
### __Step 5: Initailize list to store images__
- Create an empty list to store generated images
- For each prompt, generate an image using the pipeline
- Save the images to a local folder and display them inline
[cell 10 code]
# Create folder to save generated images
os.makedirs("generated_images", exist_ok=True)
# Initialize list to store generated images
generated_images = []
# Generate, save, and display images
for i, prompt in enumerate(image_prompts):
print(f"\nšØ Generating image {i+1} for prompt: '{prompt}'")
# Generate image from prompt
image = pipeline(prompt).images[0]
# Save image
image_path = f"generated_images/image_{i+1}.jpg"
image.save(image_path)
print(f"ā
Image saved as: {image_path}")
# Append to list
generated_images.append(image)
# Display all generated images in a 2Ć2 grid
fig, axes = plt.subplots(2, 2, figsize=(10, 10))
for ax, img, prompt in zip(axes.flat, generated_images, image_prompts):
ax.imshow(img)
ax.axis("off")
ax.set_title(prompt, fontsize=10)
plt.tight_layout()
plt.show()
print("ā
All generated images displayed successfully.")
[cell 11 markdown]
- These images will be stored inside the generated_images folder
[cell 12 markdown]
### __Conclusion__
The demo illustrated how to generate images from text prompts using the Stable Diffusion pipeline, showcasing AI's potential in creative fields. It covered setup, model loading, GPU utilization, and the generation process, emphasizing AI's role in transforming artistic expression and innovation.
---