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

object detection not woking properly #227

Closed sanjaypavo closed 2 years ago

sanjaypavo commented 2 years ago

Hello, I have tried the fasterrcnn model with some images and it was not producing good results, I have attached the output screenshots, can you please elaborate on the reason for this? torch_output

jacobgil commented 2 years ago

If you can post the original image, the model, the target_layer etc (are these like in the notebook?), Ideally some code snippet, I can try taking a look at it.

Is this EigenCAM or AblationCAM?

sanjaypavo commented 2 years ago

If you can post the original image, the model, the target_layer etc (are these like in the notebook?), Ideally some code snippet, I can try taking a look at it.

Is this EigenCAM or AblationCAM?

Hi, the model, target layers are exactly the same as in this notebook. This result was generated using Eigencam. I am also attaching the original image bus .

jacobgil commented 2 years ago

Hi,

The problem seems to be that the activations on the bus are larger than on the other objects, so the bus is shadowing everything else. One way to deal with this would be to renormalize the CAM of every bounding box. However in the notebook I just saw I had a small bug in the implementation of the renormalization. It should be like this:

def renormalize_cam_in_bounding_boxes(boxes, image_float_np, grayscale_cam):
    """Normalize the CAM to be in the range [0, 1] 
    inside every bounding boxes, and zero outside of the bounding boxes. """
    renormalized_cam = np.zeros(grayscale_cam.shape, dtype=np.float32)
    images = []
    for x1, y1, x2, y2 in boxes:
        img = renormalized_cam * 0
        img[y1:y2, x1:x2] = scale_cam_image(grayscale_cam[y1:y2, x1:x2].copy())    
        images.append(img)

    renormalized_cam = np.max(np.float32(images), axis = 0)
    renormalized_cam = scale_cam_image(renormalized_cam)
    eigencam_image_renormalized = show_cam_on_image(image_float_np, renormalized_cam, use_rgb=True)
    image_with_bounding_boxes = draw_boxes(boxes, labels, classes, eigencam_image_renormalized)
    return image_with_bounding_boxes

I'm going to soon push a fix for this.

In the meanwhile, attached the result I got for your image, which I think is an improvement. image

jacobgil commented 2 years ago

Hi, I hope this solved this issue. Closing this issue for now, please re open if you have questions.