# 6. Stable Diffusion / Image Generation

> **RTX 5090:** 5–8 seconds per image (SDXL) vs ~30 seconds on Mac
> **Why it matters:** Generate UI mockups, app icons, marketing assets, concept art — all locally and free

---

## What Is Stable Diffusion?

Stable Diffusion is an open-source text-to-image AI model. You describe what you want in plain English, and it generates a high-quality image in seconds. It runs entirely on your GPU.

```
┌──────────────────────────────────────────────────────────────────────┐
│ Stable Diffusion Pipeline                                            │
│                                                                      │
│  "A modern app dashboard with dark theme and blue accents"          │
│       │                                                              │
│       ▼                                                              │
│  [CLIP Text Encoder] → text embeddings                              │
│       │                                                              │
│       ▼                                                              │
│  [U-Net: iterative denoising × 20–50 steps]  ← GPU-intensive       │
│       │                                                              │
│       ▼                                                              │
│  [VAE Decoder] → pixel image                                        │
│       │                                                              │
│       ▼                                                              │
│  1024×1024 PNG image                                                 │
│                                                                      │
│  RTX 5090: ~5–8s per image (SDXL)                                   │
│  Mac M4 Pro: ~25–35s per image (SDXL via MPS)                       │
│                                                                      │
└──────────────────────────────────────────────────────────────────────┘
```

---

## Performance: Mac vs Razer

| Model            | Resolution | Mac M4 Pro (MPS) | Razer RTX 5090 (CUDA) | Speedup |
| ---------------- | ---------- | ---------------- | --------------------- | ------- |
| SD 1.5           | 512×512    | ~8–12s           | ~1–2s                 | ~5×     |
| SDXL             | 1024×1024  | ~25–35s          | ~5–8s                 | ~4×     |
| SDXL Turbo       | 1024×1024  | ~8–12s           | ~1–3s                 | ~4×     |
| FLUX.1 [dev]     | 1024×1024  | ~60–90s          | ~10–20s               | ~5×     |
| FLUX.1 [schnell] | 1024×1024  | ~15–25s          | ~3–6s                 | ~4×     |

### VRAM Requirements

| Model             | VRAM Needed | Fits in 24 GB? |
| ----------------- | ----------- | -------------- |
| SD 1.5            | ~4 GB       | ✅ Easily      |
| SDXL              | ~7 GB       | ✅ Easily      |
| SDXL + ControlNet | ~10 GB      | ✅ Yes         |
| FLUX.1 [dev]      | ~12 GB      | ✅ Yes         |
| FLUX.1 + LoRA     | ~14 GB      | ✅ Yes         |
| SD3 Medium        | ~12 GB      | ✅ Yes         |

**24 GB VRAM means every current image model fits comfortably.**

---

## How to Set Up

### Option A: ComfyUI (Node-Based — Recommended)

ComfyUI is a powerful node-based UI for Stable Diffusion. It's flexible, efficient, and well-suited for automation.

```bash
# Clone ComfyUI
cd ~
git clone https://github.com/comfyanonymous/ComfyUI.git
cd ComfyUI

# Create venv and install
python3.12 -m venv venv
source venv/bin/activate
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu124
pip install -r requirements.txt

# Download SDXL model (~6.5 GB)
mkdir -p models/checkpoints
cd models/checkpoints
wget "https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0/resolve/main/sd_xl_base_1.0.safetensors"

# Start ComfyUI
cd ~/ComfyUI
python main.py

# Open in browser: http://localhost:8188
# Accessible from Windows browser via WSL2 port forwarding
```

### Option B: Automatic1111 (Classic Web UI)

```bash
# Clone A1111
cd ~
git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git
cd stable-diffusion-webui

# Launch (auto-installs deps on first run)
bash webui.sh --listen --xformers

# Open: http://localhost:7860
```

### Option C: Python Script (No UI)

```python
"""Generate images with Stable Diffusion from Python."""
import torch
from diffusers import StableDiffusionXLPipeline

# Load SDXL
pipe = StableDiffusionXLPipeline.from_pretrained(
    "stabilityai/stable-diffusion-xl-base-1.0",
    torch_dtype=torch.float16,
    variant="fp16",
).to("cuda")

# Enable memory optimizations
pipe.enable_xformers_memory_efficient_attention()

# Generate
image = pipe(
    prompt="A sleek dark-themed mobile app dashboard showing AI brain categories, "
           "neon blue and teal accents, glassmorphism cards, modern UI design",
    negative_prompt="blurry, low quality, text, watermark",
    num_inference_steps=30,
    guidance_scale=7.5,
    width=1024,
    height=1024,
).images[0]

image.save("dashboard_concept.png")
print("Generated: dashboard_concept.png")
```

### Batch Generation Script

```python
"""Generate multiple variations of an image concept."""
import torch
from diffusers import StableDiffusionXLPipeline
import time

pipe = StableDiffusionXLPipeline.from_pretrained(
    "stabilityai/stable-diffusion-xl-base-1.0",
    torch_dtype=torch.float16,
    variant="fp16",
).to("cuda")
pipe.enable_xformers_memory_efficient_attention()

prompts = [
    "App icon for LysnrAI, microphone with sound waves, dark background, modern gradient",
    "App icon for MindLyst, brain with neural connections, dark background, blue teal gradient",
    "Feature graphic for LysnrAI, voice waveform visualization, dark theme, 1024x500",
    "Splash screen, abstract sound waves, dark navy background, teal highlights",
    "Dashboard mockup, dark theme, cards with charts, sidebar navigation, modern UI",
]

for i, prompt in enumerate(prompts):
    start = time.time()
    image = pipe(
        prompt=prompt,
        negative_prompt="blurry, low quality, text, watermark, ugly",
        num_inference_steps=30,
        guidance_scale=7.5,
        width=1024,
        height=1024,
        generator=torch.Generator("cuda").manual_seed(42 + i),
    ).images[0]

    elapsed = time.time() - start
    filename = f"generated_{i:02d}.png"
    image.save(filename)
    print(f"[{i+1}/{len(prompts)}] {filename} ({elapsed:.1f}s)")
```

