Visualizations#

aitlas.visualizations.classification module#

Classes and methods for visualizations for classification tasks.

plot_confusion_matrix(confusion_matrix, axes, class_label, class_names, fontsize=14)[source]#

Plots a confusion matrix.

Parameters:
  • confusion_matrix (array-like of shape (n_classes, n_classes)) – The confusion matrix to plot.

  • axes (matplotlib.axes.Axes) – The matplotlib axes object to plot on.

  • class_label (str) – The label of the class for the confusion matrix.

  • class_names (list of str) – The names of the classes.

  • fontsize (int, optional) – The fontsize for the plot, defaults to 14.

plot_multilabel_confusion_matrix(cm_array, labels, dataset_name, output_file)[source]#

Plots multiple confusion matrices in a .pdf format for multilabel tasks.

Parameters:
  • cm_array (list of array-like of shape (n_classes, n_classes)) – The array of confusion matrices.

  • labels (list of str) – The labels for the classes.

  • dataset_name (str) – The name of the dataset.

  • output_file (str) – The file path to save the plot.

plot_multiclass_confusion_matrix(cm_array, labels, dataset_name, output_file)[source]#

Plots multiple confusion matrices .pdf format useful for multiclass tasks.

Parameters:
  • cm_array (list of array-like of shape (n_classes, n_classes)) – The array of confusion matrices.

  • labels (list of str) – The labels for the classes.

  • dataset_name (str) – The name of the dataset.

  • output_file (str) – The file path to save the plot.

class PrecisionRecallCurve(y_true, y_pred, y_prob, labels, file, **kwargs)[source]#

Bases: BaseDetailedVisualization

plot()[source]#

Generates and plots the precision recall curve.

Returns:

matplotlib.figure.Figure object with the plot

Return type:

matplotlib.figure.Figure

class ImageLabelsVisualization(y_true, y_pred, y_prob, labels, file, **kwargs)[source]#

Bases: BaseDetailedVisualization

Class for visualising predictions for an image.

Initialize the ImageLabelsVisualization class.

Parameters:
  • y_true (array-like of shape (n_samples,)) – Ground truth (correct) labels.

  • y_pred (array-like of shape (n_samples,)) – Predicted labels, as returned by a classifier.

  • y_prob (list of float) – The predicted probabilities.

  • labels (list of str) – The labels for the classes.

  • file (str) – The file path to save the plot.

  • kwargs – Additional keyword arguments.

plot()[source]#

Plots the image with the predictions.

Returns:

matplotlib.figure.Figure object with the plot

Return type:

matplotlib.figure.Figure

plot_prediction(img, probs, classes)[source]#

Display image and predictions from model

Parameters:
  • img (array-like or PIL image) – Image to plot.

  • prob (list of float) – The predicted probabilities.

  • classes (list of str) – The labels for the classes.

Returns:

matplotlib.figure.Figure object with the plot

Return type:

matplotlib.figure.Figure

display_image_labels(image, y_true, y_pred, y_prob, labels, output_file)[source]#
precision_recall_curve(y_true, y_pred, y_prob, labels, output_file)[source]#

aitlas.visualizations.eopatch module#

Method for visualising predictions in EOpatch format for multi-temporal data. Useful for croptype classification tasks.

display_eopatch_predictions(eopatches_path, patch, y_pred, test_index, y_true, classmapping)[source]#

Displays the predictions of an EOPatch.

Parameters:
  • eopatches_path (str) – The path to the directory containing EOPatches.

  • patch (str) – The specific patch to be displayed.

  • y_pred (array-like of shape (n_samples,)) – The predicted labels, as returned by a classifier.

  • test_index (pandas.DataFrame) – The indices of the test set.

  • y_true (array-like of shape (n_samples,)) – Ground truth (correct) labels.

  • classmapping (pandas.DataFrame) – A mapping from class labels to class names.

Returns:

matplotlib.figure.Figure object with the plot

Return type:

matplotlib.figure.Figure

aitlas.visualizations.grad_cam module#

Classes and methods for GRAD-CAM visualizations, used for classification tasks.

Note

Based on the implementation at: jacobgil/pytorch-grad-cam

class ActivationsAndGradients(model, target_layers, reshape_transform)[source]#

Bases: object

Class for extracting activations and registering gradients from targetted intermediate layers.

Parameters:
  • model – The model to be evaluated

  • target_layers – The target layers from which to extract activations and gradients

  • reshape_transform – A function to reshape the activation and gradient data

save_activation(module, input, output)[source]#

