参考代码为:https://github.com/withtimesgo1115/gradCAM-YOLOv3-pytorch
GradCam.py运行到下面这行报错
scores.backward()
报错内容为:
RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [5, 1]],
which is output 0 of SliceBackward, is at version 2; expected version 0 instead.
Hint: enable anomaly detection to find the operation that failed to compute its gradient, with torch.autograd.set_detect_anomaly(True).
已解决,解决办法为:(博主“@一一品”提供的)
将utils.non_max_suppression中的这一行
x[..., 5:] *= x[..., 4:5] # conf = obj_conf * cls_conf
改为:
x[..., 5:] = x[..., 5:].clone() * x[..., 4:5].clone() # conf = obj_conf * cls_conf