Views
No views yet
![]() | ![]() | ![]() |
1import torch
2from diffusers import DDPMPipeline
3from PIL import Image
4
5if torch.backends.mps.is_available():
6 device = "mps"
7elif torch.cuda.is_available():
8 device = "cuda"
9else:
10 device = "cpu"
11
12pipe = DDPMPipeline.from_pretrained("DD-65/diffusionnumbers").to(device)
13
14digit = 7
15labels = torch.tensor([digit], device=device)
16images = torch.randn((1, 1, 64, 64), device=device)
17
18pipe.scheduler.set_timesteps(1000)
19for timestep in pipe.scheduler.timesteps:
20 with torch.no_grad():
21 noise_prediction = pipe.unet(
22 images,
23 timestep,
24 class_labels=labels,
25 ).sample
26
27 images = pipe.scheduler.step(
28 noise_prediction,
29 timestep,
30 images,
31 ).prev_sample
32
33image = (images[0, 0] / 2 + 0.5).clamp(0, 1)
34image = image.mul(255).round().to(torch.uint8).cpu().numpy()
35Image.fromarray(image).save("digit-7.png")digit to any integer from 0 through 9.
See also the Github repo for the complete training code and a more elaborate inference script.UNet2DModel with a DDPMScheduler2e-4 learning rate