tfmri.callbacks.TensorBoardImages

class TensorBoardImages(x, log_dir='logs', images_freq=1, max_images=3, summary_name='images', volume_mode=None, display_fn=None, concat_axis=- 2, feature_keys=None, label_keys=None, prediction_keys=None, complex_part=None)[source]

Bases: keras.callbacks.Callback

Keras callback to write image summaries to Tensorboard.

Supports 2D and 3D images. Inputs are expected to have shape NHWC for 2D images or NDHWC for 3D images.

Subclasses may override the display_image method to customize how the images to display are generated. This method should accept three arguments (features, labels and predictions for a single example) and returns the image to be written to TensorBoard for that example. Alternatively, the user may pass the display_fn parameter to override this function.

The default implementation of display_image concatenates the features, labels and predictions along axis -2 (horizontally). The concatenation axis can be changed with the parameter concat_axis. Additionally, the features/labels/predictions to display can be selected using the feature_keys, label_keys and prediction_keys parameters. By default, all features/labels/predictions are displayed.

Parameters
  • x – The input samples. Can be a NumPy array, a TensorFlow tensor, a tf.data.Dataset or a tf.keras.utils.Sequence.

  • log_dir – The directory where to save the log files to be parsed by TensorBoard.

  • images_freq – Frequency (in epochs) at which images will be written to the logs. Defaults to 1.

  • max_images – Maximum number of images to be written at each step. Defaults to 3.

  • summary_name – Name for the image summaries. Defaults to 'val_images'.

  • volume_mode

    Specifies how to save 3D images. Must be None, 'gif' or an integer. If None (default), inputs are expected to be 2D images. In 'gif' mode, each 3D volume is stored as an animated GIF. If an integer, only the corresponding slice is saved.

  • display_fn – A callable. A function which accepts three arguments (features, labels and predictions for a single example) and returns the image to be written to TensorBoard. Overrides the default function, which concatenates selected features, labels and predictions according to concat_axis, feature_keys, label_keys, prediction_keys and complex_part.

  • concat_axis – An int. The axis along which to concatenate features/labels/predictions. Defaults to -2.

  • feature_keys

    A list of str or int specifying which features to select for display. If None, all features are selected. Pass an empty list to select no features.

  • label_keys

    A list of str or int specifying which labels to select for display. If None, all labels are selected. Pass an empty list to select no labels.

  • prediction_keys

    A list of str or int specifying which predictions to select for display. If None, all predictions are selected. Pass an empty list to select no predictions.

  • complex_part

    A str. One of 'real', 'imag', 'abs' or 'angle'. Specifies which part of a complex input should be displayed.

Initialize callback.

display_image(features, labels, predictions)[source]

Returns the image to be displayed for each example.

By default, the image is created by concatenating horizontally features, labels and predictions.

Parameters
  • features – Features (model inputs for a single example).

  • labels – Labels (ground truth for a single example).

  • predictions – Predictions (model outputs for a single example).

Returns

The image to display.

on_epoch_end(epoch, logs=None)[source]

Called at the end of an epoch.