Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def vis_guided_backprop(model, gradient_saliency, neuron_selector, img_path, label):
im = load_image(img_path)
smoothgrad_mask_3d = gradient_saliency.GetSmoothedMask(im.reshape(48, 48, 1),
feed_dict={neuron_selector: label,
model.is_training: False})
smoothgrad_mask_grayscale = saliency.VisualizeImageGrayscale(smoothgrad_mask_3d)
smoothgrad_mask_grayscale = cv2.resize(smoothgrad_mask_grayscale, None, fx=4.0, fy=4.0, interpolation=cv2.INTER_AREA)
cv2.imwrite(os.path.join(FLAGS.output_dir, "saliency_map_{}".format(img_path.split('/')[-1])),
scale_values(smoothgrad_mask_grayscale))
sess: the current session.
y: the pre-softmax activation we want to assess attribution with respect to.
image: float32 image tensor with size [1, None, None].
saliency_method: string indicating saliency map type to generate.
Returns:
a saliency map and a smoothed saliency map.
Raises:
ValueError: if the saliency_method string does not match any included method
"""
if saliency_method == 'integrated_gradients':
integrated_placeholder = saliency.IntegratedGradients(graph, sess, y, image)
return integrated_placeholder
elif saliency_method == 'gradient':
gradient_placeholder = saliency.GradientSaliency(graph, sess, y, image)
return gradient_placeholder
elif saliency_method == 'guided_backprop':
gb_placeholder = saliency.GuidedBackprop(graph, sess, y, image)
return gb_placeholder
else:
raise ValueError('No saliency method method matched. Verification of'
'input needed')
# Restore trained model
saver.restore(sess, tf.train.latest_checkpoint(FLAGS.checkpoint_dir))
print("Model loaded!")
# Visualize first convolutional layer filters
vis_conv1_filters(sess)
# Visualize activation maps from conv4 layer
vis_conv4(sess, model, 'data/images/0/130.jpg')
vis_conv4(sess, model, 'data/images/0/607.jpg')
vis_conv4(sess, model, 'data/images/1/82.jpg')
vis_conv4(sess, model, 'data/images/1/791.jpg')
# Construct the saliency object. This doesn't yet compute the saliency mask, it just sets up the necessary ops.
grad_saliency = saliency.GradientSaliency(graph, sess, y, model.x)
# Visualize using guided back-propagation
vis_guided_backprop(model, grad_saliency, neuron_selector, 'data/images/0/130.jpg', 0)
vis_guided_backprop(model, grad_saliency, neuron_selector, 'data/images/0/607.jpg', 0)
vis_guided_backprop(model, grad_saliency, neuron_selector, 'data/images/1/82.jpg', 1)
vis_guided_backprop(model, grad_saliency, neuron_selector, 'data/images/1/791.jpg', 1)
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Utilities to compute an IntegratedGradients SaliencyMask."""
import numpy as np
from saliency import GradientSaliency
class IntegratedGradients(GradientSaliency):
"""A SaliencyMask class that implements the integrated gradients method.
https://arxiv.org/abs/1703.01365
"""
def GetMask(self, input_image, input_baseline=None, nsamples=100):
"""Returns a integrated gradients mask."""
if input_baseline == None:
input_baseline = np.zeros_like(input_image)
assert input_baseline.shape == input_image.shape
input_diff = input_image - input_baseline
total_gradients = np.zeros_like(input_image)
def __init__(self, graph, session, y, x):
super(GradientSaliency, self).__init__(graph, session, y, x)
self.gradients_node = tf.gradients(y, x)[0]
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Utilites to computed GuidedBackprop SaliencyMasks"""
from saliency import SaliencyMask
import tensorflow as tf
class GuidedBackprop(SaliencyMask):
"""A SaliencyMask class that computes saliency masks with GuidedBackProp.
This implementation copies the TensorFlow graph to a new graph with the ReLU
gradient overwritten as in the paper:
https://arxiv.org/abs/1412.6806
"""
GuidedReluRegistered = False
def __init__(self, graph, session, y, x):
"""Constructs a GuidedBackprop SaliencyMask."""
super(GuidedBackprop, self).__init__(graph, session, y, x)
self.x = x
if GuidedBackprop.GuidedReluRegistered is False:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Utilities to compute an Occlusion SaliencyMask."""
import numpy as np
from saliency import SaliencyMask
import tensorflow as tf
class Occlusion(SaliencyMask):
"""A SaliencyMask class that computes saliency masks by occluding the image.
This method slides a window over the image and computes how that occlusion
affects the class score. When the class score decreases, this is positive
evidence for the class, otherwise it is negative evidence.
"""
def __init__(self, graph, session, y, x):
super(Occlusion, self).__init__(graph, session, y, x)
def GetMask(self, x_value, feed_dict = {}, size = 15, value = 0):
"""Returns an occlusion mask."""
occlusion_window = np.array([size, size, x_value.shape[2]])
occlusion_window.fill(value)
occlusion_scores = np.zeros_like(x_value)
Args:
x_value: Input value, not batched.
feed_dict: (Optional) feed dictionary to pass to the session.run call.
"""
stdev = stdev_spread * (np.max(x_value) - np.min(x_value))
total_gradients = np.zeros_like(x_value)
for i in range(nsamples):
noise = np.random.normal(0, stdev, x_value.shape)
x_plus_noise = x_value + noise
total_gradients += self.GetMask(x_plus_noise, feed_dict)
return total_gradients / nsamples
class GradientSaliency(SaliencyMask):
r"""A SaliencyMask class that computes saliency masks with a gradient."""
def __init__(self, graph, session, y, x):
super(GradientSaliency, self).__init__(graph, session, y, x)
self.gradients_node = tf.gradients(y, x)[0]
def GetMask(self, x_value, feed_dict={}):
"""Returns a vanilla gradient mask.
Args:
x_value: Input value, not batched.
feed_dict: (Optional) feed dictionary to pass to the session.run call.
"""
feed_dict[self.x] = [x_value]
return self.session.run(self.gradients_node, feed_dict=feed_dict)[0]
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Utilities to compute an IntegratedGradients SaliencyMask."""
import tensorflow as tf
import numpy as np
from .base import GradientSaliency
class IntegratedGradients(GradientSaliency):
"""A SaliencyMask class that implements the integrated gradients method.
https://arxiv.org/abs/1703.01365
"""
def GetMask(self, x_value, feed_dict={}, x_baseline=None, x_steps=25):
"""Returns a integrated gradients mask.
Args:
x_value: input ndarray.
x_baseline: Baseline value used in integration. Defaults to 0.
x_steps: Number of integrated steps between baseline and x.
"""
if x_baseline is None:
x_baseline = np.zeros_like(x_value)
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Utilities to compute an IntegratedGradients SaliencyMask."""
import tensorflow as tf
import numpy as np
from .base import GradientSaliency
class IntegratedGradients(GradientSaliency):
"""A SaliencyMask class that implements the integrated gradients method.
https://arxiv.org/abs/1703.01365
"""
def GetMask(self, x_value, feed_dict={}, x_baseline=None, x_steps=25):
"""Returns a integrated gradients mask.
Args:
x_value: input ndarray.
x_baseline: Baseline value used in integration. Defaults to 0.
x_steps: Number of integrated steps between baseline and x.
"""
if x_baseline is None:
x_baseline = np.zeros_like(x_value)