mindspore_xai.visual

Computer vision data visualizations.

mindspore_xai.visual.cv.normalize_saliency(saliency)[source]

Normalize the saliency map.

Parameters

saliency (Tensor, np.ndarray) – Saliency map in shape of \((H, W)\).

Returns

np.ndarray, the normalized saliency map in shape of \((H, W)\).

Examples

>>> import numpy as np
>>> from mindspore_xai.visual.cv import normalize_saliency
>>>
>>> # prepare the saliency map
>>> saliency_np = np.array([[0.4, 0.3, 0.1], [0.5, 0.9, 0.1]])
>>> output_img = normalize_saliency(saliency_np)
>>> print(output_img.shape)
(2, 3)
mindspore_xai.visual.cv.saliency_to_image(saliency, original=None, cm=None, normalize=True, with_alpha=False)[source]

Convert the saliency map to a PIL.Image.Image object.

Parameters
  • saliency (Tensor, np.ndarray) – Saliency map in shape of \((H, W)\).

  • original (PIL.Image.Image, optional) – The original image . Default: None.

  • cm (Callable, optional) – Color map, viridis of matplotlib will be used if None is provided. Default: None.

  • normalize (bool, optional) – Normalize the input saliency map. Default: True.

  • with_alpha (bool, optional) – Add alpha channel to the returned image. Default: False.

Returns

PIL.Image.Image, the converted image object in size of \((H, W)\) with RGB or RGBA (if with_alpha is True) channels.

Examples

>>> import numpy as np
>>> from PIL import Image
>>> from mindspore_xai.visual.cv import saliency_to_image
>>>
>>> # prepare the original image
>>> img_array = np.random.randint(255, size=(400, 400), dtype=np.uint8)
>>> orig_img = Image.fromarray(img_array)
>>> # prepare the saliency map
>>> saliency_np = np.random.rand(400, 400)
>>> output_img = saliency_to_image(saliency_np, orig_img)
>>> print(output_img.size)
(400, 400)
mindspore_xai.visual.cv.saliency_to_rgba(saliency, cm=None, alpha_factor=1.2, as_uint8=True, normalize=True)[source]

Convert the saliency map to a RGBA numpy array.

Parameters
  • saliency (Tensor, np.ndarray) – Saliency map in shape of \((H, W)\).

  • cm (Callable, optional) – Color map, viridis of matplotlib will be used if None is provided. Default: None.

  • alpha_factor (float, optional) – Alpha channel multiplier. Default: 1.2.

  • as_uint8 (bool, optional) – Return as with UINT8 data type. Default: True.

  • normalize (bool, optional) – Normalize the input saliency map. Default: True.

Returns

np.ndarray, the converted RGBA map in shape of \((H, W, 4)\) if cm was set to None.

Examples

>>> import numpy as np
>>> from mindspore_xai.visual.cv import saliency_to_rgba
>>>
>>> # prepare the saliency map
>>> saliency_np = np.array([[0.4, 0.3, 0.1], [0.5, 0.9, 0.1]])
>>> output_img = saliency_to_rgba(saliency_np)
>>> print(output_img.shape)
(2, 3, 4)