MCPcopy Create free account
hub / github.com/Monalissaa/DisenDiff / ImageLogger

Class ImageLogger

train.py:515–619  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

513
514
515class ImageLogger(Callback):
516 def __init__(self, batch_frequency, max_images, clamp=True, increase_log_steps=True,
517 rescale=True, disabled=False, log_on_batch_idx=False, log_first_step=False,
518 log_images_kwargs=None):
519 super().__init__()
520 self.rescale = rescale
521 self.batch_freq = batch_frequency
522 self.max_images = max_images
523 self.save_freq = 250
524 self.logger_log_images = {
525 pl.loggers.TestTubeLogger: self._testtube,
526 }
527 self.log_steps = [2 ** n for n in range(int(np.log2(self.batch_freq)) + 1)]
528 if not increase_log_steps:
529 self.log_steps = [self.batch_freq]
530 self.clamp = clamp
531 self.disabled = disabled
532 self.log_on_batch_idx = log_on_batch_idx
533 self.log_images_kwargs = log_images_kwargs if log_images_kwargs else {}
534 self.log_first_step = log_first_step
535
536 @rank_zero_only
537 def _testtube(self, pl_module, images, batch_idx, split):
538 for k in images:
539 grid = torchvision.utils.make_grid(images[k])
540 grid = (grid + 1.0) / 2.0 # -1,1 -> 0,1; c,h,w
541
542 tag = f"{split}/{k}"
543 pl_module.logger.experiment.add_image(
544 tag, grid,
545 global_step=pl_module.global_step)
546
547 @rank_zero_only
548 def log_local(self, save_dir, split, images,
549 global_step, current_epoch, batch_idx):
550 root = os.path.join(save_dir, "images", split)
551 for k in images:
552 grid = torchvision.utils.make_grid(images[k], nrow=4)
553 if self.rescale:
554 grid = (grid + 1.0) / 2.0 # -1,1 -> 0,1; c,h,w
555 grid = grid.transpose(0, 1).transpose(1, 2).squeeze(-1)
556 grid = grid.numpy()
557 grid = (grid * 255).astype(np.uint8)
558 filename = "{}_gs-{:06}_e-{:06}_b-{:06}.png".format(
559 k,
560 global_step,
561 current_epoch,
562 batch_idx)
563 path = os.path.join(root, filename)
564 os.makedirs(os.path.split(path)[0], exist_ok=True)
565 Image.fromarray(grid).save(path)
566
567 def log_img(self, pl_module, batch, batch_idx, split="train"):
568 check_idx = batch_idx if self.log_on_batch_idx else pl_module.global_step
569 if (self.check_frequency(check_idx) and # batch_idx % self.batch_freq == 0
570 hasattr(pl_module, "log_images") and
571 callable(pl_module.log_images) and
572 self.max_images > 0):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected