I am trying to apply Saliency and grad-CAM on fine-tuned Resnet50 depending on this notebook:
https://github.com/raghakot/keras-vis/blob/master/examples/resnet/attention.ipynb
but I always get the same error when I run visualize_saliency and visualize_cam
AttributeError: 'int' object has no attribute 'dtype'
Can you provide additional information like which version of packages are you using and a screenshot of the error or maybe copy paste the error stack?
I am using keras 2.2.4 with tensorflow 1.12.0 backend.
I added GlobalAveragePooling2D and two dense layers to resnet50.
I freezed just the first 141 layers.
from vis.visualization import visualize_saliency, overlay
from vis.utils import utils
from keras import activations
layer_idx = utils.find_layer_idx(model, 'dense_2')
f, ax = plt.subplots(1, 2)
for i, img in enumerate([img1, img2]):
grads = visualize_saliency(model, layer_idx, filter_indices=0, seed_input=img)
the visualize_salilency script is from https://github.com/raghakot/keras-vis/blob/master/examples/resnet/attention.ipynb
AttributeError Traceback (most recent call last)
10 for i, img in enumerate([img1, img2]):
11 # 20 is the imagenet index corresponding to ouzel
---> 12 grads = visualize_saliency(model, layer_idx, filter_indices=0, seed_input=img)
13
14 # visualize grads as heatmap
~\AppData\Local\Continuum\anaconda3\lib\site-packages\vis\visualization\saliency.py in visualize_saliency(model, layer_idx, filter_indices, seed_input, wrt_tensor, backprop_modifier, grad_modifier, keepdims)
132 (ActivationMaximization(model.layers[layer_idx], filter_indices), -1)
133 ]
--> 134 return visualize_saliency_with_losses(model.input, losses, seed_input, wrt_tensor, grad_modifier, keepdims)
135
136
~\AppData\Local\Continuum\anaconda3\lib\site-packages\vis\visualization\saliency.py in visualize_saliency_with_losses(input_tensor, losses, seed_input, wrt_tensor, grad_modifier, keepdims)
77 """
78 opt = Optimizer(input_tensor, losses, wrt_tensor=wrt_tensor, norm_grads=False)
---> 79 grads = opt.minimize(seed_input=seed_input, max_iter=1, grad_modifier=grad_modifier, verbose=False)[1]
80
81 if not keepdims:
~\AppData\Local\Continuum\anaconda3\lib\site-packages\vis\optimizer.py in minimize(self, seed_input, max_iter, input_modifiers, grad_modifier, callbacks, verbose)
151
152 # 0 learning phase for 'test'
--> 153 computed_values = self.compute_fn([seed_input, 0])
154 losses = computed_values[:len(self.loss_names)]
155 named_losses = list(zip(self.loss_names, losses))
~\AppData\Local\Continuum\anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py in __call__(self, inputs)
2713 return self._legacy_call(inputs)
2714
-> 2715 return self._call(inputs)
2716 else:
2717 if py_any(is_tensor(x) for x in inputs):
~\AppData\Local\Continuum\anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py in _call(self, inputs)
2653 array_vals.append(
2654 np.asarray(value,
-> 2655 dtype=tf.as_dtype(tensor.dtype).as_numpy_dtype))
2656 if self.feed_dict:
2657 for key in sorted(self.feed_dict.keys()):
AttributeError: 'int' object has no attribute 'dtype'
I am getting exactly the same error
i am also seeing the same error
I was able to fix it for me. One of the modules that I was loading was setting K.set_learning_phase(1). The learning phase needs to not be set in order for keras-vis to run. Hope that helps
I was able to fix it for me. One of the modules that I was loading was setting K.set_learning_phase(1). The learning phase needs to not be set in order for keras-vis to run. Hope that helps
Did you set it to zero? I tried both and am still running into issues
No, it just needs to not be set at all.
On Aug 7, 2019, at 23:18, jntorres notifications@github.com wrote:
I was able to fix it for me. One of the modules that I was loading was setting K.set_learning_phase(1). The learning phase needs to not be set in order for keras-vis to run. Hope that helps
Did you set it to zero? I tried both and am still running into issues
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or mute the thread.
Most helpful comment
I was able to fix it for me. One of the modules that I was loading was setting K.set_learning_phase(1). The learning phase needs to not be set in order for keras-vis to run. Hope that helps