Skip to content

Visualizer

visualize_array(array, out_path=None) ¤

Simple wrapper function to visualize a NumPy matrix as image via PIL.

Info

NumPy array shape has to be (x, y, channel) like this: (224, 224, 3)

Parameters:

Name Type Description Default
array numpy.ndarray

NumPy matrix containing an image.

required
out_path str

Path in which image is stored (else live output).

None
Source code in aucmedi/utils/visualizer.py
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
def visualize_array(array, out_path=None):
    """ Simple wrapper function to visualize a NumPy matrix as image via PIL.

    ???+ info
        NumPy array shape has to be (x, y, channel) like this: (224, 224, 3)

    Args:
        array (numpy.ndarray):          NumPy matrix containing an image.
        out_path (str):                 Path in which image is stored (else live output).
    """
    # Ensure integer intensity values
    array = np.uint8(array)
    # Remove channel axis if grayscale
    if array.shape[-1] == 1 : array = np.reshape(array, array.shape[:-1])
    # Convert array to PIL image
    image = Image.fromarray(array)
    # Visualize or store image
    if out_path is None : image.show()
    else : image.save(out_path)

visualize_heatmap(image, heatmap, out_path=None, alpha=0.4) ¤

Simple wrapper function to visualize a heatmap encoded as NumPy matrix with a [0-1] range as image via matplotlib and PILLOW.

Reference - Implementation

Author: François Chollet
Date: April 26, 2020
https://keras.io/examples/vision/grad_cam/

Parameters:

Name Type Description Default
image numpy.ndarray

NumPy matrix containing an image.

required
heatmap numpy.ndarray

NumPy matrix containing a XAI heatmap.

required
out_path str

Path in which image is stored (else live output).

None
alpha float

Transparency value for heatmap overlap on image (range: [0-1]).

0.4
Source code in aucmedi/utils/visualizer.py
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
def visualize_heatmap(image, heatmap, out_path=None, alpha=0.4):
    """ Simple wrapper function to visualize a heatmap encoded as NumPy matrix with a
        [0-1] range as image via matplotlib and PILLOW.

    ??? abstract "Reference - Implementation"
        Author: François Chollet <br>
        Date: April 26, 2020 <br>
        https://keras.io/examples/vision/grad_cam/ <br>

    Args:
        image (numpy.ndarray):          NumPy matrix containing an image.
        heatmap (numpy.ndarray):        NumPy matrix containing a XAI heatmap.
        out_path (str):                 Path in which image is stored (else live output).
        alpha (float):                  Transparency value for heatmap overlap on image (range: [0-1]).
    """
    # If image is grayscale, convert to RGB
    if image.shape[-1] == 1 : image = np.concatenate((image,)*3, axis=-1)
    # Rescale heatmap to grayscale range
    heatmap = np.uint8(heatmap * 255)
    # Use jet colormap to colorize heatmap
    jet = cm.get_cmap("jet")
    # Use RGB values of the colormap
    jet_colors = jet(np.arange(256))[:,:3]
    jet_heatmap = jet_colors[heatmap] * 255
    # Superimpose the heatmap on original image
    si_img = jet_heatmap * alpha + (1-alpha) * image
    # Convert array to PIL image
    si_img = si_img.astype(np.uint8)
    pil_img = Image.fromarray(si_img)
    # Visualize or store image
    if out_path is None : pil_img.show()
    else : pil_img.save(out_path)