how can i use my own weight_pth?
1579109909 opened this issue · comments
thanks to your work for grad-cam!
however how can i use my own weight_pth to product the grad-cam images?
Please create your own code like demo3.
- Determine the layer where you want to visualize activation maps.
You can see the available layer names from your weights, e.g.,print(*your_weights.keys(), sep="\n")
. - Determine the class id to compute gradients.
- Create a
GradCAM
instance with your model. - Then
forward()
,backward()
,generate()
.
Hi, can you expound on number 3 & 4? @kazuto1011
Thanks a lot for the repo, having same question here, could you kindly elaborate step 3 & 4? @kazuto1011
Thank you in advance.
Suppose you have the model which returns logit in (num_batch, num_class) shape.
Then please create the Grad-CAM instance with the model.
gcam = GradCAM(model=model)
The GradCAM
class has three principal method forward
, backward
, and generate
.
First, please call forward
with batched images, then you get class probabilities and the corresponding indices in descending order. At the same time, gcam
saves the intermediate feature maps.
sorted_probs, sorted_ids = gcam.forward(images)
Here we take indices of the top 1 classes for example.
top1_ids = sorted_ids[:, [0]]
Please call backward
with the indices. This line computes and saves the class-specific gradients at all layers.
gcam.backward(ids=top1_ids)
Now, gcam
has {feature map, gradient} pairs of all layers.
Finally, please call generate
with the layer name you want to visualize.
regions = gcam.generate(target_layer='any_layer_names')