---

## Real-World Use Cases for Your Projects

### 1. App Store Assets

Generate icon concepts, feature graphics, and screenshot backgrounds:

```python
# LysnrAI app icon variations
prompts = [
    "Minimal app icon, single microphone, dark navy background, glowing teal outline, flat design",
    "App icon, sound wave forming a brain shape, dark background, blue to teal gradient",
    "App icon, headphones with AI sparkles, dark background, modern glassmorphism",
]
```

### 2. UI/UX Mockup Exploration

Rapidly prototype visual ideas before coding:

```python
# Generate dashboard layout concepts
prompt = """
Web dashboard design, dark theme (#06070A background), sidebar navigation,
main content area with 3 cards showing brain categories,
teal and blue accent colors, modern glassmorphism,
high fidelity UI design, clean typography
"""
```

### 3. Marketing and Social Media

```python
# Blog post hero images
prompt = "Abstract AI visualization, neural network nodes connected by light beams, dark background, blue and teal colors, cinematic lighting"

# Social media posts
prompt = "Infographic style, voice AI assistant concept, microphone with sound waves transforming into text, dark modern design"
```

### 4. Concept Art for Features

```python
# Visualize MindLyst "Brains" concept
prompts = [
    "Digital brain labeled 'Work', organized files and charts floating around it, dark theme, blue glow",
    "Digital brain labeled 'Health', fitness and medical icons floating around it, dark theme, green glow",
    "Digital brain labeled 'Finance', money and graphs floating around it, dark theme, gold glow",
]
```

### 5. ControlNet (Image-Guided Generation)

Use an existing image as a structural guide:

```python
from diffusers import StableDiffusionXLControlNetPipeline, ControlNetModel

# Load ControlNet for edge-guided generation
controlnet = ControlNetModel.from_pretrained(
    "diffusers/controlnet-canny-sdxl-1.0",
    torch_dtype=torch.float16,
).to("cuda")

# Use your existing dashboard screenshot as a structural guide
# Generate a redesigned version with new visual style
```

---

## Benefits

| Benefit                     | Description                                                      |
| --------------------------- | ---------------------------------------------------------------- |
| **Speed**                   | 5–8s per image (vs 30s on Mac or waiting for cloud APIs)         |
| **Cost**                    | $0 per image (vs $0.02–0.08 per image for DALL-E 3 / Midjourney) |
| **Privacy**                 | Generated locally — no images sent to cloud                      |
| **Control**                 | Full parameter control: steps, guidance, seed, resolution        |
| **Reproducibility**         | Same seed = same image every time                                |
| **Customization**           | LoRA fine-tuning for brand-specific styles                       |
| **Batch capability**        | Generate hundreds of variations overnight                        |
| **No content restrictions** | No cloud content policies limiting your output                   |

### Cost Comparison (100 images)

| Method                           | Cost         | Time           |
| -------------------------------- | ------------ | -------------- |
| DALL-E 3 API                     | $4–8         | ~5 min (cloud) |
| Midjourney                       | $10–30/month | ~5 min (cloud) |
| Mac M4 Pro (SDXL, local)         | $0           | ~40–55 min     |
| **Razer RTX 5090 (SDXL, local)** | **$0**       | **~8–13 min**  |

---

## Skills You'll Learn

| Skill                     | What You'll Learn                                   | Career Value                 |
| ------------------------- | --------------------------------------------------- | ---------------------------- |
| **Diffusion models**      | How iterative denoising generates images from noise | Core generative AI knowledge |
| **Prompt engineering**    | Crafting effective text prompts for visual output   | Universal AI skill           |
| **ControlNet**            | Structural guidance for image generation            | Advanced image AI            |
| **LoRA training**         | Fine-tuning image models for specific styles        | Generative AI customization  |
| **ComfyUI workflows**     | Node-based visual programming for AI pipelines      | Production image generation  |
| **Image post-processing** | Upscaling, inpainting, outpainting techniques       | Digital content creation     |
| **VRAM optimization**     | Model offloading, attention optimization, tiling    | GPU memory management        |
| **Batch automation**      | Scripting large-scale image generation              | Production engineering       |
| **Model selection**       | SD 1.5 vs SDXL vs FLUX — trade-offs                 | Practical AI judgment        |

---

## Advanced: FLUX.1 (Latest Generation)

FLUX.1 is the newest open-source image model (from Black Forest Labs / Stability AI alumni). Better quality than SDXL.

```bash
# FLUX.1 [schnell] — fast, 4 steps
# FLUX.1 [dev] — high quality, 50 steps

# Fits in 24 GB VRAM with FP16
python3 -c "
from diffusers import FluxPipeline
import torch

pipe = FluxPipeline.from_pretrained(
    'black-forest-labs/FLUX.1-schnell',
    torch_dtype=torch.float16,
).to('cuda')

image = pipe('A futuristic AI assistant interface, holographic UI, dark theme').images[0]
image.save('flux_test.png')
"
```

---

## Next Steps

- [ ] Install ComfyUI in WSL2 and verify CUDA acceleration
- [ ] Download SDXL base model and generate first test image
- [ ] Generate app icon concepts for LysnrAI and MindLyst
- [ ] Try ControlNet with an existing dashboard screenshot
- [ ] Experiment with FLUX.1 [schnell] for higher quality
- [ ] Create a batch script for generating marketing assets
- [ ] Build a style LoRA trained on your brand colors/aesthetic
