yizt / Grad-CAM.pytorch

pytorch实现Grad-CAM和Grad-CAM++,可以可视化任意分类网络的Class Activation Map (CAM)图,包括自定义的网络;同时也实现了目标检测faster r-cnn和retinanet两个网络的CAM图;欢迎试用、关注并反馈问题...
Apache License 2.0
715 stars 166 forks source link

运行RetinaNet示例 - Run the RetinaNet example #17

Open runnermo34 opened 4 years ago

runnermo34 commented 4 years ago

嗨,我尝试运行RetinaNet示例代码。 该程序开始运行,但不久后失败。 那是最后几行: Hi, I tried to run the RetinaNet sample code. The program started to run, but failed shortly after. Those are the last few lines:

feature shape:torch.Size([1, 256, 100, 100])
feature shape:torch.Size([1, 256, 50, 50])
feature shape:torch.Size([1, 256, 25, 25])
feature shape:torch.Size([1, 256, 13, 13])
feature shape:torch.Size([1, 256, 7, 7])
Traceback (most recent call last):
  File "detection/demo_retinanet.py", line 185, in <module>
    main(arguments)
  File "detection/demo_retinanet.py", line 147, in main
    mask, box, class_id = grad_cam(inputs)  # cam mask
  File "/content/drive/My Drive/Pytorch/yizt_GradCam/detection/grad_cam_retinanet.py", line 61, in __call__
    output = self.net.predict([inputs])
  File "/content/drive/My Drive/Pytorch/yizt_GradCam/detectron2/detectron2/modeling/meta_arch/retinanet.py", line 438, in predict
    results = self.inference(box_cls, box_delta, anchors, images.image_sizes)
  File "/content/drive/My Drive/Pytorch/yizt_GradCam/detectron2/detectron2/modeling/meta_arch/retinanet.py", line 335, in inference
    anchors, pred_logits_per_image, deltas_per_image, tuple(image_size)
  File "/content/drive/My Drive/Pytorch/yizt_GradCam/detectron2/detectron2/modeling/meta_arch/retinanet.py", line 367, in inference_single_image
    num_topk = min(self.topk_candidates, box_reg_i.size(0))
AttributeError: 'Boxes' object has no attribute 'size' 

有人知道,如何解决这个问题? Does anybody know, how to solve this problem?

yizt commented 4 years ago

@runnermo34 您好,麻烦发下你的pytorch 版本,detectron2版本;方便的话发下您测试的图像

lyy-fighting commented 4 years ago

@runnermo34 您好,麻烦发下你的pytorch 版本,detectron2版本;方便的话发下您测试的图像

torch 1.5 detectron2 最新版

lyy-fighting commented 4 years ago

嗨,我尝试运行RetinaNet示例代码。 该程序开始运行,但不久后失败。 那是最后几行: Hi, I tried to run the RetinaNet sample code. The program started to run, but failed shortly after. Those are the last few lines:

feature shape:torch.Size([1, 256, 100, 100])
feature shape:torch.Size([1, 256, 50, 50])
feature shape:torch.Size([1, 256, 25, 25])
feature shape:torch.Size([1, 256, 13, 13])
feature shape:torch.Size([1, 256, 7, 7])
Traceback (most recent call last):
  File "detection/demo_retinanet.py", line 185, in <module>
    main(arguments)
  File "detection/demo_retinanet.py", line 147, in main
    mask, box, class_id = grad_cam(inputs)  # cam mask
  File "/content/drive/My Drive/Pytorch/yizt_GradCam/detection/grad_cam_retinanet.py", line 61, in __call__
    output = self.net.predict([inputs])
  File "/content/drive/My Drive/Pytorch/yizt_GradCam/detectron2/detectron2/modeling/meta_arch/retinanet.py", line 438, in predict
    results = self.inference(box_cls, box_delta, anchors, images.image_sizes)
  File "/content/drive/My Drive/Pytorch/yizt_GradCam/detectron2/detectron2/modeling/meta_arch/retinanet.py", line 335, in inference
    anchors, pred_logits_per_image, deltas_per_image, tuple(image_size)
  File "/content/drive/My Drive/Pytorch/yizt_GradCam/detectron2/detectron2/modeling/meta_arch/retinanet.py", line 367, in inference_single_image
    num_topk = min(self.topk_candidates, box_reg_i.size(0))
AttributeError: 'Boxes' object has no attribute 'size' 

有人知道,如何解决这个问题? Does anybody know, how to solve this problem? 这个问题应该是这个代码的作者和我们使用的 Detectron2版本不同造成的,比如下面的部分: 这是在 retinanet.py中的方法:注意作者修改代码时候 参数的位置,其实是和这个不一样的,注意调换,调换过可以解决您的问题,但又会有新的问题,这个需要我们再次解决!!!回头我解决了再说吧

def inference(self, anchors, pred_logits, pred_anchor_deltas, image_sizes): """ Arguments: anchors (list[Boxes]): A list of #feature level Boxes. The Boxes contain anchors of this image on the specific feature level. pred_logits, pred_anchor_deltas: list[Tensor], one per level. Each has shape (N, Hi Wi Ai, K or 4) image_sizes (List[torch.Size]): the input image sizes

    Returns:
        results (List[Instances]): a list of #images elements.
    """
    results = []
    for img_idx, image_size in enumerate(image_sizes):
        pred_logits_per_image = [x[img_idx] for x in pred_logits]
        deltas_per_image = [x[img_idx] for x in pred_anchor_deltas]
        results_per_image = self.inference_single_image(
            anchors, pred_logits_per_image, deltas_per_image, tuple(image_size)
        )
        results.append(results_per_image)
    return results