lysandre HF staff commited on
Commit
33616ab
1 Parent(s): e549525

Pipeline files

Browse files
Files changed (3) hide show
  1. model_index.json +12 -0
  2. modeling_ddpm.py +60 -0
  3. scheduler_config.json +8 -0
model_index.json ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_class_name": "DDPMPipeline",
3
+ "_module": "modeling_ddpm.py",
4
+ "scheduler": [
5
+ "diffusers",
6
+ "DDPMScheduler"
7
+ ],
8
+ "unet": [
9
+ "diffusers",
10
+ "UNetUnconditionalModel"
11
+ ]
12
+ }
modeling_ddpm.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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, generator=None, torch_device=None):
31
+ torch_device = "cuda" if torch.cuda.is_available() else "cpu"
32
+
33
+ self.unet.to(torch_device)
34
+ # 1. Sample gaussian noise
35
+ image = self.noise_scheduler.sample_noise((1, self.unet.in_channels, self.unet.resolution, self.unet.resolution), device=torch_device, generator=generator)
36
+ for t in tqdm.tqdm(reversed(range(len(self.noise_scheduler))), total=len(self.noise_scheduler)):
37
+ # i) define coefficients for time step t
38
+ clip_image_coeff = 1 / torch.sqrt(self.noise_scheduler.get_alpha_prod(t))
39
+ clip_noise_coeff = torch.sqrt(1 / self.noise_scheduler.get_alpha_prod(t) - 1)
40
+ 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))
41
+ 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))
42
+
43
+ # ii) predict noise residual
44
+ with torch.no_grad():
45
+ noise_residual = self.unet(image, t)
46
+
47
+ # iii) compute predicted image from residual
48
+ # See 2nd formula at https://github.com/hojonathanho/diffusion/issues/5#issue-896554416 for comparison
49
+ pred_mean = clip_image_coeff * image - clip_noise_coeff * noise_residual
50
+ pred_mean = torch.clamp(pred_mean, -1, 1)
51
+ prev_image = clip_coeff * pred_mean + image_coeff * image
52
+
53
+ # iv) sample variance
54
+ prev_variance = self.noise_scheduler.sample_variance(t, prev_image.shape, device=torch_device, generator=generator)
55
+
56
+ # v) sample x_{t-1} ~ N(prev_image, prev_variance)
57
+ sampled_prev_image = prev_image + prev_variance
58
+ image = sampled_prev_image
59
+
60
+ return image
scheduler_config.json ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_class_name": "GaussianDDPMScheduler",
3
+ "beta_end": 0.02,
4
+ "beta_schedule": "linear",
5
+ "beta_start": 0.0001,
6
+ "timesteps": 1000,
7
+ "variance_type": "fixed_large"
8
+ }