Grad-CAM: avoid register_backward_hook, capture grads via tensor hook
@rushilara▲ 0 net votes
The handout's Grad-CAM code uses module.register_backward_hook, which is deprecated and can return wrong gradients for whole ResNet stages or DenseNet dense blocks. Instead, register a normal forward hook that stores the activation and call output.register_hook(fn) inside it to capture the gradient w.r.t. that tensor. Then backprop the target class score and weight channels by the spatially averaged gradients.