Hi, i keep getting error on my GlobalAveragePooling code, got stuck in the implementation…
Tried:
weights =tf.keras.layers.GlobalAveragePooling2D()(grads_val),
but it seems i’m missing something here.
Can you please point me towards the right direction?
1 Like
Hi @Vlad_Poltorak ,
Welcome to the community. If I am right, you are on the “grad_cam” function, right?
Assuming this is right, let me copy here the instructions of the part that you inquire about:
# 4. Compute weights using global average pooling on gradient
# grads_val has shape (Height, Width, Channels) (H,W,C)
# Take the mean across the height and also width, for each channel
# Make sure weights have shape (C)
One key part here is: “Take the mean across the height and also width, for each channel”.
From this, we can probably suspect that the function might not be this keras function but a different one. The keyword here would be ‘mean’.
Try something else and let me know how it goes.
Juan
Hi @Juan_Olano !
You’re right, i am on the “grad_cam” function.
I also tried using the mean function,
weights = K.mean(grads_val,axis=(0,1)) *referring to the height and width axis
but still got an error of “Using a tf.Tensor
as a Python bool
is not allowed”.
I think the problem is probably that little comma at the end of the line there. That ends up turning the RHS of that assignment statement into a tuple with two elements. You are doing the Functional model here, not the Sequential model and it looks like maybe you made a classic “copy/paste” error by copying from some code that was doing the Sequential model.
Hi @paulinpaloalto !
The comma wasn’t originally there, just added it editing the post;
the error i’m getting when using the code:
weights =tf.keras.layers.GlobalAveragePooling2D()(grads_val)
is ‘numpy.ndarray’ object has no attribute ‘get_shape’
Sorry, I haven’t done C3 of AI4M yet, so I don’t know this code. But it might help to actually see the full exception trace there. Of course sometimes you get 18 layers of Keras internals, but it usually helps to see the first few layers of the exception trace.
But note that get_shape
is a TF attribute of a Tensor, not a “numpy thing”. So the high level error seems to say is it is expecting a TF Tensor, but apparently got a numpy array instead. Does that shed any light?
Did you reshape grad_vals to remove the batch dimension from grads_val_all_dims?
I’d say also, print your grad_vals so that we can see what object is that.
Another thing, which is probably the issue here: your are using the keras.mean - make sure that this is the appropriate library.
@Juan_Olano
Hi! I attempted using the mean in the numpy library,
weights = np.mean(grads_val,axis=(0,1))
now the error subsided, but still something is wrong with the solution. (I attached the values of the grads_val, reshaped them in the function to “grads_val = grads_val_all_dims[0]”)
I’m glad the previous error was solved. Now we can move one. It seems now that there may be other errors upstream. If the implementation is correct, the resulting squared error should be less than 0.05
At this point we’ll need to go through each one of the steps of the function:
- Hook into model output and last layer activations.
- Get gradients of last layer activations with respect to output.
- Compute value of last layer and gradients for input image.
- Compute weights from gradients by global average pooling.
- Compute the dot product between the last layer and weights to get the score for each pixel.
- Resize, take ReLU, and return cam.
If you’d like, I can take a look at your function - just send me a direct message with the code and I’ll check it out and will come back here to give you more focused hints.
Juan
Hi @Vlad_Poltorak ,
Since the gradients we are getting are not the right ones, lets move one step up in the process: Where are these gradients coming from? If you review the instructions, at some point you’ll get a ‘hook’ which you would use to calculate the values of the spatial_maps and values of the gradients. Make sure you are getting these from the right function.
Check it out and let me know.
Got it,
used “get_spatial_maps_and_gradients” function instead of “spatial_map_and_gradient_function”.