MCPcopy Create free account
hub / github.com/CompVis/diff2flow / extract_from_batch

Method extract_from_batch

diff2flow/trainer_module.py:448–494  ·  view source on GitHub ↗

Takes batch and extracts data. Returns: x0: Samples from source distribution (can also be None if we start from noise). x0_latent: Source latent codes. If identity first stage, this is the same as x0. x1: Samples from target distribution (must al

(self, batch)

Source from the content-addressed store, hash-verified

446 return x
447
448 def extract_from_batch(self, batch):
449 """
450 Takes batch and extracts data.
451
452 Returns:
453 x0: Samples from source distribution (can also be None if we start from noise).
454 x0_latent: Source latent codes. If identity first stage, this is the same as x0.
455 x1: Samples from target distribution (must always be provided).
456 x1_latent: Target latent codes. if identity first stage, this is the same as x1.
457 mask: If available, a valid mask for the target.
458 mask_latent: If available, latent valid mask for target.
459 """
460 # source samples (x0 ~ p(x0))
461 if "x0" in batch:
462 x0 = batch["x0"]
463 # check for precomputed latents
464 if "x0_latent" in batch:
465 x0_latent = batch["x0_latent"]
466 x0_latent = x0_latent * self.scale_factor
467 else:
468 x0_latent = self.encode_first_stage(x0)
469 else:
470 x0, x0_latent = None, None
471
472 # target samples (x1 ~ p(x1)) - data
473 x1 = batch["x1"]
474 # check for precomputed latents
475 if "x1_latent" in batch:
476 x1_latent = batch["x1_latent"]
477 x1_latent = x1_latent * self.scale_factor
478 else:
479 x1_latent = self.encode_first_stage(x1)
480
481 # check for valid mask
482 if "mask" in batch or "valid_mask" in batch:
483 mask = batch["mask"] if "mask" in batch else batch["valid_mask"]
484 # resize valid mask to latent space size
485 mask_latent = resize_ims(mask.float(), size=x1_latent.shape[-2:], mode="bilinear")
486 mask_latent = mask_latent == 1
487 else:
488 mask, mask_latent = None, None
489
490 return {
491 "x0": x0, "x0_latent": x0_latent,
492 "x1": x1, "x1_latent": x1_latent,
493 "mask": mask, "mask_latent": mask_latent
494 }
495
496 def training_step(self, batch, batch_idx):
497 """ extract data """

Callers 3

training_stepMethod · 0.95
validation_stepMethod · 0.95

Calls 2

encode_first_stageMethod · 0.95
resize_imsFunction · 0.90

Tested by

no test coverage detected