class_docstr: Collection of useful functions to manage training sessions.
  
  Args:
      generator: Keras model, the super-scaling, or generator, network.
      logs_dir: path to the directory where the tensorboard logs are saved.
      weights_dir: path to the directory where the weights are saved.
      lr_train_dir: path to the directory containing the Low-Res images.
      feature_extractor: Keras model, feature extractor network for the deep features
          component of perceptual loss function.
      discriminator: Keras model, the discriminator network for the adversarial
          component of the perceptual loss.
      dataname: string, used to identify what dataset is used for the training session.
      weights_dictionarycontains the paths, if any to the
          pre-trained generator's and to the pre-trained discriminator's weights,
          for transfer learning.
      fallback_save_every_n_epochs: integer, determines after how many epochs that did not trigger
          weights saving the weights are despite no metric improvement.
      max_n_best_weights: maximum amount of weights that are best on some metric that are kept.
      max_n_other_weights: maximum amount of non-best weights that are kept.
  
  
  Methods:
      print_training_setting: see docstring.
      on_epoch_end: see docstring.
      epoch_n_from_weights_name: see docstring.
      initialize_training: see docstring.
class_docstr_tok: ["Collection","of","useful","functions","to","manage","training","sessions",".","Args",":","generator",":","Keras","model",",","the","super-scaling",",","or","generator",",","network",".","logs_dir",":","path","to","the","directory","where","the","tensorboard","logs","are","saved",".","weights_dir",":","path","to","the","directory","where","the","weights","are","saved",".","lr_train_dir",":","path","to","the","directory","containing","the","Low-Res","images",".","feature_extractor",":","Keras","model",",","feature","extractor","network","for","the","deep","features","component","of","perceptual","loss","function",".","discriminator",":","Keras","model",",","the","discriminator","network","for","the","adversarial","component","of","the","perceptual","loss",".","dataname",":","string",",","used","to","identify","what","dataset","is","used","for","the","training","session",".","weights_dictionarycontains","the","paths",",","if","any","to","the","pre-trained","generator","'s","and","to","the","pre-trained","discriminator","'s","weights",",","for","transfer","learning",".","fallback_save_every_n_epochs",":","integer",",","determines","after","how","many","epochs","that","did","not","trigger","weights","saving","the","weights","are","despite","no","metric","improvement",".","max_n_best_weights",":","maximum","amount","of","weights","that","are","best","on","some","metric","that","are","kept",".","max_n_other_weights",":","maximum","amount","of","non-best","weights","that","are","kept",".","Methods",":","print_training_setting",":","see","docstring",".","on_epoch_end",":","see","docstring",".","epoch_n_from_weights_name",":","see","docstring",".","initialize_training",":","see","docstring","."]
docstr: Manages the operations that are taken at the end of each epoch:
  metric checks, weight saves, logging.
docstr_tok: ["Manages","the","operations","that","are","taken","at","the","end","of","each","epoch",":","metric","checks",",","weight","saves",",","logging","."]
code: def on_epoch_end(
      self, epoch, losses, generator, discriminator=None, metrics={}
  ):
      """
      Manages the operations that are taken at the end of each epoch:
      metric checks, weight saves, logging.
      """
  
      self.logger.info(losses)
      monitor_op = {"max": np.greater, "min": np.less}
      extreme = {"max": -np.Inf, "min": np.Inf}
      for metric in metrics:
          if metric in losses.keys():
              if metric not in self.best_metrics.keys():
                  self.best_metrics[metric] = extreme[metrics[metric]]
  
              if monitor_op[metrics[metric]](
                  losses[metric], self.best_metrics[metric]
              ):
                  self.logger.info(
                      "{} improved from {:10.5f} to {:10.5f}".format(
                          metric,
                          self.best_metrics[metric],
                          losses[metric],
                      )
                  )
                  self.logger.info("Saving weights")
                  self.best_metrics[metric] = losses[metric]
                  self._save_weights(
                      epoch,
                      generator,
                      discriminator,
                      metric=metric,
                      best=True,
                  )
                  self.since_last_epoch = 0
                  return True
              else:
                  self.logger.info("{} did not improve.".format(metric))
                  if (
                      self.since_last_epoch
                      >= self.fallback_save_every_n_epochs
                  ):
                      self.logger.info("Saving weights anyways.")
                      self._save_weights(
                          epoch, generator, discriminator, best=False
                      )
                      self.since_last_epoch = 0
                      return True
  
          else:
              self.logger.warning(
                  "{} is not monitored, cannot save weights.".format(
                      metric
                  )
              )
      self.since_last_epoch += 1
      return False
  
file_path: image-super-resolution/ISR/utils/train_helper.py
imports: import yaml
  import numpy
  from pathlib import Path
  from ISR.utils.logger import get_logger
  from ISR.utils.utils import get_timestamp
label_desc: Use image node_id 11 for calling the TrainerHelper obj's underlying member method code with example usage: obj.on_epoch_end(epoch, losses, generator, discriminator, metrics) and returns: False, True, True