torchgan.logging

This subpackage provides strong visualization capabilities using a variety of Backends. It is strongly integrated with the Trainer. The Logger supports a variety of configurations and customizations.

Note

The Logger API is currently deeply integrated with the Trainer and hence might not be a very pleasant thing to use externally. However, work is being done to make them as much independent as possible and support extendibility of the Logger. Hence, this is expected to improve in the future.

Backends

Currently available backends are:

  1. TensorboardX:

    To enable this set the TENSORBOARD_LOGGING to 1. If the package is pre-installed on your system, this variable is enabled by default.

    If you want to disable this then os.environ["TENSORBOARD_LOGGING"] = "0". Make sure to do it before loading torchgan.

    Once the logging begins, you need to start a tensorboard server using this code tensorboard --logdir runs.

  2. Visdom:

    To enable this set the VISDOM_LOGGING to 1. If the package is pre-installed on your system, this variable is enabled by default.

    If you want to disable this then os.environ["VISDOM_LOGGING"] = "0". We recommend using visdom if you need to save your plots. In general tensorboard support is better in terms of the image display.

    Warning

    If this package is present and VISDOM_LOGGING is set to 1, then a server must be started using the command python -m visdom.server before the Training is started. Otherwise the code will simply crash.

  3. Console:

    The details of training are printed on the console. This is enabled by default but can be turned off by os.environ["CONSOLE_LOGGING"] = "0".

Add more backends for visualization is a work-in-progress.

Note

It is the responsibility of the user to install the necessary packages needed for visualization. If the necessary packages are missing the logging will not occur or if the user trys to force it the program will terminate with an error message.

Note

It is recommended to use only 1 logging service (apart from the Console). Using multiple Logging services might affect the training time. It is recommended to use Visdom only if the plots are to be downloaded easily.

Logger

class torchgan.logging.Logger(trainer, losses_list, metrics_list=None, visdom_port=8097, log_dir=None, writer=None, nrow=8, test_noise=None)[source]

Base Logger class. It controls the executions of all the Visualizers and is deeply integrated with the functioning of the Trainer.

Note

The Logger has been designed to be controlled internally by the Trainer. It is recommended that the user does not attempt to use it externally in any form.

Warning

This Logger is meant to work on the standard Visualizers available. Work is being done to support custom Visualizers in a clean way. But currently it is not possible to do so.

Parameters:
  • trainer (torchgan.trainer.Trainer) – The base trainer used for training.
  • losses_list (list) – A list of the Loss Functions that need to be minimized. For a list of pre-defined losses look at torchgan.losses. All losses in the list must be a subclass of atleast GeneratorLoss or DiscriminatorLoss.
  • metrics_list (list, optional) – List of Metric Functions that need to be logged. For a list of pre-defined metrics look at torchgan.metrics. All losses in the list must be a subclass of EvaluationMetric.
  • visdom_port (int, optional) – Port to log using visdom. A deafult server is started at port 8097. So manually a new server has to be started if the post is changed. This is ignored if VISDOM_LOGGING is 0.
  • log_dir (str, optional) – Directory where TensorboardX should store the logs. This is ignored if TENSORBOARD_LOGGING is 0.
  • writer (tensorboardX.SummaryWriter, optonal) – Send a SummaryWriter if you don’t want to start a new SummaryWriter.
  • test_noise (torch.Tensor, optional) – If provided then it will be used as the noise for image sampling.
  • nrow (int, optional) – Number of rows in which the image is to be stored.
close()[source]

Turns off the tensorboard SummaryWriter if it were created.

get_grad_viz()[source]

Get the GradientVisualize object.

get_loss_viz()[source]

Get the LossVisualize object.

get_metric_viz()[source]

Get the MetricVisualize object.

register(visualize, *args, mid_epoch=True, **kwargs)[source]

Register a new Visualize object with the Logger.

Parameters:
  • visualize (torchgan.logging.Visualize) – Class name of the visualizer.
  • mid_epoch (bool, optional) – Set it to False if it is to be executed once the epoch is over. Otherwise it is executed after every call to the train_iter.
run_end_epoch(trainer, epoch, time_duration, *args)[source]

Runs the Visualizers at the end of one epoch.

Parameters:
run_mid_epoch(trainer, *args)[source]

Runs the Visualizers after every call to the train_iter.

Parameters:trainer (torchgan.trainer.Trainer) – The base trainer used for training.

Visualization

Visualize

class torchgan.logging.Visualize(visualize_list, visdom_port=8097, log_dir=None, writer=None)[source]

Base class for all Visualizations.

Parameters:
  • visualize_list (list, optional) – List of the functions needed for visualization.
  • visdom_port (int, optional) – Port to log using visdom. The visdom server needs to be manually started at this port else an error will be thrown and the code will crash. This is ignored if VISDOM_LOGGING is 0.
  • log_dir (str, optional) – Directory where TensorboardX should store the logs. This is ignored if TENSORBOARD_LOGGING is 0.
  • writer (tensorboardX.SummaryWriter, optonal) – Send a SummaryWriter if you don’t want to start a new SummaryWriter.
log_console()[source]

Console logging function. Needs to be defined in the subclass

Raises:NotImplementedError
log_tensorboard()[source]

Tensorboard logging function. Needs to be defined in the subclass

Raises:NotImplementedError
log_visdom()[source]

Visdom logging function. Needs to be defined in the subclass

Raises:NotImplementedError
step_update()[source]

Helper function which updates the step at the end of one print iteration.

LossVisualize

class torchgan.logging.LossVisualize(visualize_list, visdom_port=8097, log_dir=None, writer=None)[source]

This class provides the Visualizations for Generator and Discriminator Losses.

