|
|
|
from diffusers import UNetUnconditionalModel, DDIMScheduler, VQModel |
|
import torch |
|
import PIL.Image |
|
import numpy as np |
|
import tqdm |
|
|
|
seed = 3 |
|
|
|
|
|
|
|
|
|
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(seed) |
|
noise = torch.randn( |
|
(1, unet.in_channels, unet.image_size, unet.image_size), |
|
generator=generator, |
|
).to(torch_device) |
|
|
|
|
|
scheduler.set_timesteps(num_inference_steps=200) |
|
|
|
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 + 1.0) * 127.5 |
|
image_processed = image_processed.clamp(0, 255).numpy().astype(np.uint8) |
|
image_pil = PIL.Image.fromarray(image_processed[0]) |
|
|
|
|
|
|
|
|
|
from diffusers import LatentDiffusionUncondPipeline |
|
import torch |
|
import PIL.Image |
|
import numpy as np |
|
import tqdm |
|
|
|
pipeline = LatentDiffusionUncondPipeline.from_pretrained("./") |
|
|
|
|
|
generator = torch.manual_seed(seed) |
|
image = pipeline(generator=generator, num_inference_steps=200)["sample"] |
|
|
|
|
|
image_processed = image.cpu().permute(0, 2, 3, 1) |
|
image_processed = (image_processed + 1.0) * 127.5 |
|
image_processed = image_processed.clamp(0, 255).numpy().astype(np.uint8) |
|
image_pil = PIL.Image.fromarray(image_processed[0]) |
|
|
|
image_pil.save(f"generated_image_{seed}.png") |
|
|