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

Class SetupCallback

train.py:443–489  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

441
442
443class SetupCallback(Callback):
444 def __init__(self, resume, now, logdir, ckptdir, cfgdir, config, lightning_config):
445 super().__init__()
446 self.resume = resume
447 self.now = now
448 self.logdir = logdir
449 self.ckptdir = ckptdir
450 self.cfgdir = cfgdir
451 self.config = config
452 self.lightning_config = lightning_config
453
454 def on_keyboard_interrupt(self, trainer, pl_module):
455 if trainer.global_rank == 0:
456 print("Summoning checkpoint.")
457 ckpt_path = os.path.join(self.ckptdir, "last.ckpt")
458 trainer.save_checkpoint(ckpt_path)
459
460 def on_pretrain_routine_start(self, trainer, pl_module):
461 if trainer.global_rank == 0:
462 # Create logdirs and save configs
463 os.makedirs(self.logdir, exist_ok=True)
464 os.makedirs(self.ckptdir, exist_ok=True)
465 os.makedirs(self.cfgdir, exist_ok=True)
466
467 if "callbacks" in self.lightning_config:
468 if 'metrics_over_trainsteps_checkpoint' in self.lightning_config['callbacks']:
469 os.makedirs(os.path.join(self.ckptdir, 'trainstep_checkpoints'), exist_ok=True)
470 print("Project config")
471 print(OmegaConf.to_yaml(self.config))
472 OmegaConf.save(self.config,
473 os.path.join(self.cfgdir, "{}-project.yaml".format(self.now)))
474
475 print("Lightning config")
476 print(OmegaConf.to_yaml(self.lightning_config))
477 OmegaConf.save(OmegaConf.create({"lightning": self.lightning_config}),
478 os.path.join(self.cfgdir, "{}-lightning.yaml".format(self.now)))
479
480 else:
481 # ModelCheckpoint callback created log directory --- remove it
482 if not self.resume and os.path.exists(self.logdir):
483 dst, name = os.path.split(self.logdir)
484 dst = os.path.join(dst, "child_runs", name)
485 os.makedirs(os.path.split(dst)[0], exist_ok=True)
486 try:
487 os.rename(self.logdir, dst)
488 except FileNotFoundError:
489 pass
490
491
492class ImageLogger(Callback):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected