MCPcopy Create free account
hub / github.com/VisionXLab/OF-Diff / ControlLDM

Class ControlLDM

cldm/cldm.py:317–556  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

315
316
317class ControlLDM(LatentDiffusion):
318
319 def __init__(self, control_stage_config, control_key, only_mid_control, *args, **kwargs):
320 super().__init__(*args, **kwargs)
321 self.control_model = instantiate_from_config(control_stage_config)
322 self.control_key = control_key
323 self.only_mid_control = only_mid_control
324 self.control_scales = [1.0] * 13
325
326 @torch.no_grad()
327 def get_input(self, batch, k, bs=None, *args, **kwargs):
328 x, c = super().get_input(batch, self.first_stage_key, *args, **kwargs)
329 control_mask = batch[self.control_key]
330 if bs is not None:
331 control_mask = control_mask[:bs]
332 control_mask = control_mask.to(self.device)
333 control_mask = einops.rearrange(control_mask, 'b h w c -> b c h w')
334 control_mask = control_mask.to(memory_format=torch.contiguous_format).float()
335
336 control_image = (batch["jpg"] + 1.0) / 2.0
337 if bs is not None:
338 control_image = control_image[:bs]
339 control_image = control_image.to(self.device)
340 control_image = einops.rearrange(control_image, 'b h w c -> b c h w')
341 control_image = control_image.to(memory_format=torch.contiguous_format).float()
342
343 return x, dict(c_crossattn=[c], c_concat_mask=[control_mask], c_concat_image=[control_image])
344
345 def apply_model(self, x_noisy, t, cond, *args, **kwargs):
346 assert isinstance(cond, dict)
347 diffusion_model = self.model.diffusion_model
348
349 cond_txt = torch.cat(cond['c_crossattn'], 1)
350
351 if cond['c_concat'] is None:
352 eps = diffusion_model(x=x_noisy, timesteps=t, context=cond_txt, control=None, only_mid_control=self.only_mid_control)
353 else:
354 if 'c_concat_image' in cond:
355 control_model_mask = copy.deepcopy(self.control_model).requires_grad_(False)
356 diffusion_model_image = copy.deepcopy(diffusion_model)
357 control_weights_mask = 1.0
358 control_weights_image = 1.0 * self.global_step / self.trainer.max_steps
359 control_image = self.control_model(x=x_noisy, hint=torch.cat(cond['c_concat_image'], 1), timesteps=t, context=cond_txt)
360 control_image = [c * scale for c, scale in zip(control_image, self.control_scales)]
361 with torch.no_grad():
362 control_mask = control_model_mask(x=x_noisy, hint=torch.cat(cond['c_concat'], 1), timesteps=t, context=cond_txt)
363 control_mask = [c * scale for c, scale in zip(control_mask, self.control_scales)]
364 control = [control_weights_mask * c_mask.detach() + control_weights_image * c_image for c_mask, c_image in zip(control_mask, control_image)]
365 eps = diffusion_model_image(x=x_noisy, timesteps=t, context=cond_txt, control=control, only_mid_control=self.only_mid_control)
366 else:
367 control = self.control_model(x=x_noisy, hint=torch.cat(cond['c_concat'], 1), timesteps=t, context=cond_txt)
368 control = [c * scale for c, scale in zip(control, self.control_scales)]
369 eps = diffusion_model(x=x_noisy, timesteps=t, context=cond_txt, control=control, only_mid_control=self.only_mid_control)
370
371 return eps
372
373 @torch.no_grad()
374 def get_unconditional_conditioning(self, N):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected