aryadomain's picture
Add files using upload-large-folder tool
ef8f3ad verified
|
Raw
History Blame Contribute Delete
34.6 kB
# Reward-Guided Gradient Ascent for Stable Diffusion
A comprehensive system for improving Stable Diffusion image generation quality using gradient ascent optimization on Latent Reward Model (LRM) scores during inference.
## Table of Contents
- [Overview](#overview)
- [Features](#features)
- [Installation](#installation)
- [Quick Start](#quick-start)
- [Architecture](#architecture)
- [Understanding Reward Calculation](#understanding-reward-calculation)
- [Learning Rate Scheduling](#learning-rate-scheduling)
- [Configuration Presets](#configuration-presets)
- [Evaluation Metrics](#evaluation-metrics)
- [Model Variants](#model-variants)
- [Datasets](#datasets)
- [Usage Examples](#usage-examples)
- [API Reference](#api-reference)
- [Command-Line Options](#command-line-options)
- [Output Files](#output-files)
- [Troubleshooting](#troubleshooting)
- [Best Practices](#best-practices)
- [Changelog](#changelog)
---
## Overview
This project implements **test-time optimization** for Stable Diffusion using gradient ascent on the LRM reward model. Unlike the main LPO training which uses the reward model for training, this approach applies it during inference to improve generation quality without retraining.
### Key Capabilities
- **Gradient Ascent Optimization**: Iteratively improve latents using reward gradients
- **Learning Rate Scheduling**: Multiple strategies (constant, linear, cosine, exponential, step)
- **Momentum Optimization**: Standard and Nesterov momentum for better convergence
- **Multiple Metrics**: FID, CLIP, Aesthetic, PickScore, HPSv2, ImageReward
- **Model Variants**: Support for Origin, SPO, DPO, and LPO SD1.5 models
- **Dataset Flexibility**: COCO and Pick-a-Pic validation datasets
- **Configuration Presets**: 15 pre-tuned configurations for various use cases
---
## Features
### 1. **Advanced Optimization**
- **5 LR Schedulers**: Constant, Linear, Cosine, Exponential, Step-wise
- **Momentum Support**: Standard momentum and Nesterov momentum
- **Configurable Timestep Ranges**: Apply gradients at specific denoising steps
- **Dynamic Learning Rates**: LR changes during optimization for better convergence
### 2. **Comprehensive Evaluation**
- **6 Quality Metrics**: FID, CLIP, Aesthetic, PickScore, HPSv2, ImageReward
- **Baseline Comparison**: Compare with and without gradient ascent
- **Detailed Statistics**: Track reward improvements, gradient norms, LR history
- **Batch Processing**: Efficient evaluation on large datasets
- **Reward Visualization**: Automatic plotting of reward progression across timesteps
- **Timestep-Aware Tracking**: Monitor rewards at every denoising step, final t=0 latent reported
### 3. **Model Flexibility**
- **4 SD1.5 Variants**: Origin, SPO, DPO, LPO
- **Auto-Configuration**: CFG scale auto-adjusted for model variants
- **Easy Switching**: Change models with a single flag
### 4. **Dataset Support**
- **COCO Validation**: Standard benchmark with reference images
- **Pick-a-Pic Validation**: Large-scale human preference dataset
- **Streaming Support**: Handle large datasets efficiently
---
## Installation
### Requirements
```bash
# Core dependencies
pip install torch diffusers transformers torchmetrics datasets huggingface-hub
# For evaluation metrics
pip install pillow numpy scipy tqdm
# Optional: for better performance
pip install xformers # For memory-efficient attention
```
### Setup
```bash
cd /path/to/LPO/Reward
# Verify installation
python -c "from lr_scheduler import create_lr_scheduler; print('βœ“ LR Scheduler OK')"
python -c "from grad_ascent_configs import list_configs; print('βœ“ Configs:', len(list_configs()))"
python -c "from gradient_ascent_utils import RewardGuidedDiffusion; print('βœ“ Gradient Utils OK')"
```
---
## Quick Start
### 1. Basic COCO Evaluation (test_grad_sd1.5.py)
```bash
# Edit Config in test_grad_sd1.5.py:
# - Set device: "cuda:0" or "cuda:6"
# - Set max_samples: 10 for quick test, None for full dataset
# - Configure gradient ascent parameters
python test_grad_sd1.5.py
```
**Output:**
- Creates `RESULTS/SD1.5_GradAscent/run_1/` (auto-incremented)
- Generates `eval.log` with detailed metrics
- Saves `reward_curve.png` showing reward progression
### 2. Basic Evaluation with Preset Config (eval.py)
```bash
python eval.py \
--grad_config cosine_nesterov \
--metrics clip aesthetic \
--max_samples 10
```
### 2. High-Quality Evaluation
```bash
python eval.py \
--grad_config high_quality \
--metrics fid clip aesthetic pickscore hpsv2 \
--max_samples 100 \
--save_images \
--output_dir results/high_quality
```
### 3. Pick-a-Pic Benchmark
```bash
python eval.py \
--dataset_type pickapic \
--grad_config cosine_nesterov \
--metrics pickscore hpsv2 imagereward \
--max_samples 500 \
--output_dir results/pickapic
```
---
## Architecture
### System Components
```
Reward/
β”œβ”€β”€ models/
β”‚ β”œβ”€β”€ reward_model.py # LRM reward model wrapper
β”‚ └── unet_2d_condition_reward.py # Custom UNet with reward tracking
β”œβ”€β”€ pipelines/
β”‚ β”œβ”€β”€ sd15_reward_pipeline.py # Base pipeline with reward tracking
β”‚ └── sd15_gradient_ascent_pipeline.py # Pipeline with gradient ascent
β”œβ”€β”€ lr_scheduler.py # Learning rate schedulers
β”œβ”€β”€ gradient_ascent_utils.py # Core gradient ascent implementation
β”œβ”€β”€ grad_ascent_configs.py # Configuration presets
β”œβ”€β”€ eval.py # Comprehensive evaluation script
└── examples.sh # Example commands
```
### Gradient Ascent Flow
```
1. Load Stable Diffusion + LRM Reward Model
2. Start denoising process (T β†’ 0)
3. At each timestep t:
a. Standard denoising step (predict noise, remove it)
b. Compute reward R(latents, prompt, t) and store in history
c. If t in gradient range:
- Enable gradients on latents
- Compute βˆ‡R w.r.t. latents
- For each gradient step:
* Get current LR from scheduler
* Apply momentum (if enabled)
* Update: latents += lr * momentum(βˆ‡R)
- Track statistics (grad norms, reward improvement)
4. At final timestep (t=0):
- Final reward computed on clean latent
- This reward is reported in logs
5. Decode final latent (xβ‚€) to image via VAE
6. Compute quality metrics on image
```
### Understanding Reward Calculation
**Key Concepts:**
- **Timestep-Aware Rewards**: The LRM reward model computes preference scores at ANY noise level (timestep t)
- **Progressive Tracking**: Rewards are calculated at every denoising step throughout generation
- **Final Latent Reward**: The reported metric is the reward for t=0 (the clean latent before decoding)
- **Not Averaged**: The final reward is specifically from the last timestep, NOT an average across all timesteps
**What gets reported:**
```python
# During generation: Rewards computed at each t (1000 β†’ 0)
Step 0: t=1000, reward=3.2
Step 1: t=990, reward=3.5
...
Step 99: t=10, reward=5.1
Step 100: t=0, reward=5.4 ← This is what gets logged!
```
The `Reward (t=0)` in logs represents the preference score of the final clean latent that was decoded into your output image.
---
## Learning Rate Scheduling
### Available Schedulers
#### 1. **Constant LR**
```python
lr_scheduler_type="constant"
```
- Fixed learning rate throughout optimization
- Simple and stable
- Good for quick experiments
#### 2. **Linear Decay**
```python
lr_scheduler_type="linear"
lr_scheduler_kwargs={
"end_lr": 0.01, # End LR (10% of initial)
"start_step": 0 # When to start decay
}
```
- Linear decrease from initial to end LR
- Smooth convergence
- Configurable warmup period
#### 3. **Cosine Annealing** (Recommended)
```python
lr_scheduler_type="cosine"
lr_scheduler_kwargs={
"min_lr": 0.001, # Minimum LR
"warmup_steps": 3 # Linear warmup steps
}
```
- Smooth cosine decay
- Optional warmup phase
- Widely used in deep learning
- **Best for most use cases**
#### 4. **Exponential Decay**
```python
lr_scheduler_type="exponential"
lr_scheduler_kwargs={
"gamma": 0.9 # Decay factor per step
}
```
- Exponential decrease
- Fast initial decay
- Good for aggressive optimization
#### 5. **Step Decay**
```python
lr_scheduler_type="step"
lr_scheduler_kwargs={
"step_size": 5, # Steps between decays
"gamma": 0.5 # Multiplicative factor
}
```
- Step-wise LR reduction
- Periodic decay
- Good for scheduled changes
### Usage Example
```python
from pipelines.sd15_gradient_ascent_pipeline import StableDiffusionGradientAscentPipeline
pipeline.enable_gradient_ascent(
grad_timestep_range=(0, 700),
num_grad_steps=15,
grad_step_size=0.1, # Initial LR
lr_scheduler_type="cosine",
lr_scheduler_kwargs={
"min_lr": 0.001,
"warmup_steps": 3
}
)
```
---
## Configuration Presets
We provide 15 pre-configured optimization strategies. Use them with `--grad_config <name>`.
### Basic Configurations
| Config | LR Schedule | Momentum | Steps | Description |
|--------|-------------|----------|-------|-------------|
| `constant` | Constant | No | 5 | Simple baseline |
| `linear` | Linear decay | No | 10 | Smooth decay |
| `linear_warmstart` | Linear w/ warmup | No | 10 | Stable start |
| `cosine` | Cosine | No | 10 | Smooth convergence |
| `cosine_warmup` | Cosine w/ warmup | No | 20 | Best convergence |
| `exponential` | Exponential | No | 15 | Fast decay |
| `step` | Step-wise | No | 20 | Periodic decay |
### Momentum Configurations
| Config | LR Schedule | Momentum | Steps | Description |
|--------|-------------|----------|-------|-------------|
| `momentum` | Constant | Standard | 10 | Faster convergence |
| `nesterov` | Constant | Nesterov | 10 | Better convergence |
### Advanced Configurations
| Config | LR Schedule | Momentum | Steps | Description |
|--------|-------------|----------|-------|-------------|
| `cosine_momentum` | Cosine | Standard | 15 | High quality |
| `cosine_nesterov` | Cosine | Nesterov | 15 | **Recommended** |
| `linear_nesterov` | Linear | Nesterov | 15 | Stable + fast |
### Quality Presets
| Config | LR Schedule | Momentum | Steps | Use Case |
|--------|-------------|----------|-------|----------|
| `high_quality` | Cosine | Nesterov | 20 | **Best quality** |
| `aggressive` | Exponential | Standard | 8 | Fast results |
| `conservative` | Cosine | Nesterov | 25 | Most stable |
### Config Details
#### `high_quality` (Recommended for Research)
```python
{
"grad_timestep_range": (200, 800), # Focus on middle timesteps
"num_grad_steps": 20,
"grad_step_size": 0.08,
"lr_scheduler_type": "cosine",
"lr_scheduler_kwargs": {"min_lr": 0.005, "warmup_steps": 5},
"use_momentum": True,
"momentum": 0.95,
"use_nesterov": True
}
```
#### `cosine_nesterov` (Recommended for General Use)
```python
{
"grad_timestep_range": (0, 700),
"num_grad_steps": 15,
"grad_step_size": 0.12,
"lr_scheduler_type": "cosine",
"lr_scheduler_kwargs": {"min_lr": 0.001, "warmup_steps": 3},
"use_momentum": True,
"momentum": 0.9,
"use_nesterov": True
}
```
#### `aggressive` (Fast Experimentation)
```python
{
"grad_timestep_range": (0, 900),
"num_grad_steps": 8,
"grad_step_size": 0.15,
"grad_scale": 1.2,
"lr_scheduler_type": "exponential",
"lr_scheduler_kwargs": {"gamma": 0.85},
"use_momentum": True,
"momentum": 0.85,
"use_nesterov": False
}
```
### Listing Configs
```python
from grad_ascent_configs import list_configs, print_config, get_config
# List all available configs
print(list_configs())
# Output: ['aggressive', 'conservative', 'constant', 'cosine', ...]
# Print config details
print_config("cosine_nesterov")
# Get config dictionary
config = get_config("high_quality")
pipeline.enable_gradient_ascent(**config)
```
---
## Evaluation Metrics
### 1. **FID (FrΓ©chet Inception Distance)**
- Measures distribution similarity between real and generated images
- **Lower is better**
- Requires reference images (COCO dataset only)
- Computationally expensive
```bash
--metrics fid
```
### 2. **CLIP Score**
- Evaluates text-image alignment using CLIP embeddings
- **Higher is better**
- Fast and reliable
- Good for general quality assessment
```bash
--metrics clip
```
### 3. **Aesthetic Score**
- Predicts aesthetic quality using CLIP + MLP
- **Higher is better**
- Trained on human aesthetic ratings
- Good for visual appeal
```bash
--metrics aesthetic
```
### 4. **PickScore** (New)
- Human preference predictor from Pick-a-Pic dataset
- **Higher is better**
- Trained on large-scale human comparisons
- State-of-the-art preference metric
```bash
--metrics pickscore
```
### 5. **HPSv2** (New)
- Human Preference Score version 2
- **Higher is better**
- Trained on aesthetic evaluations
- Complementary to PickScore
```bash
--metrics hpsv2
```
### 6. **ImageReward** (New)
- Reward model from RLHF (Reinforcement Learning from Human Feedback)
- **Higher is better**
- Comprehensive quality assessment
- Trained on diverse human feedback
```bash
--metrics imagereward
```
### Metric Recommendations
| Use Case | Recommended Metrics | Reason |
|----------|---------------------|--------|
| Research/Papers | `fid clip aesthetic pickscore hpsv2` | Comprehensive evaluation |
| Quick Iteration | `clip aesthetic` | Fast and reliable |
| Human Alignment | `pickscore hpsv2 imagereward` | Preference-based |
| Text Alignment | `clip imagereward` | Focus on prompt adherence |
| Visual Quality | `aesthetic pickscore` | Focus on aesthetics |
---
## Model Variants
Support for multiple SD1.5 model variants trained with different methods.
### Available Variants
#### 1. **Origin** (Default)
```bash
--model_variant origin
```
- Original Stable Diffusion v1.5 from RunwayML
- No additional training
- CFG scale: 7.5 (default)
- Good baseline
#### 2. **SPO** (Supervised Policy Optimization)
```bash
--model_variant spo
```
- Trained with SPO method
- Model: `SPO-Diffusion-Models/SPO-SD-v1-5_4k-p_10ep`
- **CFG scale: 5.0** (auto-adjusted)
- Better prompt adherence
#### 3. **Diffusion-DPO** (Direct Preference Optimization)
```bash
--model_variant diffusion_dpo
```
- Trained with DPO on human preferences
- Model: `mhdang/dpo-sd1.5-text2image-v1`
- CFG scale: 7.5
- Improved human alignment
#### 4. **LPO** (Latent Preference Optimization)
```bash
--model_variant lpo
```
- Trained with LPO (this project's main method)
- Model: `casiatao/LPO` (lpo_sd15_merge)
- **CFG scale: 5.0** (auto-adjusted)
- **Highest quality baseline**
### Comparison
| Variant | Training Method | Quality | Speed | Best For |
|---------|----------------|---------|-------|----------|
| Origin | Pre-training only | Good | Fast | Baseline |
| SPO | Supervised | Better | Fast | Prompt adherence |
| Diffusion-DPO | Preference learning | Better | Fast | Human preferences |
| LPO | Latent preference | **Best** | Fast | Overall quality |
### Usage Example
```bash
# Compare all variants
for variant in origin spo diffusion_dpo lpo; do
python eval.py \
--model_variant $variant \
--grad_config high_quality \
--metrics clip aesthetic pickscore \
--max_samples 100 \
--output_dir results/${variant}
done
```
---
## Datasets
### 1. **COCO Validation** (Default)
```bash
--dataset_type coco
--data_dir ./data
```
**Features:**
- Standard benchmark dataset
- Reference images available (for FID)
- ~5,000 validation samples
- Diverse prompts
**Structure:**
```
data/coco/
β”œβ”€β”€ caption_val.json
└── images/val/
β”œβ”€β”€ 000000000139.jpg
β”œβ”€β”€ 000000000285.jpg
└── ...
```
### 2. **Pick-a-Pic Validation**
```bash
--dataset_type pickapic
```
**Features:**
- Large-scale human preference dataset
- Streaming (no download needed)
- ~500,000 validation samples
- Real user prompts
- No reference images (FID not available)
**Advantages:**
- More diverse prompts
- Real-world use cases
- Human preference focus
- Large-scale evaluation
### Dataset Recommendations
| Use Case | Dataset | Reason |
|----------|---------|--------|
| Academic Research | COCO | Standard benchmark, reproducible |
| FID Evaluation | COCO | Requires reference images |
| Human Preference | Pick-a-Pic | Trained on human comparisons |
| Large-scale Tests | Pick-a-Pic | 500K+ samples available |
| Quick Tests | COCO | Smaller, faster |
---
## Usage Examples
### Example 1: Quick Test
```bash
python eval.py \
--grad_config cosine_nesterov \
--metrics clip aesthetic \
--max_samples 10 \
--output_dir examples/quick_test
```
### Example 2: High-Quality Research Evaluation
```bash
python eval.py \
--grad_config high_quality \
--metrics fid clip aesthetic pickscore hpsv2 \
--max_samples 200 \
--save_images \
--output_dir examples/research
```
### Example 3: Pick-a-Pic Benchmark
```bash
python eval.py \
--dataset_type pickapic \
--grad_config cosine_nesterov \
--metrics pickscore hpsv2 imagereward \
--max_samples 500 \
--output_dir examples/pickapic
```
### Example 4: LPO Model Evaluation
```bash
python eval.py \
--model_variant lpo \
--grad_config high_quality \
--metrics clip aesthetic pickscore \
--max_samples 100 \
--save_images \
--output_dir examples/lpo_model
```
### Example 5: Baseline Only (No Gradient Ascent)
```bash
python eval.py \
--mode baseline \
--model_variant origin \
--metrics clip aesthetic pickscore \
--max_samples 50 \
--output_dir examples/baseline_only
```
### Example 6: Manual Configuration
```bash
python eval.py \
--grad_range_start 200 \
--grad_range_end 800 \
--grad_steps 15 \
--grad_step_size 0.08 \
--metrics clip aesthetic \
--max_samples 50 \
--output_dir examples/manual_config
```
### Example 7: Model Comparison
```bash
# Evaluate all model variants
for variant in origin spo diffusion_dpo lpo; do
python eval.py \
--model_variant $variant \
--grad_config high_quality \
--metrics clip aesthetic pickscore \
--max_samples 100 \
--save_images \
--output_dir results/comparison/${variant}
done
```
### Example 8: Conservative Optimization
```bash
python eval.py \
--grad_config conservative \
--metrics clip aesthetic pickscore hpsv2 \
--max_samples 100 \
--save_images \
--output_dir examples/conservative
```
---
## API Reference
### Pipeline Usage
```python
from diffusers import StableDiffusionPipeline
from pipelines.sd15_gradient_ascent_pipeline import StableDiffusionGradientAscentPipeline
from models import LRMRewardModel
# Load base pipeline
base_pipeline = StableDiffusionPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5",
torch_dtype=torch.float16
)
# Create gradient ascent pipeline
pipeline = StableDiffusionGradientAscentPipeline(**base_pipeline.components)
# Load reward model
reward_model = LRMRewardModel(
pretrained_model_name_or_path="runwayml/stable-diffusion-v1-5",
lrm_model_path="casiatao/LRM",
guidance_scale=7.5,
device="cuda"
)
pipeline.set_reward_model(reward_model)
# Enable gradient ascent with preset
from grad_ascent_configs import get_config
config = get_config("cosine_nesterov")
pipeline.enable_gradient_ascent(**config)
# Or configure manually
pipeline.enable_gradient_ascent(
grad_timestep_range=(200, 800),
num_grad_steps=15,
grad_step_size=0.1,
lr_scheduler_type="cosine",
lr_scheduler_kwargs={"min_lr": 0.001, "warmup_steps": 3},
use_momentum=True,
momentum=0.9,
use_nesterov=True
)
# Generate with gradient ascent
output = pipeline(
prompt="a beautiful mountain landscape at sunset",
num_inference_steps=50,
guidance_scale=7.5,
)
# Get gradient statistics
stats = pipeline.grad_guidance.get_statistics()
print(f"Reward improvement: {stats['avg_reward_improvement']:.4f}")
```
### Custom LR Scheduler
```python
from lr_scheduler import create_lr_scheduler
# Create cosine scheduler with warmup
scheduler = create_lr_scheduler(
scheduler_type="cosine",
initial_lr=0.1,
num_steps=20,
min_lr=0.001,
warmup_steps=5
)
# Use in optimization loop
for step in range(20):
current_lr = scheduler.get_lr()
# ... apply gradient with current_lr ...
scheduler.step()
```
### Configuration Management
```python
from grad_ascent_configs import get_config, list_configs, print_config
# List all available configs
all_configs = list_configs()
print(f"Available configs: {all_configs}")
# Get specific config
config = get_config("high_quality")
# Print config details
print_config("cosine_nesterov")
# Create custom config
custom_config = {
"grad_timestep_range": (300, 700),
"num_grad_steps": 12,
"grad_step_size": 0.09,
"lr_scheduler_type": "cosine",
"lr_scheduler_kwargs": {"min_lr": 0.002, "warmup_steps": 4},
"use_momentum": True,
"momentum": 0.92,
"use_nesterov": True
}
pipeline.enable_gradient_ascent(**custom_config)
```
---
## Command-Line Options
### Essential Options
```bash
--data_dir PATH # Path to data directory (default: ./data)
--dataset_type TYPE # Dataset: coco or pickapic (default: coco)
--model_variant VARIANT # Model: origin, spo, diffusion_dpo, lpo (default: origin)
--max_samples N # Max samples to evaluate (default: all)
--output_dir PATH # Output directory (default: eval_outputs)
--save_images # Save generated images
```
### Gradient Ascent Options
```bash
--grad_config NAME # Use preset config (recommended)
--grad_range_start N # Gradient timestep start (default: 0)
--grad_range_end N # Gradient timestep end (default: 700)
--grad_steps N # Gradient steps per timestep (default: 5)
--grad_step_size FLOAT # Initial learning rate (default: 0.1)
```
### Evaluation Options
```bash
--metrics METRIC [METRIC...] # Metrics to evaluate (default: clip aesthetic)
# Options: fid, clip, aesthetic, pickscore, hpsv2, imagereward
--mode MODE # baseline, gradient_ascent, or both (default: both)
--num_steps N # Diffusion inference steps (default: 50)
--cfg_scale FLOAT # CFG scale (default: 7.5, auto-adjusted for some models)
--batch_size N # Batch size (default: 1)
--log_interval N # Log every N batches (default: 10)
```
### Other Options
```bash
--lrm_model PATH # LRM model path (default: casiatao/LRM)
--seed N # Random seed (default: 42)
--cuda N # CUDA device ID (default: 0)
```
### Complete Example
```bash
python eval.py \
--data_dir ./data \
--dataset_type coco \
--model_variant lpo \
--grad_config high_quality \
--metrics fid clip aesthetic pickscore hpsv2 \
--max_samples 200 \
--num_steps 50 \
--save_images \
--output_dir results/comprehensive \
--cuda 0
```
---
## Output Files
After running evaluation, the following files are created in **auto-incremented run folders**:
```
RESULTS/SD1.5_GradAscent/
β”œβ”€β”€ run_1/ # First run
β”‚ β”œβ”€β”€ eval.log # Complete execution log
β”‚ └── reward_curve.png # Reward progression plot
β”œβ”€β”€ run_2/ # Second run
β”‚ β”œβ”€β”€ eval.log
β”‚ └── reward_curve.png
└── run_3/ # Third run
β”œβ”€β”€ eval.log
└── reward_curve.png
```
### Auto-Incrementing Run Folders
Each execution automatically creates a new `run_<N>/` folder, preventing accidental overwrites and maintaining a complete experiment history. No manual folder management needed!
### eval.log Structure
The log contains detailed information for each batch:
```
======================================================================
COCO GRADIENT ASCENT EVALUATION (BATCHED)
======================================================================
Logging to: ./RESULTS/SD1.5_GradAscent/run_1/eval.log
Device: cuda:6
Batch size: 1
Metrics: fid, clip, reward, aesthetic
Gradient Ascent: Range=[0, 900], Steps=1, StepSize=0.01
======================================================================
[Batch 1/5000] Samples: 1/5000 | FID: 2.5432 | CLIP: 0.8234 | Reward (t=0): 5.2341 | Reward (Avg): 5.2341 | Aesthetic: 6.456
[Batch 161/5000] Samples: 161/5000 | FID: 2.3821 | CLIP: 0.8412 | Reward (t=0): 5.4123 | Reward (Avg): 5.3215 | Aesthetic: 6.523
...
======================================================================
FINAL RESULTS
======================================================================
FID: 2.3456
CLIP avg: 0.8378
Reward avg: 5.3421
Aesthetic: 6.489
======================================================================
```
### reward_curve.png Visualization
The reward curve plot shows two panels for the **first generated image**:
**Left Panel: Reward vs Timestep**
- X-axis: Denoising timestep (t)
- Y-axis: Reward score
- Green shaded region: Where gradient ascent is applied
- Shows how reward evolves as noise is removed
**Right Panel: Reward vs Denoising Step**
- X-axis: Sequential denoising step (0 to num_inference_steps)
- Y-axis: Reward score
- Same data, different perspective for easier interpretation
**Key Insights from the Plot:**
- **Upward trend**: Reward generally increases as denoising progresses
- **Sharp improvements**: Visible spikes where gradient ascent is effective
- **Final reward**: Last point corresponds to t=0 (decoded image reward)
- **Learning dynamics**: Shows if optimization is working at different noise levels
### Reward Tracking Details
The script now explicitly tracks:
1. **Timestep-specific rewards**: Computed at every denoising step
2. **Final latent reward**: The reward for t=0 (the latent that gets decoded)
3. **Running average**: Mean reward across all processed samples
4. **Current batch reward**: Immediate feedback per batch
Example log output:
```
Reward (t=0): 5.4123 # Reward for the final decoded latent
Reward (Avg): 5.3215 # Running average across all samples
```
### evaluation_results.json Structure
(Legacy format from eval.py - test_grad_sd1.5.py uses simplified logging)
```json
{
"mode": "both",
"metrics": ["clip", "aesthetic", "pickscore"],
"config": {
"num_samples": 100,
"num_steps": 50,
"cfg_scale": 7.5,
"grad_range": [0, 700],
"grad_steps": 15,
"grad_step_size": 0.12
},
"baseline": {
"avg_reward": 0.7234,
"clip_score": 0.8123,
"aesthetic_score": 6.234,
"pickscore": 21.45
},
"gradient_ascent": {
"avg_reward": 0.7891,
"clip_score": 0.8345,
"aesthetic_score": 6.456,
"pickscore": 22.13,
"stats": {
"num_applications": 45,
"total_reward_improvement": 2.956,
"avg_reward_improvement": 0.0657
}
},
"comparison": {
"reward_difference": 0.0657,
"clip_difference": 0.0222,
"aesthetic_difference": 0.222,
"pickscore_difference": 0.68
}
}
```
---
## Troubleshooting
### Common Issues
#### 1. Out of Memory (OOM)
**Symptoms:**
```
RuntimeError: CUDA out of memory
```
**Solutions:**
```bash
# Reduce batch size
--batch_size 1
# Reduce max samples
--max_samples 50
# Reduce gradient steps
--grad_steps 5
# Use smaller config
--grad_config aggressive # Only 8 steps
```
#### 2. Slow Evaluation
**Symptoms:**
- Takes too long to complete
- Hanging on metric computation
**Solutions:**
```bash
# Skip expensive metrics
--metrics clip aesthetic # Skip FID
# Reduce samples
--max_samples 50
# Reduce diffusion steps
--num_steps 20
# Use faster dataset
--dataset_type pickapic # No FID computation
```
#### 3. Poor Results / No Improvement
**Symptoms:**
- Reward doesn't increase
- Quality worse after gradient ascent
**Solutions:**
```bash
# Try better configs
--grad_config high_quality
--grad_config conservative
# Increase gradient steps
--grad_steps 20
# Adjust timestep range (focus on middle)
--grad_range_start 200 --grad_range_end 800
# Try different model variant
--model_variant lpo
```
#### 4. Config Not Found
**Symptoms:**
```
ValueError: Unknown config: my_config
```
**Solutions:**
```bash
# List available configs
python -c "from grad_ascent_configs import list_configs; print(list_configs())"
# Print config details
python -c "from grad_ascent_configs import print_config; print_config('high_quality')"
```
#### 5. Metric Loading Errors
**Symptoms:**
```
Warning: Could not load PickScore scorer
```
**Solutions:**
```bash
# Install missing dependencies
pip install transformers datasets
# Check HuggingFace Hub access
huggingface-cli login
# Skip problematic metrics
--metrics clip aesthetic # Skip pickscore if it fails
```
#### 6. Dataset Not Found
**Symptoms:**
```
FileNotFoundError: Validation JSON not found
```
**Solutions:**
```bash
# Check data directory structure
ls data/coco/
# Use Pick-a-Pic instead (no local files needed)
--dataset_type pickapic
# Provide correct data path
--data_dir /path/to/your/data
```
---
## Best Practices
### 1. **Start Small, Scale Up**
```bash
# First: Quick test (10 samples)
python eval.py --grad_config cosine_nesterov --metrics clip --max_samples 10
# Then: Medium test (50 samples)
python eval.py --grad_config cosine_nesterov --metrics clip aesthetic --max_samples 50
# Finally: Full evaluation (200+ samples)
python eval.py --grad_config high_quality --metrics fid clip aesthetic pickscore hpsv2 --max_samples 200
```
### 2. **Choose Right Config for Use Case**
| Goal | Config | Metrics |
|------|--------|---------|
| Quick experiment | `cosine_nesterov` | `clip` |
| Research paper | `high_quality` | `fid clip aesthetic pickscore hpsv2` |
| Production | `conservative` | `pickscore hpsv2` |
| Fast iteration | `aggressive` | `clip aesthetic` |
### 3. **Use Multiple Metrics**
Don't rely on a single metric. Recommended combinations:
```bash
# Text alignment + aesthetics
--metrics clip aesthetic
# Human preference focus
--metrics pickscore hpsv2 imagereward
# Comprehensive (research)
--metrics fid clip aesthetic pickscore hpsv2
```
### 4. **Save Important Runs**
```bash
# Always save images for important evaluations
--save_images --output_dir results/important_run_$(date +%Y%m%d)
```
### 5. **Monitor GPU Usage**
```bash
# In separate terminal
watch -n 1 nvidia-smi
# Or use
gpustat -i 1
```
### 6. **Batch Evaluation**
```bash
# Create evaluation script
cat << 'EOF' > run_evals.sh
#!/bin/bash
for config in cosine_nesterov high_quality conservative; do
for model in origin lpo; do
python eval.py \
--model_variant $model \
--grad_config $config \
--metrics clip aesthetic pickscore \
--max_samples 100 \
--save_images \
--output_dir results/${model}_${config}
done
done
EOF
chmod +x run_evals.sh
./run_evals.sh
```
### 7. **Reproducibility**
```bash
# Always set seed for reproducible results
--seed 42
# Document your runs
--output_dir results/experiment_name_$(date +%Y%m%d_%H%M)
```
### 8. **Performance Tips**
- Use `batch_size=1` for safety (reward model compatibility)
- Start with `--max_samples 10` for debugging
- Use `--dataset_type pickapic` for large-scale evaluation (no FID overhead)
- Skip `fid` metric if not needed (expensive)
- Use `--num_steps 20-30` for faster generation (vs default 50)
### 9. **Config Selection Guide**
```python
# Start here
if "just_testing":
config = "constant"
# General use
elif "standard_evaluation":
config = "cosine_nesterov" # Best balance
# Research/papers
elif "need_best_quality":
config = "high_quality" # 20 steps, nesterov
# Fast experiments
elif "need_speed":
config = "aggressive" # 8 steps
# Stability critical
elif "need_stability":
config = "conservative" # 25 steps, careful
```
### 10. **Timestep Range Tips**
```python
# Full range (default)
--grad_range_start 0 --grad_range_end 700
# Middle timesteps (often best)
--grad_range_start 200 --grad_range_end 800
# Early timesteps (structure)
--grad_range_start 500 --grad_range_end 1000
# Late timesteps (details)
--grad_range_start 0 --grad_range_end 400
```
---
## Performance Metrics
### Expected Results
Based on COCO validation set (100 samples):
| Method | CLIP ↑ | Aesthetic ↑ | PickScore ↑ | Time |
|--------|--------|-------------|-------------|------|
| Baseline (Origin) | 0.812 | 6.23 | 21.4 | 5 min |
| + Constant | 0.819 | 6.28 | 21.6 | 6 min |
| + Cosine Nesterov | 0.834 | 6.45 | 22.1 | 8 min |
| + High Quality | 0.841 | 6.52 | 22.4 | 12 min |
| Baseline (LPO) | 0.856 | 6.67 | 22.8 | 5 min |
| LPO + High Quality | 0.873 | 6.89 | 23.5 | 12 min |
*Results may vary based on hardware and specific prompts*
---
## Citation
If you use this code in your research, please cite:
```bibtex
@article{lpo2024,
title={Latent Preference Optimization for Diffusion Models},
author={Your Name},
journal={arXiv preprint},
year={2024}
}
```
---
## License
This project follows the license of the main LPO repository.
---
## Contributing
Contributions are welcome! Please:
1. Test your changes with `--max_samples 10`
2. Document new features in this README
3. Add examples to `examples.sh`
4. Follow existing code style
---
## Support
For issues and questions:
1. Check [Troubleshooting](#troubleshooting) section
2. Review [Examples](#usage-examples)
3. Open an issue on GitHub
---
## Changelog
### Latest Version (January 2026)
**New Features:**
- ✨ Learning rate scheduling (constant, linear, cosine, exponential, step)
- ✨ Momentum optimization (standard and Nesterov)
- ✨ 15 configuration presets
- ✨ Additional metrics (PickScore, HPSv2, ImageReward)
- ✨ Pick-a-Pic validation dataset support
- ✨ SD1.5 model variants (Origin, SPO, DPO, LPO)
- ✨ Comprehensive evaluation framework
- ✨ **Automatic run folder creation** - Each run creates `run_1/`, `run_2/`, etc.
- ✨ **Reward curve visualization** - Automatic plotting of reward progression across timesteps
- ✨ **Final timestep reward tracking** - Reports reward specifically from t=0 (decoded latent)
- ✨ **Detailed reward logging** - Shows both last timestep reward and running average
**Improvements:**
- πŸš€ Better convergence with LR scheduling
- πŸš€ Faster optimization with momentum
- πŸ“Š More comprehensive quality assessment
- πŸ“Š Visual feedback with reward curve plots
- πŸ“š Complete documentation
- πŸ” Enhanced debugging with timestep-specific reward tracking
---
**Happy Optimizing! πŸš€**