Views
No views yet
google/ddpm-celebahq-256 implementation and has been updated to support safetensors for model storage.UNet2DModelsafetensors.safetensors format, a safer and more efficient.

















diffusers library from Hugging Face. You can load the model in safetensors format.pip install torch diffusers safetensorsenvironment.yml file.
conda env create --file environment.ymlconda activate inpaint1import torch
2import numpy as np
3import PIL.Image
4from diffusers import UNet2DModel, DDPMScheduler
5import tqdm
6
7# 1. Initialize the model
8# Choose a model ID, use google's with use_safetensors=False, use Mou11209203's with use_safetensors=True
9repo_id = "google/ddpm-celebahq-256"
10repo_id1 = "Mou11209203/ddpm-celebahq-256"
11model = UNet2DModel.from_pretrained(repo_id1, use_safetensors=True)
12model.to("cuda") # Move the model to GPU
13print("model.config: ", model.config)
14
15# 2. Initialize the scheduler
16scheduler = DDPMScheduler.from_pretrained(repo_id1)
17print("scheduler.config: ", scheduler.config)
18
19# 3. Create an image with Gaussian noise
20torch.manual_seed(1733783271) # Fix the random seed for reproducibility
21noisy_sample = torch.randn(1, model.config.in_channels, model.config.sample_size, model.config.sample_size).to("cuda")
22print(f"Noisy sample shape: {noisy_sample.shape}")
23
24# 4. Define a function to display the image
25def display_sample(sample, i):
26 image_processed = sample.cpu().permute(0, 2, 3, 1)
27 image_processed = (image_processed + 1.0) * 127.5
28 image_processed = image_processed.numpy().astype(np.uint8)
29
30 image_pil = PIL.Image.fromarray(image_processed[0])
31 print(f"Image at step {i}")
32 image_pil.show()
33
34# 5. Reverse diffusion process
35sample = noisy_sample
36for i, t in enumerate(tqdm.tqdm(scheduler.timesteps)):
37 # 1. Predict the noise residual
38 with torch.no_grad():
39 residual = model(sample, t).sample
40
41 # 2. Compute the less noisy image and move x_t -> x_t-1
42 sample = scheduler.step(residual, t, sample).prev_sample
43
44 # 3. Optionally display the image (every 50 steps)
45 if (i + 1) % 50 == 0:
46 display_sample(sample, i + 1)
47
48print("Denoising complete.")1# !pip install diffusers
2from diffusers import DDPMPipeline, DDIMPipeline, PNDMPipelinediffusion_pytorch_model.safetensorsenvironment.ymlconfig.jsonscheduler_config.jsongoogle/ddpm-celebahq-256 repository:1@misc{google/ddpm-celebahq-256,
2 author = {Google Research},
3 title = {DDPM CelebAHQ 256},
4 year = {2022},
5 url = {https://huggingface.co/google/ddpm-celebahq-256}
6}