foxai package

Subpackages

Submodules

foxai.array_utils module

File contains functions to handle numpy arrays.

foxai.array_utils.convert_standardized_float_to_uint8(array: ndarray) ndarray[source]

Convert float standardize float array to uint8 with values scaling.

Parameters:

array – Numpy array of floats.

Returns:

Numpy array with scaled values with type np.uint8.

Raises:

ValueError – if array is not of type np.float.

foxai.array_utils.normalize_attributes(attributes: ndarray) ndarray[source]

Normalize attributes.

For attributes with color dimension calculate mean over all colors.

Parameters:

attributes – Array of attributes.

Returns:

Single channel array of attributes.

Raises:

ValueError – if shape of attribute array is incorrect.

foxai.array_utils.resize_attributes(attributes: ndarray, dest_width: int, dest_height: int) ndarray[source]

Resize attributes to match desired shape.

Parameters:
  • attributes – Array of attributes.

  • dest_width – Desired width of attributes array.

  • dest_height – Desired height of attributes array.

Returns:

Resized attributes array.

foxai.array_utils.retain_only_positive(array: ndarray) ndarray[source]

Retain only positive values from array.

Parameters:

array – Array.

Returns:

Array with negative values replaced by zero.

foxai.array_utils.standardize_array(array: ndarray) ndarray[source]

Standardize array values to range [0-1].

Parameters:

array – Numpy array of floats.

Returns:

Numpy array with scaled values.

Raises:

ValueError – if array is not of type np.float.

foxai.array_utils.transpose_array(array: ndarray) ndarray[source]

Transpoze array from (C x H x W) to (H x W x C) shape.

C stands for color, H stands for height and W stands for width.

Parameters:

array – Array of shape (C x H x W).

Returns:

Array of shape (H x W x C).

foxai.array_utils.validate_result(attributions: Tensor) None[source]

Validate calculated attributes.

Parameters:

attributions – Tensor with calculated attributions.

Raises:

RuntimeError if tensor is empty.

foxai.context_manager module

Run xai alongside with inference.

Example

with FoXaiExplainer(

model=classifier, explainers=[

ExplainerWithParams(

explainer_name=Explainers.CV_GRADIENT_SHAP_EXPLAINER, n_samples=100, stdevs=0.0005,

),

], target=pred_label_idx,

) as xai_model:

output, xai_explanations = xai_model(img_tensor)

class foxai.context_manager.CVClassificationExplainers(value)[source]

Bases: Enum

Enum of supported computer vision classification explainers types.

CV_DECONVOLUTION_EXPLAINER: str = 'DeconvolutionCVExplainer'
CV_DEEPLIFT_EXPLAINER: str = 'DeepLIFTCVExplainer'
CV_DEEPLIFT_SHAP_EXPLAINER: str = 'DeepLIFTSHAPCVExplainer'
CV_GRADIENT_SHAP_EXPLAINER: str = 'GradientSHAPCVExplainer'
CV_GUIDEDGRADCAM_EXPLAINER: str = 'GuidedGradCAMCVExplainer'
CV_GUIDED_BACKPOPAGATION_EXPLAINER: str = 'GuidedBackpropCVExplainer'
CV_INPUT_X_GRADIENT_EXPLAINER: str = 'InputXGradientCVExplainer'
CV_INTEGRATED_GRADIENTS_EXPLAINER: str = 'IntegratedGradientsCVExplainer'
CV_LAYER_CONDUCTANCE_EXPLAINER: str = 'LayerConductanceCVExplainer'
CV_LAYER_DEEPLIFT_EXPLAINER: str = 'LayerDeepLIFTCVExplainer'
CV_LAYER_DEEPLIFT_SHAP_EXPLAINER: str = 'LayerDeepLIFTSHAPCVExplainer'
CV_LAYER_GRADCAM_EXPLAINER: str = 'LayerGradCAMCVExplainer'
CV_LAYER_GRADIENT_SHAP_EXPLAINER: str = 'LayerGradientSHAPCVExplainer'
CV_LAYER_INPUT_X_GRADIENT_EXPLAINER: str = 'LayerInputXGradientCVExplainer'
CV_LAYER_INTEGRATED_GRADIENTS_EXPLAINER: str = 'LayerIntegratedGradientsCVExplainer'
CV_LAYER_LRP_EXPLAINER: str = 'LayerLRPCVExplainer'
CV_LAYER_NOISE_TUNNEL_EXPLAINER: str = 'LayerNoiseTunnelCVExplainer'
CV_LRP_EXPLAINER: str = 'LRPCVExplainer'
CV_NOISE_TUNNEL_EXPLAINER: str = 'NoiseTunnelCVExplainer'
CV_OCCLUSION_EXPLAINER: str = 'OcclusionCVExplainer'
CV_SALIENCY_EXPLAINER: str = 'SaliencyCVExplainer'
class foxai.context_manager.CVObjectDetectionExplainers(value)[source]

Bases: Enum

Enum of supported computer vision object detection explainers types.

CV_LAYER_GRADCAM_OBJECT_DETECTION_EXPLAINER: str = 'LayerGradCAMObjectDetectionExplainer'
class foxai.context_manager.ExplainerClassWithParams(explainer_class: CVExplainerT, **kwargs)[source]

