MCPcopy Create free account
hub / github.com/adobe-research/custom-diffusion / ImageLogger

Class ImageLogger

train.py:492–596  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

490
491
492class ImageLogger(Callback):
493 def __init__(self, batch_frequency, max_images, clamp=True, increase_log_steps=True,
494 rescale=True, disabled=False, log_on_batch_idx=False, log_first_step=False,
495 log_images_kwargs=None):
496 super().__init__()
497 self.rescale = rescale
498 self.batch_freq = batch_frequency
499 self.max_images = max_images
500 self.save_freq = 250
501 self.logger_log_images = {
502 pl.loggers.TestTubeLogger: self._testtube,
503 }
504 self.log_steps = [2 ** n for n in range(int(np.log2(self.batch_freq)) + 1)]
505 if not increase_log_steps:
506 self.log_steps = [self.batch_freq]
507 self.clamp = clamp
508 self.disabled = disabled
509 self.log_on_batch_idx = log_on_batch_idx
510 self.log_images_kwargs = log_images_kwargs if log_images_kwargs else {}
511 self.log_first_step = log_first_step
512
513 @rank_zero_only
514 def _testtube(self, pl_module, images, batch_idx, split):
515 for k in images:
516 grid = torchvision.utils.make_grid(images[k])
517 grid = (grid + 1.0) / 2.0 # -1,1 -> 0,1; c,h,w
518
519 tag = f"{split}/{k}"
520 pl_module.logger.experiment.add_image(
521 tag, grid,
522 global_step=pl_module.global_step)
523
524 @rank_zero_only
525 def log_local(self, save_dir, split, images,
526 global_step, current_epoch, batch_idx):
527 root = os.path.join(save_dir, "images", split)
528 for k in images:
529 grid = torchvision.utils.make_grid(images[k], nrow=4)
530 if self.rescale:
531 grid = (grid + 1.0) / 2.0 # -1,1 -> 0,1; c,h,w
532 grid = grid.transpose(0, 1).transpose(1, 2).squeeze(-1)
533 grid = grid.numpy()
534 grid = (grid * 255).astype(np.uint8)
535 filename = "{}_gs-{:06}_e-{:06}_b-{:06}.png".format(
536 k,
537 global_step,
538 current_epoch,
539 batch_idx)
540 path = os.path.join(root, filename)
541 os.makedirs(os.path.split(path)[0], exist_ok=True)
542 Image.fromarray(grid).save(path)
543
544 def log_img(self, pl_module, batch, batch_idx, split="train"):
545 check_idx = batch_idx if self.log_on_batch_idx else pl_module.global_step
546 if (self.check_frequency(check_idx) and # batch_idx % self.batch_freq == 0
547 hasattr(pl_module, "log_images") and
548 callable(pl_module.log_images) and
549 self.max_images > 0):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected