patrickvonplaten's picture
add pipeline and give image a 2nd try
a57626b
raw
history blame
2.24 kB
#!/usr/bin/env python3
from diffusers import UNetUnconditionalModel, DDIMScheduler, VQModel
import torch
import PIL.Image
import numpy as np
import tqdm
# 1. Unroll the full loop
# ==================================================================
# load all models
unet = UNetUnconditionalModel.from_pretrained("./", subfolder="unet")
vqvae = VQModel.from_pretrained("./", subfolder="vqvae")
scheduler = DDIMScheduler.from_config("./", subfolder="scheduler")
# set to cuda
torch_device = "cuda" if torch.cuda.is_available() else "cpu"
unet.to(torch_device)
vqvae.to(torch_device)
# generate gaussian noise to be decoded
generator = torch.manual_seed(0)
noise = torch.randn(
(1, unet.in_channels, unet.image_size, unet.image_size),
generator=generator,
).to(torch_device)
# set inference steps for DDIM
scheduler.set_timesteps(num_inference_steps=50)
image = noise
for t in tqdm.tqdm(scheduler.timesteps):
# predict noise residual of previous image
with torch.no_grad():
residual = unet(image, t)["sample"]
# compute previous image x_t according to DDIM formula
prev_image = scheduler.step(residual, t, image, eta=0.0)["prev_sample"]
# x_t-1 -> x_t
image = prev_image
# decode image with vae
with torch.no_grad():
image = vqvae.decode(image)
# process image
image_processed = image.cpu().permute(0, 2, 3, 1)
image_processed = image_processed * 255.
image_processed = image_processed.numpy().astype(np.uint8)
image_pil = PIL.Image.fromarray(image_processed[0])
image_pil.save("generated_image_unrolled.png")
# 2. Use pipeline
# ==================================================================
from diffusers import LatentDiffusionUncondPipeline
import torch
import PIL.Image
import numpy as np
import tqdm
pipeline = LatentDiffusionUncondPipeline.from_pretrained("./")
# generatae image by calling the pipeline
generator = torch.manual_seed(0)
image = pipeline(generator=generator, num_inference_steps=50)["sample"]
# process image
image_processed = image.cpu().permute(0, 2, 3, 1)
image_processed = image_processed * 255.
image_processed = image_processed.numpy().astype(np.uint8)
image_pil = PIL.Image.fromarray(image_processed[0])
image_pil.save("generated_image_pipeline.png")