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
9.79k stars 1.52k forks source link

AttributeError: 'AblationLayer' object has no attribute 'f' #485

Open LiuJiaji1999 opened 4 months ago

LiuJiaji1999 commented 4 months ago

when i choose AblationCAM in YOLOv5 notebook,it will promt this issue, this is my part code about YOLOBoxScoreTarget

`def init(self, labels, bounding_boxes, iou_threshold=0.5): self.labels = labels self.bounding_boxes = bounding_boxes self.iou_threshold = iou_threshold def call(self, model_output): model_output = model([rgbimg]) boxes,,_,confidences,categories = parse_detections(model_output) boxes = torch.Tensor(boxes) output = torch.Tensor([0]) if torch.cuda.is_available(): output = output.cuda() boxes = boxes.cuda() if len(boxes) == 0: return output for box, label in zip(self.bounding_boxes, self.labels):

print(type(box)) # <class 'tuple'>

        # print(box) # box[:] = (477, 271, 639, 471)
        box = torch.Tensor(np.array(box)[None,:]) 
        if torch.cuda.is_available():
            box = box.cuda()       
        ious = torchvision.ops.box_iou(box, boxes)   
        index = ious.argmax()
        if ious[0, index] > self.iou_threshold and categories[index] == label:
            score = ious[0, index] + confidences[index]
            output = output + score
    return output`

And this is my test :

targets = [YOLOBoxScoreTarget(labels=names, bounding_boxes=boxes)] cam = AblationCAM(model, target_layers, ablation_layer=AblationLayer(), use_cuda=torch.cuda.is_available() ) grayscale_cam = cam(tensor, targets=targets)[0,:,:] cam_image = show_cam_on_image(img, grayscale_cam, use_rgb=True) Image.fromarray(cam_image)

LiuJiaji1999 commented 4 months ago

@jacobgil