Grad-CAM.pytorch
Grad-CAM.pytorch copied to clipboard
TypeError: 'NoneType' object is not subscriptable
我在用自己的resnet18模型进行训练时报了如下错误:
TypeError Traceback (most recent call last)
in ----> 1 test()
in test() 74 print('actual: ' + str(int(target[0]))) 75 ---> 76 getGradCAM(images[0].numpy().transpose(1,2,0).astype(np.float) * [0.229, 0.224, 0.225] + [0.485, 0.456, 0.406], model, class_id = target[0]) 77 78 for j in range(len(output)):
in getGradCAM(img, net, layer, class_id) 236 layer_name = get_last_conv_name(net) if layer is None else layer 237 grad_cam = GradCAM(net, layer_name) --> 238 mask = grad_cam(inputs, class_id) # cam mask 239 image_dict['cam'], image_dict['heatmap'] = gen_cam(img, mask) 240 grad_cam.remove_handlers()
in call(self, inputs, index) 68 print(output[0].grad_fn) 69 ---> 70 gradient = self.gradient[0].cpu().data.numpy() # [C,H,W] 71 weight = np.mean(gradient, axis=(1, 2)) # [C] 72 TypeError: 'NoneType' object is not subscriptable
图片大小是1000 * 1500 * 3, feature的大小是:torch.Size([1, 512, 47, 32])
I have the same problem. This happens even using
image = torch.as_tensor(image.astype("float32").transpose(2, 0, 1)).requires_grad_(True)
Want to know how to solve this.
I solved my problem. Mine is caused by the layer_name. In my model, I use the X101_FPN model from detectron2 (https://github.com/facebookresearch/detectron2/blob/master/configs/COCO-Detection/faster_rcnn_X_101_32x8d_FPN_3x.yaml). The original code of this repo uses the last conv2d feature layer to compute CAM. But the last cov2d layer of X101_FPN is in the RPN module. So I modified the code to use the last conv2d feature layer instead.
What codes have you modified? I can not open the image, can you list them? thanks @ddshan
I solved my problem. Mine is caused by the layer_name. In my model, I use the X101_FPN model from detectron2 (https://github.com/facebookresearch/detectron2/blob/master/configs/COCO-Detection/faster_rcnn_X_101_32x8d_FPN_3x.yaml). The original code of this repo uses the last conv2d feature layer to compute CAM. But the last cov2d layer of X101_FPN is in the RPN module. So I modified the code to use the last conv2d feature layer instead.
I still have not solved the problem by modifying the code like this. I don't know what else could cause this error.
我也出现了类似的问题 请问有人解决了嘛