Bases: Generic[CVExplainerT]

Holder for explainer class and it’s params

explainer_class: CVExplainerT
kwargs: Dict[str, Any]
class foxai.context_manager.ExplainerWithParams(explainer_name: Union[CVClassificationExplainers, CVObjectDetectionExplainers], **kwargs)[source]

Bases: object

Holder for explainer name (class name) and it’s params

explainer_name: Union[CVClassificationExplainers, CVObjectDetectionExplainers]
kwargs: Dict[str, Any]
class foxai.context_manager.FoXaiExplainer(model: Module, explainers: List[ExplainerWithParams], target: int = 0)[source]

Bases: Generic[CVExplainerT]

Context menager for FoXAI explanation.

Example

with FoXaiExplainer(

model=classifier, explainers=[

ExplainerWithParams(

explainer_name=Explainers.CV_GRADIENT_SHAP_EXPLAINER, n_samples=100, stdevs=0.0005,

),

], target=pred_label_idx,

) as xai_model:

output, xai_explanations = xai_model(img_tensor)

Raises:

ValueError – if no explainer provided

foxai.context_manager.log() Logger[source]

Get or create logger.

foxai.logger module

foxai.logger.create_logger(logger_name: str, level: int = 10) Logger[source]

Create logger with the goven name.

Parameters:

logger_name – the name of the logger

Returns:

created logger object

foxai.visualizer module

foxai.visualizer.concat_images(images: List[ndarray]) ndarray[source]

Concatenate images into one.

Parameters:

images – List of images to merge.

Returns:

Final image.

foxai.visualizer.draw_heatmap_in_bbox(bbox: List[int], heatmap: Tensor, img: ndarray) ndarray[source]

Draw heatmap in bounding box on image.

Parameters:
  • bbox – List of coordinates for bounding box.

  • heatmap – Heatmap to display.

  • img – Original image.

Returns:

Image with displayed heatmap in bounding box area.

foxai.visualizer.generate_figure(attributions: ndarray, transformed_img: ndarray, title: str = '', figsize: Tuple[int, int] = (8, 8), alpha: float = 0.5) Figure[source]

Create figure from image and heatmap.

Parameters:
  • attributions – Heatmap.

  • transformed_img – Image in shape (H x W x C).

  • title – Title of the figure. Defaults to “”.

  • figsize – Tuple with size of figure. Defaults to (8, 8).

  • alpha – Opacity level. Defaults to 0.5,

Returns:

Heatmap of single channel applied on original image.

foxai.visualizer.get_heatmap_bbox(heatmap: ndarray, bbox: List[int], mask_value: int = 0) ndarray[source]

_summary_

Code based on https://github.com/pooya-mohammadi/deep_utils/blob/main/deep_utils/utils/box_utils/boxes.py.

Parameters:
  • heatmap – Heatmap to visualize.

  • bbox – Bounding box of detection.

  • mask_value – Masking value . Defaults to 0.

Returns:

Numpy array with heatmap only present in area of given bounding box.

foxai.visualizer.mean_channels_visualization(attributions: Tensor, transformed_img: Tensor, title: str = '', figsize: Tuple[int, int] = (8, 8), alpha: float = 0.5, only_positive_attr: bool = True) Figure[source]

Create image with calculated heatmap.

Parameters:
  • attributions – Features.

  • transformed_img – Image in shape (C x H x W) or (H x W).

  • title – Title of the figure. Defaults to “”.

  • figsize – Tuple with size of figure. Defaults to (8, 8).

  • alpha – Opacity level. Defaults to 0.5,

  • only_positive_attr – Whether to display only positive or all attributes. Defaults to True.

Returns:

Heatmap of mean channel values applied on original image.

foxai.visualizer.object_detection_visualization(detections: ObjectDetectionOutput, input_image: Tensor) ndarray[source]

Create array with detection heatmaps.

Parameters:
  • detections – Object detection data class.

  • input_image – Image in shape (C x H x W) or (H x W).

Returns:

Array of series of images with heatmap displayed on detection bounding boxes.

foxai.visualizer.preprocess_object_detection_image(input_image: Tensor) ndarray[source]

Process input image to display.

Parameters:

input_image – Original image of type float in range [0-1].

Returns:

Converted image as np.ndarray in (C x H x W).

foxai.visualizer.single_channel_visualization(attributions: Tensor, transformed_img: Tensor, selected_channel: int, title: str = '', figsize: Tuple[int, int] = (8, 8), alpha: float = 0.5, only_positive_attr: bool = True) Figure[source]

Create image with calculated heatmap.

Parameters:
  • attributions – Features.

  • transformed_img – Image in shape (C x H x W) or (H x W).

  • selected_channel – Single color channel to visualize.

  • title – Title of the figure. Defaults to “”.

  • figsize – Tuple with size of figure. Defaults to (8, 8).

  • alpha – Opacity level. Defaults to 0.5,

  • only_positive_attr – Whether to display only positive or all attributes. Defaults to True.

Returns:

Heatmap of single channel applied on original image.

Raises:

ValueError – if selected channel is negative number or exceed dimension of color channels of attributes.

Module contents