|
|
|
from diffusers import UNetUnconditionalModel, DDIMScheduler, VQModel |
|
import torch |
|
import PIL.Image |
|
import numpy as np |
|
import tqdm |
|
|
|
|
|
|
|
|
|
unet = UNetUnconditionalModel.from_pretrained("./", subfolder="unet") |
|
vqvae = VQModel.from_pretrained("./", subfolder="vqvae") |
|
scheduler = DDIMScheduler.from_config("./", subfolder="scheduler") |
|
|
|
|
|
torch_device = "cuda" if torch.cuda.is_available() else "cpu" |
|
|
|
unet.to(torch_device) |
|
vqvae.to(torch_device) |
|
|
|
|
|
generator = torch.manual_seed(0) |
|
noise = torch.randn( |
|
(1, unet.in_channels, unet.image_size, unet.image_size), |
|
generator=generator, |
|
).to(torch_device) |
|
|
|
|
|
scheduler.set_timesteps(num_inference_steps=50) |
|
|
|
image = noise |
|
for t in tqdm.tqdm(scheduler.timesteps): |
|
|
|
with torch.no_grad(): |
|
residual = unet(image, t)["sample"] |
|
|
|
|
|
prev_image = scheduler.step(residual, t, image, eta=0.0)["prev_sample"] |
|
|
|
|
|
image = prev_image |
|
|
|
|
|
with torch.no_grad(): |
|
image = vqvae.decode(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") |
|
|
|
|
|
|
|
|
|
from diffusers import LatentDiffusionUncondPipeline |
|
import torch |
|
import PIL.Image |
|
import numpy as np |
|
import tqdm |
|
|
|
pipeline = LatentDiffusionUncondPipeline.from_pretrained("./") |
|
|
|
|
|
generator = torch.manual_seed(0) |
|
image = pipeline(generator=generator, num_inference_steps=50)["sample"] |
|
|
|
|
|
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") |
|
|