Embark on an exciting journey as I reveal how to harness the power of deep learning to generate captivating images (Generative AI) from textual prompts using Python with Data Storytelling. Explore the extensive possibilities in design, art, and advertising as this comprehensive guide takes you step-by-step through using pre-trained models to craft striking visuals. Dive into a complete end-to-end solution, complete with code, results, to master the art of generating images from text prompts.
Discover the fascinating world of generative AI in education through my captivating blog! In this immersive guide, we’ll explore:
This article was published as a part of the Data Science Blogathon.
In this project, we will delve into a deep learning method to produce quality images from textual descriptions, specifically targeting applications within the education sector. This approach offers significant opportunities for enriching learning experiences by providing personalized and captivating visual stories. By leveraging pre-trained models such as Stable Diffusion and GPT-2, we will generate visually appealing images that accurately capture the essence of the provided text inputs, ultimately enhancing educational materials and catering to a variety of learning styles.
The primary objective of this project is to create a deep learning pipeline capable of generating visually engaging and precise images based on textual inputs. The project’s success will be gauged by the quality and accuracy of the images generated in comparison to the given text prompts, showcasing the potential for enriching educational experiences through captivating visuals.
To successfully follow along with this project, you will need the following:
This comprehensive guide provides a detailed end-to-end solution, including code and output harnessing the power of two robust models, Stable Diffusion and GPT-2, to generate visually engaging images from the textual stimulus.
Stable Diffusion is a generative model rooted in the denoising score-matching framework, designed to create visually cohesive and intricate images by emulating a stochastic diffusion process. The model functions by progressively introducing noise to an image and subsequently reversing the process, reconstructing the image from a noisy version to its original form. A deep neural network, known as the denoising score network, guides this reconstruction by learning to predict the gradient of the data distribution’s log-density. The final outcome is the generation of visually engaging images that closely align with the desired output, guided by the input textual prompts.
Source: www.eyerys.com
GPT-2, the Generative Pre-trained Transformer 2, is a sophisticated language model created by OpenAI. It builds on the Transformer architecture and has undergone extensive pre-training on a substantial volume of textual data, empowering it to produce a contextually relevant and coherent text. In our project, GPT-2 is employed to convert the given textual inputs into a format suitable for the Stable Diffusion model, guiding the image generation process. The model’s ability to comprehend and generate contextually fitting text ensures that the resulting images align closely with the input prompts.
Combining these two models’ strengths, we generate visually impressive images that accurately represent the given textual prompts. The fusion of Stable Diffusion’s image generation capabilities and GPT-2’s language understanding allows us to create a powerful and efficient end-to-end solution for generating high-quality images from text.
Source:jalammar.github.io
Step 1: Set up the environment
We begin by installing the required libraries and importing the necessary components for our project. We will use the Diffusers and Transformers libraries for deep learning, OpenCV and Matplotlib for image display and manipulation, and Google Drive for file storage and access.
# Install required libraries
!pip install --upgrade diffusers transformers -q
# Import necessary libraries
from pathlib import Path
import tqdm
import torch
import pandas as pd
import numpy as np
from diffusers import StableDiffusionPipeline
from transformers import pipeline, set_seed
import matplotlib.pyplot as plt
import cv2
from google.colab import drive
Step 2: Access the dataset
We will mount Google Drive to access our dataset and other files in this step. We will load the CSV file containing the textual prompts and image IDs and update the file paths accordingly.
# Mount Google Drive
drive.mount('/content/drive')
# Update file paths
data = pd.read_csv('/content/drive/MyDrive/SD/promptsRandom.csv', encoding='ISO-8859-1')
prompts = data['prompt'].tolist()
ids = data['imgId'].tolist()
dir0 = '/content/drive/MyDrive/SD/'
Step 3: Visualize the images and prompts
Using OpenCV and Matplotlib, we will display the images from the dataset and print their corresponding textual prompts. This step allows us to familiarize ourselves with the data and ensure it has been loaded correctly.
# Display images
for i in range(len(data)):
img = cv2.imread(dir0 + 'sample/' + ids[i] + '.png') # Include 'sample/' in the path
plt.figure(figsize=(2, 2))
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.axis('off')
plt.show()
print(prompts[i])
print()
Step 4: Configure the deep learning models: We will define a configuration class (CFG) to set up the deep learning models used in the project. This class specifies parameters such as the device used (GPU or CPU), the number of inference steps, and the model IDs for the Stable Diffusion and GPT-2 models.
We will also load the pre-trained models using the Hugging Face API and configure them with the necessary parameters.
# Configuration
class CFG:
device = "cuda"
seed = 42
generator = torch.Generator(device).manual_seed(seed)
image_gen_steps = 35
image_gen_model_id = "stabilityai/stable-diffusion-2"
image_gen_size = (400, 400)
image_gen_guidance_scale = 9
prompt_gen_model_id = "gpt2"
prompt_dataset_size = 6
prompt_max_length = 12
# Replace with your Hugging Face API token
secret_hf_token = "XXXXXXXXXXXX"
# Load the pre-trained models
image_gen_model = StableDiffusionPipeline.from_pretrained(
CFG.image_gen_model_id, torch_dtype=torch.float16,
revision="fp16", use_auth_token=secret_hf_token, guidance_scale=9
)
image_gen_model = image_gen_model.to(CFG.device)
prompt_gen_model = pipeline(
model=CFG.prompt_gen_model_id,
device=CFG.device,
truncation=True,
max_length=CFG.prompt_max_length,
num_return_sequences=CFG.prompt_dataset_size,
seed=CFG.seed,
use_auth_token=secret_hf_token
)
Step 5: Generate images from prompts: We will create a function called ‘generate_image’ to generate images from textual prompts using the Stable Diffusion model. The function will input the textual prompt and model and generate the corresponding image.
Afterward, we will display the generated images alongside their corresponding textual prompts using Matplotlib.
# Generate images function
def generate_image(prompt, model):
image = model(
prompt, num_inference_steps=CFG.image_gen_steps,
generator=CFG.generator,
guidance_scale=CFG.image_gen_guidance_scale
).images[0]
image = image.resize(CFG.image_gen_size)
return image
# Generate and display images for given prompts
for prompt in prompts:
generated_image = generate_image(prompt, image_gen_model)
plt.figure(figsize=(4, 4))
plt.imshow(generated_image)
plt.axis('off')
plt.show()
print(prompt)
print()
Our project also experimented with generating images using custom textual prompts. We used the ‘generate_image’ function with a user-defined prompt to showcase this. In this example, we chose the custom prompt: “The International Space Station orbits gracefully above Earth, its solar panels shimmering”. The code snippet for this is shown below:
custom_prompt = "The International Space Station orbits gracefully above Earth, its solar panels shimmering"
generated_image = generate_image(custom_prompt, image_gen_model)
plt.figure(figsize=(4, 4))
plt.imshow(generated_image)
plt.axis('off')
plt.show()
print(custom_prompt)
print()
Let’s create a simple story with five textual prompts, generate images for each, and display them sequentially.
Story:
A lonely astronaut floats in space, surrounded by stars.
The astronaut discovers a mysterious, abandoned spaceship.
The astronaut enters the spaceship and finds an alien map.
The map leads the astronaut to a hidden planet filled with lush vegetation.
The astronaut explores the new planet, filled with excitement and wonder.
Now, let’s write the code to generate and display images for each prompt:
story_prompts = [
"A lonely astronaut floats in space, surrounded by stars.",
"The astronaut discovers a mysterious, abandoned spaceship.",
"The astronaut enters the spaceship and finds an alien map.",
"The map leads the astronaut to a hidden planet filled with lush vegetation.",
"The astronaut decides to explore the new planet, filled with excitement and wonder."
]
# Generate and display images for each prompt in the story
for prompt in story_prompts:
generated_image = generate_image(prompt, image_gen_model)
plt.figure(figsize=(4, 4))
plt.imshow(generated_image)
plt.axis('off')
plt.show()
print(prompt)
print()#import csv
Executing the above code will generate images for each story prompt, displaying them sequentially along with their corresponding textual prompts. This demonstrates the model’s ability to create a visual narrative based on a sequence of textual prompts, showcasing its potential for storytelling and animation.
This comprehensive guide explores a deep learning approach to generate visually captivating images from textual prompts. By harnessing the power of pre-trained Stable Diffusion and GPT-2 models, an end-to-end solution is provided in Python, complete with code and outputs. This project demonstrates the vast potential deep learning holds in industries that require custom and unique visuals for various applications like storytelling, which is highly useful for AI in Education.
5 Key Takeaways: Harnessing Generative AI for Visual Storytelling in Education
The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.