torchkit.logger

The Logger class is a simple wrapper over Pytorch’s SummaryWriter. You can use it to log scalars, images and videos in numpy ndarray or torch Tensor format.

class torchkit.logger.Logger(log_dir, force_write=False)[source]

A Tensorboard-based logger.

Parameters
  • log_dir (str) –

  • force_write (bool) –

Return type

None

__init__(log_dir, force_write=False)[source]

Constructor.

Parameters
  • log_dir (str) – The directory in which to store Tensorboard logs.

  • force_write (bool) – Whether to force write to an already existing log dir. Set to True if resuming training.

Return type

None

close()[source]
Return type

None

flush()[source]
Return type

None

log_image(image, global_step, name, prefix='', nrow=5)[source]

Log an image or batch of images.

Parameters
  • image (Union[Tensor, ndarray]) – A numpy ndarray or a torch Tensor. If the image is 4D (i.e. batched), it will be converted to a 3D image using make_grid. The numpy array should be in channel-last format while the torch Tensor should be in channel-first format.

  • global_step (int) – The training iteration step.

  • name (str) – The name of the logged image(s).

  • prefix (str) – A prefix to prepend to the logged image(s).

  • nrow (int) – The number of images displayed in each row of the grid if the input image is 4D.

Return type

None

log_learning_rate(optimizer, global_step, prefix='')[source]

Log the learning rate.

Parameters
  • optimizer (Type[Optimizer]) – An optimizer.

  • global_step (int) – The training iteration step.

  • prefix (str) –

Return type

None

log_scalar(scalar, global_step, name, prefix='')[source]

Log a scalar value.

Parameters
  • scalar (Union[Tensor, float]) – A scalar torch.Tensor or float.

  • global_step (int) – The training iteration step.

  • name (str) – The name of the logged scalar.

  • prefix (str) – A prefix to prepend to the logged scalar.

Return type

None

log_video(video, global_step, name, prefix='', fps=4)[source]

Log a sequence of images or a batch of sequence of images.

Parameters
  • video – A torch Tensor or numpy ndarray. The numpy array should be in channel-last format while the torch Tensor should be in channel-first format. Should be either a single sequence of images of shape (T, CHW/HWC) or a batch of sequences of shape (B, T, CHW/HWC). The batch of sequences will get converted to one grid sequence of images.

  • global_step (int) – The training iteration step.

  • name (str) – The name of the logged video(s).

  • prefix (str) – A prefix to prepend to the logged video(s).

  • fps (int) – The frames per second.

Return type

None