### Install Dependencies and Set Environment Variables Source: https://context7.com/salesforceairesearch/diffusiondpo/llms.txt Installs necessary dependencies and sets environment variables for model and dataset names before launching training. ```bash pip install -r requirements.txt export MODEL_NAME="runwayml/stable-diffusion-v1-5" export DATASET_NAME="yuvalkirstain/pickapic_v2" ``` -------------------------------- ### Install project dependencies Source: https://github.com/salesforceairesearch/diffusiondpo/blob/main/README.md Install the required Python packages listed in the requirements file. ```bash pip install -r requirements.txt ``` -------------------------------- ### Generate and Score Example Prompts Source: https://github.com/salesforceairesearch/diffusiondpo/blob/main/quick_samples.ipynb Provides a list of example prompts and iterates through them. For each prompt, it generates images using the `gen` function, then scores these images using the `ps_selector` with the prompt as context, and prints the scores. This demonstrates the end-to-end process of generation and evaluation. ```python example_prompts = [ "A pile of sand swirling in the wind forming the shape of a dancer", "A giant dinosaur frozen into a glacier and recently discovered by scientists, cinematic still", "a smiling beautiful sorceress with long dark hair and closed eyes wearing a dark top surrounded by glowing fire sparks at night, magical light fog, deep focus+closeup, hyper-realistic, volumetric lighting, dramatic lighting, beautiful composition, intricate details, instagram, trending, photograph, film grain and noise, 8K, cinematic, post-production", "A purple raven flying over big sur, light fog, deep focus+closeup, hyper-realistic, volumetric lighting, dramatic lighting, beautiful composition, intricate details, instagram, trending, photograph, film grain and noise, 8K, cinematic, post-production", "a smiling beautiful sorceress wearing a modest high necked blue suit surrounded by swirling rainbow aurora, hyper-realistic, cinematic, post-production", "Anthro humanoid turtle skydiving wearing goggles, gopro footage", "A man in a suit surfing in a river", "photo of a zebra dressed suit and tie sitting at a table in a bar with a bar stools, award winning photography", "A typhoon in a tea cup, digital render", "A cute puppy leading a session of the United Nations, newspaper photography", "Worm eye view of rocketship", "Glass spheres in the desert, refraction render", "anthropmorphic coffee bean drinking coffee", "A baby kangaroo in a trenchcoat", "A towering hurricane of rainbow colors towering over a city, cinematic digital art", "A redwood tree rising up out of the ocean", ] for p in example_prompts: ims = gen(p) # could save these if desired scores = ps_selector.score(ims, p) print(scores) ``` -------------------------------- ### Generate Image with DPO Model Source: https://context7.com/salesforceairesearch/diffusiondpo/llms.txt Basic inference example using a pre-configured pipeline. ```python generator = torch.Generator(device='cuda').manual_seed(42) image = pipe( prompt="A beautiful sunset over mountains, photorealistic", generator=generator, guidance_scale=7.5 ).images[0] image.save("dpo_output.png") ``` -------------------------------- ### Launch DPO Training for Stable Diffusion 1.5 Source: https://context7.com/salesforceairesearch/diffusiondpo/llms.txt Launches DPO training for a Stable Diffusion 1.5 model using the Pick-a-Pic dataset. Configure effective batch size, training steps, learning rate, and checkpointing. ```bash accelerate launch --mixed_precision="fp16" train.py \ --pretrained_model_name_or_path=$MODEL_NAME \ --dataset_name=$DATASET_NAME \ --train_batch_size=1 \ --dataloader_num_workers=16 \ --gradient_accumulation_steps=128 \ --max_train_steps=2000 \ --lr_scheduler="constant_with_warmup" \ --lr_warmup_steps=500 \ --learning_rate=1e-8 \ --scale_lr \ --cache_dir="/path/to/dataset/cache/" \ --checkpointing_steps=500 \ --beta_dpo=5000 \ --output_dir="dpo-sd15-output" ``` -------------------------------- ### Key Training Arguments Reference Source: https://context7.com/salesforceairesearch/diffusiondpo/llms.txt Provides an overview of essential training arguments for Diffusion DPO, including DPO parameters, model configuration, hyperparameters, memory optimization, data configuration, AI feedback models, and checkpointing settings. ```bash # Core DPO parameters --beta_dpo 5000 # KL penalty strength (higher = closer to reference) --sft # Use SFT instead of DPO # Model configuration --pretrained_model_name_or_path # Base model (SD1.5, SDXL, etc.) --sdxl # Enable SDXL mode --pretrained_vae_model_name_or_path # Custom VAE (recommended for SDXL) --unet_init # Initialize UNet from different checkpoint # Training hyperparameters --learning_rate 1e-8 # Base learning rate --scale_lr # Scale LR by batch size and GPUs (recommended) --max_train_steps 2000 # Total training steps --train_batch_size 1 # Per-device batch size --gradient_accumulation_steps 128 # Accumulation for effective batch size --lr_scheduler "constant_with_warmup" # LR schedule type --lr_warmup_steps 500 # Warmup steps # Memory optimization --use_adafactor # Use Adafactor optimizer (auto for SDXL) --gradient_checkpointing # Trade compute for memory (auto for SDXL) # Data configuration --dataset_name # HuggingFace dataset name --cache_dir # Local cache directory for dataset --resolution 512 # Image resolution (1024 for SDXL) --random_crop # Use random instead of center crop --no_hflip # Disable horizontal flip augmentation # AI Feedback --choice_model "pickscore" # Use AI model for preferences # Options: pickscore, hps, clip, aes # Checkpointing --checkpointing_steps 500 # Save checkpoint every N steps --resume_from_checkpoint "latest" # Resume from checkpoint --output_dir # Output directory for checkpoints ``` -------------------------------- ### Launch Supervised Fine-Tuning (SFT) for SD1.5 Source: https://context7.com/salesforceairesearch/diffusiondpo/llms.txt Launches supervised fine-tuning (SFT) for a Stable Diffusion 1.5 model, using only the preferred images from the dataset. Adjust batch size and gradient accumulation steps as needed. ```bash export MODEL_NAME="runwayml/stable-diffusion-v1-5" export DATASET_NAME="yuvalkirstain/pickapic_v2" accelerate launch train.py \ --pretrained_model_name_or_path=$MODEL_NAME \ --dataset_name=$DATASET_NAME \ --train_batch_size=4 \ --gradient_accumulation_steps=32 \ --max_train_steps=2000 \ --learning_rate=1e-8 \ --scale_lr \ --sft \ --output_dir="sft-sd15-output" ``` -------------------------------- ### Launch SD1.5 training Source: https://github.com/salesforceairesearch/diffusiondpo/blob/main/README.md Execute the training script for StableDiffusion 1.5 using accelerate. Ensure the environment variables for model and dataset paths are set correctly. ```bash # from launchers/sd15.sh export MODEL_NAME="runwayml/stable-diffusion-v1-5" export DATASET_NAME="yuvalkirstain/pickapic_v2" # Effective BS will be (N_GPU * train_batch_size * gradient_accumulation_steps) # Paper used 2048. Training takes ~24 hours / 2000 steps accelerate launch --mixed_precision="fp16" train.py \ --pretrained_model_name_or_path=$MODEL_NAME \ --dataset_name=$DATASET_NAME \ --train_batch_size=1 \ --dataloader_num_workers=16 \ --gradient_accumulation_steps=1 \ --max_train_steps=2000 \ --lr_scheduler="constant_with_warmup" --lr_warmup_steps=500 \ --learning_rate=1e-8 --scale_lr \ --cache_dir="/export/share/datasets/vision_language/pick_a_pic_v2/" \ --checkpointing_steps 500 \ --beta_dpo 5000 \ --output_dir="tmp-sd15" ``` -------------------------------- ### Load Parti-Prompts Dataset Source: https://github.com/salesforceairesearch/diffusiondpo/blob/main/quick_samples.ipynb Loads the 'nateraw/parti-prompts' dataset using the `datasets` library and prints the 'Prompt' column from the training split. This is useful for obtaining captions for image generation experiments. ```python # to get partiprompts captions from datasets import load_dataset dataset = load_dataset("nateraw/parti-prompts") print(dataset['train']['Prompt']) ``` -------------------------------- ### Fast Resume Training from Checkpoint Source: https://context7.com/salesforceairesearch/diffusiondpo/llms.txt Performs a fast resume by skipping dataloader iteration up to the checkpoint step. Use the 'latest' checkpoint for this. ```bash # Fast resume (skip dataloader iteration to checkpoint step) accelerate launch train.py \ --pretrained_model_name_or_path="runwayml/stable-diffusion-v1-5" \ --dataset_name="yuvalkirstain/pickapic_v2" \ --output_dir="dpo-sd15-output" \ --resume_from_checkpoint="latest" \ --hard_skip_resume ``` -------------------------------- ### Launch DPO Training for Stable Diffusion XL Source: https://context7.com/salesforceairesearch/diffusiondpo/llms.txt Launches DPO training for a Stable Diffusion XL model, specifying a custom VAE for numerical stability. Training duration is approximately 30 hours for 2000 steps with an effective batch size of 2048. ```bash export MODEL_NAME="stabilityai/stable-diffusion-xl-base-1.0" export VAE="madebyollin/sdxl-vae-fp16-fix" export DATASET_NAME="yuvalkirstain/pickapic_v2" accelerate launch train.py \ --pretrained_model_name_or_path=$MODEL_NAME \ --pretrained_vae_model_name_or_path=$VAE \ --dataset_name=$DATASET_NAME \ --train_batch_size=1 \ --dataloader_num_workers=16 \ --gradient_accumulation_steps=128 \ --max_train_steps=2000 \ --lr_scheduler="constant_with_warmup" \ --lr_warmup_steps=200 \ --learning_rate=1e-8 \ --scale_lr \ --cache_dir="/path/to/dataset/cache/" \ --checkpointing_steps=200 \ --beta_dpo=5000 \ --sdxl \ --output_dir="dpo-sdxl-output" ``` -------------------------------- ### Resume Training from Specific Checkpoint Source: https://context7.com/salesforceairesearch/diffusiondpo/llms.txt Resumes training from a specific checkpoint identified by its step number. Ensure the checkpoint directory exists. ```bash # Resume from specific checkpoint accelerate launch train.py \ --pretrained_model_name_or_path="runwayml/stable-diffusion-v1-5" \ --dataset_name="yuvalkirstain/pickapic_v2" \ --output_dir="dpo-sd15-output" \ --resume_from_checkpoint="checkpoint-1000" ``` -------------------------------- ### Load DPO-Trained SDXL Models Source: https://context7.com/salesforceairesearch/diffusiondpo/llms.txt Integrates a DPO-trained UNet into a standard SDXL pipeline. Requires setting grad to False and using float16 precision. ```python from diffusers import StableDiffusionXLPipeline, UNet2DConditionModel import torch torch.set_grad_enabled(False) # Load DPO-trained UNet for SDXL dpo_unet = UNet2DConditionModel.from_pretrained( 'mhdang/dpo-sdxl-text2image-v1', subfolder='unet', torch_dtype=torch.float16 ).to('cuda') # Create SDXL pipeline pipe = StableDiffusionXLPipeline.from_pretrained( "stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16, variant="fp16", use_safetensors=True ).to("cuda") pipe.unet = dpo_unet # Generate with lower guidance scale for SDXL (5.0 recommended) generator = torch.Generator(device='cuda').manual_seed(42) image = pipe( prompt="A purple raven flying over big sur, cinematic, 8K", generator=generator, guidance_scale=5.0 ).images[0] image.save("sdxl_dpo_output.png") ``` -------------------------------- ### Upload Entire Model Folder Source: https://context7.com/salesforceairesearch/diffusiondpo/llms.txt Uploads an entire local directory containing model checkpoints to the Hugging Face Hub. Ensure the directory path is correct. ```python from huggingface_hub import upload_folder, upload_file import sys # Path to local checkpoint directory containing unet files checkpoint_dir = 'dpo-sd15-output/checkpoint-2000/unet/' # Or upload entire folder upload_folder( folder_path=checkpoint_dir, repo_id="your-username/your-model-name", token="your_hf_token" ) ``` -------------------------------- ### Compare Baseline vs DPO Models Source: https://context7.com/salesforceairesearch/diffusiondpo/llms.txt Swaps UNets within the same pipeline to generate side-by-side comparisons for specific prompts. ```python from diffusers import StableDiffusionXLPipeline, UNet2DConditionModel import torch torch.set_grad_enabled(False) # Load both original and DPO UNets pretrained_model_name = "stabilityai/stable-diffusion-xl-base-1.0" pipe = StableDiffusionXLPipeline.from_pretrained( pretrained_model_name, torch_dtype=torch.float16, variant="fp16", use_safetensors=True ).to("cuda") original_unet = pipe.unet dpo_unet = UNet2DConditionModel.from_pretrained( 'mhdang/dpo-sdxl-text2image-v1', subfolder='unet', torch_dtype=torch.float16 ).to('cuda') def generate_comparison(prompt, seed=0): """Generate images with both baseline and DPO models.""" generator = torch.Generator(device='cuda') results = {} for name, unet in [("baseline", original_unet), ("dpo", dpo_unet)]: pipe.unet = unet generator.manual_seed(seed) image = pipe( prompt=prompt, generator=generator, guidance_scale=5.0 ).images[0] results[name] = image return results # Compare on example prompts prompts = [ "A pile of sand swirling in the wind forming the shape of a dancer", "A giant dinosaur frozen into a glacier, cinematic still", "Anthropomorphic coffee bean drinking coffee", ] for prompt in prompts: images = generate_comparison(prompt, seed=42) images["baseline"].save(f"baseline_{hash(prompt)}.png") images["dpo"].save(f"dpo_{hash(prompt)}.png") ``` -------------------------------- ### Upload Individual Model Files Source: https://context7.com/salesforceairesearch/diffusiondpo/llms.txt Uploads individual model files (config.json, safetensors) to the Hugging Face Hub. Requires a valid Hugging Face token. ```python from huggingface_hub import upload_folder, upload_file import sys # Path to local checkpoint directory containing unet files checkpoint_dir = 'dpo-sd15-output/checkpoint-2000/unet/' # Upload individual files for fname in ['config.json', 'diffusion_pytorch_model.safetensors']: print(f"Uploading {fname}...") upload_file( path_or_fileobj=checkpoint_dir + fname, path_in_repo=fname, repo_id="your-username/your-model-name", token="your_hf_token" # or use huggingface-cli login ) ``` -------------------------------- ### Define UNet Models and Names Source: https://github.com/salesforceairesearch/diffusiondpo/blob/main/quick_samples.ipynb Creates a list of UNet models to be used, including the original pipeline's UNet and the DPO-tuned UNet. Corresponding names are also defined for identification. ```python unets = [pipe.unet, dpo_unet] names = ["Orig. SDXL", "DPO SDXL"] ``` -------------------------------- ### Evaluate with CLIP Score Source: https://context7.com/salesforceairesearch/diffusiondpo/llms.txt Initializes the CLIP selector for image-text similarity evaluation. ```python from utils.clip_utils import Selector from PIL import Image # Initialize CLIP selector (ViT-H-14, LAION-2B pretrained) clip_selector = Selector('cuda') ``` -------------------------------- ### Train with PickScore for AI Feedback Source: https://context7.com/salesforceairesearch/diffusiondpo/llms.txt Trains a model using PickScore as the preference selector for AI feedback. Other scoring models like HPS, CLIP, or Aesthetics can be used by changing the --choice_model argument. ```bash # Train with PickScore as the preference selector accelerate launch train.py \ --pretrained_model_name_or_path="runwayml/stable-diffusion-v1-5" \ --dataset_name="yuvalkirstain/pickapic_v2" \ --train_batch_size=1 \ --gradient_accumulation_steps=128 \ --max_train_steps=2000 \ --beta_dpo=5000 \ --choice_model="pickscore" \ --output_dir="dpo-pickscore-output" ``` -------------------------------- ### Initialize PickScore Selector Source: https://github.com/salesforceairesearch/diffusiondpo/blob/main/quick_samples.ipynb Initializes the `Selector` from `utils.pickscore_utils` for automatically scoring generations using a reward model. The selector is moved to the CUDA device. ```python # Can do clip_utils, aes_utils, hps_utils from utils.pickscore_utils import Selector # Score generations automatically w/ reward model ps_selector = Selector('cuda') ``` -------------------------------- ### Resume Training from Latest Checkpoint Source: https://context7.com/salesforceairesearch/diffusiondpo/llms.txt Resumes training from the most recent checkpoint. This is the default behavior when `--resume_from_checkpoint` is set to 'latest'. ```bash # Resume from latest checkpoint (default behavior) accelerate launch train.py \ --pretrained_model_name_or_path="runwayml/stable-diffusion-v1-5" \ --dataset_name="yuvalkirstain/pickapic_v2" \ --train_batch_size=1 \ --gradient_accumulation_steps=128 \ --max_train_steps=2000 \ --beta_dpo=5000 \ --output_dir="dpo-sd15-output" \ --resume_from_checkpoint="latest" ``` -------------------------------- ### Score Images Against Prompt Source: https://context7.com/salesforceairesearch/diffusiondpo/llms.txt Uses CLIP to calculate similarity scores between a list of images and a given text prompt. Ensure CLIP model is initialized. ```python from PIL import Image images = [ Image.open("image1.png"), Image.open("image2.png") ] prompt = "A robot playing chess" # Get CLIP similarity scores scores = clip_selector.score(images, prompt) print(f"CLIP scores: {scores}") ``` -------------------------------- ### Load DPO UNet Model Source: https://github.com/salesforceairesearch/diffusiondpo/blob/main/quick_samples.ipynb Loads a pre-trained UNet model for Direct Preference Optimization (DPO). Supports both SDXL and potentially other versions. Ensure the correct model name and subfolder are specified. The model is moved to the CUDA device and uses float16 precision. ```python dpo_unet = UNet2DConditionModel.from_pretrained( # 'mhdang/dpo-sd1.5-text2image-v1', 'mhdang/dpo-sdxl-text2image-v1', # alternatively use local ckptdir (*/checkpoint-n/) subfolder='unet', torch_dtype=torch.float16 ).to('cuda') ``` -------------------------------- ### Evaluate with PickScore Source: https://context7.com/salesforceairesearch/diffusiondpo/llms.txt Measures image-text alignment using the PickScore utility. ```python from utils.pickscore_utils import Selector from PIL import Image import torch # Initialize PickScore selector ps_selector = Selector('cuda') # Score a list of images against a prompt images = [ Image.open("baseline_image.png"), Image.open("dpo_image.png") ] prompt = "A beautiful landscape with mountains and a lake" # Get scores (higher is better, measures image-text alignment) scores = ps_selector.score(images, prompt) print(f"Baseline score: {scores[0]:.4f}") print(f"DPO score: {scores[1]:.4f}") # Get softmax probabilities for ranking probs = ps_selector.score(images, prompt, softmax=True) print(f"Preference probability - Baseline: {probs[0]:.2%}, DPO: {probs[1]:.2%}") ``` -------------------------------- ### Image Generation Function Source: https://github.com/salesforceairesearch/diffusiondpo/blob/main/quick_samples.ipynb Defines a function `gen` that takes a prompt and an optional seed to generate images. It iterates through the specified UNet models (either both or just the DPO one), sets the pipeline's UNet, generates an image using the provided prompt and seed, displays it, and returns a list of generated images. The `run_baseline` parameter controls whether to compare with the original UNet. ```python def gen(prompt, seed=0, run_baseline=True): ims = [] generator = torch.Generator(device='cuda') for unet_i in ([0, 1] if run_baseline else [1]): print(f"Prompt: {prompt}\nSeed: {seed}\n{names[unet_i]}") pipe.unet = unets[unet_i] generator = generator.manual_seed(seed) im = pipe(prompt=prompt, generator=generator, guidance_scale=gs).images[0] display(im) ims.append(im) return ims ``` -------------------------------- ### Evaluate with Human Preference Score (HPS) Source: https://context7.com/salesforceairesearch/diffusiondpo/llms.txt Uses HPS v2 to score images based on human preference alignment. ```python from utils.hps_utils import Selector from PIL import Image # Initialize HPS selector (downloads model on first use) hps_selector = Selector('cuda') # Score images against prompt images = [ Image.open("image1.png"), Image.open("image2.png") ] prompt = "A serene forest scene with sunlight filtering through trees" # Get HPS scores (higher indicates better alignment with human preferences) scores = hps_selector.score(images, prompt) print(f"HPS scores: {scores}") ``` -------------------------------- ### Load Diffusion Pipeline Source: https://github.com/salesforceairesearch/diffusiondpo/blob/main/quick_samples.ipynb Loads the appropriate diffusion pipeline (StableDiffusionXLPipeline or StableDiffusionPipeline) based on the `pretrained_model_name`. It's configured for float16 precision, uses safetensors if available, and is moved to the CUDA device. The safety checker is disabled to prevent over-filtering. ```python if 'stable-diffusion-xl' in pretrained_model_name: pipe = StableDiffusionXLPipeline.from_pretrained( pretrained_model_name, torch_dtype=torch.float16, variant="fp16", use_safetensors=True ).to("cuda") else: pipe = StableDiffusionPipeline.from_pretrained(pretrained_model_name, torch_dtype=torch.float16) pipe = pipe.to('cuda') pipe.safety_checker = None # Trigger-happy, blacks out >50% of "robot tiger" ``` -------------------------------- ### Load DPO-Trained UNet and Replace in Pipeline Source: https://context7.com/salesforceairesearch/diffusiondpo/llms.txt Loads a DPO-trained UNet model for Stable Diffusion 1.5 and replaces the default UNet in a StableDiffusionPipeline. Ensure torch_dtype is set to torch.float16 for CUDA compatibility. ```python from diffusers import StableDiffusionPipeline, StableDiffusionXLPipeline, UNet2DConditionModel import torch torch.set_grad_enabled(False) # Load DPO-trained UNet for SD1.5 dpo_unet = UNet2DConditionModel.from_pretrained( 'mhdang/dpo-sd1.5-text2image-v1', # or local checkpoint path subfolder='unet', torch_dtype=torch.float16 ).to('cuda') # Create pipeline and replace UNet pipe = StableDiffusionPipeline.from_pretrained( "runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16 ).to('cuda') pipe.unet = dpo_unet ``` -------------------------------- ### Evaluate with Aesthetics Score Source: https://context7.com/salesforceairesearch/diffusiondpo/llms.txt Predicts aesthetic quality independent of the input prompt. ```python from utils.aes_utils import Selector from PIL import Image # Initialize aesthetics selector (uses CLIP ViT-L/14 + MLP) aes_selector = Selector('cuda') # Score images (prompt is ignored for aesthetics scoring) images = [ Image.open("image1.png"), Image.open("image2.png") ] # Higher scores indicate more aesthetically pleasing images scores = aes_selector.score(images, prompt_not_used="") print(f"Aesthetics scores: {scores}") ``` -------------------------------- ### Import Libraries and Disable Gradients Source: https://github.com/salesforceairesearch/diffusiondpo/blob/main/quick_samples.ipynb Imports the necessary libraries from the diffusers package and PyTorch. Disables gradient calculation to save memory and speed up inference. ```python from diffusers import StableDiffusionPipeline, UNet2DConditionModel, StableDiffusionXLPipeline import torch torch.set_grad_enabled(False) ``` -------------------------------- ### Cite Diffusion-DPO Source: https://github.com/salesforceairesearch/diffusiondpo/blob/main/README.md BibTeX entry for referencing the Diffusion-DPO research paper. ```bibtex @misc{wallace2023diffusion, title={Diffusion Model Alignment Using Direct Preference Optimization}, author={Bram Wallace and Meihua Dang and Rafael Rafailov and Linqi Zhou and Aaron Lou and Senthil Purushwalkam and Stefano Ermon and Caiming Xiong and Shafiq Joty and Nikhil Naik}, year={2023}, eprint={2311.12908}, archivePrefix={arXiv}, primaryClass={cs.CV} ``` -------------------------------- ### Set Pretrained Model Name and Guidance Scale Source: https://github.com/salesforceairesearch/diffusiondpo/blob/main/quick_samples.ipynb Defines the name of the pre-trained diffusion model to be used. The guidance scale 'gs' is set conditionally based on whether the model name contains 'stable-diffusion-xl'. ```python # pretrained_model_name = "CompVis/stable-diffusion-v1-4" # pretrained_model_name = "runwayml/stable-diffusion-v1-5" pretrained_model_name = "stabilityai/stable-diffusion-xl-base-1.0" gs = (5 if 'stable-diffusion-xl' in pretrained_model_name else 7.5) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.