valhalla commited on
Commit
070bfbf
1 Parent(s): ebee4b3
Files changed (2) hide show
  1. model_index.json +11 -10
  2. modeling_ddpm.py +61 -0
model_index.json CHANGED
@@ -1,11 +1,12 @@
1
  {
2
- "_class_name": "DDPM",
3
- "noise_scheduler": [
4
- "diffusers",
5
- "GaussianDDPMScheduler"
6
- ],
7
- "unet": [
8
- "diffusers",
9
- "UNetModel"
10
- ]
11
- }
 
 
1
  {
2
+ "_class_name": "DDPM",
3
+ "_module": "modeling_ddpm.py",
4
+ "noise_scheduler": [
5
+ "diffusers",
6
+ "GaussianDDPMScheduler"
7
+ ],
8
+ "unet": [
9
+ "diffusers",
10
+ "UNetModel"
11
+ ]
12
+ }
modeling_ddpm.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2022 The HuggingFace Team. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+
14
+ # limitations under the License.
15
+
16
+
17
+ from diffusers import DiffusionPipeline
18
+ import tqdm
19
+ import torch
20
+
21
+
22
+ class DDPM(DiffusionPipeline):
23
+
24
+ modeling_file = "modeling_ddpm.py"
25
+
26
+ def __init__(self, unet, noise_scheduler):
27
+ super().__init__()
28
+ self.register_modules(unet=unet, noise_scheduler=noise_scheduler)
29
+
30
+ def __call__(self, batch_size=1, generator=None, torch_device=None):
31
+ if torch_device is None:
32
+ torch_device = "cuda" if torch.cuda.is_available() else "cpu"
33
+
34
+ self.unet.to(torch_device)
35
+ # 1. Sample gaussian noise
36
+ image = self.noise_scheduler.sample_noise((batch_size, self.unet.in_channels, self.unet.resolution, self.unet.resolution), device=torch_device, generator=generator)
37
+ for t in tqdm.tqdm(reversed(range(len(self.noise_scheduler))), total=len(self.noise_scheduler)):
38
+ # i) define coefficients for time step t
39
+ clip_image_coeff = 1 / torch.sqrt(self.noise_scheduler.get_alpha_prod(t))
40
+ clip_noise_coeff = torch.sqrt(1 / self.noise_scheduler.get_alpha_prod(t) - 1)
41
+ image_coeff = (1 - self.noise_scheduler.get_alpha_prod(t - 1)) * torch.sqrt(self.noise_scheduler.get_alpha(t)) / (1 - self.noise_scheduler.get_alpha_prod(t))
42
+ clip_coeff = torch.sqrt(self.noise_scheduler.get_alpha_prod(t - 1)) * self.noise_scheduler.get_beta(t) / (1 - self.noise_scheduler.get_alpha_prod(t))
43
+
44
+ # ii) predict noise residual
45
+ with torch.no_grad():
46
+ noise_residual = self.unet(image, t)
47
+
48
+ # iii) compute predicted image from residual
49
+ # See 2nd formula at https://github.com/hojonathanho/diffusion/issues/5#issue-896554416 for comparison
50
+ pred_mean = clip_image_coeff * image - clip_noise_coeff * noise_residual
51
+ pred_mean = torch.clamp(pred_mean, -1, 1)
52
+ prev_image = clip_coeff * pred_mean + image_coeff * image
53
+
54
+ # iv) sample variance
55
+ prev_variance = self.noise_scheduler.sample_variance(t, prev_image.shape, device=torch_device, generator=generator)
56
+
57
+ # v) sample x_{t-1} ~ N(prev_image, prev_variance)
58
+ sampled_prev_image = prev_image + prev_variance
59
+ image = sampled_prev_image
60
+
61
+ return image