patrickvonplaten
commited on
Commit
•
c9e4251
1
Parent(s):
8f575ac
Upload modeling_ddim.py
Browse files- modeling_ddim.py +94 -0
modeling_ddim.py
ADDED
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
import torch
|
18 |
+
|
19 |
+
import tqdm
|
20 |
+
from diffusers import DiffusionPipeline
|
21 |
+
|
22 |
+
|
23 |
+
class DDIM(DiffusionPipeline):
|
24 |
+
def __init__(self, unet, noise_scheduler):
|
25 |
+
super().__init__()
|
26 |
+
self.register_modules(unet=unet, noise_scheduler=noise_scheduler)
|
27 |
+
|
28 |
+
def __call__(self, batch_size=1, generator=None, torch_device=None, eta=0.0, num_inference_steps=50):
|
29 |
+
# eta corresponds to η in paper and should be between [0, 1]
|
30 |
+
if torch_device is None:
|
31 |
+
torch_device = "cuda" if torch.cuda.is_available() else "cpu"
|
32 |
+
|
33 |
+
num_trained_timesteps = self.noise_scheduler.num_timesteps
|
34 |
+
inference_step_times = range(0, num_trained_timesteps, num_trained_timesteps // num_inference_steps)
|
35 |
+
|
36 |
+
self.unet.to(torch_device)
|
37 |
+
|
38 |
+
# Sample gaussian noise to begin loop
|
39 |
+
image = self.noise_scheduler.sample_noise(
|
40 |
+
(batch_size, self.unet.in_channels, self.unet.resolution, self.unet.resolution),
|
41 |
+
device=torch_device,
|
42 |
+
generator=generator,
|
43 |
+
)
|
44 |
+
|
45 |
+
# See formulas (9), (10) and (7) of DDIM paper https://arxiv.org/pdf/2010.02502.pdf
|
46 |
+
# Ideally, read DDIM paper in-detail understanding
|
47 |
+
|
48 |
+
# Notation (<variable name> -> <name in paper>
|
49 |
+
# - pred_noise_t -> e_theta(x_t, t)
|
50 |
+
# - pred_original_image -> f_theta(x_t, t) or x_0
|
51 |
+
# - std_dev_t -> sigma_t
|
52 |
+
|
53 |
+
for t in tqdm.tqdm(reversed(range(num_inference_steps)), total=num_inference_steps):
|
54 |
+
# 1. predict noise residual
|
55 |
+
with torch.no_grad():
|
56 |
+
pred_noise_t = self.unet(image, inference_step_times[t])
|
57 |
+
|
58 |
+
# 2. get actual t and t-1
|
59 |
+
train_step = inference_step_times[t]
|
60 |
+
prev_train_step = inference_step_times[t - 1] if t > 0 else -1
|
61 |
+
|
62 |
+
# 3. compute alphas, betas
|
63 |
+
alpha_prod_t = self.noise_scheduler.get_alpha_prod(train_step)
|
64 |
+
alpha_prod_t_prev = self.noise_scheduler.get_alpha_prod(prev_train_step)
|
65 |
+
beta_prod_t_sqrt = (1 - alpha_prod_t).sqrt()
|
66 |
+
beta_prod_t_prev_sqrt = (1 - alpha_prod_t_prev).sqrt()
|
67 |
+
|
68 |
+
# 4. Compute predicted previous image from predicted noise
|
69 |
+
# First: compute predicted original image from predicted noise also called
|
70 |
+
# "predicted x_0" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf
|
71 |
+
pred_original_image = (image - beta_prod_t_sqrt * pred_noise_t) / alpha_prod_t.sqrt()
|
72 |
+
# Second: Clip "predicted x_0"
|
73 |
+
pred_original_image = torch.clamp(pred_original_image, -1, 1)
|
74 |
+
# Third: Compute variance: "sigma_t" -> see
|
75 |
+
# std_dev_t = (1 - alpha_prod_t / alpha_prod_t_prev).sqrt() * beta_prod_t_prev_sqrt / beta_prod_t_sqrt
|
76 |
+
std_dev_t = (1 - alpha_prod_t / alpha_prod_t_prev).sqrt()
|
77 |
+
std_dev_t = std_dev_t * eta
|
78 |
+
# Fourth: Compute "direction pointing to x_t" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf
|
79 |
+
pred_image_direction = (1 - alpha_prod_t_prev - std_dev_t**2).sqrt() * pred_noise_t
|
80 |
+
|
81 |
+
# Fourth: Compute outer formula (DDIM formula)
|
82 |
+
pred_prev_image = alpha_prod_t_prev.sqrt() * pred_original_image + pred_image_direction
|
83 |
+
|
84 |
+
# if eta > 0.0 add noise. Note eta = 1.0 essentially corresponds to DDPM
|
85 |
+
if eta > 0.0:
|
86 |
+
noise = self.noise_scheduler.sample_noise(image.shape, device=image.device, generator=generator)
|
87 |
+
prev_image = pred_prev_image + std_dev_t * noise
|
88 |
+
else:
|
89 |
+
prev_image = pred_prev_image
|
90 |
+
|
91 |
+
# Set current image to prev_image: x_t -> x_t-1
|
92 |
+
image = prev_image
|
93 |
+
|
94 |
+
return image
|