|
|
|
|
@@ -1070,7 +1070,7 @@ class DiffusionPriorNetwork(nn.Module):
|
|
|
|
|
|
|
|
|
|
null_text_embeds = self.null_text_embeds.to(text_embed.dtype)
|
|
|
|
|
|
|
|
|
|
text_embeds = torch.where(
|
|
|
|
|
text_embed = torch.where(
|
|
|
|
|
text_keep_mask,
|
|
|
|
|
text_embed,
|
|
|
|
|
null_text_embeds
|
|
|
|
|
@@ -1166,6 +1166,10 @@ class DiffusionPrior(nn.Module):
|
|
|
|
|
|
|
|
|
|
self.net = net
|
|
|
|
|
self.image_embed_dim = default(image_embed_dim, lambda: clip.dim_latent)
|
|
|
|
|
|
|
|
|
|
assert net.dim == self.image_embed_dim, f'your diffusion prior network has a dimension of {net.dim}, but you set your image embedding dimension (keyword image_embed_dim) on DiffusionPrior to {self.image_embed_dim}'
|
|
|
|
|
assert not exists(clip) or clip.dim_latent == self.image_embed_dim, f'you passed in a CLIP to the diffusion prior with latent dimensions of {clip.dim_latent}, but your image embedding dimension (keyword image_embed_dim) for the DiffusionPrior was set to {self.image_embed_dim}'
|
|
|
|
|
|
|
|
|
|
self.channels = default(image_channels, lambda: clip.image_channels)
|
|
|
|
|
|
|
|
|
|
self.text_cond_drop_prob = default(text_cond_drop_prob, cond_drop_prob)
|
|
|
|
|
@@ -1255,7 +1259,7 @@ class DiffusionPrior(nn.Module):
|
|
|
|
|
def p_sample_loop_ddim(self, shape, text_cond, *, timesteps, eta = 1., cond_scale = 1.):
|
|
|
|
|
batch, device, alphas, total_timesteps = shape[0], self.device, self.noise_scheduler.alphas_cumprod_prev, self.noise_scheduler.num_timesteps
|
|
|
|
|
|
|
|
|
|
times = torch.linspace(0., total_timesteps, steps = timesteps + 2)[:-1]
|
|
|
|
|
times = torch.linspace(-1., total_timesteps, steps = timesteps + 1)[:-1]
|
|
|
|
|
|
|
|
|
|
times = list(reversed(times.int().tolist()))
|
|
|
|
|
time_pairs = list(zip(times[:-1], times[1:]))
|
|
|
|
|
@@ -1277,12 +1281,14 @@ class DiffusionPrior(nn.Module):
|
|
|
|
|
|
|
|
|
|
pred = self.net.forward_with_cond_scale(image_embed, time_cond, self_cond = self_cond, cond_scale = cond_scale, **text_cond)
|
|
|
|
|
|
|
|
|
|
# derive x0
|
|
|
|
|
|
|
|
|
|
if self.predict_x_start:
|
|
|
|
|
x_start = pred
|
|
|
|
|
pred_noise = self.noise_scheduler.predict_noise_from_start(image_embed, t = time_cond, x0 = pred)
|
|
|
|
|
else:
|
|
|
|
|
x_start = self.noise_scheduler.predict_start_from_noise(image_embed, t = time_cond, noise = pred)
|
|
|
|
|
pred_noise = pred
|
|
|
|
|
x_start = self.noise_scheduler.predict_start_from_noise(image_embed, t = time_cond, noise = pred_noise)
|
|
|
|
|
|
|
|
|
|
# clip x0 before maybe predicting noise
|
|
|
|
|
|
|
|
|
|
if not self.predict_x_start:
|
|
|
|
|
x_start.clamp_(-1., 1.)
|
|
|
|
|
@@ -1290,6 +1296,17 @@ class DiffusionPrior(nn.Module):
|
|
|
|
|
if self.predict_x_start and self.sampling_clamp_l2norm:
|
|
|
|
|
x_start = self.l2norm_clamp_embed(x_start)
|
|
|
|
|
|
|
|
|
|
# predict noise
|
|
|
|
|
|
|
|
|
|
if self.predict_x_start:
|
|
|
|
|
pred_noise = self.noise_scheduler.predict_noise_from_start(image_embed, t = time_cond, x0 = x_start)
|
|
|
|
|
else:
|
|
|
|
|
pred_noise = pred
|
|
|
|
|
|
|
|
|
|
if time_next < 0:
|
|
|
|
|
image_embed = x_start
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
c1 = eta * ((1 - alpha / alpha_next) * (1 - alpha_next) / (1 - alpha)).sqrt()
|
|
|
|
|
c2 = ((1 - alpha_next) - torch.square(c1)).sqrt()
|
|
|
|
|
noise = torch.randn_like(image_embed) if time_next > 0 else 0.
|
|
|
|
|
@@ -2841,12 +2858,13 @@ class Decoder(nn.Module):
|
|
|
|
|
inpaint_mask = None,
|
|
|
|
|
inpaint_resample_times = 5
|
|
|
|
|
):
|
|
|
|
|
batch, device, total_timesteps, alphas, eta = shape[0], self.device, noise_scheduler.num_timesteps, noise_scheduler.alphas_cumprod_prev, self.ddim_sampling_eta
|
|
|
|
|
batch, device, total_timesteps, alphas, eta = shape[0], self.device, noise_scheduler.num_timesteps, noise_scheduler.alphas_cumprod, self.ddim_sampling_eta
|
|
|
|
|
|
|
|
|
|
times = torch.linspace(0., total_timesteps, steps = timesteps + 2)[:-1]
|
|
|
|
|
|
|
|
|
|
times = list(reversed(times.int().tolist()))
|
|
|
|
|
time_pairs = list(zip(times[:-1], times[1:]))
|
|
|
|
|
time_pairs = list(filter(lambda t: t[0] > t[1], time_pairs))
|
|
|
|
|
|
|
|
|
|
is_inpaint = exists(inpaint_image)
|
|
|
|
|
resample_times = inpaint_resample_times if is_inpaint else 1
|
|
|
|
|
@@ -2888,16 +2906,25 @@ class Decoder(nn.Module):
|
|
|
|
|
|
|
|
|
|
pred, _ = self.parse_unet_output(learned_variance, unet_output)
|
|
|
|
|
|
|
|
|
|
# predict x0
|
|
|
|
|
|
|
|
|
|
if predict_x_start:
|
|
|
|
|
x_start = pred
|
|
|
|
|
pred_noise = noise_scheduler.predict_noise_from_start(img, t = time_cond, x0 = pred)
|
|
|
|
|
else:
|
|
|
|
|
x_start = noise_scheduler.predict_start_from_noise(img, t = time_cond, noise = pred)
|
|
|
|
|
pred_noise = pred
|
|
|
|
|
|
|
|
|
|
# maybe clip x0
|
|
|
|
|
|
|
|
|
|
if clip_denoised:
|
|
|
|
|
x_start = self.dynamic_threshold(x_start)
|
|
|
|
|
|
|
|
|
|
# predict noise
|
|
|
|
|
|
|
|
|
|
if predict_x_start:
|
|
|
|
|
pred_noise = noise_scheduler.predict_noise_from_start(img, t = time_cond, x0 = pred)
|
|
|
|
|
else:
|
|
|
|
|
pred_noise = pred
|
|
|
|
|
|
|
|
|
|
c1 = eta * ((1 - alpha / alpha_next) * (1 - alpha_next) / (1 - alpha)).sqrt()
|
|
|
|
|
c2 = ((1 - alpha_next) - torch.square(c1)).sqrt()
|
|
|
|
|
noise = torch.randn_like(img) if not is_last_timestep else 0.
|
|
|
|
|
|