Saves an activation.

Parameters:
  • module – The module from which to save the activation

  • input – The input data to the module

  • output – The output data from the module

save_gradient(module, input, output)[source]#

Saves the gradient.

Parameters:
  • module – The module from which to save the gradient

  • input – The input data to the module

  • output – The output data from the module

release()[source]#
get_2d_projection(activation_batch)[source]#
scale_cam_image(cam, target_size=None)[source]#
show_cam_on_image(img, mask, use_rgb=False, colormap=2, image_weight=0.5)[source]#

This function overlays the cam mask on the image as an heatmap.

By default the heatmap is in BGR format. :param img: The base image in RGB or BGR format. :param mask: The cam mask. :param use_rgb: Whether to use an RGB or BGR heatmap, this should be set to True if ‘img’ is in RGB format. :param colormap: The OpenCV colormap to be used. :param image_weight: The final result is image_weight * img + (1-image_weight) * mask. :returns: The default image with the cam overlay.

Parameters:
Return type:

ndarray

class ClassifierOutputTarget(category)[source]#

Bases: object

reshape_transform(tensor, height=14, width=14)[source]#
class BaseCAM(model, target_layers, use_cuda=False, reshape_transform=None, compute_input_gradient=False, uses_gradients=True)[source]#

Bases: object

Parameters:
get_cam_weights(input_tensor, target_layers, targets, activations, grads)[source]#
Parameters:
Return type:

ndarray

get_cam_image(input_tensor, target_layer, targets, activations, grads, eigen_smooth=False)[source]#
Parameters:
Return type:

ndarray

forward(input_tensor, targets, eigen_smooth=False)[source]#
Parameters:
Return type:

ndarray

get_target_width_height(input_tensor)[source]#
Parameters:

input_tensor (Tensor) –

Return type:

Tuple[int, int]

compute_cam_per_layer(input_tensor, targets, eigen_smooth)[source]#
Parameters:
Return type:

ndarray

aggregate_multi_layers(cam_per_target_layer)[source]#
Parameters:

cam_per_target_layer (ndarray) –

Return type:

ndarray

forward_augmentation_smoothing(input_tensor, targets, eigen_smooth=False)[source]#
Parameters:
Return type:

ndarray

class GradCAM(model, target_layers, use_cuda=False, reshape_transform=None)[source]#

Bases: BaseCAM

Gradient-weighted Class Activation Mapping (Grad-CAM) class.

Parameters:
  • model – The model to be evaluated

  • target_layers – The target layers from which to extract activations and gradients

  • use_cuda – Whether to use CUDA for computation (default: False)

  • reshape_transform – A function to reshape the activation and gradient data (default: None)

get_cam_weights(input_tensor, target_layer, target_category, activations, grads)[source]#

aitlas.visualizations.segmentation module#

Classes and methods for visualizations for segmentation tasks.

class ImageMaskPredictionVisualization(y_true, y_pred, y_prob, labels, file, **kwargs)[source]#

Bases: BaseDetailedVisualization

Class for visualizing the image mask predictions.

Initialisation

Parameters:
  • y_true (array-like of shape (n_samples,)) – The ground truth labels

  • y_pred (array-like of shape (n_samples,)) – The predicted labels

  • y_prob (list of float) – The predicted probabilities

  • labels (list of str) – The class labels

  • file (str) – The output file path

plot()[source]#

Plots the image mask predictions and saves the plot to the output file.

plot_segmenation(img, probs, labels)[source]#

Displays the image and the predicted segmentation masks for each label.

Parameters:
  • img (array-like or PIL image) – The input image

  • probs (list of float) – The predicted probabilities

  • labels (list of str) – The class labels

Returns:

The figure containing the plots

Return type:

matplotlib.figure.Figure

display_image_segmentation(image, y_true, y_pred, y_prob, labels, file)[source]#

Displays the predicted segmentation masks for each label.

Parameters:
  • image (array-like or PIL image) – The input image

  • y_true (array-like of shape (n_samples,)) – The ground truth labels

  • y_pred (array-like of shape (n_samples,)) – The predicted labels

  • y_prob (list of float) – The predicted probabilities

  • labels (list of str) – The class labels

  • file (str) – The output file path

save_predicted_masks(y_pred, labels, base_filepath_name)[source]#

Saves the predicted masks to the specified file path.

Parameters:
  • y_pred (array-like of shape (n_samples,)) – The predicted labels

  • labels (list of str) – The class labels

  • base_filepath_name (str) – The base file path name