jacobgil / pytorch-grad-cam

Advanced AI Explainability for computer vision. Support for CNNs, Vision Transformers, Classification, Object detection, Segmentation, Image similarity and more.
https://jacobgil.github.io/pytorch-gradcam-book
MIT License
10.06k stars 1.52k forks source link

YoloV4-tiny #308

Closed jnsgnstr closed 2 years ago

jnsgnstr commented 2 years ago

EigenCAM: I wanted to use some of the CAMs with the YOLOV4-tiny model. However, pytorch-grad-cam/base_cam.py (line 76) outputs a list and has no attribute .cpu(). The error is fixed by replacing outputs with outputs[0]. But this is not an elegant solution. I implemented everything as described in the YOLOV5-EigenCAM notebook.

Additionally to EigenCAM I wanted to use GradCAM and its derivatives. After running the same code, but with GradCAM, the following error occured in pytorch-grad-cam/base_cam.py (line 82): RuntimeError: grad can be implicitly created only for scalar outputs

As for the first problem, a quick fix is done by replacing target(output) with target(output).sum().

As target I use ClassifierOutputTarget.

Are those fixes okay or do they distort the results?

jacobgil commented 2 years ago

Hi, Looks like we need to define a custom reshape_transform.

def yolo4_reshape_transform(outputs):
     return outputs[0]

and pass it to the reshape_transform argument.

The result would be the same as what you did, but this is the "correct" way of doing it.

For grad-cam, it's a different story. Usually YOLO doesn't really expose gradients. You would need to use a custom target. Is there some tensor that represents the bounding box you want to taget? Does the yolo model you're doing support doing back-propagation on one of the bounding box objects? To get an idea you could look at the example for faster-rcnn, this would need adapting: https://github.com/jacobgil/pytorch-grad-cam/blob/master/pytorch_grad_cam/utils/model_targets.py#L69

jnsgnstr commented 2 years ago

Hi, thanks for the fast answer. I try to implement a custom target for YOLOV4-tiny.