Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ddim noise comparative analysis pipeline #2665

Conversation

aengusng8
Copy link
Contributor

Research question: What visual concepts do the diffusion models learn from each noise level during training?

The P2 weighting (CVPR 2022) paper proposed an approach to answer this question, which is their second contribution.
The approach consists of the following steps:

  1. The input is an image x0.
  2. Perturb it to xt using a diffusion process q(xt|x0).
    • strength is a value between 0.0 and 1.0, that controls the amount of noise that is added to the input image. Values that approach 1.0 allow for lots of variations but will also produce images that are not semantically consistent with the input.
  3. Reconstruct the image with the learned denoising process pθ(ˆx0|xt).
  4. Compare x0 and ˆx0 among various t to show how each step contributes to the sample.
    The authors used openai/guided-diffusion model to denoise images in FFHQ dataset. This pipeline extends their second contribution by investigating DDIM on any input image.
import torch
from PIL import Image
import numpy as np

image_path = "path/to/your/image" # images from CelebA-HQ might be better
image_pil = Image.open(image_path)
image_name = image_path.split("/")[-1].split(".")[0]

device = torch.device("cpu" if not torch.cuda.is_available() else "cuda")
pipe = DiffusionPipeline.from_pretrained(
    "google/ddpm-ema-celebahq-256",
    custom_pipeline="ddim_noise_comparative_analysis",
)
pipe = pipe.to(device)

for strength in np.linspace(0.1, 1, 25):
    denoised_image, latent_timestep = pipe(
        image_pil, strength=strength, return_dict=False
    )
    denoised_image = denoised_image[0]
    denoised_image.save(
        f"noise_comparative_analysis_{image_name}_{latent_timestep}.png"
    )

Here is the result of this pipeline (which is DDIM) on CelebA-HQ dataset.

noise-comparative-analysis

@HuggingFaceDocBuilderDev
Copy link

HuggingFaceDocBuilderDev commented Mar 14, 2023

The documentation is not available anymore as the PR was closed or merged.

@aengusng8 aengusng8 force-pushed the add-DDIM-noise-comparative-analysis-pipeline branch from 18241d1 to 35ba559 Compare March 14, 2023 05:05
Copy link
Contributor

@patrickvonplaten patrickvonplaten left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super cool!

@patrickvonplaten patrickvonplaten merged commit 268ebcb into huggingface:main Mar 14, 2023
w4ffl35 pushed a commit to w4ffl35/diffusers that referenced this pull request Apr 14, 2023
* add DDIM Noise Comparative Analysis pipeline

* update README

* add comments

* run BLACK format
AmericanPresidentJimmyCarter pushed a commit to AmericanPresidentJimmyCarter/diffusers that referenced this pull request Apr 26, 2024
* add DDIM Noise Comparative Analysis pipeline

* update README

* add comments

* run BLACK format
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants