点击下方卡片,关注“机器视觉与AI深度学习”
视觉/图像重磅干货,第一时间送达!
def generata_grad_cam(self):
# weights by averaging the gradients
layer_weights=F.adaptive_avg_pool2d(self.backward_gradients,(1,1))
# Combining forward features with weights
grad_cam=torch.mul(self.forward_features,layer_weights).sum(dim=1,keepdim=True)
grad_cam=F.relu(grad_cam)
# resizing GradCAM to the input image
grad_cam_image=F.interpolate(grad_cam, size=(224,224), mode='bilinear', align_corners=False)
# normalizing GradCAM
grad_cam_image=grad_cam_image-torch.min(grad_cam_image)
grad_cam_image=grad_cam_image/torch.max(grad_cam_image)
return grad_cam_image
实际操作:
def forward_features_hook(self,module,input,output):
# save forward pass output
self.forward_features=output
def backward_gradients_hook(self,module,grad_input,grad_output):
# save backward pass gradients
self.backward_gradients=grad_output[0]
def __init__(self,model,layer_name):
# Initializing variables and registering hooks
self.model=model
self.forward_features=None
self.backward_gradients=None
self.cam_layer=getattr(self.model,layer_name)
self.cam_layer[-1].register_forward_hook(self.forward_features_hook)
self.cam_layer[-1].register_full_backward_hook(self.backward_gradients_hook)
(layer4): Sequential(
(0): Bottleneck(
(conv1): Conv2d(1024, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)
(bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(conv3): Conv2d(512, 2048, kernel_size=(1, 1), stride=(1, 1), bias=False)
(bn3): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace=True)
(downsample): Sequential(
(0): Conv2d(1024, 2048, kernel_size=(1, 1), stride=(2, 2), bias=False)
(1): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
)
(1): Bottleneck(
(conv1): Conv2d(2048, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)
(bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(conv3): Conv2d(512, 2048, kernel_size=(1, 1), stride=(1, 1), bias=False)
(bn3): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace=True)
)
(2): Bottleneck(
(conv1): Conv2d(2048, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)
(bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(conv3): Conv2d(512, 2048, kernel_size=(1, 1), stride=(1, 1), bias=False)
(bn3): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace=True)
)
)
Prediction: 207, 'golden retriever', 8.249414
Prediction: 250, 'Siberian husky', 8.082574
https://github.com/dev-essbee/gradcam-pytorch.git?source=post_page-----e678a848ad44--------------------------------
—THE END—
觉得有用,麻烦给个赞和在看