Alex-23 commited on
Commit
cd9c708
·
1 Parent(s): 2ef4a46

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -0
app.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from diffusers import DDPMPipeline
2
+ image_pipe = DDPMPipeline.from_pretrained("google/ddpm-celebahq-256")
3
+ image_pipe.to("cuda")
4
+ images = image_pipe().images
5
+ image_pipe
6
+ from diffusers import UNet2DModel
7
+
8
+ repo_id = "google/ddpm-church-256"
9
+ model = UNet2DModel.from_pretrained(repo_id)
10
+ model
11
+ model.config
12
+ model_random = UNet2DModel(**model.config)
13
+ model_random.save_pretrained("my_model")
14
+ model_random = UNet2DModel.from_pretrained("my_model")
15
+ import torch
16
+
17
+ torch.manual_seed(0)
18
+
19
+ noisy_sample = torch.randn(
20
+ 1, model.config.in_channels, model.config.sample_size, model.config.sample_size
21
+ )
22
+ noisy_sample.shape
23
+ with torch.no_grad():
24
+ noisy_residual = model(sample=noisy_sample, timestep=2).sample
25
+ noisy_residual.shape
26
+ from diffusers import DDPMScheduler
27
+
28
+ scheduler = DDPMScheduler.from_config(repo_id)
29
+ scheduler.config
30
+ scheduler.save_config("my_scheduler")
31
+ new_scheduler = DDPMScheduler.from_config("my_scheduler")
32
+ less_noisy_sample = scheduler.step(
33
+ model_output=noisy_residual, timestep=2, sample=noisy_sample
34
+ ).prev_sample
35
+ less_noisy_sample.shape
36
+ import PIL.Image
37
+ import numpy as np
38
+
39
+ def display_sample(sample, i):
40
+ image_processed = sample.cpu().permute(0, 2, 3, 1)
41
+ image_processed = (image_processed + 1.0) * 127.5
42
+ image_processed = image_processed.numpy().astype(np.uint8)
43
+
44
+ image_pil = PIL.Image.fromarray(image_processed[0])
45
+ display(f"Image at step {i}")
46
+ display(image_pil)
47
+ model.to("cuda")
48
+ noisy_sample = noisy_sample.to("cuda")
49
+ import tqdm
50
+
51
+ sample = noisy_sample
52
+
53
+ for i, t in enumerate(tqdm.tqdm(scheduler.timesteps)):
54
+ # 1. predict noise residual
55
+ with torch.no_grad():
56
+ residual = model(sample, t).sample
57
+
58
+ # 2. compute less noisy image and set x_t -> x_t-1
59
+ sample = scheduler.step(residual, t, sample).prev_sample
60
+
61
+ # 3. optionally look at image
62
+ if (i + 1) % 50 == 0:
63
+ display_sample(sample, i + 1)
64
+ from diffusers import DDIMScheduler
65
+
66
+ scheduler = DDIMScheduler.from_config(repo_id)
67
+ scheduler.set_timesteps(num_inference_steps=50)
68
+ import tqdm
69
+
70
+ sample = noisy_sample
71
+
72
+ for i, t in enumerate(tqdm.tqdm(scheduler.timesteps)):
73
+ # 1. predict noise residual
74
+ with torch.no_grad():
75
+ residual = model(sample, t).sample
76
+
77
+ # 2. compute previous image and set x_t -> x_t-1
78
+ sample = scheduler.step(residual, t, sample).prev_sample
79
+
80
+ # 3. optionally look at image
81
+ if (i + 1) % 10 == 0:
82
+ display_sample(sample, i + 1)