Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
diffusers
Commits
dcb23b2d
Commit
dcb23b2d
authored
Jun 17, 2022
by
Patrick von Platen
Browse files
rename image to sample in schedulers
parent
13a78b3c
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
39 additions
and
39 deletions
+39
-39
src/diffusers/schedulers/scheduling_ddim.py
src/diffusers/schedulers/scheduling_ddim.py
+14
-14
src/diffusers/schedulers/scheduling_ddpm.py
src/diffusers/schedulers/scheduling_ddpm.py
+19
-19
src/diffusers/schedulers/scheduling_pndm.py
src/diffusers/schedulers/scheduling_pndm.py
+6
-6
No files found.
src/diffusers/schedulers/scheduling_ddim.py
View file @
dcb23b2d
...
...
@@ -28,7 +28,7 @@ class DDIMScheduler(SchedulerMixin, ConfigMixin):
beta_schedule
=
"linear"
,
trained_betas
=
None
,
timestep_values
=
None
,
clip_predicted_
imag
e
=
True
,
clip_predicted_
sampl
e
=
True
,
tensor_format
=
"np"
,
):
super
().
__init__
()
...
...
@@ -40,7 +40,7 @@ class DDIMScheduler(SchedulerMixin, ConfigMixin):
)
self
.
timesteps
=
int
(
timesteps
)
self
.
timestep_values
=
timestep_values
# save the fixed timestep values for BDDM
self
.
clip_
imag
e
=
clip_predicted_
imag
e
self
.
clip_
sampl
e
=
clip_predicted_
sampl
e
if
beta_schedule
==
"linear"
:
self
.
betas
=
linear_beta_schedule
(
timesteps
,
beta_start
=
beta_start
,
beta_end
=
beta_end
)
...
...
@@ -111,17 +111,17 @@ class DDIMScheduler(SchedulerMixin, ConfigMixin):
return
variance
def
step
(
self
,
residual
,
imag
e
,
t
,
num_inference_steps
,
eta
,
use_clipped_residual
=
False
):
def
step
(
self
,
residual
,
sampl
e
,
t
,
num_inference_steps
,
eta
,
use_clipped_residual
=
False
):
# See formulas (12) and (16) of DDIM paper https://arxiv.org/pdf/2010.02502.pdf
# Ideally, read DDIM paper in-detail understanding
# Notation (<variable name> -> <name in paper>
# - pred_noise_t -> e_theta(x_t, t)
# - pred_original_
imag
e -> f_theta(x_t, t) or x_0
# - pred_original_
sampl
e -> f_theta(x_t, t) or x_0
# - std_dev_t -> sigma_t
# - eta -> η
# - pred_
imag
e_direction -> "direction pointingc to x_t"
# - pred_prev_
imag
e -> "x_t-1"
# - pred_
sampl
e_direction -> "direction pointingc to x_t"
# - pred_prev_
sampl
e -> "x_t-1"
# 1. get actual t and t-1
orig_t
=
self
.
get_orig_t
(
t
,
num_inference_steps
)
...
...
@@ -132,13 +132,13 @@ class DDIMScheduler(SchedulerMixin, ConfigMixin):
alpha_prod_t_prev
=
self
.
get_alpha_prod
(
orig_prev_t
)
beta_prod_t
=
1
-
alpha_prod_t
# 3. compute predicted original
imag
e from predicted noise also called
# 3. compute predicted original
sampl
e from predicted noise also called
# "predicted x_0" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf
pred_original_
image
=
(
imag
e
-
beta_prod_t
**
(
0.5
)
*
residual
)
/
alpha_prod_t
**
(
0.5
)
pred_original_
sample
=
(
sampl
e
-
beta_prod_t
**
(
0.5
)
*
residual
)
/
alpha_prod_t
**
(
0.5
)
# 4. Clip "predicted x_0"
if
self
.
clip_
imag
e
:
pred_original_
imag
e
=
self
.
clip
(
pred_original_
imag
e
,
-
1
,
1
)
if
self
.
clip_
sampl
e
:
pred_original_
sampl
e
=
self
.
clip
(
pred_original_
sampl
e
,
-
1
,
1
)
# 5. compute variance: "sigma_t(η)" -> see formula (16)
# σ_t = sqrt((1 − α_t−1)/(1 − α_t)) * sqrt(1 − α_t/α_t−1)
...
...
@@ -147,15 +147,15 @@ class DDIMScheduler(SchedulerMixin, ConfigMixin):
if
use_clipped_residual
:
# the residual is always re-derived from the clipped x_0 in GLIDE
residual
=
(
imag
e
-
alpha_prod_t
**
(
0.5
)
*
pred_original_
imag
e
)
/
beta_prod_t
**
(
0.5
)
residual
=
(
sampl
e
-
alpha_prod_t
**
(
0.5
)
*
pred_original_
sampl
e
)
/
beta_prod_t
**
(
0.5
)
# 6. compute "direction pointing to x_t" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf
pred_
imag
e_direction
=
(
1
-
alpha_prod_t_prev
-
std_dev_t
**
2
)
**
(
0.5
)
*
residual
pred_
sampl
e_direction
=
(
1
-
alpha_prod_t_prev
-
std_dev_t
**
2
)
**
(
0.5
)
*
residual
# 7. compute x_t without "random noise" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf
pred_prev_
imag
e
=
alpha_prod_t_prev
**
(
0.5
)
*
pred_original_
imag
e
+
pred_
imag
e_direction
pred_prev_
sampl
e
=
alpha_prod_t_prev
**
(
0.5
)
*
pred_original_
sampl
e
+
pred_
sampl
e_direction
return
pred_prev_
imag
e
return
pred_prev_
sampl
e
def
__len__
(
self
):
return
self
.
timesteps
src/diffusers/schedulers/scheduling_ddpm.py
View file @
dcb23b2d
...
...
@@ -29,7 +29,7 @@ class DDPMScheduler(SchedulerMixin, ConfigMixin):
trained_betas
=
None
,
timestep_values
=
None
,
variance_type
=
"fixed_small"
,
clip_predicted_
imag
e
=
True
,
clip_predicted_
sampl
e
=
True
,
tensor_format
=
"np"
,
):
super
().
__init__
()
...
...
@@ -41,11 +41,11 @@ class DDPMScheduler(SchedulerMixin, ConfigMixin):
trained_betas
=
trained_betas
,
timestep_values
=
timestep_values
,
variance_type
=
variance_type
,
clip_predicted_
imag
e
=
clip_predicted_
imag
e
,
clip_predicted_
sampl
e
=
clip_predicted_
sampl
e
,
)
self
.
timesteps
=
int
(
timesteps
)
self
.
timestep_values
=
timestep_values
# save the fixed timestep values for BDDM
self
.
clip_
imag
e
=
clip_predicted_
imag
e
self
.
clip_
sampl
e
=
clip_predicted_
sampl
e
self
.
variance_type
=
variance_type
if
trained_betas
is
not
None
:
...
...
@@ -100,8 +100,8 @@ class DDPMScheduler(SchedulerMixin, ConfigMixin):
alpha_prod_t_prev
=
self
.
get_alpha_prod
(
t
-
1
)
# For t > 0, compute predicted variance βt (see formala (6) and (7) from https://arxiv.org/pdf/2006.11239.pdf)
# and sample from it to get previous
imag
e
# x_{t-1} ~ N(pred_prev_
imag
e, variance) == add variane to pred_
imag
e
# and sample from it to get previous
sampl
e
# x_{t-1} ~ N(pred_prev_
sampl
e, variance) == add variane to pred_
sampl
e
variance
=
(
1
-
alpha_prod_t_prev
)
/
(
1
-
alpha_prod_t
)
*
self
.
get_beta
(
t
)
# hacks - were probs added for training stability
...
...
@@ -112,37 +112,37 @@ class DDPMScheduler(SchedulerMixin, ConfigMixin):
return
variance
def
step
(
self
,
residual
,
imag
e
,
t
):
def
step
(
self
,
residual
,
sampl
e
,
t
):
# 1. compute alphas, betas
alpha_prod_t
=
self
.
get_alpha_prod
(
t
)
alpha_prod_t_prev
=
self
.
get_alpha_prod
(
t
-
1
)
beta_prod_t
=
1
-
alpha_prod_t
beta_prod_t_prev
=
1
-
alpha_prod_t_prev
# 2. compute predicted original
imag
e from predicted noise also called
# 2. compute predicted original
sampl
e from predicted noise also called
# "predicted x_0" of formula (15) from https://arxiv.org/pdf/2006.11239.pdf
pred_original_
image
=
(
imag
e
-
beta_prod_t
**
(
0.5
)
*
residual
)
/
alpha_prod_t
**
(
0.5
)
pred_original_
sample
=
(
sampl
e
-
beta_prod_t
**
(
0.5
)
*
residual
)
/
alpha_prod_t
**
(
0.5
)
# 3. Clip "predicted x_0"
if
self
.
clip_predicted_
imag
e
:
pred_original_
imag
e
=
self
.
clip
(
pred_original_
imag
e
,
-
1
,
1
)
if
self
.
clip_predicted_
sampl
e
:
pred_original_
sampl
e
=
self
.
clip
(
pred_original_
sampl
e
,
-
1
,
1
)
# 4. Compute coefficients for pred_original_
imag
e x_0 and current
imag
e x_t
# 4. Compute coefficients for pred_original_
sampl
e x_0 and current
sampl
e x_t
# See formula (7) from https://arxiv.org/pdf/2006.11239.pdf
pred_original_
imag
e_coeff
=
(
alpha_prod_t_prev
**
(
0.5
)
*
self
.
get_beta
(
t
))
/
beta_prod_t
current_
imag
e_coeff
=
self
.
get_alpha
(
t
)
**
(
0.5
)
*
beta_prod_t_prev
/
beta_prod_t
pred_original_
sampl
e_coeff
=
(
alpha_prod_t_prev
**
(
0.5
)
*
self
.
get_beta
(
t
))
/
beta_prod_t
current_
sampl
e_coeff
=
self
.
get_alpha
(
t
)
**
(
0.5
)
*
beta_prod_t_prev
/
beta_prod_t
# 5. Compute predicted previous
imag
e µ_t
# 5. Compute predicted previous
sampl
e µ_t
# See formula (7) from https://arxiv.org/pdf/2006.11239.pdf
pred_prev_
imag
e
=
pred_original_
imag
e_coeff
*
pred_original_
imag
e
+
current_
imag
e_coeff
*
imag
e
pred_prev_
sampl
e
=
pred_original_
sampl
e_coeff
*
pred_original_
sampl
e
+
current_
sampl
e_coeff
*
sampl
e
return
pred_prev_
imag
e
return
pred_prev_
sampl
e
def
forward_step
(
self
,
original_
imag
e
,
noise
,
t
):
def
forward_step
(
self
,
original_
sampl
e
,
noise
,
t
):
sqrt_alpha_prod
=
self
.
get_alpha_prod
(
t
)
**
0.5
sqrt_one_minus_alpha_prod
=
(
1
-
self
.
get_alpha_prod
(
t
))
**
0.5
noisy_
imag
e
=
sqrt_alpha_prod
*
original_
imag
e
+
sqrt_one_minus_alpha_prod
*
noise
return
noisy_
imag
e
noisy_
sampl
e
=
sqrt_alpha_prod
*
original_
sampl
e
+
sqrt_one_minus_alpha_prod
*
noise
return
noisy_
sampl
e
def
__len__
(
self
):
return
self
.
timesteps
src/diffusers/schedulers/scheduling_pndm.py
View file @
dcb23b2d
...
...
@@ -62,7 +62,7 @@ class PNDMScheduler(SchedulerMixin, ConfigMixin):
# running values
self
.
cur_residual
=
0
self
.
cur_
imag
e
=
None
self
.
cur_
sampl
e
=
None
self
.
ets
=
[]
self
.
warmup_time_steps
=
{}
self
.
time_steps
=
{}
...
...
@@ -100,7 +100,7 @@ class PNDMScheduler(SchedulerMixin, ConfigMixin):
return
self
.
time_steps
[
num_inference_steps
]
def
step_prk
(
self
,
residual
,
imag
e
,
t
,
num_inference_steps
):
def
step_prk
(
self
,
residual
,
sampl
e
,
t
,
num_inference_steps
):
# TODO(Patrick) - need to rethink whether the "warmup" way is the correct API design here
warmup_time_steps
=
self
.
get_warmup_time_steps
(
num_inference_steps
)
...
...
@@ -110,7 +110,7 @@ class PNDMScheduler(SchedulerMixin, ConfigMixin):
if
t
%
4
==
0
:
self
.
cur_residual
+=
1
/
6
*
residual
self
.
ets
.
append
(
residual
)
self
.
cur_
image
=
imag
e
self
.
cur_
sample
=
sampl
e
elif
(
t
-
1
)
%
4
==
0
:
self
.
cur_residual
+=
1
/
3
*
residual
elif
(
t
-
2
)
%
4
==
0
:
...
...
@@ -119,9 +119,9 @@ class PNDMScheduler(SchedulerMixin, ConfigMixin):
residual
=
self
.
cur_residual
+
1
/
6
*
residual
self
.
cur_residual
=
0
return
self
.
transfer
(
self
.
cur_
imag
e
,
t_prev
,
t_next
,
residual
)
return
self
.
transfer
(
self
.
cur_
sampl
e
,
t_prev
,
t_next
,
residual
)
def
step_plms
(
self
,
residual
,
imag
e
,
t
,
num_inference_steps
):
def
step_plms
(
self
,
residual
,
sampl
e
,
t
,
num_inference_steps
):
timesteps
=
self
.
get_time_steps
(
num_inference_steps
)
t_prev
=
timesteps
[
t
]
...
...
@@ -130,7 +130,7 @@ class PNDMScheduler(SchedulerMixin, ConfigMixin):
residual
=
(
1
/
24
)
*
(
55
*
self
.
ets
[
-
1
]
-
59
*
self
.
ets
[
-
2
]
+
37
*
self
.
ets
[
-
3
]
-
9
*
self
.
ets
[
-
4
])
return
self
.
transfer
(
imag
e
,
t_prev
,
t_next
,
residual
)
return
self
.
transfer
(
sampl
e
,
t_prev
,
t_next
,
residual
)
def
transfer
(
self
,
x
,
t
,
t_next
,
et
):
# TODO(Patrick): clean up to be compatible with numpy and give better names
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment