Noise and denoise images
#1
by
MiszaNika
- opened
Hello,
I am wondering how I can feed my image into this model to see what it will look like after adding noise and denoising by the model, so that I can then count for example the fid metric? I would be grateful for a hint on how to use the model in this way.
I don't know if this is what you want, but I tried to make a demo to show the step-by-step denoising process. Hope this helps.
The code blow is modified and combined from the Diffusers's Doc:
import torch
from diffusers import DDPMPipeline
from diffusers.utils import make_image_grid
from PIL import Image
import numpy as np
pipe = DDPMPipeline.from_pretrained("ceyda/ddpm-ema-butterflies-64")
pipe.to("cuda")
scheduler = pipe.scheduler
model = pipe.unet
scheduler.set_timesteps(20)
sample_size = model.config.sample_size
noise = torch.randn((1, 3, sample_size, sample_size), device="cuda")
input = noise
images = []
for t in scheduler.timesteps:
with torch.no_grad():
noisy_residual = model(input, t).sample
previous_noisy_sample = scheduler.step(noisy_residual, t, input).prev_sample
input = previous_noisy_sample
image = (input / 2 + 0.5).clamp(0, 1).squeeze()
image = (image.permute(1, 2, 0) * 255).round().to(torch.uint8).cpu().numpy()
image = Image.fromarray(image)
images.append(image)
make_image_grid(images, rows=4, cols=5)