Parameters:
  • visualize_list (list, optional) – List of the functions needed for visualization.
  • visdom_port (int, optional) – Port to log using visdom. The visdom server needs to be manually started at this port else an error will be thrown and the code will crash. This is ignored if VISDOM_LOGGING is 0.
  • log_dir (str, optional) – Directory where TensorboardX should store the logs. This is ignored if TENSORBOARD_LOGGING is 0.
  • writer (tensorboardX.SummaryWriter, optonal) – Send a SummaryWriter if you don’t want to start a new SummaryWriter.
log_console(running_losses)[source]

Console logging function. This function logs the mean generator and discriminator losses.

Parameters:running_losses (dict) – A dict with 2 items namely, Running Discriminator Loss, and Running Generator Loss.
log_tensorboard(running_losses)[source]

Tensorboard logging function. This function logs the following:

  • Running Discriminator Loss
  • Running Generator Loss
  • Running Losses
  • Loss Values of the individual Losses.
Parameters:running_losses (dict) – A dict with 2 items namely, Running Discriminator Loss, and Running Generator Loss.
log_visdom(running_losses)[source]

Visdom logging function. This function logs the following:

  • Running Discriminator Loss
  • Running Generator Loss
  • Running Losses
  • Loss Values of the individual Losses.
Parameters:running_losses (dict) – A dict with 2 items namely, Running Discriminator Loss, and Running Generator Loss.

GradientVisualize

class torchgan.logging.GradientVisualize(visualize_list, visdom_port=8097, log_dir=None, writer=None)[source]

This class provides the Visualizations for the Gradients.

Parameters:
  • visualize_list (list, optional) – List of the functions needed for visualization.
  • visdom_port (int, optional) – Port to log using visdom. The visdom server needs to be manually started at this port else an error will be thrown and the code will crash. This is ignored if VISDOM_LOGGING is 0.
  • log_dir (str, optional) – Directory where TensorboardX should store the logs. This is ignored if TENSORBOARD_LOGGING is 0.
  • writer (tensorboardX.SummaryWriter, optonal) – Send a SummaryWriter if you don’t want to start a new SummaryWriter.
log_console(name)[source]

Console logging function. This function logs the mean gradients.

Parameters:name (str) – Name of the model whose gradients are to be logged.
log_tensorboard(name)[source]

Tensorboard logging function. This function logs the values of the individual gradients.

Parameters:name (str) – Name of the model whose gradients are to be logged.
log_visdom(name)[source]

Visdom logging function. This function logs the values of the individual gradients.

Parameters:name (str) – Name of the model whose gradients are to be logged.
report_end_epoch()[source]

Prints to the console at the end of the epoch.

update_grads(name, model, eps=1e-05)[source]

Updates the gradient logs.

Parameters:
  • name (str) – Name of the model.
  • model (torch.nn.Module) – Either a torchgan.models.Generator or a torchgan.models.Discriminator or their subclass.
  • eps (float, optional) – Tolerance value.

MetricVisualize

class torchgan.logging.MetricVisualize(visualize_list, visdom_port=8097, log_dir=None, writer=None)[source]

This class provides the Visualizations for Metrics.

Parameters:
  • visualize_list (list, optional) – List of the functions needed for visualization.
  • visdom_port (int, optional) – Port to log using visdom. The visdom server needs to be manually started at this port else an error will be thrown and the code will crash. This is ignored if VISDOM_LOGGING is 0.
  • log_dir (str, optional) – Directory where TensorboardX should store the logs. This is ignored if TENSORBOARD_LOGGING is 0.
  • writer (tensorboardX.SummaryWriter, optonal) – Send a SummaryWriter if you don’t want to start a new SummaryWriter.
log_console()[source]

Console logging function. This function logs the mean metrics.

log_tensorboard()[source]

Tensorboard logging function. This function logs the values of the individual metrics.

log_visdom()[source]

Visdom logging function. This function logs the values of the individual metrics.

ImageVisualize

class torchgan.logging.ImageVisualize(trainer, visdom_port=8097, log_dir=None, writer=None, test_noise=None, nrow=8)[source]

This class provides the Logging for the Images.

Parameters:
  • trainer (torchgan.trainer.Trainer) – The base trainer used for training.
  • visdom_port (int, optional) – Port to log using visdom. The visdom server needs to be manually started at this port else an error will be thrown and the code will crash. This is ignored if VISDOM_LOGGING is 0.
  • log_dir (str, optional) – Directory where TensorboardX should store the logs. This is ignored if TENSORBOARD_LOGGING is 0.
  • writer (tensorboardX.SummaryWriter, optonal) – Send a SummaryWriter if you don’t want to start a new SummaryWriter.
  • test_noise (torch.Tensor, optional) – If provided then it will be used as the noise for image sampling.
  • nrow (int, optional) – Number of rows in which the image is to be stored.
log_console(trainer, image, model)[source]

Saves a generated image at the end of an epoch. The path where the image is being stored is controlled by the trainer.

Parameters:
  • trainer (torchgan.trainer.Trainer) – The base trainer used for training.
  • image (Image) – The generated image.
  • model (str) – The name of the model which generated the image.
log_tensorboard(trainer, image, model)[source]

Logs a generated image in tensorboard at the end of an epoch.

Parameters:
  • trainer (torchgan.trainer.Trainer) – The base trainer used for training.
  • image (Image) – The generated image.
  • model (str) – The name of the model which generated the image.
log_visdom(trainer, image, model)[source]

Logs a generated image in visdom at the end of an epoch.

Parameters:
  • trainer (torchgan.trainer.Trainer) – The base trainer used for training.
  • image (Image) – The generated image.
  • model (str) – The name of the model which generated the image.