keras-io
keras-io copied to clipboard
Grad-CAM - Which layer to choose?
trafficstars
Hi, which layer would you choose to be the last convolution layer?
My network model is:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
mobilenetv2_1.00_224 (Funct (None, 7, 7, 1280) 2257984
ional)
flatten_2 (Flatten) (None, 62720) 0
dense_8 (Dense) (None, 64) 4014144
batch_normalization_6 (Batc (None, 64) 256
hNormalization)
activation_6 (Activation) (None, 64) 0
dropout_6 (Dropout) (None, 64) 0
dense_9 (Dense) (None, 32) 2080
batch_normalization_7 (Batc (None, 32) 128
hNormalization)
activation_7 (Activation) (None, 32) 0
dropout_7 (Dropout) (None, 32) 0
dense_10 (Dense) (None, 2) 66
=================================================================
Total params: 6,274,658
Trainable params: 4,016,482
Non-trainable params: 2,258,176
_________________________________________________________________
When choosing dense_10 the following error appears:
InvalidArgumentError Traceback (most recent call last)
g:\Outros computadores\Meu modelo Computador\IFES\10 Período\TCC\Códigos\GradCAM.ipynb Célula: 5 in <cell line: 16>()
13 print(preds)
15 # Generate class activation heatmap
---> 16 heatmap = make_gradcam_heatmap(img_array, model, last_conv_layer_name)
18 # Display heatmap
19 plt.matshow(heatmap)
g:\Outros computadores\Meu modelo Computador\IFES\10 Período\TCC\Códigos\GradCAM.ipynb Célula: 5 in make_gradcam_heatmap(img_array, model, last_conv_layer_name, pred_index)
29 grads = tape.gradient(class_channel, last_conv_layer_output)
31 # This is a vector where each entry is the mean intensity of the gradient
32 # over a specific feature map channel
---> 33 pooled_grads = tf.reduce_mean(grads, axis=(0, 1, 2))
35 # We multiply each channel in the feature map array
36 # by "how important this channel is" with regard to the top predicted class
37 # then sum all the channels to obtain the heatmap class activation
38 last_conv_layer_output = last_conv_layer_output[0]
File c:\Users\danie\AppData\Local\Programs\Python\Python310\lib\site-packages\tensorflow\python\util\traceback_utils.py:153, in filter_traceback.<locals>.error_handler(*args, **kwargs)
151 except Exception as e:
152 filtered_tb = _process_traceback_frames(e.__traceback__)
--> 153 raise e.with_traceback(filtered_tb) from None
154 finally:
155 del filtered_tb
...
55 inputs, attrs, num_outputs)
56 except core._NotOkStatusException as e:
57 if name is not None:
InvalidArgumentError: Invalid reduction dimension (2 for input with 2 dimension(s) [Op:Mean]
How can I resolve this?