MCPcopy Create free account
hub / github.com/MegaScenes/nvs / encode

Method encode

ldm/models/diffusion/ddim.py:261–304  ·  view source on GitHub ↗
(self, x0, c, t_enc, use_original_steps=False, return_intermediates=None,
               unconditional_guidance_scale=1.0, unconditional_conditioning=None)

Source from the content-addressed store, hash-verified

259
260 @torch.no_grad()
261 def encode(self, x0, c, t_enc, use_original_steps=False, return_intermediates=None,
262 unconditional_guidance_scale=1.0, unconditional_conditioning=None):
263 num_reference_steps = self.ddpm_num_timesteps if use_original_steps else self.ddim_timesteps.shape[0]
264
265 assert t_enc <= num_reference_steps
266 num_steps = t_enc
267
268 if use_original_steps:
269 alphas_next = self.alphas_cumprod[:num_steps]
270 alphas = self.alphas_cumprod_prev[:num_steps]
271 else:
272 alphas_next = self.ddim_alphas[:num_steps]
273 alphas = torch.tensor(self.ddim_alphas_prev[:num_steps])
274
275 x_next = x0
276 intermediates = []
277 inter_steps = []
278 for i in tqdm(range(num_steps), desc='Encoding Image'):
279 t = torch.full((x0.shape[0],), i, device=self.model.device, dtype=torch.long)
280 if unconditional_guidance_scale == 1.:
281 noise_pred = self.model.apply_model(x_next, t, c)
282 else:
283 assert unconditional_conditioning is not None
284 e_t_uncond, noise_pred = torch.chunk(
285 self.model.apply_model(torch.cat((x_next, x_next)), torch.cat((t, t)),
286 torch.cat((unconditional_conditioning, c))), 2)
287 noise_pred = e_t_uncond + unconditional_guidance_scale * (noise_pred - e_t_uncond)
288
289 xt_weighted = (alphas_next[i] / alphas[i]).sqrt() * x_next
290 weighted_noise_pred = alphas_next[i].sqrt() * (
291 (1 / alphas_next[i] - 1).sqrt() - (1 / alphas[i] - 1).sqrt()) * noise_pred
292 x_next = xt_weighted + weighted_noise_pred
293 if return_intermediates and i % (
294 num_steps // return_intermediates) == 0 and i < num_steps - 1:
295 intermediates.append(x_next)
296 inter_steps.append(i)
297 elif return_intermediates and i >= num_steps - 2:
298 intermediates.append(x_next)
299 inter_steps.append(i)
300
301 out = {'x_encoded': x_next, 'intermediate_steps': inter_steps}
302 if return_intermediates:
303 out.update({'intermediates': intermediates})
304 return x_next, out
305
306 @torch.no_grad()
307 def stochastic_encode(self, x0, t, use_original_steps=False, noise=None):

Callers 6

encode_first_stageMethod · 0.45
encode_first_stageMethod · 0.45
encode_first_stageMethod · 0.45

Calls 2

apply_modelMethod · 0.45
updateMethod · 0.45

Tested by

no test coverage detected