|
|
|
|
@@ -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:]))
|
|
|
|
|
@@ -1290,6 +1294,10 @@ class DiffusionPrior(nn.Module):
|
|
|
|
|
if self.predict_x_start and self.sampling_clamp_l2norm:
|
|
|
|
|
x_start = self.l2norm_clamp_embed(x_start)
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
@@ -1479,9 +1487,14 @@ class PixelShuffleUpsample(nn.Module):
|
|
|
|
|
def forward(self, x):
|
|
|
|
|
return self.net(x)
|
|
|
|
|
|
|
|
|
|
def Downsample(dim, *, dim_out = None):
|
|
|
|
|
def Downsample(dim, dim_out = None):
|
|
|
|
|
# https://arxiv.org/abs/2208.03641 shows this is the most optimal way to downsample
|
|
|
|
|
# named SP-conv in the paper, but basically a pixel unshuffle
|
|
|
|
|
dim_out = default(dim_out, dim)
|
|
|
|
|
return nn.Conv2d(dim, dim_out, 4, 2, 1)
|
|
|
|
|
return nn.Sequential(
|
|
|
|
|
Rearrange('b c (h s1) (w s2) -> b (c s1 s2) h w', s1 = 2, s2 = 2),
|
|
|
|
|
nn.Conv2d(dim * 4, dim_out, 1)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
class WeightStandardizedConv2d(nn.Conv2d):
|
|
|
|
|
"""
|
|
|
|
|
@@ -2836,12 +2849,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
|
|
|
|
|
|