Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement callback support for DDIM sampler #167

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions optimizedSD/ddpm.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,9 @@ def sample(self,
)

elif sampler == "ddim":
samples = self.ddim_sampling(x_latent, conditioning, S, unconditional_guidance_scale=unconditional_guidance_scale,
samples = self.ddim_sampling(x_latent, conditioning, S,
callback=callback, img_callback=img_callback,
unconditional_guidance_scale=unconditional_guidance_scale,
unconditional_conditioning=unconditional_conditioning,
mask = mask,init_latent=x_T,use_original_steps=False)

Expand Down Expand Up @@ -687,7 +689,8 @@ def add_noise(self, x0, t):

@torch.no_grad()
def ddim_sampling(self, x_latent, cond, t_start, unconditional_guidance_scale=1.0, unconditional_conditioning=None,
mask = None,init_latent=None,use_original_steps=False):
mask = None,init_latent=None,use_original_steps=False,
callback=None, img_callback=None):

timesteps = self.ddim_timesteps
timesteps = timesteps[:t_start]
Expand All @@ -707,10 +710,12 @@ def ddim_sampling(self, x_latent, cond, t_start, unconditional_guidance_scale=1.
x0_noisy = x0
x_dec = x0_noisy* mask + (1. - mask) * x_dec

x_dec = self.p_sample_ddim(x_dec, cond, ts, index=index, use_original_steps=use_original_steps,
x_dec, pred_x0 = self.p_sample_ddim(x_dec, cond, ts, index=index, use_original_steps=use_original_steps,
unconditional_guidance_scale=unconditional_guidance_scale,
unconditional_conditioning=unconditional_conditioning)

if callback: callback(i)
if img_callback: img_callback(pred_x0, i)

if mask is not None:
return x0 * mask + (1. - mask) * x_dec

Expand Down Expand Up @@ -756,7 +761,7 @@ def p_sample_ddim(self, x, c, t, index, repeat_noise=False, use_original_steps=F
if noise_dropout > 0.:
noise = torch.nn.functional.dropout(noise, p=noise_dropout)
x_prev = a_prev.sqrt() * pred_x0 + dir_xt + noise
return x_prev
return x_prev, pred_x0


def append_zero(self, x):
Expand Down