@@ -12,7 +12,7 @@ def save_vanilla_gradient(network, data, labels):
12
12
13
13
# Create a saliency map for each data point
14
14
for i , image in enumerate (data ):
15
- # Put input into layers
15
+ # Forward pass on image
16
16
output = image
17
17
for l in range (len (network .layers )):
18
18
output = network .layers [l ].forward (output )
@@ -23,9 +23,9 @@ def save_vanilla_gradient(network, data, labels):
23
23
for l in range (len (network .layers )- 1 , - 1 , - 1 ):
24
24
dout = network .layers [l ].backward (dy )
25
25
dy = dout
26
+ raw_saliency_map = dout
26
27
27
28
# Process saliency map
28
- raw_saliency_map = dout
29
29
trimmed_saliency_map = trim_map (raw_saliency_map )
30
30
saliency_map = normalize_array (trimmed_saliency_map )
31
31
@@ -37,7 +37,7 @@ def save_vanilla_gradient(network, data, labels):
37
37
# Export saliency map renderings
38
38
filename = "index-{0}_class-{1}_vanilla" .format (
39
39
str (i ), str (np .argmax (label_one_hot )))
40
- save_gradient_overlay_images (image , saliency_map , filename )
40
+ save_gradient_images (image , saliency_map , filename )
41
41
42
42
print ("Saved Vanilla Gradient image to results folder" )
43
43
@@ -63,21 +63,6 @@ def normalize_array(arr):
63
63
return arr
64
64
65
65
66
- def save_gradient_images (gradient , file_name ):
67
- """
68
- Exports the original gradient image
69
-
70
- Args:
71
- gradient (np arr): Numpy array of the gradient with shape (3, 224, 224)
72
- file_name (str): File name to be exported
73
- """
74
- # Normalize
75
- gradient = normalize_array (gradient )
76
- # Save image
77
- path_to_file = os .path .join (RESULTS_FOLDER , file_name + '.jpg' )
78
- save_image (gradient , path_to_file )
79
-
80
-
81
66
def save_image (im , path ):
82
67
"""
83
68
Saves a numpy matrix or PIL image as an image
@@ -120,17 +105,17 @@ def format_np_output(np_arr):
120
105
return np_arr
121
106
122
107
123
- def save_gradient_overlay_images (org_img , saliency_map , file_name ):
108
+ def save_gradient_images (org_img , saliency_map , file_name ):
124
109
"""
125
110
Saves saliency map and overlay on the original image
126
111
127
112
Args:
128
113
org_img (PIL img): Original image
129
- activation_map (numpy arr): Activation map (grayscale) 0-255
114
+ saliency_map (numpy arr): Saliency map (grayscale) 0-255
130
115
file_name (str): File name of the exported image
131
116
"""
132
117
133
- # Grayscale activation map
118
+ # Grayscale saliency map
134
119
heatmap , heatmap_on_image = apply_colormap_on_image (
135
120
org_img , saliency_map , 'RdBu' )
136
121
@@ -149,20 +134,20 @@ def save_gradient_overlay_images(org_img, saliency_map, file_name):
149
134
150
135
# Save grayscale heatmap
151
136
# path_to_file = os.path.join(RESULTS_FOLDER, file_name+'_grayscale.png')
152
- # save_image(activation_map , path_to_file)
137
+ # save_image(saliency_map , path_to_file)
153
138
154
139
155
- def apply_colormap_on_image (org_im , activation , colormap_name ):
140
+ def apply_colormap_on_image (org_im , saliency_map , colormap_name ):
156
141
"""
157
142
Apply heatmap on image
158
143
Args:
159
144
org_img (PIL img): Original image
160
- activation_map (numpy arr): Activation map (grayscale) 0-255
145
+ saliency_map (numpy arr): Saliency map (grayscale) 0-255
161
146
colormap_name (str): Name of the colormap
162
147
"""
163
148
# Get colormap
164
149
color_map = mpl_color_map .get_cmap (colormap_name )
165
- no_trans_heatmap = color_map (activation )
150
+ no_trans_heatmap = color_map (saliency_map )
166
151
# Change alpha channel in colormap to make sure original image is displayed
167
152
heatmap = copy .copy (no_trans_heatmap )
168
153
heatmap [:, :, 3 ] = 0.5
0 commit comments