Bird
Raised Fist0
Prompt Engineering / GenAIml~10 mins

Stable Diffusion overview in Prompt Engineering / GenAI - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the main library used for Stable Diffusion.

Prompt Engineering / GenAI
import [1]
Drag options to blanks, or click blank then click option'
Atorch
Btensorflow
Cnumpy
Dpandas
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing tensorflow which is another deep learning library but not the main one used here.
Choosing numpy or pandas which are not for deep learning.
2fill in blank
medium

Complete the code to load a pre-trained Stable Diffusion model using the diffusers library.

Prompt Engineering / GenAI
from diffusers import StableDiffusionPipeline
pipeline = StableDiffusionPipeline.from_pretrained([1])
Drag options to blanks, or click blank then click option'
A'gpt2'
B'bert-base-uncased'
C'resnet50'
D'stable-diffusion-v1-4'
Attempts:
3 left
💡 Hint
Common Mistakes
Using model names for other AI models like GPT or BERT.
Using image classification model names like resnet50.
3fill in blank
hard

Fix the error in the code to generate an image from a text prompt using the pipeline.

Prompt Engineering / GenAI
image = pipeline([1]="a beautiful sunset over mountains").images[0]
Drag options to blanks, or click blank then click option'
Aprompt
Bdescription
Cinput
Dtext
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'text' or 'input' which are not valid parameter names here.
Using 'description' which is not recognized by the pipeline.
4fill in blank
hard

Fill both blanks to convert the generated image to a numpy array and display its shape.

Prompt Engineering / GenAI
import numpy as np
image_array = np.[1](image)
print(image_array.[2])
Drag options to blanks, or click blank then click option'
Aarray
Bshape
Csize
Dtolist
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'size' which gives total elements, not shape.
Using 'tolist' which converts array to list, not what is asked.
5fill in blank
hard

Fill all three blanks to save the generated image to a file named 'output.png'.

Prompt Engineering / GenAI
from PIL import Image
img = Image.[1](image_array)
img.[2]('output.png', [3]='PNG')
Drag options to blanks, or click blank then click option'
Afromarray
Bsave
Cformat
Dopen
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'open' instead of 'fromarray' to create the image.
Not specifying the format or using wrong method to save.

Practice

(1/5)
1. What is the main purpose of Stable Diffusion in AI?
easy
A. To translate languages automatically
B. To analyze financial data
C. To create images from text descriptions
D. To detect spam emails

Solution

  1. Step 1: Understand Stable Diffusion's function

    Stable Diffusion is designed to generate images based on text prompts.
  2. Step 2: Compare with other options

    Other options describe different AI tasks unrelated to image generation.
  3. Final Answer:

    To create images from text descriptions -> Option C
  4. Quick Check:

    Stable Diffusion = image generation from text [OK]
Hint: Remember: Stable Diffusion = text to image [OK]
Common Mistakes:
  • Confusing Stable Diffusion with language translation
  • Thinking it analyzes data instead of creating images
  • Mixing it up with spam detection tools
2. Which of the following is the correct way to give a prompt to Stable Diffusion?
easy
A. "A sunny beach with palm trees"
B. generate_image(sunny beach palm trees)
C. image.create('sunny beach')
D. createImage: sunny beach, palm trees

Solution

  1. Step 1: Identify proper prompt format

    Stable Diffusion accepts text prompts as strings describing the image.
  2. Step 2: Check options for correct syntax

    Only "A sunny beach with palm trees" uses a simple text string suitable as a prompt.
  3. Final Answer:

    "A sunny beach with palm trees" -> Option A
  4. Quick Check:

    Prompt = plain text string [OK]
Hint: Prompts are plain text descriptions in quotes [OK]
Common Mistakes:
  • Using code-like syntax instead of plain text
  • Omitting quotes around the prompt
  • Mixing function calls with prompt text
3. Given the prompt "A cat sitting on a red chair", what kind of output should Stable Diffusion produce?
medium
A. A text description of a cat on a chair
B. An image showing a cat sitting on a red chair
C. A list of cat breeds
D. A video of a cat on a chair

Solution

  1. Step 1: Understand prompt to output relation

    Stable Diffusion generates images based on text prompts.
  2. Step 2: Match prompt to output type

    The prompt describes a scene; the output is an image of that scene.
  3. Final Answer:

    An image showing a cat sitting on a red chair -> Option B
  4. Quick Check:

    Text prompt -> image output [OK]
Hint: Text prompt means image output, not text or video [OK]
Common Mistakes:
  • Expecting text output instead of image
  • Confusing image generation with video creation
  • Thinking it lists information instead of creating visuals
4. You gave the prompt "A futuristic cityscape at night" but the output image is blurry and unclear. What is a likely cause?
medium
A. The input text was too long
B. The model does not support night scenes
C. Stable Diffusion only creates black and white images
D. The prompt was too simple or vague

Solution

  1. Step 1: Analyze prompt clarity impact

    Simple or vague prompts can cause unclear images because the model lacks detail to generate sharp visuals.
  2. Step 2: Evaluate other options

    Stable Diffusion supports night scenes and color images; prompt length is not the main issue here.
  3. Final Answer:

    The prompt was too simple or vague -> Option D
  4. Quick Check:

    Clear prompts = better images [OK]
Hint: Use detailed prompts for clear images [OK]
Common Mistakes:
  • Assuming model can't create night scenes
  • Thinking Stable Diffusion only makes black and white images
  • Blaming prompt length instead of prompt detail
5. You want to create an image of a "red apple on a wooden table" but the generated image shows a green apple. What should you do to fix this?
hard
A. Add more detail to the prompt like "a bright red apple on a rustic wooden table"
B. Use a shorter prompt like "apple table"
C. Change the model to one that only creates fruit images
D. Remove color words from the prompt

Solution

  1. Step 1: Understand prompt specificity effect

    Adding more descriptive details helps the model focus on the correct colors and objects.
  2. Step 2: Evaluate other options

    Shorter or vague prompts reduce clarity; changing models unnecessarily or removing color words won't fix the color issue.
  3. Final Answer:

    Add more detail to the prompt like "a bright red apple on a rustic wooden table" -> Option A
  4. Quick Check:

    Detailed prompts improve image accuracy [OK]
Hint: Make prompts detailed to get correct colors [OK]
Common Mistakes:
  • Using vague or too short prompts
  • Ignoring color details in the prompt
  • Switching models without reason